各位大神,我在用NRF24L01+模块时遇到一个问题,就是模块工作一段时间后接收端就不能正常工作了,需要重启接收端NRF24L01+模块才能恢复工作。 这个是接收端单片机程序
- #include<reg52.h>
- #include<api.h>
- #include<intrins.h>
- #include <DS18B20.h>
- #include<lcd12864.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
- #define TX_PLOAD_WIDTH 10 // 20 bytes TX payload
- uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
- uchar rx_buf[TX_PLOAD_WIDTH];
- uchar tx_buf[TX_PLOAD_WIDTH];
- /**************************************************/
- sbit CE = P1^2;
- sbit CSN= P1^3;
- sbit SCK= P1^7;
- sbit MOSI= P1^5;
- sbit MISO= P1^6;
- sbit IRQ = P1^4;
- /**************************************************/
- uchar bdata sta;
- sbit RX_DR =sta^6;
- sbit TX_DS =sta^5;
- sbit MAX_RT =sta^4;
- /**************************************************/
- sbit JDQ = P2^0;
- uint temperature;
- //NRF24L01+
- void delayus(uint z)
- {
- while(z!=0) z--;
- }
- uchar SPI_RW(uchar dat)//写一字节并读出此地址的状态
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- SCK=0;
- MOSI=(dat & 0x80);
- dat<<=1;
- SCK=1;
- dat|=MISO;
- }
- SCK=0; //拉低时钟保持通信状态
- return dat;
- }
- uchar SPI_RW_Reg(uchar reg,value)//写一字节并读出此地址的状态
- {
- uchar status;
- CSN=0;
- status=SPI_RW(reg);
- SPI_RW(value);
- CSN=1;
- return status;
- }
- uchar SPI_Read(uchar reg) //读一字节
- {
- uchar value;
- CSN=0;
- SPI_RW(reg);
- value=SPI_RW(0);
- CSN=1;
- return value;
- }
- uchar SPI_Read_Buf(uchar reg,uchar *pBuf,uchar uchars)//读多个字节并读出此地址的状态
- {
- uchar status,i;
- CSN=0;
- status=SPI_RW(reg);
- for(i=0;i<uchars;i++)
- pBuf[i]=SPI_RW(0);
- CSN=1;
- return status;
- }
- uchar SPI_write_Buf(uchar reg,uchar *pBuf,uchar uchars)//写多个字节并读出此地址的状态
- {
- uchar status,i;
- CSN=0;
- status=SPI_RW(reg);
- for(i=0;i<uchars;i++)
- SPI_RW(*pBuf++);
- CSN=1;
- return status;
- }/*
- void nrf24l01_TXBUF(uchar *tx_buf) //发送tx_buf中的数据
- {
- CE=0;//停止射频工作
- SPI_write_Buf(0x20+10,0x01,5);//写本机地址
- SPI_write_Buf(0x20+10,0x01,5);//装载接收端O通道地址
- SPI_write_Buf(0xa0,tx_buf,2);//装载要发送的数据
- SPI_RW_Reg(0x20+0x00,0x0e);//IRQ收发完成中断响应16位CRC,发送模式
- CE=1; //置高CE,激发数据发射
- delay(1);
- } */
- void nrf24l01_init()
- {
- CE=0;
- CSN=1;
- SCK=0;
- IRQ=1;
- delayus(15);
- }
- void setRX_Mode()
- {
- CE=0;
- SPI_write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 写接收地址到0通道
- SPI_RW_Reg(WRITE_REG + EN_AA, 0x00); // Enable Auto.Ack:Pipe0
- SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
- SPI_RW_Reg(WRITE_REG + RF_CH,40); // Select 工作频段 channel 2.4G
- SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
- SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
- SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); //IRQ中断响应16位CRC校验,接收模式
- CE = 1; // Set CE pin high to enable RX device
- delayus(35);
- }
- bit nRF24L01_RxPacket(uchar *rx_buf)
- {
- bit flag=0;
- sta=SPI_Read(STATUS); // read register STATUS's value
- if(RX_DR) // if receive data ready (RX_DR) interrupt
- {
- SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
- flag=1;
- SPI_RW_Reg(WRITE_REG+STATUS,0xff);//清空状态寄存器
- }
- return flag;
- }
- void DS18B20_dis()
- {
- lcd_dis(0,4,uchartostr(rx_buf[0]));
- lcd_dis(0,5,".");
- lcd_dis(0,6,uchartostr(rx_buf[1]));
- lcd_dis(0,7,"℃");
- }
- void DS1302_dis()
- {
- lcd_dis(2,3,DS1302tostr(rx_buf[2]));
- lcd_dis(2,4,":");
- lcd_dis(2,5,DS1302tostr(rx_buf[3]));
- lcd_dis(2,6,":");
- lcd_dis(2,7,DS1302tostr(rx_buf[4]));
- //switch(rx_buf[2])
- //{
- //case 1: JDQ = 0; lcd_dis(2,4,"开");break; //定时脉冲宽度;
- //case 2: JDQ = 1; lcd_dis(2,5,"关");break; //补充5ms;
- //}
-
- }
- uchar ds2_dis[2];
- void DS18B20_dis2()
- {
- if(ds_reset())
- {
- temperature=get_temperature();
- ds2_dis[0]=temperature/10;
- ds2_dis[1]=temperature%100%10;
- lcd_dis(1,4,uchartostr(ds2_dis[0]));
- lcd_dis(1,5,".");
- lcd_dis(1,6,uchartostr(ds2_dis[1]));
- lcd_dis(1,7,"℃");
- }
- else
- {
- lcd_dis(1,4,"无硬件!");
- }
- }
- void main()
- {
- uchar i=0;
- uchar buf0[5]={0,0,0,0,0};
- lcd_init();
- nrf24l01_init();
- setRX_Mode();
- SPI_Read_Buf(READ_REG+RX_ADDR_P0,buf0,TX_PLOAD_WIDTH);
- delay(100);
- lcd_dis(0,0,"RX_ADDR_P0:");
- lcd_dis(1,0,uchartostr(buf0[0]));
- lcd_dis(1,2,uchartostr(buf0[1]));
- lcd_dis(1,4,uchartostr(buf0[2]));
- lcd_dis(1,6,uchartostr(buf0[3]));
- lcd_dis(2,0,uchartostr(buf0[4]));
- delay(1000);
- while(1)
- {
- if(nRF24L01_RxPacket(rx_buf))
- {
- lcd_wcmd(0x01); //清除LCD的显示内容
- delay(5);
- lcd_dis(0,0,"室外温度");
- lcd_dis(1,0,"室内温度");
- lcd_dis(2,0,"时间:");//显示提示符
- lcd_dis(3,1,"2.4G无线测试");
- DS18B20_dis();
- DS1302_dis();
- DS18B20_dis2();
- delay(910);
- }else
- {delay(5);lcd_dis(0,4,"无信息!");delay(800);}
- }
- }
- 发射端程序
- #include <reg52.h>
- #include <intrins.h>
- #include <api.h>
- #include <DS18B20.h>
- #include <DS1302.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
- #define TX_PLOAD_WIDTH 10 // 20 bytes TX payload
- uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
- //uchar rx_buf[TX_PLOAD_WIDTH];
- uchar tx_buf[TX_PLOAD_WIDTH];
- /**************************************************/
- sbit CE = P1^2;
- sbit CSN= P1^3;
- sbit SCK= P1^7;
- sbit MOSI= P1^5;
- sbit MISO= P1^6;
- sbit IRQ = P1^4;
- /**************************************************/
- uchar bdata sta;
- sbit RX_DR =sta^6;
- sbit TX_DS =sta^5;
- sbit MAX_RT =sta^4;
- //uchar str[4];//uchartostr函数转换的字符串
- sbit K = P3^0;sbit G = P3^1;
- uint temperature;
- /*uchar RXaddr[5]={0x20,0x20,0x03,0x04,0x05};
- uchar TXaddr[5]={0x20,0x20,0x03,0x04,0x05};
- uchar const TXdat[]={0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,};*/
- /*uchar *uchartostr(uchar num) //将一个字节的数据转换为字符串
- {
- uchar x2,x1,x0,i;
- x2=num/100;
- x1=num%100/10;
- x0=num%100%10;
- i=0;
- if(x2!=0)
- {
- str[i]=x2+48;
- i++;
- }
- if(x1!=0)
- {
- str[i]=x1+48;
- i++;
- }
- str[i]=x0+48;
- i++;
- str[i]='\0';
- return str;
- }*/
- //NRF24L01+
- uchar SPI_RW(uchar dat)//写一字节并读出此地址的状态
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- MOSI=(dat & 0x80);
- dat<<=1;
- SCK=1;
- dat|=MISO;
- SCK=0;
- }
- return dat;
- }
- uchar SPI_RW_Reg(uchar reg,value)//写一字节并读出此地址的状态
- {
- uchar status;
- CSN=0;
- status=SPI_RW(reg);
- SPI_RW(value);
- CSN=1;
- return status;
- }
- uchar SPI_Read(uchar reg) //读一字节
- {
- uchar value;
- CSN=0;
- SPI_RW(reg);
- value=SPI_RW(0);
- CSN=1;
- return value;
- }
- //read buffer!
- /*uchar SPI_Read_Buf(uchar reg,uchar *pBuf,uchar uchars)//读多个字节并读出此地址的状态
- {
- uchar status,i;
- CSN=0;
- status=SPI_RW(reg);
- for(i=0;i<uchars;i++)
- pBuf[i]=SPI_RW(0);
- CSN=1;
- return status;
- }*/
- uchar SPI_write_Buf(uchar reg,uchar *pBuf,uchar uchars)//写多个字节并读出此地址的状态
- {
- uchar status,i;
- CSN=0;
- status=SPI_RW(reg);
- for(i=0;i<uchars;i++)
- SPI_RW(*pBuf++);
- CSN=1;
- return status;
- }
- void nrf24l01_TxPacket(uchar *tx_buf) //发送tx_buf中的数据
- {
- CE=0;//停止射频工作
- SPI_write_Buf(WRITE_REG+TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);//写本机地址
- SPI_write_Buf(WRITE_REG+RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);//装载接收端O通道地址
- SPI_write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);//装载要发送的数据
- SPI_RW_Reg(WRITE_REG+CONFIG,0x0e);//IRQ收发完成中断响应16位CRC,发送模式
- CE=1; //置高CE,激发数据发射
- delay(1);
- }
- void nrf24l01_init()
- {
- delay(1);
- CSN=1;
- SCK=0;
- IRQ=1;
- CE=0;
- //SPI_write_Buf(0x30,TXaddr,5);
- //SPI_Read_Buf(0x10,RXaddr,5);
- SPI_RW_Reg(WRITE_REG+EN_AA,0x00);//频道0自动ACK
- SPI_RW_Reg(WRITE_REG+SETUP_RETR,0x00);//禁止自动重发
- SPI_RW_Reg(WRITE_REG+EN_RXADDR,0x01);//允许接收地址只有通道0
- SPI_RW_Reg(WRITE_REG+RF_CH,40); //设置信道工作频率为2.4GHZ。收发必须一致
- SPI_RW_Reg(WRITE_REG+RX_PW_P0,TX_PLOAD_WIDTH); //设置接收数据长度
- SPI_RW_Reg(WRITE_REG+RF_SETUP,0x07);//发射速率为2MHZ,发射功率为最大值0db
- }
- void DSdat_tostr()
- {
- temperature=get_temperature();
- tx_buf[0]=temperature/10;
- tx_buf[1]=temperature%100%10;
- }
- void Read_DS1302_Dat()
- {
- uchar hour,minute,second;
- write_1302(0x8e,0x00);
-
- hour=read_1302(0x85);
-
- minute=read_1302(0x83);
-
- second=read_1302(0x81);
- write_1302(0x8e,0x80);
- tx_buf[2]=hour;
- tx_buf[3]=minute;
- tx_buf[4]=second;
- }
- void main()
- {
- nrf24l01_init();
- ds1302_init();
- while(1)
- { if(K ==0)tx_buf[2]=1;
- if(G ==0)tx_buf[2]=2;
- DSdat_tostr();
- Read_DS1302_Dat();
- SPI_RW_Reg(WRITE_REG+STATUS,0xff); //清除状态寄存器
- nrf24l01_TxPacket(tx_buf); //进行一次发送操?
- delay(400);
- }
- }
复制代码
|