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

来自ALSROBOT WiKi
跳转至: 导航搜索
产品相关推荐
应用例程
 
(未显示1个用户的5个中间版本)
第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'>
 +
 
/*********************************************************************
 
/*********************************************************************
**  Device:  nRF24L01+                                             **
+
**  Device:  nRF24L01+  TX                                          **
**  File:   EF_nRF24L01_TX.c                                        **
+
 
**                                                                  **
+
**   SPI***********                                                 **
**                                                                 **
+
**  CE -   to digital pin 8                                        **
**  Copyright (C) 2011 ElecFraks.                                  **
+
**  CSN - to digital pin 9  (SS pin)                              **
**  This example code is in the public domain.                      **
+
**  CLK - to digital pin 10 (SCK pin)                             **
**                                                                  **
+
**  Description:                                                    **
+
**  This file is a sample code for your reference.                  **
+
**  It's the v1.0 nRF24L01+ Hardware SPI by arduino                **
+
** Created by ElecFreaks. Robi.W,11 June 2011                      **
+
**                                                                 **
+
**  http://www.elecfreaks.com                                      **
+
**                                                                  **
+
**  SPI-compatible                                                **
+
**  CS - to digital pin 8                                          **
+
**  CSN - to digital pin (SS 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)                            **
**  CLK - to digital pin 13 (SCK pin)                              **
+
**  IRQ - to digital pin 13                                       **
 
*********************************************************************/
 
*********************************************************************/
  
#include <SPI.h>
+
#include "NRF24L01.h"
#include "API.h"
+
#include "nRF24L01.h"
+
  
 
//***************************************************
 
//***************************************************
#define TX_ADR_WIDTH    5  // 5 unsigned chars TX(RX) address width
+
#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};
  
unsigned char rx_buf[TX_PLOAD_WIDTH] = {0}; // initialize value
 
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);
   pinMode(CE,  OUTPUT);
+
    
   pinMode(CSN, OUTPUT);
+
   TX_BUF[1] = 0x01 ;
   pinMode(IRQ, INPUT);
+
   TX_BUF[2] = 0x02 ;
   SPI.begin();
+
   nRF24L01_TxPacket(TX_BUF);
 
   delay(50);
 
   delay(50);
  init_io();                        // Initialize IO port
 
  unsigned char sstatus=SPI_Read(STATUS);
 
  Serial.println("*******************TX_Mode Start****************************");
 
  Serial.print("status = ");   
 
  Serial.println(sstatus,HEX);    // There is read the mode’s status register, the default value should be ‘E’
 
  TX_Mode();                      // set TX mode
 
 
}
 
}
  
 
void loop()  
 
void loop()  
 
{
 
{
  int k = 0;
+
  unsigned char status=0;
   for(;;)
+
    
   {
+
  //  status=SPI_Read(STATUS);
     for(int i=0; i<32; i++)
+
//   if(status&TX_DS)
        tx_buf[i] = k++;      
+
     for(; ;)
    unsigned char sstatus = SPI_Read(STATUS);                   // read register STATUS's value
+
    {   
    if(sstatus&TX_DS)                                           // if receive data ready (TX_DS) interrupt
+
      TX_BUF[1] = 0x01 ;
    {
+
      TX_BUF[2] = 0x02 ;     
       SPI_RW_Reg(FLUSH_TX,0);                                
+
      Serial.println("****************START TX**********************");
       SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);      // write playload to TX_FIFO
+
      Serial.print("TX_BUF[1]=0x");
    }
+
       Serial.println(TX_BUF[1],HEX);
    if(sstatus&MAX_RT)                                         // if receive data ready (MAX_RT) interrupt, this is retransmit than SETUP_RETR                         
+
       Serial.print("TX_BUF[2]=0x");
    {
+
       Serial.println(TX_BUF[2],HEX);  
      SPI_RW_Reg(FLUSH_TX,0);
+
     
      SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);     // disable standy-mode
+
    delay(1000);  
    }
+
    nRF24L01_TxPacket(TX_BUF); // Transmit Tx buffer data
    SPI_RW_Reg(WRITE_REG+STATUS,sstatus);                     // clear RX_DR or TX_DS or MAX_RT interrupt flag
+
    SPI_RW_Reg(WRITE_REG+STATUS,0XFF);  
    delay(1000);
+
   
  }
+
    TX_BUF[1] = 0x00;
 +
    TX_BUF[2] = 0x00;
 +
  }
 
}
 
}
  
第136行: 第144行:
 
void init_io(void)
 
void init_io(void)
 
{
 
{
   digitalWrite(IRQ, 0);
+
   SPI_PORT&=~NRFCE; // chip enable
  digitalWrite(CE, 0); // chip enable
+
   SPI_PORT|=NRFCSN; // Spi disable
   digitalWrite(CSN, 1);                 // Spi disable
+
  SPI_PORT&=~NRFSCK; // Spi clock line init high
 
}
 
}
  
/************************************************************************
+
/**************************************************
**  * Function: SPI_RW();
+
* 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)
 
{
 
{
   return SPI.transfer(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;
  
   digitalWrite(CSN, 0);                  // CSN low, init SPI transaction
+
   SPI_PORT&=~NRFCSN;                  // CSN low, init SPI transaction
   SPI_RW(reg);                           // select register
+
   status = SPI_RW(reg);             // select register
   SPI_RW(value);                         // ..and write value to it..
+
   SPI_RW(value);                   // ..and write value to it..
   digitalWrite(CSN, 1);                   // CSN high again
+
   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;
  
   digitalWrite(CSN, 0);                // CSN low, initialize SPI communication...
+
   SPI_PORT&=~NRFCSN;                // CSN low, initialize SPI communication...
   SPI_RW(reg);                         // Select register to read from..
+
   SPI_RW(reg);                   // Select register to read from..
   reg_val = SPI_RW(0);                 // ..then read register value
+
   reg_val = SPI_RW(0);           // ..then read register value
   digitalWrite(CSN, 1);               // CSN high, terminate SPI communication
+
   SPI_PORT|=NRFCSN;                 // CSN high, terminate SPI communication
  
   return(reg_val);                     // return register value
+
   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 sstatus,i;
+
   unsigned char status,i;
  
   digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
+
   SPI_PORT&=~NRFCSN;                  // Set CSN low, init SPI tranaction
   sstatus = SPI_RW(reg);          // Select register to write to and read status unsigned char
+
   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行:
 
   }
 
   }
  
   digitalWrite(CSN, 1);                  // Set CSN high again
+
   SPI_PORT|=NRFCSN;                  // Set CSN high again
  
   return(sstatus);                  // return nRF24L01 status unsigned char
+
   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 sstatus,i;
+
   unsigned char status,i;
  
   digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
+
   SPI_PORT&=~NRFCSN;                  // Set CSN low, init SPI tranaction
   sstatus = SPI_RW(reg);            // Select register to write to and read status unsigned char
+
   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++);
 
   }
 
   }
   digitalWrite(CSN, 1);                  // Set CSN high again
+
   SPI_PORT|=NRFCSN;                  // Set CSN high again
   return(sstatus);                  // return nRF24L01 status unsigned char
+
   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: TX_Mode();
+
  * 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 TX_Mode(void)
+
void init_NRF24L01(void)
 
{
 
{
   digitalWrite(CE, 0);
+
   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, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack
+
   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);     // 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 + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
+
   SPI_RW_Reg(WRITE_REG + RF_CH, 0);       // setup channel is 2.4GHZ
   SPI_RW_Reg(WRITE_REG + RF_CH, 40);       // Select RF channel 40
+
   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_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);    // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:TX. MAX_RT & TX_DS enabled..
+
  SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
+
  
   digitalWrite(CE, 1);
+
   SPI_PORT|=NRFCE;
 
}
 
}
 
</pre>
 
</pre>
第273行: 第314行:
 
接收端代码:<br/>
 
接收端代码:<br/>
 
<pre style='color:blue'>
 
<pre style='color:blue'>
 +
 
/*********************************************************************
 
/*********************************************************************
**  Device:  nRF24L01+                                             **
+
**  Device:  nRF24L01+  RX                                          **
**  File:   EF_nRF24L01_RX.c                                        **
+
 
**                                                                  **
+
**   SPI***********                                                 **
**                                                                 **
+
**  CE -   to digital pin 8                                        **
**  Copyright (C) 2011 ElecFraks.                                  **
+
**  CSN - to digital pin 9  (SS pin)                              **
**  This example code is in the public domain.                      **
+
**  CLK - to digital pin 10 (SCK pin)                             **
**                                                                  **
+
**  Description:                                                    **
+
**  This file is a sample code for your reference.                  **
+
**  It's the v1.0 nRF24L01+ Hardware SPI by arduino                **
+
** Created by ElecFreaks. Robi.W,11 June 2011                      **
+
**                                                                 **
+
**  http://www.elecfreaks.com                                      **
+
**                                                                  **
+
**  SPI-compatible                                                **
+
**  CS - to digital pin 8                                          **
+
**  CSN - to digital pin (SS 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)                            **
**  CLK - to digital pin 13 (SCK pin)                              **
+
**  IRQ - to digital pin 13                                         **
 
*********************************************************************/
 
*********************************************************************/
  
 
+
#include "NRF24L01.h"
#include <SPI.h>
+
#include "API.h"
+
#include "nRF24L01.h"
+
  
 
//***************************************************
 
//***************************************************
#define TX_ADR_WIDTH    5  // 5 unsigned chars TX(RX) address width
+
#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 rx_buf[TX_PLOAD_WIDTH] = {0}; // initialize value
+
unsigned char RX_ADDRESS[RX_ADR_WIDTH] =  
unsigned char tx_buf[TX_PLOAD_WIDTH] = {0};
+
{
 +
  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()  
 
{
 
{
   Serial.begin(9600);
+
   SPI_DIR = ( NRFCE + NRFSCK + NRFCSN + NRFMOSI);
   pinMode(CE,  OUTPUT);
+
   SPI_DIR &=~ ( NRFIRQ + NRFMISO);
  pinMode(CSN, OUTPUT);
+
 
  pinMode(IRQ, INPUT);
+
   delay(100);
  SPI.begin();
+
   init_io();
   delay(50);
+
   init_NRF24L01();
   init_io();                       // Initialize IO port
+
    
   unsigned char sstatus=SPI_Read(STATUS);
+
   Serial.begin(9600);
   Serial.println("*******************RX_Mode Start****************************");
+
   Serial.print("status = ");  
+
  Serial.println(sstatus,HEX);    // There is read the mode’s status register, the default value should be ‘E’
+
  RX_Mode();                        // set RX mode
+
 
}
 
}
  
 
void loop()  
 
void loop()  
{
+
{  
  for(;;)
+
nRF24L01_RxPacket(RX_BUF);
  {
+
if((RX_BUF[1]==0x01)&&(RX_BUF[2]==0x02))
    unsigned char status = SPI_Read(STATUS);                         // read register STATUS's value
+
{  
    if(status&RX_DR)                                                 // if receive data ready (TX_DS) interrupt
+
                    Serial.println("****************RX SUCCEED**********************");                  
    {
+
                    Serial.print("RX_BUF[1]=0x");
      SPI_Read_Buf(RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH);            // read playload to rx_buf
+
                    Serial.println(RX_BUF[1],HEX);
      SPI_RW_Reg(FLUSH_RX,0);                                       // clear RX_FIFO
+
                    Serial.print("RX_BUF[2]=0x");
      for(int i=0; i<32; i++)
+
                    Serial.println(RX_BUF[2],HEX);                    
      {
+
}
          Serial.print(" ");
+
RX_BUF[1] = 0x00;
          Serial.print(rx_buf[i],HEX);                             // print rx_buf
+
RX_BUF[2] = 0x00;
      }
+
      Serial.println(" ");
+
    }
+
    SPI_RW_Reg(WRITE_REG+STATUS,status);                             // clear RX_DR or TX_DS or MAX_RT interrupt flag
+
    delay(1000);
+
  }
+
 
}
 
}
 +
  
 
//**************************************************
 
//**************************************************
第358行: 第386行:
 
void init_io(void)
 
void init_io(void)
 
{
 
{
   digitalWrite(IRQ, 0);
+
   SPI_PORT&=~NRFCE; // chip enable
  digitalWrite(CE, 0); // chip enable
+
   SPI_PORT|=NRFCSN; // Spi disable
   digitalWrite(CSN, 1);                 // Spi disable
+
  SPI_PORT&=~NRFSCK; // Spi clock line init high
 
}
 
}
  
/************************************************************************
+
/**************************************************
**  * Function: SPI_RW();
+
* 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)
 
{
 
{
   return SPI.transfer(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;
  
   digitalWrite(CSN, 0);                  // CSN low, init SPI transaction
+
   SPI_PORT&=~NRFCSN;                  // CSN low, init SPI transaction
   SPI_RW(reg);                           // select register
+
   status = SPI_RW(reg);             // select register
   SPI_RW(value);                         // ..and write value to it..
+
   SPI_RW(value);                   // ..and write value to it..
   digitalWrite(CSN, 1);                   // CSN high again
+
   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;
  
   digitalWrite(CSN, 0);                // CSN low, initialize SPI communication...
+
   SPI_PORT&=~NRFCSN;                // CSN low, initialize SPI communication...
   SPI_RW(reg);                         // Select register to read from..
+
   SPI_RW(reg);                   // Select register to read from..
   reg_val = SPI_RW(0);                 // ..then read register value
+
   reg_val = SPI_RW(0);           // ..then read register value
   digitalWrite(CSN, 1);               // CSN high, terminate SPI communication
+
   SPI_PORT|=NRFCSN;                 // CSN high, terminate SPI communication
  
   return(reg_val);                     // return register value
+
   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 sstatus,i;
+
   unsigned char status,i;
  
   digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
+
   SPI_PORT&=~NRFCSN;                  // Set CSN low, init SPI tranaction
   sstatus = SPI_RW(reg);          // Select register to write to and read status unsigned char
+
   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行:
 
   }
 
   }
  
   digitalWrite(CSN, 1);                  // Set CSN high again
+
   SPI_PORT|=NRFCSN;                  // Set CSN high again
  
   return(sstatus);                  // return nRF24L01 status unsigned char
+
   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 sstatus,i;
+
   unsigned char status,i;
  
   digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
+
   SPI_PORT&=~NRFCSN;                  // Set CSN low, init SPI tranaction
   sstatus = SPI_RW(reg);            // Select register to write to and read status unsigned char
+
   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++);
 
   }
 
   }
   digitalWrite(CSN, 1);                  // Set CSN high again
+
   SPI_PORT|=NRFCSN;                  // Set CSN high again
   return(sstatus);                  // return nRF24L01 status unsigned char
+
   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 RX_Mode(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)
 
{
 
{
   digitalWrite(CE, 0);
+
   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_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device
+
   SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);     // Enable Auto.Ack:Pipe0
   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 + 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, 40);        // Select RF channel 40
+
   SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //Setup reivce data length 20byte
   SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
+
   SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
   SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
+
 
  SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);    // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:RX. RX_DR enabled..
+
   SPI_PORT|=NRFCE;
   digitalWrite(CE, 1);                             // Set CE pin high to enable RX device
+
  //  This device is now ready to receive one packet of 16 unsigned chars payload from a TX device sending to address
+
  //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
+
 
}
 
}
 
</pre>
 
</pre>

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无线数传模块

周边产品推荐

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

相关问题解答

相关学习资料

奥松机器人技术论坛