微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 用nRF905和DS18B20制作的51单片机温度数据无线传输系统

用nRF905和DS18B20制作的51单片机温度数据无线传输系统

时间:11-28 来源:互联网 点击:
我用DS18B20和两套由nRF905组成的无线数传模块制作了一套温度数据的无线传输采集系统,已经调试通过,效果良好。其硬件电路框图如下:

(AM,CD与单片机的P1.3,P1.4的连接未画出)

DS18B20是MAXIM公司推出的数字温度传感器,将它测量的温度数据直接送到单片机,再由单片机传给nRF905,数据经调制后被发送出去。nRF905工作于433/868/915MHz三个ISM(工业、科学和医学)频道,这里使用的是433MHz的载频。另外一套nRF905无线数传模块收到信号后,经解调得到的原始温度数据传给单片机,单片机通过串口把数据再传给上位电脑,最后经过处理的温度数据连同日期、时间、采集点等相关信息可以被自动录入电脑保存并实时显示在电脑屏幕上。简单的电脑数据显示界面如下图:


本系统实物照片如下:


照片中左边为带有DS18B20数字温度传感器的用作发射数据的nRF905无线数传模块,右边为用作接收数据的nRF905数传模块,其中的单片机可以通过串口和电脑相连,上传数据。

/

#include 
#include 
#define uchar unsigned char
#define uintunsigned int
#define WC0x00// Write configuration register command
#define RC0x10// Readconfiguration register command
#define WTP0x20// Write TX Payloadcommand
#define RTP0x21// ReadTX Payloadcommand
#define WTA0x22// Write TX Addresscommand
#define RTA0x23// ReadTX Addresscommand
#define RRP0x24// ReadRX Payloadcommand
typedef struct RFConfig
{
uchar n;
uchar buf[10];
}RFConfig;
code RFConfig RxTxConf =
{
10,
0x01, 0x0c, 0x44, 0x20, 0x20, 0xcc, 0xcc, 0xcc,0xcc, 0x58
};
// The content of this struct is nRF905s initialize data.初始化数据
// CH_NO=1;433MHZ;Normal Opration,No Retrans;RX,TX Address is 4 Bytes
// RX TX Payload Width is 32 Bytes;Disable Extern Clock;Fosc=16MHZ
// 8 Bits CRC And enable
uchar data TxBuf[32];
uchar data RxBuf[32];
uchar key,rsbrf,delayf,tf;
uchar bdata DATA_BUF;
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
sbit TX_EN =P1^7;
sbit TRX_CE =P1^6;
sbit PWR_UP =P1^5;
sbit MISO =P1^1;
sbit MOSI =P1^0;
sbit SCK=P3^7;
sbit CSN=P3^3;
sbit AM=P1^3;
sbit DR=P3^2;
sbit CD=P1^4;
sbitLED1=P3^4;
sbitLED2=P3^5;
sbitKEY1=P3^0;
sbitKEY2=P3^1;
void InitIO(void);// Initialize IO port
void Inituart(void);// initialize 232 uart
void Config905(void);// Config nRF905 module配置NRF905模式
void SetTxMode(void);// Set nRF905 in Tx mode
void SetRxMode(void);// Set nRF905 in Rx mode
void TxPacket(void);// Send data by nRF905
void RxPacket(void);// Recive data by nRF905
void SpiWrite(uchar);// Write data to nRF905
uchar SpiRead(void);// Read data to nRF905
void Delay(uchar n);// Delay 100us
void Scankey(void);// Scan key
void TxData (uchar x);// Send key_value to CRT display
void comdebug(void);
void Delay(uchar n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
void main()
{
unsigned int cyc=0;
unsigned int distan=0;
//================NRF905初始化
CSN=1;// Spidisable
SCK=0;// Spi clock line init high
DR=1;// Init DR for input
AM=1;// Init AM for input
PWR_UP=1;// nRF905 power on
TRX_CE=0;// Set nRF905 in standby mode
TX_EN=0;// set radio in Rx mode
//================
Config905();// Config nRF905 module
TxBuf[0] = 1;
TxBuf[1] = 1;
SetTxMode();// Set Tx Mode
TxPacket();// Transmit Tx buffer data
LED1 = 0;
LED2 = 0;
Delay(500);// delay for led light
LED1 = 1;
LED2 = 1;
TxBuf[0] = 0xff;
TxBuf[1] = 0xff;
// led close
SetRxMode();// Set nRF905 in Rx mode
while(1)
{
//=====================从键盘获得数据
if(KEY1 ==0 ) { LED1 = 0; TxBuf[0] = 1 ;tf = 1 ; }
if(KEY2 ==0 ) { LED2 = 0; TxBuf[1] = 1 ;tf = 1 ; }
if (tf==1)
{
//===============NRF905发送相应的按键
SetTxMode();// Set Tx Mode
TxPacket();// Transmit Tx buffer data
TxBuf[0] = 0xff;
TxBuf[1] = 0xff;
SetRxMode();// Set nRF905 in Rx mode
tf=0;
Delay(500);
LED1 = 1;
LED2 = 1;
}
//=====================等待NRF905接收
if (DR)// If recive data ready...
RxPacket();// ... recive data
}
}
//function Config905();
void Config905(void)
{
uchar i;
CSN=0;// Spi enable for write a spi command
SpiWrite(WC);// Write config command写放配置命令
for (i=0;i写放配置字
{
SpiWrite(RxTxConf.buf[i]);
}
CSN=1;// Disable Spi
}
//function SpiWrite();
void SpiWrite(ucharbyte)
{
uchar i;
DATA_BUF=byte;// Put functions parameter into a bdata variable
for (i=0;i<8;i++)// Setup byte circulation bits
{
if (flag)// Put DATA_BUF.7 on data line
MOSI=1;
else
MOSI=0;
SCK=1;// Set clock line high
DATA_BUF=DATA_BUF<1; // Shift DATA_BUF
SCK=0;// Set clock line low
}
}
//function SpiRead();
uchar SpiRead(void)
{
uchar i;
for (i=0;i<8;i++)// Setup byte circulation bits
{
DATA_BUF=DATA_BUF<1; // Right shift DATA_BUF
SCK=1;// Set clock line high
if (MISO)
flag1=1;// Read data
else
flag1=0;
SCK=0;// Set clock line low
}
return DATA_BUF;// Return function parameter
}
//function TxPacket();
void TxPacket(void)
{
uchar i;
//Config905();
CSN=0;// Spi enable for write a spi command
SpiWrite(WTP);// Write payload command
for (i=0;i<32;i++)
{
SpiWrite(TxBuf[i]);// Write 32 bytes Tx data
}
CSN=1;// Spi disable
Delay(1);
CSN=0;// Spi enable for write a spi command
SpiWrite(WTA);// Write address command
for (i=0;i<4;i++)// Write 4 bytes address
{
SpiWrite(RxTxConf.buf[i+5]);
}
CSN=1;// Spi disable
TRX_CE=1;// Set TRX_CE high,start Tx data transmission
Delay(1);// while (DR!=1);
TRX_CE=0;// Set TRX_CE low
}
//function RxPacket();
void RxPacket(void)
{
uchar i;
TRX_CE=0;// Set nRF905 in standby mode
CSN=0;// Spi enable for write a spi command
SpiWrite(RRP);// Read payload command
for (i=0;i<32;i++)
{
RxBuf[i]=SpiRead();// Read data and save to buffer
}
CSN=1;// Disable spi
while(DR||AM);
// P0= RxBuf[0];// Buffer[0] output from P0 port...
TRX_CE=1;
//xx=(RxBuf[0]>>4)&0x0f;
//TxData(xx);
//=====================
if(RxBuf[0] == 1) LED1 = 0 ;
if(RxBuf[1] == 1) LED2 = 0 ;
Delay(500);// ...light led
// P0=0xff;// Close led
LED1 = 1 ;
LED2 = 1 ;
}
//function SetTxMode();
void SetTxMode(void)
{
TX_EN=1;
TRX_CE=0;
Delay(1);// delay for mode change(>=650us)
}
//function SetRxMode();
void SetRxMode(void)
{
TX_EN=0;
TRX_CE=1;
Delay(1);// delay for mode change(>=650us)
}****************************************

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top