OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4397|回复: 1

请教nrf24l01接收调不通原因

[复制链接]

10

主题

16

帖子

0

精华

初级会员

Rank: 2

积分
79
金钱
79
注册时间
2013-2-11
在线时间
1 小时
发表于 2013-7-14 11:03:23 | 显示全部楼层 |阅读模式
手上有一对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); &nbsp;</div>
<div>&nbsp; <span class="Apple-tab-span" style="white-space:pre;">        </span>nRF_WriteRegister(WRITE_REG+EN_RXADDR,0x00)&nbsp;</div>
返回正常(0X01);应该说发射没问题<br />
<br />
问题是接收,我设置了SPI_RW_Reg(WRITE_REG + EN_AA, 0x00)关掉自动应答,接收端收不任何信号,如果把参数设置0X01打开自动应答,但是发送端没通电的情况下,接收端会有收到状态,缓存有0XFF的数据,&nbsp;我现在都不知道是不是模块坏了,麻烦各位帮忙看下,另外关于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 &lt;iom16v.h&gt;</div>
<div>#include &lt;macros.h&gt;</div>
<div>//------------------------------------------------------------------------------</div>
<div>//--------------------------------NRF24L01接口定义--------------------------------</div>
<div>//------------------------------------------------------------------------------</div>
<div>//#define NRF24L01_MISO &nbsp; &nbsp; &nbsp; &nbsp; &nbspB4<span class="Apple-tab-span" style="white-space:pre;">                </span>//输入0<span class="Apple-tab-span" style="white-space:pre;">        </span></div>
<div>#define &nbsp; Hign_24L01_MISO &nbsp; &nbspORTB |= (1 &lt;&lt; PB4)&nbsp;</div>
<div>#define &nbsp; Low_24L01_MISO &nbsp; &nbsp; PORTB &amp;= ~(1 &lt;&lt; PB4)</div>
<div>#define &nbsp; Read_24L01_MISO &nbsp; &nbspINB &amp; (1 &lt;&lt; PB4)&nbsp;</div>
<div><br />
</div>
<div>//#define NRF24L01_MOSI &nbsp; &nbsp; &nbsp; &nbsp; PB2 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span> &nbsp; &nbsp;//输出1</div>
<div>#define &nbsp; Hign_24L01_MOSI &nbsp; &nbspORTB |= (1 &lt;&lt; PB2) &nbsp;</div>
<div>#define &nbsp; Low_24L01_MOSI &nbsp; &nbsp; PORTB &amp;= ~(1 &lt;&lt; PB2)</div>
<div>#define &nbsp; Read_24L01_MOSI &nbsp; &nbspINB &amp; (1 &lt;&lt; PB2)&nbsp;</div>
<div><br />
</div>
<div>//#define NRF24L01_CSN &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre;">                </span>B7<span class="Apple-tab-span" style="white-space:pre;">        </span> &nbsp; &nbsp; &nbsp;//输出1</div>
<div>#define &nbsp; Low_24L01_CSN &nbsp; &nbsp; PORTB &amp;= ~(1 &lt;&lt; PB7)</div>
<div>#define &nbsp; Hign_24L01_CSN &nbsp; <span class="Apple-tab-span" style="white-space:pre;">        </span>ORTB |= (1 &lt;&lt; PB7)</div>
<div><br />
</div>
<div>//#define NRF24L01_SCK &nbsp;<span class="Apple-tab-span" style="white-space:pre;">                </span>B6 &nbsp; <span class="Apple-tab-span" style="white-space:pre;">        </span> &nbsp;//输出1</div>
<div>#define &nbsp; Hign_24L01_SCK &nbsp; &nbspORTB |= (1 &lt;&lt; PB6) &nbsp;&nbsp;</div>
<div>#define &nbsp; Low_24L01_SCK &nbsp; &nbsp; PORTB &amp;= ~(1 &lt;&lt; PB6) <span class="Apple-tab-span" style="white-space:pre;">                </span></div>
<div>#define &nbsp; Read_24L01_SCK &nbsp; &nbspINB &amp; (1 &lt;&lt; PB6);</div>
<div><br />
</div>
<div>//#define NRF24L01_CE &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span> &nbsp; &nbsp;PB5<span class="Apple-tab-span" style="white-space:pre;">        </span> &nbsp; &nbsp; &nbsp;//输出1</div>
<div>#define &nbsp; Hign_24L01_CE &nbsp; &nbsp;PORTB |= (1 &lt;&lt; PB5) &nbsp;</div>
<div>#define &nbsp; Low_24L01_CE &nbsp; &nbsp; PORTB &amp;= ~(1 &lt;&lt; PB5)<span class="Apple-tab-span" style="white-space:pre;">                </span></div>
<div>#define &nbsp; Read_24L01_CE &nbsp; &nbsp;PINB &amp; (1 &lt;&lt; PB5)</div>
<div><br />
</div>
<div>//#define &nbsp;NRF24L01_IRQ &nbsp; &nbsp; PB3<span class="Apple-tab-span" style="white-space:pre;">        </span> &nbsp; &nbsp; &nbsp; //输入</div>
<div>#define &nbsp; Hign_NRF24L01_IRQ &nbsp; &nbsp;PORTB |= (1 &lt;&lt; PB3) &nbsp;&nbsp;</div>
<div>#define &nbsp; Low_NRF24L01_IRQ &nbsp; &nbsp; PORTB &amp;= ~(1 &lt;&lt; PB3)</div>
<div>#define &nbsp; Read_NRF24L01_IRQ &nbsp; &nbsp;PINB &amp; (1 &lt;&lt; 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 &nbsp; &nbsp;5 &nbsp; <span class="Apple-tab-span" style="white-space:pre;">        </span>// 5 uints TX address width</div>
<div>#define RX_ADR_WIDTH &nbsp; &nbsp;5 &nbsp; <span class="Apple-tab-span" style="white-space:pre;">        </span>// 5 uints RX address width</div>
<div>#define TX_PLOAD_WIDTH &nbsp;32 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 20 uints TX payload</div>
<div>#define RX_PLOAD_WIDTH &nbsp;32 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 20 uints TX payload</div>
<div>char &nbsp;TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01};<span class="Apple-tab-span" style="white-space:pre;">        </span>//本地地址</div>
<div>char &nbsp;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 &nbsp; &nbsp; &nbsp; &nbsp;0x00 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 读寄存器指令</div>
<div>#define WRITE_REG &nbsp; &nbsp; &nbsp; 0x20 <span class="Apple-tab-span" style="white-space:pre;">        </span>// 写寄存器指令</div>
<div>#define RD_RX_PLOAD &nbsp; &nbsp; 0x61 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 读取接收数据指令</div>
<div>#define WR_TX_PLOAD &nbsp; &nbsp; 0xA0 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 写待发数据指令</div>
<div>#define FLUSH_TX &nbsp; &nbsp; &nbsp; &nbsp;0xE1 <span class="Apple-tab-span" style="white-space:pre;">        </span>// 冲洗发送 FIFO指令</div>
<div>#define FLUSH_RX &nbsp; &nbsp; &nbsp; &nbsp;0xE2 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 冲洗接收 FIFO指令</div>
<div>#define REUSE_TX_PL &nbsp; &nbsp; 0xE3 &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 定义重复装载数据指令</div>
<div>#define NOP1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xFF &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>// 保留</div>
<div>//*************************************SPI(nRF24L01)寄存器地址****************************************************</div>
<div>#define CONFIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x00 &nbsp;// 配置收发状态,CRC校验模式以及收发状态响应方式</div>
<div>#define EN_AA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x01 &nbsp;// 自动应答功能设置</div>
<div>#define EN_RXADDR &nbsp; &nbsp; &nbsp; 0x02 &nbsp;// 可用信道设置</div>
<div>#define SETUP_AW &nbsp; &nbsp; &nbsp; &nbsp;0x03 &nbsp;// 收发地址宽度设置</div>
<div>#define SETUP_RETR &nbsp; &nbsp; &nbsp;0x04 &nbsp;// 自动重发功能设置</div>
<div>#define RF_CH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x05 &nbsp;// 工作频率设置</div>
<div>#define RF_SETUP &nbsp; &nbsp; &nbsp; &nbsp;0x06 &nbsp;// 发射速率、功耗功能设置</div>
<div>#define STATUS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x07 &nbsp;// 状态寄存器</div>
<div>#define OBSERVE_TX &nbsp; &nbsp; &nbsp;0x08 &nbsp;// 发送监测功能</div>
<div>#define CD &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0x09 &nbsp;// 地址检测 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>#define RX_ADDR_P0 &nbsp; &nbsp; &nbsp;0x0A &nbsp;// 频道0接收数据地址</div>
<div>#define RX_ADDR_P1 &nbsp; &nbsp; &nbsp;0x0B &nbsp;// 频道1接收数据地址</div>
<div>#define RX_ADDR_P2 &nbsp; &nbsp; &nbsp;0x0C &nbsp;// 频道2接收数据地址</div>
<div>#define RX_ADDR_P3 &nbsp; &nbsp; &nbsp;0x0D &nbsp;// 频道3接收数据地址</div>
<div>#define RX_ADDR_P4 &nbsp; &nbsp; &nbsp;0x0E &nbsp;// 频道4接收数据地址</div>
<div>#define RX_ADDR_P5 &nbsp; &nbsp; &nbsp;0x0F &nbsp;// 频道5接收数据地址</div>
<div>#define TX_ADDR &nbsp; &nbsp; &nbsp; &nbsp; 0x10 &nbsp;// 发送地址寄存器</div>
<div>#define RX_PW_P0 &nbsp; &nbsp; &nbsp; &nbsp;0x11 &nbsp;// 接收频道0接收数据长度</div>
<div>#define RX_PW_P1 &nbsp; &nbsp; &nbsp; &nbsp;0x12 &nbsp;// 接收频道0接收数据长度</div>
<div>#define RX_PW_P2 &nbsp; &nbsp; &nbsp; &nbsp;0x13 &nbsp;// 接收频道0接收数据长度</div>
<div>#define RX_PW_P3 &nbsp; &nbsp; &nbsp; &nbsp;0x14 &nbsp;// 接收频道0接收数据长度</div>
<div>#define RX_PW_P4 &nbsp; &nbsp; &nbsp; &nbsp;0x15 &nbsp;// 接收频道0接收数据长度</div>
<div>#define RX_PW_P5 &nbsp; &nbsp; &nbsp; &nbsp;0x16 &nbsp;// 接收频道0接收数据长度</div>
<div>#define FIFO_STATUS &nbsp; &nbsp; 0x17 &nbsp;// 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&lt;s; i++);</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>for(i=0; i&lt;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>&nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>for(i=0;i&lt;8;i++) // output 8-bit</div>
<div>&nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>if(data &amp; 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 ; &nbsp; &nbsp; &nbsp; &nbsp; // 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;&nbsp;</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 &lt;&lt; 1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // shift next bit into MSB..</div>
<div><span class="Apple-tab-span" style="white-space:pre;">                </span>temp&lt;&lt;=1;</div>
<div><span class="Apple-tab-span" style="white-space:pre;">                </span>Hign_24L01_SCK ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Set SCK high..</div>
<div><span class="Apple-tab-span" style="white-space:pre;">                </span>if(PINB&amp;0x10)temp++; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre;">                </span> &nbsp;// capture current MISO bit</div>
<div><span class="Apple-tab-span" style="white-space:pre;">                </span>Low_24L01_SCK; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">                </span> &nbsp;// ..then set SCK low again</div>
<div>&nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>}</div>
<div>&nbsp; &nbsp; return(temp); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre;">                </span> &nbsp;// 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// CSN low, initialize SPI communication...</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>SPI_RW(reg); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Select register to read from..</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>reg_val = SPI_RW(0); &nbsp; &nbsp;// ..then read registervalue</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>Hign_24L01_CSN; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 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); &nbsp; &nbsp; &nbsp; &nbsp;// 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // CSN low, init SPI transaction</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>status = SPI_RW(reg); &nbsp; &nbsp; &nbsp;// select register</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>SPI_RW(value); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ..and write value to it..</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>Hign_24L01_CSN; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 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); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<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); &nbsp; &nbsp; &nbsp; <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&lt;uchars;uchar_ctr++)</div>
<div><span class="Apple-tab-span" style="white-space:pre;">                </span>pBuf[uchar_ctr] = SPI_RW(0); &nbsp; &nbsp;//&nbsp;</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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</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); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //SPI使能 &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>status1 = SPI_RW(reg); &nbsp;&nbsp;</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>for(uchar_ctr=0; uchar_ctr&lt;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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //关闭SPI</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>return(status1); &nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">                </span> &nbsp;//&nbsp;</div>
<div>}</div>
<div>/****************************************************************************************************/</div>
<div>/*函数:void SetRX_Mode(void)</div>
<div>/*功能:数据接收配置&nbsp;</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); &nbsp; <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;&nbsp;</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>&nbsp; &nbsp; 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> &nbsp; &nbsp; // 读取状态寄存其来判断数据接收状况<span class="Apple-tab-span" style="white-space:pre;">                </span></div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>if(sta&amp;0x40) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 判断是否接收到数据</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>{</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span> &nbsp; &nbsp;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); &nbsp; //接收到数据后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); &nbsp; <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>&nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>Low_24L01_CE; &nbsp; &nbsp;// chip enable</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>Hign_24L01_CSN; &nbsp; // Spi disable&nbsp;</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>Low_24L01_SCK; &nbsp; // 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); &nbsp; &nbsp;// 写本地地址<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); &nbsp; &nbsp; &nbsp;// &nbsp;频道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); &nbsp;// &nbsp;允许接收地址只有频道0,如果需要多频道可以参考Page21 &nbsp;</div>
<div><span class="Apple-tab-span" style="white-space:pre;">        </span>SPI_RW_Reg(WRITE_REG + RF_CH,40); &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp; 设置信道工作为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); &nbsp; <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); &nbsp; <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>&nbsp; &nbsp; Delay(6000);</div>
<div>}</div>
<div><br />
</div>
<div><br />
</div>
<div>//******************************************************************************</div>
<div>void main()</div>
<div>{</div>
<div>&nbsp;<span class="Apple-tab-span" style="white-space:pre;">        </span>char i,RxBuf[1];</div>
<div>&nbsp;<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>&nbsp; &nbsp; 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>&nbsp; &nbsp;<span class="Apple-tab-span" style="white-space:pre;">                </span>if(nRF24L01_RxPacket(RxBuf)) &nbsp; //判断是否收到数据</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显示接收到的数据 &nbsp; &nbsp; &nbsp;&nbsp;</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 />
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2013-7-14 15:05:26 | 显示全部楼层
参考下我们的例程试试吧.
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-23 18:26

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表