初级会员
- 积分
- 79
- 金钱
- 79
- 注册时间
- 2013-2-11
- 在线时间
- 1 小时
|
手上有一对NRF24L01模块,但是调试时这一对模块都出现相同问题<br />
<br />
我在调发射时修改了<span class="Apple-tab-span" style="white-space:pre;"> </span>nRF_WriteRegister(WRITE_REG+SETUP_RETR,0x00);<span class="Apple-tab-span" style="white-space:pre;"> </span><div><span class="Apple-tab-span" style="white-space:pre;"> </span>nRF_WriteRegister(WRITE_REG+EN_AA,0x00); </div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>nRF_WriteRegister(WRITE_REG+EN_RXADDR,0x00) </div>
返回正常(0X01);应该说发射没问题<br />
<br />
问题是接收,我设置了SPI_RW_Reg(WRITE_REG + EN_AA, 0x00)关掉自动应答,接收端收不任何信号,如果把参数设置0X01打开自动应答,但是发送端没通电的情况下,接收端会有收到状态,缓存有0XFF的数据, 我现在都不知道是不是模块坏了,麻烦各位帮忙看下,另外关于SPI是模拟的但是用硬件的话都是这样情况<br />
<div><br />
<br />
代码是这样的:<br />
<br />
<br />
<br />
//ICC-AVR application builder : 2007-8-11 20:37:32</div>
<div>// Target : M16</div>
<div>// Crystal: 8.0000Mhz</div>
<div>#include <iom16v.h></div>
<div>#include <macros.h></div>
<div>//------------------------------------------------------------------------------</div>
<div>//--------------------------------NRF24L01接口定义--------------------------------</div>
<div>//------------------------------------------------------------------------------</div>
<div>//#define NRF24L01_MISO  B4<span class="Apple-tab-span" style="white-space:pre;"> </span>//输入0<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>#define Hign_24L01_MISO  ORTB |= (1 << PB4) </div>
<div>#define Low_24L01_MISO PORTB &= ~(1 << PB4)</div>
<div>#define Read_24L01_MISO  INB & (1 << PB4) </div>
<div><br />
</div>
<div>//#define NRF24L01_MOSI PB2 <span class="Apple-tab-span" style="white-space:pre;"> </span> //输出1</div>
<div>#define Hign_24L01_MOSI  ORTB |= (1 << PB2) </div>
<div>#define Low_24L01_MOSI PORTB &= ~(1 << PB2)</div>
<div>#define Read_24L01_MOSI  INB & (1 << PB2) </div>
<div><br />
</div>
<div>//#define NRF24L01_CSN <span class="Apple-tab-span" style="white-space:pre;"> </span>B7<span class="Apple-tab-span" style="white-space:pre;"> </span> //输出1</div>
<div>#define Low_24L01_CSN PORTB &= ~(1 << PB7)</div>
<div>#define Hign_24L01_CSN <span class="Apple-tab-span" style="white-space:pre;"> </span>ORTB |= (1 << PB7)</div>
<div><br />
</div>
<div>//#define NRF24L01_SCK <span class="Apple-tab-span" style="white-space:pre;"> </span>B6 <span class="Apple-tab-span" style="white-space:pre;"> </span> //输出1</div>
<div>#define Hign_24L01_SCK  ORTB |= (1 << PB6) </div>
<div>#define Low_24L01_SCK PORTB &= ~(1 << PB6) <span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>#define Read_24L01_SCK  INB & (1 << PB6);</div>
<div><br />
</div>
<div>//#define NRF24L01_CE <span class="Apple-tab-span" style="white-space:pre;"> </span> PB5<span class="Apple-tab-span" style="white-space:pre;"> </span> //输出1</div>
<div>#define Hign_24L01_CE PORTB |= (1 << PB5) </div>
<div>#define Low_24L01_CE PORTB &= ~(1 << PB5)<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>#define Read_24L01_CE PINB & (1 << PB5)</div>
<div><br />
</div>
<div>//#define NRF24L01_IRQ PB3<span class="Apple-tab-span" style="white-space:pre;"> </span> //输入</div>
<div>#define Hign_NRF24L01_IRQ PORTB |= (1 << PB3) </div>
<div>#define Low_NRF24L01_IRQ PORTB &= ~(1 << PB3)</div>
<div>#define Read_NRF24L01_IRQ PINB & (1 << PB3)</div>
<div><br />
</div>
<div>char TxBuf[32]=</div>
<div>{</div>
<div>0x01,0x02,0x03,0x4,0x05,0x06,0x07,0x08,</div>
<div>0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,</div>
<div>0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,</div>
<div>0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,</div>
<div>};</div>
<div>char sta,count;</div>
<div>//*********************************************NRF24L01*************************************</div>
<div>#define TX_ADR_WIDTH 5 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 5 uints TX address width</div>
<div>#define RX_ADR_WIDTH 5 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 5 uints RX address width</div>
<div>#define TX_PLOAD_WIDTH 32 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 20 uints TX payload</div>
<div>#define RX_PLOAD_WIDTH 32 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 20 uints TX payload</div>
<div>char TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01};<span class="Apple-tab-span" style="white-space:pre;"> </span>//本地地址</div>
<div>char RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01};<span class="Apple-tab-span" style="white-space:pre;"> </span>//接收地址</div>
<div>//***************************************NRF24L01寄存器指令*******************************************************</div>
<div>#define READ_REG 0x00 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 读寄存器指令</div>
<div>#define WRITE_REG 0x20 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 写寄存器指令</div>
<div>#define RD_RX_PLOAD 0x61 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 读取接收数据指令</div>
<div>#define WR_TX_PLOAD 0xA0 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 写待发数据指令</div>
<div>#define FLUSH_TX 0xE1 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 冲洗发送 FIFO指令</div>
<div>#define FLUSH_RX 0xE2 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 冲洗接收 FIFO指令</div>
<div>#define REUSE_TX_PL 0xE3 <span class="Apple-tab-span" style="white-space:pre;"> </span>// 定义重复装载数据指令</div>
<div>#define NOP1 0xFF <span class="Apple-tab-span" style="white-space:pre;"> </span>// 保留</div>
<div>//*************************************SPI(nRF24L01)寄存器地址****************************************************</div>
<div>#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式</div>
<div>#define EN_AA 0x01 // 自动应答功能设置</div>
<div>#define EN_RXADDR 0x02 // 可用信道设置</div>
<div>#define SETUP_AW 0x03 // 收发地址宽度设置</div>
<div>#define SETUP_RETR 0x04 // 自动重发功能设置</div>
<div>#define RF_CH 0x05 // 工作频率设置</div>
<div>#define RF_SETUP 0x06 // 发射速率、功耗功能设置</div>
<div>#define STATUS 0x07 // 状态寄存器</div>
<div>#define OBSERVE_TX 0x08 // 发送监测功能</div>
<div>#define CD 0x09 // 地址检测 </div>
<div>#define RX_ADDR_P0 0x0A // 频道0接收数据地址</div>
<div>#define RX_ADDR_P1 0x0B // 频道1接收数据地址</div>
<div>#define RX_ADDR_P2 0x0C // 频道2接收数据地址</div>
<div>#define RX_ADDR_P3 0x0D // 频道3接收数据地址</div>
<div>#define RX_ADDR_P4 0x0E // 频道4接收数据地址</div>
<div>#define RX_ADDR_P5 0x0F // 频道5接收数据地址</div>
<div>#define TX_ADDR 0x10 // 发送地址寄存器</div>
<div>#define RX_PW_P0 0x11 // 接收频道0接收数据长度</div>
<div>#define RX_PW_P1 0x12 // 接收频道0接收数据长度</div>
<div>#define RX_PW_P2 0x13 // 接收频道0接收数据长度</div>
<div>#define RX_PW_P3 0x14 // 接收频道0接收数据长度</div>
<div>#define RX_PW_P4 0x15 // 接收频道0接收数据长度</div>
<div>#define RX_PW_P5 0x16 // 接收频道0接收数据长度</div>
<div>#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置</div>
<div>//**************************************************************************************</div>
<div>void Delay(int s);</div>
<div>void inerDelay_us(char n);</div>
<div>void init_NRF24L01(void);</div>
<div>char SPI_RW(char data);</div>
<div>char SPI_Read(char reg);</div>
<div>char SPI_RW_Reg(char reg, char value);</div>
<div>char SPI_Read_Buf(char reg, char *pBuf, char uchars);</div>
<div>char SPI_Write_Buf(char reg, char *pBuf, char uchars);</div>
<div>void SetRX_Mode(void);</div>
<div>char nRF24L01_RxPacket(char* rx_buf);</div>
<div>void nRF24L01_TxPacket(char * tx_buf);</div>
<div>void StartUART(void);</div>
<div>void R_S_Byte(char R_Byte);</div>
<div>void display();</div>
<div><br />
</div>
<div>//*****************************************长延时*****************************************</div>
<div>void Delay(int s)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>unsigned int i;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(i=0; i<s; i++);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(i=0; i<s; i++);</div>
<div>}</div>
<div><br />
</div>
<div>/****************************************************************************************************</div>
<div>/*函数:uint SPI_RW(uint uchar)</div>
<div>/*功能:NRF24L01的SPI写时序</div>
<div>/****************************************************************************************************/</div>
<div>char SPI_RW(char data)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>char i,temp=0;</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>for(i=0;i<8;i++) // output 8-bit</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if(data & 0x80)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_MOSI ; // output 'uchar', MSB to MOSI</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>else</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> <span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_MOSI; </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>data = (data << 1); // shift next bit into MSB..</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>temp<<=1;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_SCK ; // Set SCK high..</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if(PINB&0x10)temp++; <span class="Apple-tab-span" style="white-space:pre;"> </span> // capture current MISO bit</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_SCK; <span class="Apple-tab-span" style="white-space:pre;"> </span> // ..then set SCK low again</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div> return(temp); <span class="Apple-tab-span" style="white-space:pre;"> </span> // return read uchar</div>
<div>}</div>
<div>/****************************************************************************************************</div>
<div>/*函数:uchar SPI_Read(uchar reg)</div>
<div>/*功能:NRF24L01的SPI时序</div>
<div>/****************************************************************************************************/</div>
<div>char SPI_Read(char reg)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>char reg_val;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_CSN; // CSN low, initialize SPI communication...</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW(reg); // Select register to read from..</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>reg_val = SPI_RW(0); // ..then read registervalue</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_CSN; // CSN high, terminate SPI communication</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return(reg_val); // return register value</div>
<div>}</div>
<div>/****************************************************************************************************/</div>
<div>/*功能:NRF24L01读写寄存器函数</div>
<div>/****************************************************************************************************/</div>
<div>char SPI_RW_Reg(char reg, char value)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>char status;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_CSN; // CSN low, init SPI transaction</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>status = SPI_RW(reg); // select register</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW(value); // ..and write value to it..</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_CSN; // CSN high again</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return(status); // return nRF24L01 status uchar</div>
<div>}</div>
<div>/****************************************************************************************************/</div>
<div>/*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)</div>
<div>/*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数</div>
<div>/****************************************************************************************************/</div>
<div>char SPI_Read_Buf(char reg, char *pBuf, char uchars)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>int status1,uchar_ctr;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_CSN; <span class="Apple-tab-span" style="white-space:pre;"> </span>// Set CSN low, init SPI tranaction</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>status1 = SPI_RW(reg); <span class="Apple-tab-span" style="white-space:pre;"> </span>// Select register to write to and read status uchar</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>pBuf[uchar_ctr] = SPI_RW(0); // </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_CSN; </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return(status1); // return nRF24L01 status uchar</div>
<div>}</div>
<div>/*********************************************************************************************************</div>
<div>/*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)</div>
<div>/*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数</div>
<div>/*********************************************************************************************************/</div>
<div>char SPI_Write_Buf(char reg, char *pBuf, char uchars)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>char status1,uchar_ctr;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_CSN; //SPI使能 </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>status1 = SPI_RW(reg); </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>for(uchar_ctr=0; uchar_ctr<uchars; uchar_ctr++) //</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW(*pBuf++);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_CSN; //关闭SPI</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return(status1); <span class="Apple-tab-span" style="white-space:pre;"> </span> // </div>
<div>}</div>
<div>/****************************************************************************************************/</div>
<div>/*函数:void SetRX_Mode(void)</div>
<div>/*功能:数据接收配置 </div>
<div>/****************************************************************************************************/</div>
<div>void SetRX_Mode(void)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_CE;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); <span class="Apple-tab-span" style="white-space:pre;"> </span>// IRQ收发完成中断响应,16位CRC<span class="Apple-tab-span" style="white-space:pre;"> </span>,主接收</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_CE; </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Delay(1000);//注意不能太小</div>
<div>}</div>
<div>/******************************************************************************************************/</div>
<div>/*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)</div>
<div>/*功能:数据读取后放如rx_buf接收缓冲区中</div>
<div>/******************************************************************************************************/</div>
<div>char nRF24L01_RxPacket(char* rx_buf)</div>
<div>{</div>
<div> char revale=0;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>sta=SPI_Read(STATUS);<span class="Apple-tab-span" style="white-space:pre;"> </span> // 读取状态寄存其来判断数据接收状况<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>if(sta&0x40) // 判断是否接收到数据</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span> Low_24L01_CE; <span class="Apple-tab-span" style="white-space:pre;"> </span>//SPI使能</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>revale =1;<span class="Apple-tab-span" style="white-space:pre;"> </span>//读取数据完成标志</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>return revale;</div>
<div>}</div>
<div>/***********************************************************************************************************</div>
<div>/*函数:void nRF24L01_TxPacket(char * tx_buf)</div>
<div>/*功能:发送 tx_buf中数据</div>
<div>/**********************************************************************************************************/</div>
<div>void nRF24L01_TxPacket(char * tx_buf)</div>
<div>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_CE;<span class="Apple-tab-span" style="white-space:pre;"> </span>//StandBy I模式<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); <span class="Apple-tab-span" style="white-space:pre;"> </span> // 装载数据<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>//<span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); <span class="Apple-tab-span" style="white-space:pre;"> </span> // IRQ收发完成中断响应,16位CRC,主发送</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_CE;<span class="Apple-tab-span" style="white-space:pre;"> </span> //置高CE,激发数据发送</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>Delay(600);</div>
<div>}</div>
<div>//****************************************************************************************</div>
<div>/*NRF24L01初始化</div>
<div>//***************************************************************************************/</div>
<div>void init_NRF24L01(void)</div>
<div>{</div>
<div><br />
</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_CE; // chip enable</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>Hign_24L01_CSN; // Spi disable </div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>Low_24L01_SCK; // Spi clock line init high</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG + EN_AA, 0x00); // 频道0自动<span class="Apple-tab-span" style="white-space:pre;"> </span>ACK应答允许,测试时为0关掉<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21 </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG + RF_CH,40); // 设置信道工作为2.4GHZ,收发必须一致</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//SPI_RW_Reg(WRITE_REG + RX_PW_P0, 1);</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); <span class="Apple-tab-span" style="white-space:pre;"> </span>//设置发射速率为1MHZ,发射功率为最大值0dB</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); <span class="Apple-tab-span" style="white-space:pre;"> </span>// IRQ收发完成中断响应,16位CRC<span class="Apple-tab-span" style="white-space:pre;"> </span>,主接收}</div>
<div> Delay(6000);</div>
<div>}</div>
<div><br />
</div>
<div><br />
</div>
<div>//******************************************************************************</div>
<div>void main()</div>
<div>{</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>char i,RxBuf[1];</div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>DDRB=0xe4;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>DDRC=0xff;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>DDRA=0xFF;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>PORTA=0x01;</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>PORTC=0x00;</div>
<div>//******************************************************************************</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>//StartUART();</div>
<div> init_NRF24L01() ;SetRX_Mode();</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>while(1)</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div> <span class="Apple-tab-span" style="white-space:pre;"> </span>if(nRF24L01_RxPacket(RxBuf)) //判断是否收到数据</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>{PORTA|=0x02;//PA2口点亮LED状态灯</div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>PORTC|=RxBuf[0];//LED显示接收到的数据 </div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;"> </span>}<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div>}</div>
<br />
<br /> |
|