u8 NRF24L01_RxPacket(u8 *rxbuf) { u8 sta; sta=SPI_R_byte(STATUS); //读取状态寄存器的值 SPI_W_Reg(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志 if(sta&RX_OK)//接收到数据 { SPI_R_DBuffer(R_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据 SPI_W_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器 return 0; } return 1;//没收到任何数据 }
/**************************************************/ 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 } 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 } char SPI_W_Reg(char reg){ char status1; RF24L01_CSN_0; // CSN low, init SPI transaction status1 = SPI_RW(reg); // select register RF24L01_CSN_1; // CSN high again return(status1); } 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; // Set CSN high return(status2); // return nRF24L01 status uchar } char SPI_Write_Buf(char reg, char *pBuf, char chars) { char status1,uchar_ctr; RF24L01_CSN_0; // Set CSN low, init SPI tranaction status1 = SPI_RW(reg); // Select register to write to and read status byte for(uchar_ctr=0; uchar_ctr<chars; uchar_ctr++) // then write all byte in buffer(*pBuf) { SPI_RW(*pBuf++); } RF24L01_CSN_1; // Set CSN high return(status1); // } void SetRX_Mode(void) { RF24L01_CE_0 ; SPI_RW_Reg(WRITE_REG + CONFIG, 0x3B); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled.. RF24L01_CE_1; // Set CE pin high to enable RX device inerDelay_us(1000);//delay for about 1 second } char nRF24L01_RxPacket(char* rx_buf) { char revale=0; sta=SPI_Read(STATUS); // read register STATUS's value if(sta&0x40) // success! { RF24L01_CE_0 ; //SPI enable SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer revale =1; //set flag to 1 } SPI_RW_Reg(WRITE_REG+STATUS,sta); return revale; } void nRF24L01_TxPacket(char * tx_buf) { RF24L01_CE_0 ; //StandBy SPI_Write_Buf(0x2A, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack SPI_Write_Buf(0x30, TX_ADDRESS, TX_ADR_WIDTH); SPI_RW_Reg(0x20,0x0A); SPI_Write_Buf(0xA0, tx_buf, 0x05); // Writes data to TX payload RF24L01_CE_1; // finish inerDelay_us(1000); } //**************************************************************************************** //NRF24L01 init //***************************************************************************************/ void init_NRF24L01(void) { inerDelay_us(1000); RF24L01_CE_0 ; // chip enable RF24L01_CSN_1; // Spi disable RF24L01_SCK_0; // Spi clock line init high inerDelay_us(15); SPI_Write_Buf(0x2B, TX_ADDRESS, TX_ADR_WIDTH); inerDelay_us(15); SPI_RW_Reg(WRITE_REG + RF_CH, 0x00); inerDelay_us(20); //SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x00); inerDelay_us(20); SPI_RW_Reg(0x25,0x00); SPI_RW_Reg(0x31, 0x05); inerDelay_us(20); SPI_RW_Reg(0x32, 0x05); inerDelay_us(20); SPI_RW_Reg(WRITE_REG + RF_SETUP, 0b00100110); inerDelay_us(20); SPI_RW_Reg(0x31, 0x05); inerDelay_us(20); SPI_RW_Reg(0x32, 0x05); inerDelay_us(20); SPI_RW_Reg(0x21, 0x00); //disable acks!! inerDelay_us(20); SPI_RW_Reg(0x20, 0x00); inerDelay_us(20); SPI_RW_Reg(0x27, 0x30); inerDelay_us(20); SPI_W_Reg(FLUSH_RX); inerDelay_us(20); }
//启动NRF24L01发送一次数据 //txbuf:待发送数据首地址 //返回值:发送完成状况 u8 NRF24L01_TxPacket(u8 *txbuf) { u8 sta; CE_CLR; SPI_W_DBuffer(W_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//写数据到TX BUF 32个字节 CE_SET;//启动发送 while(IRQ_S!=0);//等待发送完成 sta=SPI_R_byte(STATUS); //读取状态寄存器的值 SPI_W_Reg(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志 if(sta&MAX_TX)//达到最大重发次数 { SPI_W_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器 return MAX_TX; } if(sta&TX_OK)//发送完成 { return TX_OK; } return 0xff;//其他原因发送失败 }
//该函数初始化NRF24L01到RX模式 //设置RX地址,写RX数据宽度,选择RF频道,波特率和LNA HCURR //当CE变高后,即进入RX模式,并可以接收数据了 void NRF24L01_RX_Mode(void) { CE_CLR; SPI_W_DBuffer(W_REGISTER+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH);//写RX节点地址 SPI_W_Reg(W_REGISTER+EN_AA,0x01); //使能通道0的自动应答 SPI_W_Reg(W_REGISTER+EN_RX_ADDR,0x01);//使能通道0的接收地址 SPI_W_Reg(W_REGISTER+RF_CH,40); //设置RF通信频率 SPI_W_Reg(W_REGISTER+RX_PW_P0,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度 SPI_W_Reg(W_REGISTER+RF_SETUP,0x0f);//设置TX发射参数,0db增益,2Mbps,低噪声增益开启 SPI_W_Reg(W_REGISTER+CONFIG, 0x0f);//配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式 CE_SET; //CE为高,进入接收模式 }
//该函数初始化NRF24L01到TX模式 //设置TX地址,写TX数据宽度,设置RX自动应答的地址,填充TX发送数据,选择RF频道,波特率和LNA HCURR //PWR_UP,CRC使能 //当CE变高后,即进入RX模式,并可以接收数据了 //CE为高大于10us,则启动发送. void NRF24L01_TX_Mode(void) { CE_CLR; SPI_W_DBuffer(W_REGISTER+TX_ADDR,(u8*)TX_ADDRESS,TX_ADR_WIDTH);//写TX节点地址 SPI_W_DBuffer(W_REGISTER+RX_ADDR_P0,(u8*)RX_ADDRESS,RX_ADR_WIDTH); //设置TX节点地址,主要为了使能ACK SPI_W_Reg(W_REGISTER+EN_AA,0x01); //使能通道0的自动应答 SPI_W_Reg(W_REGISTER+EN_RX_ADDR,0x01); //使能通道0的接收地址 SPI_W_Reg(W_REGISTER+SETUP_RETR,0x1a);//设置自动重发间隔时间:500us + 86us;最大自动重发次数:10次 SPI_W_Reg(W_REGISTER+RF_CH,40); //设置RF通道为40 SPI_W_Reg(W_REGISTER+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启 SPI_W_Reg(W_REGISTER+CONFIG,0x0e); //配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断 CE_SET;//CE为高,10us后启动发送 delay_ms(1); }