📄 nrf905.c
字号:
#include<reg52.h>
#include<intrins.h>
#include"LCD1602\LCD1602.h"
#define uchar unsigned char
#define uint unsigned int
#define WC 0x00 // Write configuration register command
#define RC 0x10 // Read configuration register command
#define WTP 0x20 // Write TX Payload command
#define RTP 0x21 // Read TX Payload command
#define WTA 0x22 // Write TX Address command
#define RTA 0x23 // Read TX Address command
#define RRP 0x24 // Read RX Payload command
/*******************************************************************************************/
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 nRF905's 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[33];
uchar data RxBuf[32];
/*******************************************************************************************/
//uchar rsbrf,delayf,tf,Key;
/*******************************************************************************************/
uchar bdata DATA_BUF;
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
/*******************************************************************************************/
sbit TX_EN =P2^6;
sbit TRX_CE =P2^5;
sbit PWR_UP =P2^4;
sbit MISO =P2^3;
sbit MOSI =P2^2;
sbit SCK =P2^1;
sbit CSN =P2^0;
sbit AM =P3^2;
sbit DR =P3^3;
sbit CD =P3^5;
sbit Key1 = P0^1;
sbit Key2 = P0^0;
/*******************************************************************************************/
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 delays(void);
uchar *pUART; //字符串指针
void print_Char(unsigned char c)
{
SBUF = c;
c = '\0';
pUART=&c;
while (pUART!=0);
}
/*******************************************************************************************/
//function Delay100us();Delay 100us
/*******************************************************************************************/
void Delay(uchar n)
{
uint i;
while(n--)
for(i=0;i<80;i++);
}
/*******************************************************************************************/
volatile unsigned char RxWriteIndex = 0;
//volatile unsigned char cc = 0;
void UART_INT(void) interrupt 4
{
if (RI)
{
TxBuf[RxWriteIndex] = SBUF;
if (TxBuf[RxWriteIndex]==0x00)
TxBuf[RxWriteIndex]=0x01;
RxWriteIndex++;
TxBuf[RxWriteIndex] = '\0';
RI = 0;
}
else //TI
{
TI=0; //发送中断标志位(SCON)
if (*pUART != '\0')
{
SBUF = *pUART;
pUART++;
}
else pUART=0; //不会启动下一次发送
/*
if (cc!=32)
{
cc++;
SBUF = *pUART;
pUART++;
}
if(cc==32)
cc=0; */
}
}
void print_Str(const uchar*p)
{
pUART=p;
TI=1;
while (pUART!=0); //遇到 '\0',输出完毕
//while(cc!=32);
}
unsigned char code displayLCD[] = {"Temperature is :"}; //11
unsigned char ci = 0;
void main()
{
LCDInit();
SCON=0x50; //模式1
TMOD=0x21; //定时模式2
IE=0x90;
//=====9600=======设置串口通讯寄存器
//PCON=0x00; //波特率9600BPS,晶振11.0592MHZ
PCON=0x80; //波特率加倍
TH1=0xFD; //
TL1=0xFD; //计数器初值
//================NRF905初始化
CSN=1; // Spi disable
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
TI = 0;//串行中断标志位清零
TR1 = 1;//打开计数器1
//================
Config905(); // Config nRF905 module
SetRxMode();
// print_Char('a');
TxBuf[32] = '\0';
while(1)
{
DisplayListChar_code(0,0,displayLCD);
//=====================从键盘获得数据
if (Key1==0) //睡眠键
{
delays();
if (Key1==0)
{
//Model_0ne();
TxBuf[0]=0x11;
SetTxMode(); // Set Tx Mode
TxPacket(); // Transmit Tx buffer data
TxBuf[0] = '\0';
SetRxMode();
}
while(Key1==0);
}
if (Key2==0) //温度键
{
delays();
if (Key2==0)
{
//Model_Two();
TxBuf[0]=0x02;
SetTxMode(); // Set Tx Mode
TxPacket(); // Transmit Tx buffer data
TxBuf[0] = '\0';
SetRxMode();
}
while(Key2==0);
}
//=====================等待NRF905接收
if (DR)
{ // If recive data ready...
RxPacket(); // ... recive data
//print_Str(RxBuf);
DisplayListChar_code(0,0,displayLCD);
DisplayOneChar(0,1, RxBuf[0] +'0');
//print_Char(RxBuf[0]+'0');
//print_Char(RxBuf[1]+'0');
DisplayOneChar(1,1, RxBuf[1] +'0');
DisplayOneChar(2,1, '.');
DisplayOneChar(3,1, RxBuf[2] +'0');
DisplayOneChar(4,1, RxBuf[3] +'0');
RxBuf[0]='\0';
}
if (RxWriteIndex>0)
{
print_Str(TxBuf);
SetTxMode(); // Set Tx Mode
TxPacket(); // Transmit Tx buffer data
TxBuf[0] = '\0';
for (ci=0; ci <32; ++ci)
{
TxBuf[ci] = '\0';
}
SetRxMode(); // Set nRF905 in Rx mode
RxWriteIndex = 0;
}
}
}
/*******************************************************************************************/
/*******************************************************************************************/
//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<RxTxConf.n;i++) // Write configration words 写放配置字
{
SpiWrite(RxTxConf.buf[i]);
}
CSN=1; // Disable Spi
}
/*******************************************************************************************/
//function SpiWrite();
/*******************************************************************************************/
void SpiWrite(uchar byte)
{
uchar i;
DATA_BUF=byte; // Put function's 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;
}
/*******************************************************************************************/
//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)
}
void delays(void)
{
uchar i;
for (i=1800; i>0; --i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -