nRF24L01 Applications

Publisher:CrystalSparkleLatest update time:2016-09-06 Source: eefocusKeywords:nRF24L01 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I recently used the nRF24L01 module and debugged it with the circuit board that I used to debug nRF2401. I referred to other people's program codes, which are programs that simulate SPI. I digested and understood the program in combination with the nRF24L01 data sheet, made some modifications, combined with my own circuit, and debugged it successfully. This is a program that transmits and receives.
nRF24L01 Application Program - Xiaowen - Xiaowen Electronic Design
Source program:
#include
#include

#define uint unsigned int
#define uchar unsigned char
//********************************************IO port definition***************************************
//****************************************NRF24L01 port definition***************************************
sbit MISO =P1^2;
sbit MOSI =P1^6;
sbit SCK =P1^3;
sbit CE =P1^4;
sbit CSN =P1^5;
sbit IRQ =P1^7;
//****************************************Buttons***************************************************
sbit KEY1=P3^4;

//************************************************NRF24L01** *************************************
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 20 // 20 uints TX payload
#define RX_PLOAD_WIDTH 20 // 20 uints TX payload
uchar TX_ADDRESS[TX_ADR_WIDTH]= {0x12,0x34,0x56,0x78,0x90}; //Local address
uchar RX_ADDRESS[RX_ADR_WIDTH]= {0x12,0x34,0x56,0x78,0x90}; //Receive address
//****************************************NRF24L01 register instruction** ************************************************** ***
#define READ_REG 0x00 // Read register instruction
#define WRITE_REG 0x20 // Write register instruction
#define RD_RX_PLOAD 0x61 // Read receive data instruction
#define WR_TX_PLOAD 0xA0 // Write data to be sent instruction
#define FLUSH_TX 0xE1 // Flush Send FIFO instruction
#define FLUSH_RX 0xE2 // Flush receive FIFO instruction
#define REUSE_TX_PL 0xE3 // Define repeat load data instruction
#define NOP 0xFF // Reserved
//******************** ********************SPI(nRF24L01)Register Address**************************** ****************************
#define CONFIG 0x00 // Configure the receiving and sending status, CRC check mode and receiving and sending status response mode
#define EN_AA 0x01 // Automatic answer function setting
#define EN_RXADDR 0x02 // Available channel setting
#define SETUP_AW 0x03 // Send and receive address width setting
#define SETUP_RETR 0x04 // Automatic reset Transmit function settings
#define RF_CH 0x05 // Operating frequency settings
#define RF_SETUP 0x06 // Transmit rate, power consumption function settings
#define STATUS 0x07 // Status register
#define OBSERVE_TX 0x08 // Transmit monitoring function
#define CD 0x09 // Address detection           
#define RX_ADDR_P0 0x0A // Channel 0 receive data address
#define RX_ADDR_P1 0x0B // Channel 1 receive data address
#define RX_ADDR_P2 0x0C // Channel 2 receive data address
#define RX_ADDR_P3 0x0D // Channel 3 receive data address
#define RX_ADDR_P4 0x0E // Channel 4 receive data address
#define RX_ADDR_P5 0x0F // Channel 5 receive data address
#define TX_ADDR 0x10 // Transmit address register
#define RX_PW_P0 0x11 // Receive data length for receiving channel 0
#define RX_PW_P1 0x12 // Receive data length for receiving channel 0
#define RX_PW_P2 0x13 // Receive data length for receiving channel 0
#define RX_PW_P3 0x14 // Receive data length for receiving channel 0
#define RX_PW_P4 0x15 / / Receive data length for receiving channel 0
#define RX_PW_P5 0x16 // Receive data length for receiving channel 0
#define FIFO_STATUS 0x17 //FIFO stack push and pull status register settings
//***************************************** *************************************************
void Delay(unsigned int s);
void inerDelay_us(unsigned char n);
void init_NRF24L01(void);
uchar SPI_RW(uchar byte);
uchar SPI_Read(uchar reg);
void SetRX_Mode(void);
uchar SPI_RW_Reg(uchar reg, uchar value) ;
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num);
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);
void delayms(unsigned int count);
//*****************************************长延时*****************************************
void Delay(unsigned int s)
{
 unsigned int i;
 for(i=0; i  for(i=0; i }
//******************************************************************************************
uchar  sta;   //状态标志
#define RX_DR (sta & 0x40)
#define TX_DS (sta & 0x20)
#define MAX_RT (sta & 0x10)
 
//*********** millisecond delay program************************/
void delayms(unsigned int count)
{
 unsigned int i,j;
 for(i=0;i  for(j=0;j<120;j++);
}
 
//****************************************************************************************
//NRF24L01 initialization
void init_NRF24L01(void)
{
    delayms(1);
  CE=0; // chip enable
  CSN=1; // Spi disable 
  SCK=0; // Spi clock line init high
 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // write local address 
 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // write receiving address
 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // channel 0 automatic ACK response enable 
 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Only channel 0 is allowed to receive the address. If multiple channels are needed, please refer to Page21  
 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // Set the channel to work at 2.4GHZ, and the sending and receiving must be consistent
 SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); // Set the receive data length, this time it is set to 32 bytes
 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // Set the transmission rate to 1MHZ and the transmission power to the maximum value of 0dB
}
//*******************************************************************************************************
//Function: uint SPI_RW(uint uchar)
//Function: SPI write timing of NRF24L01
//*******************************************************************************************************/
uchar SPI_RW(uchar byte)
{
 uchar i;
    for(i=0;i<8;i++) // output 8-bit
    {
  MOSI = (byte & 0x80); // output 'uchar', MSB to MOSI
  byte = (byte << 1); // shift next bit into MSB..
  SCK = 1; // Set SCK high..
  byte |= MISO; // capture current MISO bit
  SCK = 0; // ..then set SCK low again
    }
    return(byte); // return read uchar
}
//****** *********************************************************************************************
//Function: uchar SPI_Read(uchar reg)
//Function: SPI timing of NRF24L01
/************************************************************************************************/
uchar SPI_Read(uchar reg)
{
 uchar reg_val;
 
 CSN = 0; // CSN low, initialize SPI communication...
 SPI_RW(reg); // Select register to read from..
 reg_val = SPI_RW(0); // ..then read registervalue
 CSN = 1; // CSN high, terminate SPI communication
 
 return(reg_val); // return register value
}
//***************************************************************************************************/
//Function: NRF24L01 read and write register function
//***************************************************************************************************/
uchar SPI_RW_Reg(uchar reg, uchar value)
{
 uchar status;
 
 CSN = 0; // CSN low, init SPI transaction
 status = SPI_RW(reg); // select register
 SPI_RW(value); // ..and write value to it..
 CSN = 1; // CSN high again
 
 return(status); // return nRF24L01 status uchar
}
//***********************************************************************************************/
//函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
//功能: used to read data, reg: register address, pBuf: address of data to be read, uchars: number of read data
//***********************************************************************************************/
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num)
{
 uchar status,i;
 
 CSN = 0; // Set CSN low, init SPI tranaction
 status = SPI_RW(reg); // Select register to write to and read status uchar
 
 for(i=0;i   pBuf[i] = SPI_RW(0); // 
 
 CSN = 1;                           
 
 return(status); // return nRF24L01 status uchar
}
//********************************************************************************************************
//Function: uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
//Function: used to write data: register address, pBuf: address of data to be written, uchars: number of written data
//****************************************************************************************************/
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num)
{
 uchar status,i;
 
 CSN = 0; //SPI enable       
 status = SPI_RW(reg);   
 for(i=0; i   SPI_RW(*pBuf++);
 CSN = 1; //Close SPI
 return(status); // 
}
//***********************************************************************************************/
//Function: void SetRX_Mode(void)
//Function: data receiving configuration 
//***************************************************************************************************/
void SetRX_Mode(void)
{
 CE=0;
 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ receiving and sending completion interrupt response, 16-bit CRC, main receiving
 CE = 1; 
 delayms(1);
}
//************************************************************************************************/
//Function: unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
//Function: After reading the data, put it in the rx_buf receiving buffer
/****************************************************************************************************/
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
    unsigned char revale=0;
 sta=SPI_Read(STATUS); // Read the status register to determine the data reception status
 if(RX_DR) // Determine whether data is received
 {
     CE = 0; //SPI enables
  SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
  revale =1; //Read data completion flag
 }
 SPI_RW_Reg(WRITE_REG+STATUS,sta); //After receiving the data, RX_DR,TX_DS,MAX_PT are all set to 1, and the interrupt flag is cleared by writing 1
 return revale;
}
//**********************************************************************************************************
//Function: void nRF24L01_TxPacket(unsigned char * tx_buf)
//Function: Send data in tx_buf
//********************************************************************************************************/
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
 CE=0; //StandBy I mode 
 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); //Load receiving end address
 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); //Load data 
 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); //IRQ transmit and receive completion interrupt response, 16-bit CRC, master transmit
 CE=1; //Set CE high to stimulate data transmission
 delayms(1);
}
//************************************Main function************************************************************
void main(void)
{
 unsigned char TxBuf[20]={0}; // 
 unsigned char RxBuf[20]={0};
 unsigned char led_num; 
  delayms(100);
    init_NRF24L01() ;
 TxBuf[1] = 0x55 ;
 nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data
 Delay(6000);
 led_num=0x00;
 while(1)
 {
     if(KEY1 ==0 ) 
    {
      led_num++; 
      TxBuf[1] =led_num ;
   nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data     
      P0=~led_num;
   Delay(5000);
     }
  
   
//********************************************************************************************
  SetRX_Mode(); // Start sending each time and then jump directly to receiving without pressing any button.
  nRF24L01_RxPacket(RxBuf);
  if(RX_DR==0) P0=~RxBuf[1];  
  
 }
 
}
Keywords:nRF24L01 Reference address:nRF24L01 Applications

Previous article:A Design of USB Download Cable for AT89S52
Next article:Model aircraft remote control modification---PPM waveform generation

Recommended ReadingLatest update time:2024-11-15 14:55

stm32 nRF24L01 wireless module (2): Address of the wireless module
    Those who have just started to use nRF24L01 may notice that there are two defined addresses in the routine function. const u8 TX_ADDRESS ={0x34,0x43,0x10,0x10,0x01}; //Send address const u8 RX_ADDRESS ={0x34,0x43,0x10,0x10,0x01};      This is very confusing. Why is the address still an array? And a search on the I
[Microcontroller]
stm32 nRF24L01 wireless module (2): Address of the wireless module
Implementing IAP (In Application Programming) on ​​STM8L
1. To download Boot and App to different addresses of FLASH, we need to configure the link script. The default link script of IAR is in the installation directory C:\Program Files (x86)\IAR Systems\Embedded Workbench 7.0\stm8\config. Find the corresponding chip and open it. You can see the following words: ////////
[Microcontroller]
NRF24l01 Wireless Temperature Sensor (1)
/* The part of transport programm ,used MCU is AT89C52*/ //////////////////////////////////////////////////////////////////////////////////////////////////// #include reg52.h #include intrins.h typedef unsigned char uchar; typedef unsigned char uint; //****************************************define I/O prot***
[Microcontroller]
nRF24L01 AVR Transmit Receive Driver
#include   #include   #include "12864.h"      Zhixinrui Electronic Technology Community: http://www.zhixinrui.com //------------------------------------------------------------------------------   //spi flag   #define DDR_SPI DDRB   #define DD_ONE 5   #define DD_MISO 6   #define DD_SCK 7   #define
[Microcontroller]
STC12C5A60S2 uses NRF24L01 and stm32 to communicate
/******************************************/ // // The main frequency of this program is 12MHz, and the microcontroller uses STC12C5A60S2 // /******************************************/ #include "STC12C5A60S2.H" #define uchar unsigned char #define uint  unsigned int /********** NRF24L01 register operation comman
[Microcontroller]
NRF24L01 transmission + receiving program passed the test
Main control STC89C52RC crystal oscillator: 11.0592 while(NRF24L01_TxPacket(tmp_buf)!=TX_OK); //If the transmission fails, it will continue to send until the other party receives it successfully.      This sentence is mainly used to detect the completion of the transmission signal. You can take this sentence for your
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号