(SKU:RB-03T006)NRF24L01無線數(shù)傳模塊
來自ALSROBOT WiKi
目錄 |
產(chǎn)品概述
NRF24L01是一款新型單片射頻收發(fā)器件,工作于2.4 GHz~2.5 GHz ISM頻段。內(nèi)置頻率合成器、功率放大器、晶體振蕩器、調(diào)制器等功能模塊,并融合了增強型ShockBurst技術,其中輸出功率和通信頻道可通過程序進 行配置。NRF24L01功耗低,在以-6 dBm的功率發(fā)射時,工作電流也只有9 mA;接收時,工作電流只有12.3 mA,多種低功率工作模式(掉電模式和空閑模式)使節(jié)能設計更方便。
規(guī)格參數(shù)
- 2Mbit/s速率下接收時的峰值電流12.5mA
- 在2Mbit/s速率下@0dBm輸出時的峰值電流11mA
- 掉電模式下的功耗400nA
- 待機模式下的功耗32uA
- 130us 的快速切換和喚醒時間
- 具有片內(nèi)穩(wěn)壓器oltage regulators
- 可在1.9 to 3.6V低電壓工作
- MultiCeiverMT硬件提供同時6個接收機的功能,2Mbit/s 使得高質(zhì)量的VoIP成為可能
使用方法
引腳說明
引腳說明:
- CE:使能發(fā)射或接收;
- CSN、 SCK、 MOSI、 MISO: SPI引腳,通過此引腳配置nRF24L01
- IRQ:中斷
應用例程
1.庫文件下載
NRF2401庫文件及使用例程下載地址
2.連接Arduino和NRF2401模塊
連接注意事項:
- VCC引腳的電壓范圍2.3 - 3.6之間,超過 3.6V 模塊會燒掉, 建議使用3.3V左右。
- 該模塊也可以通過普通 IO 口模擬 SPI 時序進行讀寫數(shù)據(jù)操作。
- 使用 2 個模塊同時發(fā)射時,兩者頻道間隔應該至少相差1MHZ,否則同頻道之間易干擾。
NRF2401 | Arduino |
VCC | 3V3 |
GND | GND |
CSN | D9 |
CE | D8 |
MOSI | D11 |
MISO | D12 |
SCK | D10 |
IRQ | D13 |
3.代碼下載
發(fā)送端代碼
/********************************************************************* ** Device: nRF24L01+ TX ** ** SPI*********** ** ** CE - to digital pin 8 ** ** CSN - to digital pin 9 (SS pin) ** ** CLK - to digital pin 10 (SCK pin) ** ** MOSI - to digital pin 11 (MOSI pin) ** ** MISO - to digital pin 12 (MISO pin) ** ** IRQ - to digital pin 13 ** *********************************************************************/ #include "NRF24L01.h" //*************************************************** #define TX_ADR_WIDTH 5 // 5 unsigned chars TX address width #define RX_ADR_WIDTH 5 // 5 unsigned chars RX address width #define TX_PLOAD_WIDTH 32 // 32 unsigned chars TX payload #define RX_PLOAD_WIDTH 32 // 32 unsigned chars RX payload unsigned char TX_ADDRESS[TX_ADR_WIDTH] = { 0x34,0x43,0x10,0x10,0x01 }; // Define a static TX address unsigned char RX_ADDRESS[RX_ADR_WIDTH] = { 0x34,0x43,0x10,0x10,0x01 }; // Define a static RX address unsigned char TX_BUF[TX_PLOAD_WIDTH]={0}; //*************************************************** void setup() { SPI_DIR = ( NRFCE + NRFSCK + NRFCSN + NRFMOSI); SPI_DIR &=~ ( NRFIRQ + NRFMISO); delay(100); init_io(); init_NRF24L01(); Serial.begin(9600); TX_BUF[1] = 0x01 ; TX_BUF[2] = 0x02 ; nRF24L01_TxPacket(TX_BUF); delay(50); } void loop() { unsigned char status=0; // status=SPI_Read(STATUS); // if(status&TX_DS) for(; ;) { TX_BUF[1] = 0x01 ; TX_BUF[2] = 0x02 ; Serial.println("****************START TX**********************"); Serial.print("TX_BUF[1]=0x"); Serial.println(TX_BUF[1],HEX); Serial.print("TX_BUF[2]=0x"); Serial.println(TX_BUF[2],HEX); delay(1000); nRF24L01_TxPacket(TX_BUF); // Transmit Tx buffer data SPI_RW_Reg(WRITE_REG+STATUS,0XFF); TX_BUF[1] = 0x00; TX_BUF[2] = 0x00; } } //************************************************** // Function: init_io(); // Description: // flash led one time,chip enable(ready to TX or RX Mode), // Spi disable,Spi clock line init high //************************************************** void init_io(void) { SPI_PORT&=~NRFCE; // chip enable SPI_PORT|=NRFCSN; // Spi disable SPI_PORT&=~NRFSCK; // Spi clock line init high } /************************************************** * Function: SPI_RW(); * * Description: * Writes one unsigned char to nRF24L01, and return the unsigned char read * from nRF24L01 during write, according to SPI protocol **************************************************/ unsigned char SPI_RW(unsigned char Byte) { unsigned char i; for(i=0;i<8;i++) // output 8-bit { if(Byte&0x80) { SPI_PORT |=NRFMOSI; // output 'unsigned char', MSB to MOSI } else { SPI_PORT &=~NRFMOSI; } SPI_PORT|=NRFSCK; // Set SCK high.. Byte <<= 1; // shift next bit into MSB.. if(SPI_IN & NRFMISO) { Byte |= 1; // capture current MISO bit } SPI_PORT&=~NRFSCK; // ..then set SCK low again } return(Byte); // return read unsigned char } /**************************************************/ /************************************************** * Function: SPI_RW_Reg(); * * Description: * Writes value 'value' to register 'reg' /**************************************************/ unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value) { unsigned char status; SPI_PORT&=~NRFCSN; // CSN low, init SPI transaction status = SPI_RW(reg); // select register SPI_RW(value); // ..and write value to it.. SPI_PORT|=NRFCSN; // CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /************************************************** * Function: SPI_Read(); * * Description: * Read one unsigned char from nRF24L01 register, 'reg' /**************************************************/ unsigned char SPI_Read(unsigned char reg) { unsigned char reg_val; SPI_PORT&=~NRFCSN; // CSN low, initialize SPI communication... SPI_RW(reg); // Select register to read from.. reg_val = SPI_RW(0); // ..then read register value SPI_PORT|=NRFCSN; // CSN high, terminate SPI communication return(reg_val); // return register value } /**************************************************/ /************************************************** * Function: SPI_Read_Buf(); * * Description: * Reads 'unsigned chars' #of unsigned chars from register 'reg' * Typically used to read RX payload, Rx/Tx address /**************************************************/ unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) { unsigned char status,i; SPI_PORT&=~NRFCSN; // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status unsigned char for(i=0;i<bytes;i++) { pBuf[i] = SPI_RW(0); // Perform SPI_RW to read unsigned char from nRF24L01 } SPI_PORT|=NRFCSN; // Set CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /************************************************** * Function: SPI_Write_Buf(); * * Description: * Writes contents of buffer '*pBuf' to nRF24L01 * Typically used to write TX payload, Rx/Tx address /**************************************************/ unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) { unsigned char status,i; SPI_PORT&=~NRFCSN; // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status unsigned char for(i=0;i<bytes; i++) // then write all unsigned char in buffer(*pBuf) { SPI_RW(*pBuf++); } SPI_PORT|=NRFCSN; // Set CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /*************************************************** * Function: nRF24L01_TxPacket(unsigned char * tx_buf) * Description: * sent tx_buf data /**************************************************/ void nRF24L01_TxPacket(unsigned char * tx_buf) { SPI_PORT&=~NRFCE; //StandBy I mode 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); SPI_PORT|=NRFCE; delay(10); } /************************************************** * Function: init_NRF24L01(); * * Description: * This function initializes one nRF24L01 device to * TX mode, set TX address, set RX address for auto.ack, * fill TX payload, select RF channel, datarate & TX pwr. * PWR_UP is set, CRC(2 unsigned chars) is enabled, & PRIM:TX. * * ToDo: One high pulse(>10us) on CE will now send this * packet and expext an acknowledgment from the RX device. **************************************************/ void init_NRF24L01(void) { SPI_PORT&=~NRFCE; SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 If need more channel ,pls refer to age21 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // setup channel is 2.4GHZ SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //Setup reivce data length 20byte SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR SPI_PORT|=NRFCE; }
接收端代碼:
/********************************************************************* ** Device: nRF24L01+ RX ** ** SPI*********** ** ** CE - to digital pin 8 ** ** CSN - to digital pin 9 (SS pin) ** ** CLK - to digital pin 10 (SCK pin) ** ** MOSI - to digital pin 11 (MOSI pin) ** ** MISO - to digital pin 12 (MISO pin) ** ** IRQ - to digital pin 13 ** *********************************************************************/ #include "NRF24L01.h" //*************************************************** #define TX_ADR_WIDTH 5 // 5 unsigned chars TX address width #define RX_ADR_WIDTH 5 // 5 unsigned chars RX address width #define TX_PLOAD_WIDTH 32 // 32 unsigned chars TX payload #define RX_PLOAD_WIDTH 32 // 32 unsigned chars RX payload unsigned char status=0; unsigned char TX_ADDRESS[TX_ADR_WIDTH] = { 0x34,0x43,0x10,0x10,0x01 }; // Define a static TX address unsigned char RX_ADDRESS[RX_ADR_WIDTH] = { 0x34,0x43,0x10,0x10,0x01 }; // Define a static RX address unsigned char RX_BUF[TX_PLOAD_WIDTH]={0}; unsigned char TX_BUF[TX_PLOAD_WIDTH]={0}; //*************************************************** void setup() { SPI_DIR = ( NRFCE + NRFSCK + NRFCSN + NRFMOSI); SPI_DIR &=~ ( NRFIRQ + NRFMISO); delay(100); init_io(); init_NRF24L01(); Serial.begin(9600); } void loop() { nRF24L01_RxPacket(RX_BUF); if((RX_BUF[1]==0x01)&&(RX_BUF[2]==0x02)) { Serial.println("****************RX SUCCEED**********************"); Serial.print("RX_BUF[1]=0x"); Serial.println(RX_BUF[1],HEX); Serial.print("RX_BUF[2]=0x"); Serial.println(RX_BUF[2],HEX); } RX_BUF[1] = 0x00; RX_BUF[2] = 0x00; } //************************************************** // Function: init_io(); // Description: // flash led one time,chip enable(ready to TX or RX Mode), // Spi disable,Spi clock line init high //************************************************** void init_io(void) { SPI_PORT&=~NRFCE; // chip enable SPI_PORT|=NRFCSN; // Spi disable SPI_PORT&=~NRFSCK; // Spi clock line init high } /************************************************** * Function: SPI_RW(); * * Description: * Writes one unsigned char to nRF24L01, and return the unsigned char read * from nRF24L01 during write, according to SPI protocol **************************************************/ unsigned char SPI_RW(unsigned char Byte) { unsigned char i; for(i=0;i<8;i++) // output 8-bit { if(Byte&0x80) { SPI_PORT |=NRFMOSI; // output 'unsigned char', MSB to MOSI } else { SPI_PORT &=~NRFMOSI; } SPI_PORT|=NRFSCK; // Set SCK high.. Byte <<= 1; // shift next bit into MSB.. if(SPI_IN & NRFMISO) { Byte |= 1; // capture current MISO bit } SPI_PORT&=~NRFSCK; // ..then set SCK low again } return(Byte); // return read unsigned char } /**************************************************/ /************************************************** * Function: SPI_RW_Reg(); * * Description: * Writes value 'value' to register 'reg' /**************************************************/ unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value) { unsigned char status; SPI_PORT&=~NRFCSN; // CSN low, init SPI transaction status = SPI_RW(reg); // select register SPI_RW(value); // ..and write value to it.. SPI_PORT|=NRFCSN; // CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /************************************************** * Function: SPI_Read(); * * Description: * Read one unsigned char from nRF24L01 register, 'reg' /**************************************************/ unsigned char SPI_Read(unsigned char reg) { unsigned char reg_val; SPI_PORT&=~NRFCSN; // CSN low, initialize SPI communication... SPI_RW(reg); // Select register to read from.. reg_val = SPI_RW(0); // ..then read register value SPI_PORT|=NRFCSN; // CSN high, terminate SPI communication return(reg_val); // return register value } /**************************************************/ /************************************************** * Function: SPI_Read_Buf(); * * Description: * Reads 'unsigned chars' #of unsigned chars from register 'reg' * Typically used to read RX payload, Rx/Tx address /**************************************************/ unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) { unsigned char status,i; SPI_PORT&=~NRFCSN; // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status unsigned char for(i=0;i<bytes;i++) { pBuf[i] = SPI_RW(0); // Perform SPI_RW to read unsigned char from nRF24L01 } SPI_PORT|=NRFCSN; // Set CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /************************************************** * Function: SPI_Write_Buf(); * * Description: * Writes contents of buffer '*pBuf' to nRF24L01 * Typically used to write TX payload, Rx/Tx address /**************************************************/ unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) { unsigned char status,i; SPI_PORT&=~NRFCSN; // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status unsigned char for(i=0;i<bytes; i++) // then write all unsigned char in buffer(*pBuf) { SPI_RW(*pBuf++); } SPI_PORT|=NRFCSN; // Set CSN high again return(status); // return nRF24L01 status unsigned char } /*************************************************** * Function: nRF24L01_RxPacket(unsigned char* rx_buf) * Description: * Receive data put into rx_buf /**************************************************/ unsigned char nRF24L01_RxPacket(unsigned char* rx_buf) { unsigned char status; unsigned char ret=0; SPI_PORT&=~NRFCE; // chip enable SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收發(fā)完成中斷響應,16位CRC ,主接收 SPI_PORT|=NRFCE; delay(10); status=SPI_Read(STATUS); //read status to judge if receive data if(status&RX_DR) { SPI_PORT&=~NRFCE; SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer ret =1; //finish read data signal } SPI_RW_Reg(WRITE_REG+STATUS,status); //after receive data ,RX_DR,TX_DS,MAX_PT all set 1 to clear interupt signal return ret; } /************************************************** * Function: init_NRF24L01(); * * Description: * This function initializes one nRF24L01 device to * TX mode, set TX address, set RX address for auto.ack, * fill TX payload, select RF channel, datarate & TX pwr. * PWR_UP is set, CRC(2 unsigned chars) is enabled, & PRIM:TX. * * ToDo: One high pulse(>10us) on CE will now send this * packet and expext an acknowledgment from the RX device. **************************************************/ void init_NRF24L01(void) { SPI_PORT&=~NRFCE; SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 If need more channel ,pls refer to age21 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // setup channel is 2.4GHZ SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //Setup reivce data length 20byte SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR SPI_PORT|=NRFCE; }