常驻嘉宾
- 积分
- 3024
- 威望
- 1478
- 贡献
- 922
- 兑换币
- 132
- 注册时间
- 2011-3-26
- 在线时间
- 312 小时
|
本帖最后由 lcokenm 于 2012-10-4 11:40 编辑
- #include <hidef.h> /* common defines and macros */
- #include "derivative.h" /* derivative-specific definitions */
- #include"nrf24l01.h"
- /***********************************************************/
- char RxBuf[1]={0};
- //=======================NRF24L01_CE端口=========================================
- #define RF24L01_CE_0 PORTA_PA0=0
- #define RF24L01_CE_1 PORTA_PA0=1
- //=============================RF24L01_CSN端口==================================
- #define RF24L01_CSN_0 PORTA_PA1=0
- #define RF24L01_CSN_1 PORTA_PA1=1
- //=============================RF24L01_SCK端口======================================
- #define RF24L01_SCK_0 PORTA_PA2=0
- #define RF24L01_SCK_1 PORTA_PA2=1
- //============================= RF24L01_MOSI端口================================
- #define RF24L01_MOSI_0 PORTA_PA3=0
- #define RF24L01_MOSI_1 PORTA_PA3=1
- //=============================RF24L01_MISO端口=========================================
- #define RF24L01_MISO_0 PORTA_PA4=0
- #define RF24L01_MISO_1 PORTA_PA4=1
- //==========================IRQ状态============================================
- #define RF24L01_IRQ_0 PORTA_PA5=0
- #define RF24L01_IRQ_1 PORTA_PA5=1
- char TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
- char RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址
- char sta;
- void RF24L01_IO_set(void);
- void ms_delay(void);
- void Delay(int s);
- char SPI_RW(char data);
- char SPI_Read(char reg);
- char SPI_RW_Reg(char reg, char value);
- char SPI_Read_Buf(char reg, char *pBuf, char uchars);
- char SPI_Write_Buf(char reg, char *pBuf, char uchars);
- void SetRX_Mode(void);
- char nRF24L01_RxPacket(char* rx_buf);
- void nRF24L01_TxPacket(char * tx_buf);
- void init_NRF24L01(void);
- //===========================RF24L01端口设置==========================================
- void RF24L01_IO_set(void)
- {
- DDRA=0X0F;
- }
- //******************************************************************************
- //========================延时约5ms=============================================
- void ms_delay(void)
- {
- unsigned int i=40000;
- while (i != 0)
- {
- i--;
- }
- }
- //========================================长延时================================
- void Delay(int s)
- {
- unsigned int i,j;
- for(i=0; i<s; i++);
- for(j=0; j<s; j++);
- }
- //******************************************************************************************
- //延时函数
- //******************************************************************************************
- void inerDelay_us(char n)
- {
- for(;n>0;n--);
- }
- //==============================================================================
- //函数:uint SPI_RW(uint uchar)
- //功能:NRF24L01的SPI写时序
- //******************************************************************************
- char SPI_RW(char data)
- {
- char i,temp=0;
- for(i=0;i<8;i++) // output 8-bit
- {
- if((data & 0x80)==0x80)
- {
- RF24L01_MOSI_1; // output 'uchar', MSB to MOSI
- }
- else
- {
- RF24L01_MOSI_0;
- }
- data = (data << 1); // shift next bit into MSB..
- temp<<=1;
- RF24L01_SCK_1; // Set SCK high..
- if(PORTA_PA4==1)temp++; // capture current MISO bit
- RF24L01_SCK_0; // ..then set SCK low again
- }
- return(temp); // return read uchar
- }
- //****************************************************************************************************
- //函数:uchar SPI_Read(uchar reg)
- //功能:NRF24L01的SPI时序
- //****************************************************************************************************
- char SPI_Read(char reg)
- {
- char reg_val;
- RF24L01_CSN_0; // CSN low, initialize SPI communication...
- SPI_RW(reg); // Select register to read from..
- reg_val = SPI_RW(0); // ..then read registervalue
- RF24L01_CSN_1; // CSN high, terminate SPI communication
- return(reg_val); // return register value
- }
- //****************************************************************************************************/
- //功能:NRF24L01读写寄存器函数
- //****************************************************************************************************/
- char SPI_RW_Reg(char reg, char value)
- {
- char status1;
- RF24L01_CSN_0; // CSN low, init SPI transaction
- status1 = SPI_RW(reg); // select register
- SPI_RW(value); // ..and write value to it..
- RF24L01_CSN_1; // CSN high again
- return(status1); // return nRF24L01 status uchar
- }
- //****************************************************************************************************/
- //函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
- //功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
- //****************************************************************************************************/
- char SPI_Read_Buf(char reg, char *pBuf, char chars)
- {
- char status2,uchar_ctr;
- RF24L01_CSN_0; // Set CSN low, init SPI tranaction
- status2 = SPI_RW(reg); // Select register to write to and read status uchar
- for(uchar_ctr=0;uchar_ctr<chars;uchar_ctr++)
- {
- pBuf[uchar_ctr] = SPI_RW(0);
- }
- RF24L01_CSN_1;
- return(status2); // return nRF24L01 status uchar
- }
- //*********************************************************************************************************
- //函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
- //功能: 用于写数据 reg:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
- //*********************************************************************************************************/
- char SPI_Write_Buf(char reg, char *pBuf, char chars)
- {
- char status1,uchar_ctr;
- RF24L01_CSN_0; //SPI使能
- status1 = SPI_RW(reg);
- for(uchar_ctr=0; uchar_ctr<chars; uchar_ctr++)
- {
- SPI_RW(*pBuf++);
- }
- RF24L01_CSN_1; //关闭SPI
- return(status1);
- }
- //****************************************************************************************************/
- //函数:void SetRX_Mode(void)
- //功能:数据接收配置
- //****************************************************************************************************/
- void SetRX_Mode(void)
- {
- RF24L01_CE_0;
- SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC,主接收
- RF24L01_CE_1;
- inerDelay_us(130); //注意不能太小
- }
- //******************************************************************************************************/
- //函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
- //功能:数据读取后放如rx_buf接收缓冲区中
- //******************************************************************************************************/
- char nRF24L01_RxPacket(char* rx_buf)
- {
- char revale=0;
- sta=SPI_Read(STATUS); //读取状态寄存其来判断数据接收状况
- if(sta&0x40) // 判断是否接收到数据
- {
- RF24L01_CE_0 ; //SPI使能
- SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH); // read receive payload from RX_FIFO buffer
- revale =1; //读取数据完成标志
- }
- // SPI_RW_Reg(WRITE_REG+FIFO_STATUS, FLUSH_RX);//清空缓冲区
- // SPI_RW(FLUSH_RX);
- SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
- return revale;
- }
- //***********************************************************************************************************
- //函数:void nRF24L01_TxPacket(char * tx_buf)
- //功能:发送 tx_buf中数据
- //**********************************************************************************************************/
- void nRF24L01_TxPacket(char * tx_buf)
- {
- RF24L01_CE_0 ; //StandBy I模式
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
- SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
- // SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
- RF24L01_CE_1; //置高CE,激发数据发送
- inerDelay_us(10);
- }
- //****************************************************************************************
- //NRF24L01初始化
- //***************************************************************************************/
- void init_NRF24L01(void)
- {
- inerDelay_us(100);
- RF24L01_CE_0 ; // chip enable
- RF24L01_CSN_1; // Spi disable
- RF24L01_SCK_0; // Spi clock line init high
- SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
- SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许
- SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21
- SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
- SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); // 设置接收数据长度,本次设置为32字节
- SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // 设置发射速率为1MHZ,发射功率为最大值0dB
- SPI_RW_Reg(WRITE_REG + CONFIG, 0x0E); // IRQ收发完成中断响应,16位CRC ,主接收}
- }
- //void main(void) {
- DisableInterrupts;
- /* put your own code here */
- DDRB=0XFF;
- NRF24L01_IO_set();
- init_NRF24L01();
- SetRX_Mode();
- EnableInterrupts;
- while(1)
- {
-
- if(nRF24L01_RxPacket(RxBuf)) //判断是否收到数据
- // NRF24L01在判断是否接收数据是可以通过中断引脚IRQ和内部 RX_DR,TX_DS,MAX_PT状态来判断,此处是后者,可以将IRQ引脚接到单片机中断口,通过中断来判断 推荐使用中断这样可以节约MCU消耗
- {
- receive=RxBuf[0];
- speed=(receive & 0xf0)>>4;
- PORTB=receive;
-
- SPI_RW_Reg(WRITE_REG+STATUS,0XFF);
- }
- _FEED_COP();
- }
-
- /* please make sure that you never leave main */
- }
- 2401跟K10的通信问题,发送和接收都可以,只是每次只能接收一次数据,接受完之后,接收端复位一次才能再次受到发送端的信数据,有人遇到这个问题没有?求教!是因为FIFO没有清空么?FIFO要在哪里清空?
复制代码 |
|