void SetRX_Mode(void) { CE=0; SPI_RW_Reg(WRITE_REG+STATUS,0xff); SPI_RW_Reg(FLUSH_RX, 0x00); SPI_RW_Reg(WRITE_REG + CONFIG2, 0x7f); // IRQ收发完成中断响应,16位CRC ,主接收 CE = 1; inerDelay_us(2600); }
void nRF24L01_TxPacket(unsigned char * tx_buf) { 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,主发送 CE=1; //置高CE,激发数据发送 inerDelay_us(10); }
void nRF24L01_TxPacket(unsigned char * tx_buf) { SPI_RW_Reg(WRITE_REG+STATUS,0xff); SPI_RW_Reg(0xE1,0xff); CE=0; SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); CE=1; inerDelay_us(200); //CE高电平大于10us才能进入发射模式 }
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf) { unsigned char revale=0; CE=0; SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); CE=1; inerDelay_us(10); inerDelay_us(10); sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况 // if(RX_DR) // 判断是否接收到数据 // { 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+STATUS,0x07); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志 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); }
void nRF24L01_TxPacket(unsigned char * tx_buf) { CE=0; //StandBy I模式 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据 CE=1; //置高CE,激发数据发送 inerDelay_us(10); inerDelay_us(10); SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC,主发送 SPI_RW_Reg(WRITE_REG+STATUS,0x07); /* if(TX_DS) { CE = 0; //SPI使能 SPI_RW_Reg(WRITE_REG+STATUS,0x07); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志 R_S_Byte(0x73); } */ CE=1; //置高CE,激发数据发送 inerDelay_us(10); }
/*NRF24L01初始化 //***************************************************************************************/ void init_NRF24L01(void) { inerDelay_us(100); CE=0; // chip enable CSN=1; // Spi disable 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 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 設置工作頻率2.4GHZ,收發必需一致 SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //設置接收數據長度,本次設置為20 bit SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //設置工作頻率1MHZ,發射功率為最大值0dB }
/*NRF24L01初始化 //***************************************************************************************/ void init_NRF24L01(void) { inerDelay_us(100); CE=0; // chip enable CSN=1; // Spi disable 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); //设置接收数据长度,本次设置为20字节 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB }
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; inerDelay_us(2); RF24L01_SCK_1; // Set SCK high.. if((P1IN&0x02))temp++; // capture current MISO bit inerDelay_us(2); RF24L01_SCK_0; // ..then set SCK low again } return(temp); // return read uchar }
/*NRF24L01初始化 //***************************************************************************************/ void init_NRF24L01(void) { inerDelay_us(100); CE=0; // chip enable CSN=1; // Spi disable 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 + SETUP_AW, 0x03); //设置地址宽度 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x00); //自动重发 SPI_RW_Reg(WRITE_REG + RF_CH, 0x00); //设置信道工作为2.4GHZ,收发必须一致 SPI_RW_Reg(WRITE_REG + RX_PW_P0, 0x3f); //设置接收数据长度,本次设置为32字节 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送 }
uchar nRF24L01_RxPacket(uchar *rx_buf) { uchar revale=0; SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,RX0_ADDRESS, RX_ADR_WIDTH); CE=0; SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收 CE = 1; inerDelay_us(130); sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况 if(RX_DR) // 判断是否接收到数据 { CE = 0; //SPI使能 SPI_Read_Buf(RD_RX_PLOAD,rx_buf,RX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer revale =1; //读取数据完成标志 switch(sta%16/2) { case 0: { write_com(0x80+0x43); write_data((rx_buf[1]*256+rx_buf[2])/100%10+48); write_data(((rx_buf[1]*256+rx_buf[2])/10)%10+48); write_data('.'); write_data((rx_buf[1]*256+rx_buf[2])%10+48); write_data(0xdf); write_data('c'); break; } case 1: { write_com(0x80+0x03); write_data(((rx_buf[1]*256+rx_buf[2])/100)%10+48); write_data(((rx_buf[1]*256+rx_buf[2])/10)%10+48); write_data('.'); write_data((rx_buf[1]*256+rx_buf[2])%10+48); write_data(0xdf); write_data('c'); break; } } } SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志 return revale; }
/**************************************************/ 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); }