- NRF的引脚有 IRQ:中断引脚 接收到数据或者发射数据完成的时候会被拉低 可以设置成引发中断
- MOSI :SPI总线的数据发射引脚
- MISO :SPI 总线的数据接收引脚
- CS:低电平有效 SPI的片选引脚
- CE:NRF的发射或者接收选择引脚
- SCK:SPI总线的时钟线
- 有硬件SPI功能的MCU 可以忽略这部分
- typedef unsigned char uint8_t;
- uint8_t SPI_SR(uint8_t Data)
- {
- uint8_t i,buff=0;
- SCK_L;
- for(i=0;i<8;i++)
- {
- if(Data&0x80)
- MOSI_H;
- else
- MOSI_L;
- SCK_H;
- Data<<=1;
- buff++;
- if(MISO)
- buff++;
- SCK_L;
- }
- return Status;
- }
- uint8_t NRF_WriteReg(uint8_t reg,uint8_t data)
- {
- uint8_t Status;
- CS_L;
- CE_L;
- Status=SPI_SR(reg);
- SPI_SR(Data);
- CS_H;
- return Status;
- }
- uint8_t NRF_ReadReg(uint8_t reg)
- {
- uint8_t val;
- CS_L;
- CE_L;
- SPI_SR(reg);
- val=SPI_SR(0xff);
- CS_H;
- return val;
- }
- uint8_t NRF_WriteBuff(uint8_t reg,uint8_t *buff,uint8_t Byte)
- {
- uint8_t i,Status;
- CE_L;
- CS_L;
- Status=NRF_SR(reg);
- for(i=0;i<Byte;i++)
- SPI_SR(buff[i]);
- CS_H;
- return Status;
- }
- uint8_t NRF_ReadReg(uint8_t reg,uint8_t buff,uint8_t Byte)
- {
- uint8_t i,Status;
- CE_L;
- CS_L;
- Status=NRF_SR(reg);
- for(i=0;i<Byte,i++)
- buff[i]=SPI_SR(0xff);
- CS_H;
- return Status;
- }
- void NRF_SetMode(uint8_t flag) //1:发射,0接收
- {
- if(flag!=0)
- {
- NRF_CE_L;
- NRF_WriteBuff(NRF_WRITE_REG+RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH);//写RX节点地址 //寄存器地址 地址 地址宽度
- NRF_WriteReg(NRF_WRITE_REG+EN_AA,0x01); //使能通道0的自动应答
- NRF_WriteReg(NRF_WRITE_REG+EN_RXADDR,0x01);//使能通道0的接收地址
- NRF_WriteReg(NRF_WRITE_REG+RF_CH,CHANAL); //设置RF通信频率
- NRF_WriteReg(NRF_WRITE_REG+RX_PW_P0,RX_PLOAD_WIDTH);//选择通道0的有效数据宽度
- NRF_WriteReg(NRF_WRITE_REG+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启
- NRF_WriteReg(NRF_WRITE_REG+CONFIG, 0x0f); //配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式
- /*CE拉高,进入接收模式*/
- NRF_CE_H;
- }
- else
- {
- NRF_CE_L;
- NRF_WriteBuff(NRF_WRITE_REG+TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH); //写TX节点地址
- NRF_WriteBuff(NRF_WRITE_REG+RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH); //设置TX节点地址,主要为了使能ACK
- NRF_WriteReg(NRF_WRITE_REG+EN_AA,0x01); //使能通道0的自动应答
- NRF_WriteReg(NRF_WRITE_REG+EN_RXADDR,0x01); //使能通道0的接收地址
- NRF_WriteReg(NRF_WRITE_REG+SETUP_RETR,0x1a);//设置自动重发间隔时间:500us + 86us;最大自动重发次数:10次
- NRF_WriteReg(NRF_WRITE_REG+RF_CH,CHANAL); //设置RF通道为CHANAL
- NRF_WriteReg(NRF_WRITE_REG+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启
- NRF_WriteReg(NRF_WRITE_REG+CONFIG,0x0e); //配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,发射模式,开启所有中断
- /*CE拉高,进入发送模式*/
- NRF_CE_H;
- Delay(0xffff); //CE要拉高一段时间才进入发送模式
- }
- }
复制代码
- /**
- * @brief 用于向NRF的发送缓冲区中写入数据
- * @param
- * @arg txBuf:存储了将要发送的数据的数组,外部定义
- * @retval 发送结果,成功返回TXDS,失败返回MAXRT或ERROR
- */
- uint8_t NRF_Tx_Data(uint8_t *txbuf)
- {
- uint8_t State;
- /*ce为低,进入待机模式1*/
- NRF_CE_L;
- /*写数据到TX BUF 最大 32个字节*/
- NRF_WriteBuff(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);
- /*CE为高,txbuf非空,发送数据包 */
- NRF_CE_H;
-
- /*等待发送完成中断 */
- while(NRF_IRQ!=0);
- /*读取状态寄存器的值 */
- State = NRF_ReadReg(STATUS);
- /*清除TX_DS或MAX_RT中断标志*/
- NRF_WriteReg(NRF_WRITE_REG+STATUS,State);
- NRF_WriteReg(FLUSH_TX,NOP); //清除TX FIFO寄存器
- /*判断中断类型*/
- if(State&MAX_RT) //达到最大重发次数
- return MAX_RT;
- else if(State&TX_DS) //发送完成
- return TX_DS;
- else
- return ERROR; //其他原因发送失败
- }
- /**
- * @brief 用于从NRF的接收缓冲区中读出数据
- * @param
- * @arg rxBuf :用于接收该数据的数组,外部定义
- * @retval
- * @arg 接收结果
- */
- uint8_t NRF_Rx_Dat(uint8_t *rxbuf)
- {
- uint8_t State;
- NRF_CE_H; //进入接收状态
- /*等待接收中断*/
- while(NRF_IRQ!=0);
- NRF_CE_L; //进入待机状态
- /*读取status寄存器的值 */
- State=NRF_ReadReg(STATUS);
-
- /* 清除中断标志*/
- NRF_WriteReg(NRF_WRITE_REG+STATUS,State);
- /*判断是否接收到数据*/
- if(State&RX_DR) //接收到数据
- {
- NRF_ReadBuff(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
- NRF_WriteReg(FLUSH_RX,NOP); //清除RX FIFO寄存器
- return RX_DR;
- }
- else
- return ERROR; //没收到任何数据
- }
- #define TX_ADR_WIDTH 5 //发射地址宽度
- #define TX_PLOAD_WIDTH 4 //发射数据通道有效数据宽度0~32Byte
- #define RX_ADR_WIDTH 5
- #define RX_PLOAD_WIDTH 4
- #define CHANAL 40 //频道选择
- // SPI(nRF24L01) commands , NRF的SPI命令宏定义,详见NRF功能使用文档
- #define NRF_READ_REG 0x00 // Define read command to register
- #define NRF_WRITE_REG 0x20 // Define write command to register
- #define RD_RX_PLOAD 0x61 // Define RX payload register address
- #define WR_TX_PLOAD 0xA0 // Define TX payload register address
- #define FLUSH_TX 0xE1 // Define flush TX register command
- #define FLUSH_RX 0xE2 // Define flush RX register command
- #define REUSE_TX_PL 0xE3 // Define reuse TX payload register command
- #define NOP 0xFF // Define No Operation, might be used to read status register
- // SPI(nRF24L01) registers(addresses) ,NRF24L01 相关寄存器地址的宏定义
- #define CONFIG 0x00 // 'Config' register address
- #define EN_AA 0x01 // 'Enable Auto Acknowledgment' register address
- #define EN_RXADDR 0x02 // 'Enabled RX addresses' register address
- #define SETUP_AW 0x03 // 'Setup address width' register address
- #define SETUP_RETR 0x04 // 'Setup Auto. Retrans' register address
- #define RF_CH 0x05 // 'RF channel' register address
- #define RF_SETUP 0x06 // 'RF setup' register address
- #define STATUS 0x07 // 'Status' register address
- #define OBSERVE_TX 0x08 // 'Observe TX' register address
- #define CD 0x09 // 'Carrier Detect' register address
- #define RX_ADDR_P0 0x0A // 'RX address pipe0' register address
- #define RX_ADDR_P1 0x0B // 'RX address pipe1' register address
- #define RX_ADDR_P2 0x0C // 'RX address pipe2' register address
- #define RX_ADDR_P3 0x0D // 'RX address pipe3' register address
- #define RX_ADDR_P4 0x0E // 'RX address pipe4' register address
- #define RX_ADDR_P5 0x0F // 'RX address pipe5' register address
- #define TX_ADDR 0x10 // 'TX address' register address
- #define RX_PW_P0 0x11 // 'RX payload width, pipe0' register address
- #define RX_PW_P1 0x12 // 'RX payload width, pipe1' register address
- #define RX_PW_P2 0x13 // 'RX payload width, pipe2' register address
- #define RX_PW_P3 0x14 // 'RX payload width, pipe3' register address
- #define RX_PW_P4 0x15 // 'RX payload width, pipe4' register address
- #define RX_PW_P5 0x16 // 'RX payload width, pipe5' register address
- #define FIFO_STATUS 0x17 // 'FIFO Status Register' register address
- #define MAX_RT 0x10 //达到最大重发次数中断标志位
- #define TX_DS 0x20 //发送完成中断标志位 //
- #define RX_DR 0x40 //接收到数据中断标志位
复制代码
|