SKU:RB-03T006 NRF24L01无线数传模块

来自ALSROBOT WiKi
2021年12月13日 (一) 09:33Zhangxin讨论 | 贡献的版本

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索
RB-03T006.jpg

目录

产品概述

NRF24L01是一款新型单片射频收发器件,工作于2.4 GHz~2.5 GHz ISM频段。内置频率合成器、功率放大器、晶体振荡器、调制器等功能模块,并融合了增强型ShockBurst技术,其中输出功率和通信频道可通过程序进 行配置。NRF24L01功耗低,在以-6 dBm的功率发射时,工作电流也只有9 mA;接收时,工作电流只有12.3 mA,多种低功率工作模式(掉电模式和空闲模式)使节能设计更方便。

规格参数

  1. 2Mbit/s速率下接收时的峰值电流12.5mA
  2. 在2Mbit/s速率下@0dBm输出时的峰值电流11mA
  3. 掉电模式下的功耗400nA
  4. 待机模式下的功耗32uA
  5. 130us 的快速切换和唤醒时间
  6. 具有片内稳压器oltage regulators
  7. 可在1.9 to 3.6V低电压工作
  8. MultiCeiverMT硬件提供同时6个接收机的功能,2Mbit/s 使得高质量的VoIP成为可能

接口说明

03T00601.png

引脚说明:

  • CE:使能发射或接收;
  • CSN、 SCK、 MOSI、 MISO: SPI引脚,通过此引脚配置nRF24L01
  • IRQ:中断

使用方法

  • 硬件连接

连接注意事项:

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


  • 示例程序

发送端代码


/*********************************************************************
**  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;
}


相关资料

Erweima.png
  • 产品资料

下载链接:https://pan.baidu.com/s/1mtvLUaZQPVl_FMMHIvwuRg 提取码:6bel

  • 更多例程
NRF24L01 使用例子程序
NRF24L01 使用详解
Arduino 官方教程