“(SKU:RB-03T006)NRF24L01无线数传模块”的版本间的差异

来自ALSROBOT WiKi
跳转至: 导航搜索
应用例程1
应用例程
 
(未显示2个用户的7个中间版本)
第13行: 第13行:
 
==使用方法==
 
==使用方法==
 
===引脚说明===
 
===引脚说明===
[[文件:hohn7.jpg|500px|有框|居中]]
+
[[文件:03T00601.png|500px|有框|居中]]
 +
引脚说明:<br/>
 +
* CE:使能发射或接收;
 +
* CSN、 SCK、 MOSI、 MISO: SPI引脚,通过此引脚配置nRF24L01
 +
* IRQ:中断
 +
 
 
==应用例程==
 
==应用例程==
 
1.库文件下载<br/>
 
1.库文件下载<br/>
[http://playground.arduino.cc/uploads/InterfacingWithHardware/Mirf.zip  mirf 库下载地址]<br/>
+
[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"|D7
+
|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'>
#include <SPI.h>
+
 
#include <Mirf.h>
+
/*********************************************************************
#include <nRF24L01.h>
+
**  Device:  nRF24L01+  TX                                          **
#include <MirfHardwareSpiDriver.h>
+
 
void setup()
+
**  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]  =
 
{
 
{
Serial.begin(9600);
+
  0x34,0x43,0x10,0x10,0x01
Mirf.spi = &MirfHardwareSpi;
+
}; // Define a static TX address
Mirf.init();
+
unsigned char RX_ADDRESS[RX_ADR_WIDTH]  =
Mirf.setRADDR((byte *)"clie1");  
+
{
Mirf.payload = 5;  
+
  0x34,0x43,0x10,0x10,0x01
Mirf.channel = 3;  
+
}; // Define a static RX address
Mirf.config();
+
 
 +
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 long time = millis();
+
void loop()  
Mirf.setTADDR((byte *)"serv1"); // 标记对方的地址
+
Sends("hello"); //发送了一个5字节的hello
+
delay(1000);
+
}
+
void Sends(char *str)
+
 
{
 
{
int lens;
+
  unsigned char status=0;
lens=strlen(str);
+
 
char msg[lens];
+
  //  status=SPI_Read(STATUS);
int i;
+
//  if(status&TX_DS)
for (i=0;i<lens;i++)
+
    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)
 
{
 
{
msg[i]= int(str[i]);
+
  SPI_PORT&=~NRFCE; // chip enable
 +
  SPI_PORT|=NRFCSN; // Spi disable
 +
  SPI_PORT&=~NRFSCK; // Spi clock line init high
 
}
 
}
Mirf.send((byte *)&msg);
 
while(Mirf.isSending()){}
 
  
}</pre>
+
/**************************************************
 +
* 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;
 +
}
 +
</pre>
 
<br/>
 
<br/>
 
接收端代码:<br/>
 
接收端代码:<br/>
<pre style='color:blue'>#include <SPI.h>
+
<pre style='color:blue'>
#include <Mirf.h>
+
 
#include <nRF24L01.h>
+
/*********************************************************************
#include <MirfHardwareSpiDriver.h>
+
**  Device:  nRF24L01+  RX                                          **
void setup(){
+
 
Serial.begin(9600);
+
**  SPI***********                                                **
Mirf.spi = &MirfHardwareSpi;
+
**  CE -  to digital pin 8                                        **
Mirf.init();
+
**  CSN -  to digital pin 9  (SS pin)                              **
Mirf.setRADDR((byte *)"serv1"); // 本地名
+
**  CLK -  to digital pin 10 (SCK pin)                              **
Mirf.payload = 5; // 长度
+
**  MOSI - to digital pin 11 (MOSI pin)                            **
Mirf.channel = 3; // 信道
+
**  MISO - to digital pin 12 (MISO pin)                            **
Mirf.config();
+
**  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()
+
 
 +
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)
 
{
 
{
byte data[Mirf.payload];
+
  SPI_PORT&=~NRFCE; // chip enable
if(!Mirf.isSending() && Mirf.dataReady())
+
  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)
 
{
 
{
Mirf.getData(data);
+
  unsigned char i;
int i;
+
  for(i=0;i<8;i++)                     // output 8-bit
String Temp;
+
  {
for (i = 0; i < Mirf.payload; i++) //把收到的信息拼起来,到一个串里面
+
    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)
 
{
 
{
Temp += char(data[i]);
+
  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
 
}
 
}
Serial.print("Get:");
+
/**************************************************/
Serial.print(Mirf.payload);
+
 
Serial.print(" ");
+
/**************************************************
Serial.println(Temp);
+
* Function: SPI_Read();
Mirf.setTADDR((byte *)"clie1");
+
*
Mirf.send(data);
+
* 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
 
}
 
}
}</pre>
+
/**************************************************/
 +
 
 +
/**************************************************
 +
* 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;
 +
}
 +
</pre>
  
==应用例程2==
 
[[文件:hohn10.jpg|500px|有框|居中]]
 
[[文件:hohn11.jpg|500px|有框|居中]]
 
 
==产品相关推荐==
 
==产品相关推荐==
购买地址:[http://www.alsrobot.cn/goods-54.html NRF24L01无线数传模块]
+
[[文件:erweima.png|230px|无框|右]]
 +
===购买地址===
 +
[http://www.alsrobot.cn/goods-54.html NRF24L01无线数传模块]
 +
 
 +
===周边产品推荐===
 +
[http://www.alsrobot.cn/goods-53.html 无线数传模块]
 +
<br/>
 +
[http://www.alsrobot.cn/goods-158.html 无线射频模块]
 +
===相关问题解答===
 +
 
 +
===相关学习资料===
 +
 
 +
[http://www.makerspace.cn/portal.php 奥松机器人技术论坛]

2016年4月11日 (一) 17:29的最后版本

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:中断

应用例程

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

产品相关推荐

Erweima.png

购买地址

NRF24L01无线数传模块

周边产品推荐

无线数传模块
无线射频模块

相关问题解答

相关学习资料

奥松机器人技术论坛