#include
#define uint unsigned int
#define uchar unsigned char
//****************************************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
//******************************************************************************************
uchar sta; //状态标志
#define RX_DR (sta & 0x40)
#define TX_DS (sta & 0x20)
#define MAX_RT (sta & 0x10)
void delayms(unsigned int count)
{
unsigned int i,j;
for(i=0;i
}
//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
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
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;
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];
}
}
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
- Popular Resources
- Popular amplifiers
- Application Specific Integrated Circuit Design Topic 3 - Verification and Test
- Application Specific Integrated Circuit Design Topic 2 - FPGA Design with Verilog
- Application Specific Integrated Circuit Design Topic 1 - Introduction
- EXPLORE ESP32 MICROPYTHON Python Coding, Arduino Coding, Raspberry Pi, ESP8266, IoT Projects, Androi
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Wi-Fi 6 and Wi-Fi 6E: The key to the Internet of Things
- Two boards can be cascaded, but not placed on the same layout.
- [ATmega4809 Curiosity Nano Review] How to use RTC
- EEWORLD University Hall----Set up UCD3138 for JTAG communication
- Can the silkscreen on the board be deleted if there is insufficient space? What are the key files required for patch?
- [Mill MYB-YT507 development board trial experience] tkinterLabel learning
- [Automatic clock-in walking timing system based on face recognition] Node communication - LoRa technology
- TI: Development platform compatible with multiple wireless communication protocols
- What is this error?
- I want to use CD4051 as the RX expansion of USART serial port, but how to calculate the baud rate that CD4051 can meet...