“(SKU:RB-03T006)NRF24L01无线数传模块”的版本间的差异
来自ALSROBOT WiKi
(→产品相关推荐) |
(→应用例程) |
||
(未显示1个用户的5个中间版本) | |||
第13行: | 第13行: | ||
==使用方法== | ==使用方法== | ||
===引脚说明=== | ===引脚说明=== | ||
− | [[文件: | + | [[文件:03T00601.png|500px|有框|居中]] |
+ | 引脚说明:<br/> | ||
+ | * CE:使能发射或接收; | ||
+ | * CSN、 SCK、 MOSI、 MISO: SPI引脚,通过此引脚配置nRF24L01 | ||
+ | * IRQ:中断 | ||
+ | |||
==应用例程== | ==应用例程== | ||
1.库文件下载<br/> | 1.库文件下载<br/> | ||
− | [http:// | + | [http://pan.baidu.com/s/1slzfS65 NRF2401库文件及使用例程下载地址]<br/> |
2.连接Arduino和NRF2401模块<br/> | 2.连接Arduino和NRF2401模块<br/> | ||
+ | 连接注意事项:<br/> | ||
+ | * VCC引脚的电压范围2.3 - 3.6之间,超过 3.6V 模块会烧掉, 建议使用3.3V左右。 | ||
+ | * 该模块也可以通过普通 IO 口模拟 SPI 时序进行读写数据操作。 | ||
+ | * 使用 2 个模块同时发射时,两者频道间隔应该至少相差1MHZ,否则同频道之间易干扰。 | ||
{|border="1" cellspacing="0" align="center" cellpadding="5" width="400px" | {|border="1" cellspacing="0" align="center" cellpadding="5" width="400px" | ||
|- | |- | ||
第30行: | 第39行: | ||
|- | |- | ||
|align="center"|CSN | |align="center"|CSN | ||
− | |align="center"| | + | |align="center"|D9 |
|- | |- | ||
|align="center"|CE | |align="center"|CE | ||
第42行: | 第51行: | ||
|- | |- | ||
|align="center"|SCK | |align="center"|SCK | ||
+ | |align="center"|D10 | ||
+ | |- | ||
+ | |align="center"|IRQ | ||
|align="center"|D13 | |align="center"|D13 | ||
− | |||
|} | |} | ||
<br> | <br> | ||
第50行: | 第61行: | ||
发送端代码<br/> | 发送端代码<br/> | ||
<pre style='color:blue'> | <pre style='color:blue'> | ||
+ | |||
/********************************************************************* | /********************************************************************* | ||
− | ** Device: nRF24L01+ | + | ** 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) ** | ** MOSI - to digital pin 11 (MOSI pin) ** | ||
** MISO - to digital pin 12 (MISO pin) ** | ** MISO - to digital pin 12 (MISO pin) ** | ||
− | ** | + | ** IRQ - to digital pin 13 ** |
*********************************************************************/ | *********************************************************************/ | ||
− | + | #include "NRF24L01.h" | |
− | #include " | + | |
− | + | ||
//*************************************************** | //*************************************************** | ||
− | #define TX_ADR_WIDTH 5 // 5 unsigned chars TX | + | #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 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] = | unsigned char TX_ADDRESS[TX_ADR_WIDTH] = | ||
第85行: | 第86行: | ||
0x34,0x43,0x10,0x10,0x01 | 0x34,0x43,0x10,0x10,0x01 | ||
}; // Define a static TX address | }; // 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() | void setup() | ||
{ | { | ||
+ | SPI_DIR = ( NRFCE + NRFSCK + NRFCSN + NRFMOSI); | ||
+ | SPI_DIR &=~ ( NRFIRQ + NRFMISO); | ||
+ | |||
+ | delay(100); | ||
+ | init_io(); | ||
+ | init_NRF24L01(); | ||
+ | |||
Serial.begin(9600); | Serial.begin(9600); | ||
− | + | ||
− | + | TX_BUF[1] = 0x01 ; | |
− | + | TX_BUF[2] = 0x02 ; | |
− | + | nRF24L01_TxPacket(TX_BUF); | |
delay(50); | delay(50); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
void loop() | void loop() | ||
{ | { | ||
− | + | unsigned char status=0; | |
− | + | ||
− | + | // status=SPI_Read(STATUS); | |
− | for( | + | // 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; | ||
+ | } | ||
} | } | ||
第136行: | 第144行: | ||
void init_io(void) | 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: | * Description: | ||
* Writes one unsigned char to nRF24L01, and return the unsigned char read | * Writes one unsigned char to nRF24L01, and return the unsigned char read | ||
* from nRF24L01 during write, according to SPI protocol | * from nRF24L01 during write, according to SPI protocol | ||
− | + | **************************************************/ | |
unsigned char SPI_RW(unsigned char Byte) | 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 | ||
} | } | ||
− | |||
/**************************************************/ | /**************************************************/ | ||
第165行: | 第191行: | ||
unsigned char status; | unsigned char status; | ||
− | + | SPI_PORT&=~NRFCSN; // CSN low, init SPI transaction | |
− | SPI_RW(reg); | + | status = SPI_RW(reg); // select register |
− | SPI_RW(value); | + | SPI_RW(value); // ..and write value to it.. |
− | + | SPI_PORT|=NRFCSN; // CSN high again | |
return(status); // return nRF24L01 status unsigned char | return(status); // return nRF24L01 status unsigned char | ||
第184行: | 第210行: | ||
unsigned char reg_val; | unsigned char reg_val; | ||
− | + | SPI_PORT&=~NRFCSN; // CSN low, initialize SPI communication... | |
− | SPI_RW(reg); | + | SPI_RW(reg); // Select register to read from.. |
− | reg_val = SPI_RW(0); | + | reg_val = SPI_RW(0); // ..then read register value |
− | + | SPI_PORT|=NRFCSN; // CSN high, terminate SPI communication | |
− | return(reg_val); | + | return(reg_val); // return register value |
} | } | ||
/**************************************************/ | /**************************************************/ | ||
第202行: | 第228行: | ||
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | ||
{ | { | ||
− | unsigned char | + | 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++) | for(i=0;i<bytes;i++) | ||
第212行: | 第238行: | ||
} | } | ||
− | + | SPI_PORT|=NRFCSN; // Set CSN high again | |
− | return( | + | return(status); // return nRF24L01 status unsigned char |
} | } | ||
/**************************************************/ | /**************************************************/ | ||
第227行: | 第253行: | ||
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | ||
{ | { | ||
− | unsigned char | + | 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) | for(i=0;i<bytes; i++) // then write all unsigned char in buffer(*pBuf) | ||
{ | { | ||
SPI_RW(*pBuf++); | SPI_RW(*pBuf++); | ||
} | } | ||
− | + | SPI_PORT|=NRFCSN; // Set CSN high again | |
− | return( | + | 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: | + | * Function: init_NRF24L01(); |
* | * | ||
* Description: | * Description: | ||
第252行: | 第295行: | ||
* packet and expext an acknowledgment from the RX device. | * packet and expext an acknowledgment from the RX device. | ||
**************************************************/ | **************************************************/ | ||
− | void | + | 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 + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01 | ||
− | SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, | + | 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); | + | SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0 |
− | SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 | + | SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 If need more channel ,pls refer to age21 |
− | SPI_RW_Reg(WRITE_REG + | + | SPI_RW_Reg(WRITE_REG + RF_CH, 0); // setup channel is 2.4GHZ |
− | SPI_RW_Reg(WRITE_REG + | + | SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //Setup reivce data length 20byte |
− | SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); | + | SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR |
− | + | ||
− | + | ||
− | + | SPI_PORT|=NRFCE; | |
} | } | ||
</pre> | </pre> | ||
第273行: | 第314行: | ||
接收端代码:<br/> | 接收端代码:<br/> | ||
<pre style='color:blue'> | <pre style='color:blue'> | ||
+ | |||
/********************************************************************* | /********************************************************************* | ||
− | ** Device: nRF24L01+ | + | ** 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) ** | ** MOSI - to digital pin 11 (MOSI pin) ** | ||
** MISO - to digital pin 12 (MISO pin) ** | ** MISO - to digital pin 12 (MISO pin) ** | ||
− | ** | + | ** IRQ - to digital pin 13 ** |
*********************************************************************/ | *********************************************************************/ | ||
− | + | #include "NRF24L01.h" | |
− | + | ||
− | + | ||
− | #include " | + | |
//*************************************************** | //*************************************************** | ||
− | #define TX_ADR_WIDTH 5 // 5 unsigned chars TX | + | #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 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] = | unsigned char TX_ADDRESS[TX_ADR_WIDTH] = | ||
{ | { | ||
第310行: | 第341行: | ||
}; // Define a static TX address | }; // Define a static TX address | ||
− | unsigned char | + | unsigned char RX_ADDRESS[RX_ADR_WIDTH] = |
− | unsigned char | + | { |
+ | 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() | void setup() | ||
{ | { | ||
− | + | SPI_DIR = ( NRFCE + NRFSCK + NRFCSN + NRFMOSI); | |
− | + | SPI_DIR &=~ ( NRFIRQ + NRFMISO); | |
− | + | ||
− | + | delay(100); | |
− | + | init_io(); | |
− | delay( | + | init_NRF24L01(); |
− | init_io(); | + | |
− | + | Serial.begin(9600); | |
− | + | ||
− | Serial. | + | |
− | + | ||
− | + | ||
} | } | ||
void loop() | 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; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
+ | |||
//************************************************** | //************************************************** | ||
第358行: | 第386行: | ||
void init_io(void) | 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: | * Description: | ||
* Writes one unsigned char to nRF24L01, and return the unsigned char read | * Writes one unsigned char to nRF24L01, and return the unsigned char read | ||
* from nRF24L01 during write, according to SPI protocol | * from nRF24L01 during write, according to SPI protocol | ||
− | + | **************************************************/ | |
unsigned char SPI_RW(unsigned char Byte) | 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 | ||
} | } | ||
− | |||
/**************************************************/ | /**************************************************/ | ||
第387行: | 第433行: | ||
unsigned char status; | unsigned char status; | ||
− | + | SPI_PORT&=~NRFCSN; // CSN low, init SPI transaction | |
− | SPI_RW(reg); | + | status = SPI_RW(reg); // select register |
− | SPI_RW(value); | + | SPI_RW(value); // ..and write value to it.. |
− | + | SPI_PORT|=NRFCSN; // CSN high again | |
return(status); // return nRF24L01 status unsigned char | return(status); // return nRF24L01 status unsigned char | ||
第406行: | 第452行: | ||
unsigned char reg_val; | unsigned char reg_val; | ||
− | + | SPI_PORT&=~NRFCSN; // CSN low, initialize SPI communication... | |
− | SPI_RW(reg); | + | SPI_RW(reg); // Select register to read from.. |
− | reg_val = SPI_RW(0); | + | reg_val = SPI_RW(0); // ..then read register value |
− | + | SPI_PORT|=NRFCSN; // CSN high, terminate SPI communication | |
− | return(reg_val); | + | return(reg_val); // return register value |
} | } | ||
/**************************************************/ | /**************************************************/ | ||
第424行: | 第470行: | ||
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | ||
{ | { | ||
− | unsigned char | + | 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++) | for(i=0;i<bytes;i++) | ||
第434行: | 第480行: | ||
} | } | ||
− | + | SPI_PORT|=NRFCSN; // Set CSN high again | |
− | return( | + | return(status); // return nRF24L01 status unsigned char |
} | } | ||
/**************************************************/ | /**************************************************/ | ||
第449行: | 第495行: | ||
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) | ||
{ | { | ||
− | unsigned char | + | 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) | for(i=0;i<bytes; i++) // then write all unsigned char in buffer(*pBuf) | ||
{ | { | ||
SPI_RW(*pBuf++); | SPI_RW(*pBuf++); | ||
} | } | ||
− | + | SPI_PORT|=NRFCSN; // Set CSN high again | |
− | return( | + | 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; | ||
− | void | + | SPI_PORT&=~NRFCE; // chip enable |
+ | SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,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_AA, 0x01); | + | SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 If need more channel ,pls refer to age21 |
− | SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 | + | SPI_RW_Reg(WRITE_REG + RF_CH, 0); // setup channel is 2.4GHZ |
− | SPI_RW_Reg(WRITE_REG + RF_CH, | + | SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //Setup reivce data length 20byte |
− | SPI_RW_Reg(WRITE_REG + RX_PW_P0, | + | SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR |
− | SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); | + | |
− | + | SPI_PORT|=NRFCE; | |
− | + | ||
− | + | ||
− | + | ||
} | } | ||
</pre> | </pre> |
2016年4月11日 (一) 17:29的最后版本
目录 |
产品概述
NRF24L01是一款新型单片射频收发器件,工作于2.4 GHz~2.5 GHz ISM频段。内置频率合成器、功率放大器、晶体振荡器、调制器等功能模块,并融合了增强型ShockBurst技术,其中输出功率和通信频道可通过程序进 行配置。NRF24L01功耗低,在以-6 dBm的功率发射时,工作电流也只有9 mA;接收时,工作电流只有12.3 mA,多种低功率工作模式(掉电模式和空闲模式)使节能设计更方便。
规格参数
- 2Mbit/s速率下接收时的峰值电流12.5mA
- 在2Mbit/s速率下@0dBm输出时的峰值电流11mA
- 掉电模式下的功耗400nA
- 待机模式下的功耗32uA
- 130us 的快速切换和唤醒时间
- 具有片内稳压器oltage regulators
- 可在1.9 to 3.6V低电压工作
- MultiCeiverMT硬件提供同时6个接收机的功能,2Mbit/s 使得高质量的VoIP成为可能
使用方法
引脚说明
引脚说明:
- CE:使能发射或接收;
- CSN、 SCK、 MOSI、 MISO: SPI引脚,通过此引脚配置nRF24L01
- IRQ:中断
应用例程
1.库文件下载
NRF2401库文件及使用例程下载地址
2.连接Arduino和NRF2401模块
连接注意事项:
- VCC引脚的电压范围2.3 - 3.6之间,超过 3.6V 模块会烧掉, 建议使用3.3V左右。
- 该模块也可以通过普通 IO 口模拟 SPI 时序进行读写数据操作。
- 使用 2 个模块同时发射时,两者频道间隔应该至少相差1MHZ,否则同频道之间易干扰。
NRF2401 | Arduino |
VCC | 3V3 |
GND | GND |
CSN | D9 |
CE | D8 |
MOSI | D11 |
MISO | D12 |
SCK | D10 |
IRQ | D13 |
3.代码下载
发送端代码
/********************************************************************* ** 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收发完成中断响应,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; }