void nrf_init(char *address, int address_size) { P2DIR |= NRF_CE; P2OUT |= NRF_CE; //auto ack pipe 0 nrf_write(NRF_EN_AA, EN_AA); //pipe 0 only nrf_write(NRF_EN_RXADDR, EN_RXADDR); //set channel nrf_set_channel(); //setup RF nrf_write(NRF_RF_SETUP,RF_SETUP); set_address(address,address_size); }
unsigned char NRF24L01_RxPacket(unsigned char *rxbuf) { unsigned char sta; //SPIx_SetSpeed(SPI_SPEED_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz) sta=nrf_read(STATUS); //读取状态寄存器的值 nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志 if(sta&0x40)//接收到数据 { NRF24L01_Read_Buf(0x61,rxbuf,TX_DATA_WITDH);//读取数据 nrf_write(FLUSH_RX,0xff);//清除RX FIFO寄存器 return 0; } return 1;//没收到任何数据 }
unsigned char NRF24L01_TxPacket(unsigned char *txbuf) { unsigned char sta; DrvGPIO_ClrBit(E_GPB, 1); nrf_writebuf(W_TX_PAYLOAD,txbuf,TX_DATA_WITDH);//写数据到TX BUF 32个字节 DrvGPIO_SetBit(E_GPB,1); // This device is now ready to receive one packet of 16 bytes payload from a TX device while(DrvGPIO_GetBit(E_GPB, 2)!=0);//等待发送完成 sta=nrf_read(STATUS); //读取状态寄存器的值 nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志 if(sta&0x10)//达到最大重发次数 { nrf_wf(FLUSH_TX);//清除TX FIFO寄存器 nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志 sta=nrf_read(STATUS); //读取状态寄存器的值 return 0x10; } if(sta&0x20)//发送完成 { return 0x20; } return 0xff;//其他原因发送失败 }
void RX_Mode(void) { DrvGPIO_ClrBit(E_GPB, 0x01); nrf_writebuf(W_REGISTER+TX_ADDR,(uchar*)TxAddr,5);//写TX节点地址 nrf_writebuf(W_REGISTER+RX_ADDR_P0,(uchar*)RxAddr,5); //设置TX节点地址,主要为了使能ACK nrf_write(W_REGISTER + EN_AA, 0x01); // Enable Auto.Ack:Pipe0 nrf_write(W_REGISTER + EN_RXADDR, 0x01); // Enable Pipe0 nrf_write(W_REGISTER + RF_CH, 40); // Select RF channel 40 nrf_write(W_REGISTER + RX_PW_P0, 4); nrf_write(W_REGISTER + RF_SETUP, 0x0f); nrf_write(W_REGISTER + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) DrvGPIO_SetBit(E_GPB,1); // This device is now ready to receive one packet of 16 bytes payload from a TX device // '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and }
/*****************NRF????????????******************************/ void NRFSetTxMode(uchar *TxDate) {//???? char ret; DrvGPIO_ClrBit(E_GPB, 1); nrf_writebuf(W_REGISTER+TX_ADDR,(uchar*)TxAddr,5);//写TX节点地址 nrf_writebuf(W_REGISTER+RX_ADDR_P0,(uchar*)TxAddr,5); //设置TX节点地址,主要为了使能ACK ret=nrf_read(STATUS); delay_s(); nrf_write(W_REGISTER+STATUS,0x10); //使能通道0的自动应答 ret=nrf_read(STATUS); nrf_write(W_REGISTER+EN_AA,0x01); //使能通道0的自动应答 nrf_write(W_REGISTER+EN_RXADDR,0x01); //使能通道0的接收地址 nrf_write(W_REGISTER+SETUP_RETR,0x1a);//设置自动重发间隔时间:500us + 86us;最大自动重发次数:10次 nrf_write(W_REGISTER+RF_CH,40); //设置RF通道为40 nrf_write(W_REGISTER+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启 nrf_write(W_REGISTER+CONFIG,0x0e); //配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断 DrvGPIO_SetBit(E_GPB,1); // This device is now ready to receive one packet of 16 bytes payload from a TX device }
void radio_off(void) { char tmp = nrf_read(NRF_CONFIG); nrf_write(NRF_CONFIG, tmp&(~NRF_CONFIG_RADIO&0xff)); }
void radio_on(void) { char tmp = nrf_read(NRF_CONFIG); nrf_write(NRF_CONFIG, tmp|NRF_CONFIG_RADIO); //wait a little for radio to power up __delay_cycles(2000); }
void nrf_init_radio(void) { nrf_write(NRF_SETUP_RETR, SETUP_RETR); //no auto retransmit nrf_write(NRF_CONFIG, CONFIG); radio_off(); }
void nrf_set_channel(void) { nrf_write(NRF_RF_CH, CHANNEL); }
void nrf_set_config(char config) { nrf_write(NRF_CONFIG, config); }