OpenEdv-开源电子网

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

关于NRF24L01的问题。。。求指导。。。

[复制链接]

12

主题

24

帖子

0

精华

初级会员

Rank: 2

积分
92
金钱
92
注册时间
2011-7-14
在线时间
0 小时
发表于 2011-8-1 19:21:18 | 显示全部楼层 |阅读模式
<p>  #include&lt;reg51.h&gt;<br />
&nbsp;#include&lt;intrins.h&gt;<br />
&nbsp;#define uchar unsigned char<br />
&nbsp;#define uint unsigned int<br />
&nbsp;sbit k1=P3^2;<br />
&nbsp;sbit k2=P3^3;<br />
&nbsp;sbit k3=P3^4;<br />
&nbsp;sbit LED0=P0^0;<br />
&nbsp;sbit LED1=P0^1;<br />
&nbsp;sbit LED2=P0^2;</p>
<p>&nbsp;uchar Txbuf[]={0x01,0x03,0x03,0x01};<br />
&nbsp; /**************************************<br />
&nbsp; ********延时***********<br />
&nbsp; void Delay(uint i)<br />
&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint j;<br />
&nbsp;&nbsp;&nbsp; for(;i&gt;0;i--)<br />
&nbsp;&nbsp;&nbsp; for(j=0;j++;j&lt;125)<br />
&nbsp;&nbsp;&nbsp; {;}<br />
&nbsp;&nbsp;&nbsp; }&nbsp; */<br />
/****************************************<br />
**********NRF24L01 端口定义**********************/<br />
sbit IRQ=P2^7;<br />
sbit SCK=P2^1;<br />
sbit CE=P2^2;<br />
sbit CSN=P2^5;<br />
sbit MOSI=P2^6;<br />
sbit MISO=P2^0;</p>
<p>#define TX_ADR_WIDTH&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp; &nbsp;// 5 uints TX address width<br />
#define RX_ADR_WIDTH&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp; &nbsp;// 5 uints RX address width<br />
#define TX_PLOAD_WIDTH&nbsp; 32 &nbsp;// 20 uints TX payload<br />
#define RX_PLOAD_WIDTH&nbsp; 32&nbsp; &nbsp;// 20 uints TX payload<br />
uint const TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01};&nbsp;//本地地址<br />
uint const RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01};&nbsp;//接收地址<br />
//***************************************NRF24L01寄存器指令*******************************************************<br />
#define READ_REG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x00&nbsp; &nbsp;// 读寄存器指令<br />
#define WRITE_REG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x20 &nbsp;// 写寄存器指令<br />
#define RD_RX_PLOAD&nbsp;&nbsp;&nbsp;&nbsp; 0x61&nbsp; &nbsp;// 读取接收数据指令<br />
#define WR_TX_PLOAD&nbsp;&nbsp;&nbsp;&nbsp; 0xA0&nbsp; &nbsp;// 写待发数据指令<br />
#define FLUSH_TX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0xE1 &nbsp;// 冲洗发送 FIFO指令<br />
#define FLUSH_RX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0xE2&nbsp; &nbsp;// 冲洗接收 FIFO指令<br />
#define REUSE_TX_PL&nbsp;&nbsp;&nbsp;&nbsp; 0xE3&nbsp; &nbsp;// 定义重复装载数据指令<br />
#define NOP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0xFF&nbsp; &nbsp;// 保留<br />
//*************************************SPI(nRF24L01)寄存器地址****************************************************<br />
#define CONFIG&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x00&nbsp; // 配置收发状态,CRC校验模式以及收发状态响应方式<br />
#define EN_AA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x01&nbsp; // 自动应答功能设置<br />
#define EN_RXADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x02&nbsp; // 可用信道设置<br />
#define SETUP_AW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x03&nbsp; // 收发地址宽度设置<br />
#define SETUP_RETR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x04&nbsp; // 自动重发功能设置<br />
#define RF_CH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x05&nbsp; // 工作频率设置<br />
#define RF_SETUP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x06&nbsp; // 发射速率、功耗功能设置<br />
#define STATUS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x07&nbsp; // 状态寄存器<br />
#define OBSERVE_TX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x08&nbsp; // 发送监测功能<br />
#define CD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x09&nbsp; // 地址检测&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
#define RX_ADDR_P0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x0A&nbsp; // 频道0接收数据地址<br />
#define RX_ADDR_P1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x0B&nbsp; // 频道1接收数据地址<br />
#define RX_ADDR_P2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x0C&nbsp; // 频道2接收数据地址<br />
#define RX_ADDR_P3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x0D&nbsp; // 频道3接收数据地址<br />
#define RX_ADDR_P4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x0E&nbsp; // 频道4接收数据地址<br />
#define RX_ADDR_P5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x0F&nbsp; // 频道5接收数据地址<br />
#define TX_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x10&nbsp; // 发送地址寄存器<br />
#define RX_PW_P0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x11&nbsp; // 接收频道0接收数据长度<br />
#define RX_PW_P1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x12&nbsp; // 接收频道0接收数据长度<br />
#define RX_PW_P2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x13&nbsp; // 接收频道0接收数据长度<br />
#define RX_PW_P3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x14&nbsp; // 接收频道0接收数据长度<br />
#define RX_PW_P4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x15&nbsp; // 接收频道0接收数据长度<br />
#define RX_PW_P5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x16&nbsp; // 接收频道0接收数据长度<br />
#define FIFO_STATUS&nbsp;&nbsp;&nbsp;&nbsp; 0x17&nbsp; // FIFO栈入栈出状态寄存器设置<br />
//**************************************************************************************<br />
void Delay(unsigned int s);<br />
void inerDelay_us(unsigned char n);<br />
void init_NRF24L01(void);<br />
uint SPI_RW(uint uhar);<br />
uchar SPI_Read(uchar reg);<br />
void SetRX_Mode(void);<br />
uint SPI_RW_Reg(uchar reg, uchar value);<br />
uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars);<br />
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars);<br />
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);<br />
int nRF24L01_TxPacket(unsigned char * tx_buf);<br />
//*****************************************长延时*****************************************<br />
void Delay(unsigned int s)<br />
{<br />
&nbsp;unsigned int i;<br />
&nbsp;for(i=0; i&lt;s; i++);<br />
&nbsp;for(i=0; i&lt;s; i++);<br />
}<br />
//******************************************************************************************<br />
uint &nbsp;bdata sta;&nbsp;&nbsp; //状态标志<br />
sbit&nbsp;RX_DR&nbsp;=sta^6;<br />
sbit&nbsp;TX_DS&nbsp;=sta^5;<br />
sbit&nbsp;MAX_RT&nbsp;=sta^4;<br />
/******************************************************************************************<br />
/*延时函数<br />
/******************************************************************************************/<br />
void inerDelay_us(unsigned char n)<br />
{<br />
&nbsp;for(;n&gt;0;n--)<br />
&nbsp;&nbsp;_nop_();<br />
}<br />
//****************************************************************************************<br />
/*NRF24L01初始化<br />
//***************************************************************************************/<br />
void init_NRF24L01(void)<br />
{<br />
&nbsp;&nbsp; // inerDelay_us(100);<br />
&nbsp;&nbsp;CE=0;&nbsp;&nbsp;&nbsp; // chip enable<br />
&nbsp;&nbsp;CSN=1;&nbsp;&nbsp; // Spi disable <br />
&nbsp;&nbsp;SCK=0;&nbsp;&nbsp; // Spi clock line init high<br />
&nbsp;&nbsp;&nbsp; IRQ = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 中断复位<br />
}<br />
/****************************************************************************************************<br />
/*函数:uint SPI_RW(uint uchar)<br />
/*功能:NRF24L01的SPI写时序<br />
/****************************************************************************************************/<br />
uint SPI_RW(uint uhar)<br />
{<br />
&nbsp;uint bit_ctr;<br />
&nbsp;&nbsp; &nbsp;for(bit_ctr=0;bit_ctr&lt;8;bit_ctr++) // output 8-bit<br />
&nbsp;&nbsp; &nbsp;{<br />
&nbsp;&nbsp;MOSI = (uhar &amp; 0x80);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // output 'uchar', MSB to MOSI<br />
&nbsp;&nbsp;uhar = (uhar &lt;&lt; 1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // shift next bit into MSB..<br />
&nbsp;&nbsp;SCK = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Set SCK high..<br />
&nbsp;&nbsp;uhar |= MISO;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // capture current MISO bit<br />
&nbsp;&nbsp;SCK = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // ..then set SCK low again<br />
&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp;&nbsp; return(uhar);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // return read uchar<br />
}<br />
/****************************************************************************************************<br />
/*函数:uchar SPI_Read(uchar reg)<br />
/*功能:NRF24L01的SPI时序<br />
/****************************************************************************************************/<br />
uchar SPI_Read(uchar reg)<br />
{<br />
&nbsp;uchar reg_val;<br />
&nbsp;<br />
&nbsp;CSN = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // CSN low, initialize SPI communication...<br />
&nbsp;SPI_RW(reg);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Select register to read from..<br />
&nbsp;reg_val = SPI_RW(0);&nbsp;&nbsp;&nbsp; // ..then read registervalue<br />
&nbsp;CSN = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // CSN high, terminate SPI communication<br />
&nbsp;<br />
&nbsp;return(reg_val);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // return register value<br />
}<br />
/****************************************************************************************************/<br />
/*功能:NRF24L01读写寄存器函数<br />
/****************************************************************************************************/<br />
uint SPI_RW_Reg(uchar reg, uchar value)<br />
{<br />
&nbsp;uint status;<br />
&nbsp;<br />
&nbsp;CSN = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // CSN low, init SPI transaction<br />
&nbsp;status = SPI_RW(reg);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // select register<br />
&nbsp;SPI_RW(value);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // ..and write value to it..<br />
&nbsp;CSN = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // CSN high again<br />
&nbsp;<br />
&nbsp;return(status);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // return nRF24L01 status uchar<br />
}<br />
/****************************************************************************************************/<br />
/*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)<br />
/*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数<br />
/****************************************************************************************************/<br />
uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)<br />
{<br />
&nbsp;uint status,uchar_ctr;<br />
&nbsp;<br />
&nbsp;CSN = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;// Set CSN low, init SPI tranaction<br />
&nbsp;status = SPI_RW(reg);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;// Select register to write to and read status uchar<br />
&nbsp;<br />
&nbsp;for(uchar_ctr=0;uchar_ctr&lt;uchars;uchar_ctr++)<br />
&nbsp;&nbsp;pBuf[uchar_ctr] = SPI_RW(0);&nbsp;&nbsp;&nbsp; // <br />
&nbsp;<br />
&nbsp;CSN = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;<br />
&nbsp;return(status);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // return nRF24L01 status uchar<br />
}<br />
/*********************************************************************************************************<br />
/*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)<br />
/*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数<br />
/*********************************************************************************************************/<br />
uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)<br />
{<br />
&nbsp;uint status,uchar_ctr;<br />
&nbsp;<br />
&nbsp;CSN = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //SPI使能&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;status = SPI_RW(reg);&nbsp;&nbsp; <br />
&nbsp;for(uchar_ctr=0; uchar_ctr&lt;uchars; uchar_ctr++) //<br />
&nbsp;&nbsp;SPI_RW(*pBuf++);<br />
&nbsp;CSN = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //关闭SPI<br />
&nbsp;return(status);&nbsp;&nbsp;&nbsp; // <br />
}<br />
/****************************************************************************************************/<br />
/*函数:void SetRX_Mode(void)<br />
/*功能:数据接收配置 <br />
/****************************************************************************************************/<br />
void SetRX_Mode(void)<br />
{<br />
&nbsp;CE=0;<br />
//&nbsp;SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);&nbsp;&nbsp; &nbsp;&nbsp;// IRQ收发完成中断响应,16位CRC&nbsp;,主接收<br />
&nbsp;CE = 1; <br />
&nbsp;inerDelay_us(200);<br />
}<br />
/*************************************************&nbsp;&nbsp;&nbsp;&nbsp; /<br />
/************************发送设置*******************************/<br />
void SetTX_Mode()<br />
{</p>
<p>CE=0;<br />
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);<br />
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);<br />
//SPI_Write_Buf(WR_TX_PLOAD, Txbuf, TX_PLOAD_WIDTH); // Writes data to TX payload<br />
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ackipe0<br />
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0<br />
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...<br />
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40<br />
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0F); // TX_PWR:0dBm, Datarate:2Mbps,LNA:HCURR</p>
<p>SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) &amp; Prim:TX. MAX_RT &amp; TX_DS enabled..</p>
<p>CE=1;<br />
inerDelay_us(20);<br />
}</p>
<p><br />
/******************************************************************************************************/<br />
/*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)<br />
/*功能:数据读取后放如rx_buf接收缓冲区中<br />
/******************************************************************************************************/<br />
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)<br />
{<br />
&nbsp;&nbsp;&nbsp; unsigned char revale=0;<br />
&nbsp;sta=SPI_Read(STATUS);&nbsp;// 读取状态寄存其来判断数据接收状况<br />
&nbsp;if(RX_DR)&nbsp;&nbsp;&nbsp;&nbsp;// 判断是否接收到数据<br />
&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp; CE = 0; &nbsp;&nbsp;&nbsp;//SPI使能<br />
&nbsp;&nbsp;SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer<br />
&nbsp;&nbsp;revale =1;&nbsp;&nbsp;&nbsp;//读取数据完成标志<br />
&nbsp;}<br />
&nbsp;SPI_RW_Reg(WRITE_REG+STATUS,sta);&nbsp;&nbsp; //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志<br />
&nbsp;return revale;<br />
}<br />
/***********************************************************************************************************<br />
/*函数:void nRF24L01_TxPacket(unsigned char * tx_buf)<br />
/*功能:发送 tx_buf中数据<br />
/**********************************************************************************************************/<br />
void Test(uchar R_Byte);<br />
/////////////////////////////////////////////////////////<br />
int nRF24L01_TxPacket(unsigned char * tx_buf)<br />
{<br />
&nbsp;CE=0;&nbsp;&nbsp;&nbsp;//StandBy I模式&nbsp;</p>
<p>&nbsp;SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); &nbsp;&nbsp;&nbsp; // 装载数据&nbsp;</p>
<p>&nbsp;CE=1;&nbsp;&nbsp; //置高CE,激发数据发送<br />
&nbsp;Delay(10);<br />
&nbsp;while(IRQ);&nbsp;<br />
&nbsp;sta = SPI_RW(STATUS);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 返回状态寄存器<br />
//&nbsp;&nbsp; Test(sta); <br />
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;SPI_RW_Reg(WRITE_REG + STATUS, sta);&nbsp; // 清除TX_DS或MAX_RT中断标志<br />
&nbsp;if(MAX_RT)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 是否清除TX FIFO,没有清除在复位MAX_RT中断标志后重发<br />
&nbsp; {&nbsp;//&nbsp;&nbsp;SPI_RW(FLUSH_TX);<br />
&nbsp;SPI_RW_Reg(FLUSH_TX,0xff);<br />
&nbsp;LED2=0;<br />
&nbsp;}<br />
&nbsp;Delay(50);<br />
//&nbsp;IRQ = 1;<br />
&nbsp;if(TX_DS)<br />
&nbsp;{<br />
&nbsp;SPI_RW_Reg(WRITE_REG + STATUS, sta);<br />
&nbsp;&nbsp;return(1);<br />
&nbsp;&nbsp;}<br />
&nbsp;else<br />
&nbsp;&nbsp;return(0);<br />
}</p>
<p>//检测24L01是否存在<br />
//返回值:0,成功;1,失败&nbsp;<br />
int NRF24L01_Check(void)<br />
{<br />
&nbsp;uchar buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};<br />
&nbsp;uchar i;<br />
&nbsp;&nbsp; &nbsp; <br />
&nbsp;SPI_Write_Buf(WRITE_REG+TX_ADDR,buf,5);//写入5个字节的地址.&nbsp;<br />
&nbsp;SPI_Read_Buf(TX_ADDR,buf,5); //读出写入的地址&nbsp; <br />
&nbsp;for(i=0;i&lt;5;i++)if(buf!=0XA5)break;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;if(i!=5)return 1;//检测24L01错误&nbsp;<br />
&nbsp;return 0;&nbsp;&nbsp; //检测到24L01<br />
}&nbsp; &nbsp; <br />
//************************************串口初始化*********************************************************<br />
void StartUART( void )<br />
{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//波特率4800<br />
&nbsp;&nbsp;&nbsp;&nbsp; SCON = 0x50;<br />
&nbsp;&nbsp;&nbsp;&nbsp; TMOD = 0x20;<br />
&nbsp;&nbsp;&nbsp;&nbsp; TH1 = 0xFA;<br />
&nbsp;&nbsp;&nbsp;&nbsp; TL1 = 0xFA;<br />
&nbsp;&nbsp;&nbsp;&nbsp; PCON = 0x00;<br />
&nbsp;&nbsp;&nbsp;&nbsp; TR1 = 1;<br />
}<br />
//************************************通过串口将接收到数据发送给PC端**************************************<br />
void Test(uchar R_Byte)<br />
{&nbsp;<br />
&nbsp; SBUF = R_Byte;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp; while( TI == 0 );&nbsp;&nbsp;&nbsp;&nbsp;//查询法<br />
&nbsp; &nbsp; TI = 0;&nbsp;&nbsp;&nbsp; <br />
}<br />
//************************************主函数************************************************************<br />
void main(void)<br />
{<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;uchar i,temp;<br />
//&nbsp;uchar RxBuf[32];&nbsp;<br />
&nbsp;&nbsp;&nbsp; init_NRF24L01();<br />
//&nbsp;led0=0;led1=0;led2=0;led3=0;<br />
&nbsp;StartUART();<br />
&nbsp;Delay(100);<br />
&nbsp;SetTX_Mode();&nbsp; P0=0xff;<br />
&nbsp;while(1)<br />
&nbsp;{</p>
<p>&nbsp; while(NRF24L01_Check()!=0) //不在位<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;{LED2=~LED2;<br />
&nbsp;&nbsp;&nbsp;&nbsp; Delay(1000);<br />
&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp; &nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; if(nRF24L01_TxPacket(Txbuf)==1)&nbsp;// 发送成功<br />
&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; LED0=~LED0;<br />
&nbsp;&nbsp;&nbsp; Delay(1000);<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; if(nRF24L01_TxPacket(Txbuf)==0)&nbsp; //发送失败<br />
&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; LED1=~LED1;<br />
&nbsp;&nbsp;&nbsp; Delay(1000);<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Delay(50000);<br />
&nbsp;&nbsp;}<br />
&nbsp;</p>
<p>}&nbsp;<br />
<br />
<br />
这个代码哪 里出问题了?咋就是发送不成功?<br />
</p>
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2011-8-1 19:24:02 | 显示全部楼层
1,先看写入NRF的寄存器值是否正确.
<br>2,看你两个NRF模块是否连接成功.
<br>
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

12

主题

24

帖子

0

精华

初级会员

Rank: 2

积分
92
金钱
92
注册时间
2011-7-14
在线时间
0 小时
 楼主| 发表于 2011-8-1 19:29:53 | 显示全部楼层
哦,NRF的寄存器值大都是参照你给的STM32的改的,应该不会有错。。检测NRF24L01是在位的,但发射不成功,一般问题出在哪?
回复 支持 反对

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2011-8-2 01:42:18 | 显示全部楼层
那你接收端呢?
<br>是否配置成功了?
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

6

主题

56

帖子

0

精华

初级会员

Rank: 2

积分
119
金钱
119
注册时间
2014-12-11
在线时间
3 小时
发表于 2014-12-15 11:19:41 | 显示全部楼层
mark....
回复 支持 反对

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2024-11-23 15:31

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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