当前位置:网站首页>Arduino uses nrf24l01+ communication

Arduino uses nrf24l01+ communication

2022-07-05 08:01:00 Drink more hot water-

Arduino Originally meant as open source to open up , There are a lot of Libraries , But uneven , This 24L01+ Many libraries have been changed all nm no way , It's speechless , I found a program on the Internet Changed , Finally, we can communicate simply

Record :

CE: Module control line ,CSN For the low ,CE synergy CONFIG Registers jointly decide NRF24L01 state
CSN: SPI Film selection line
SCK: SPI Clock line
MOSI:SPI cable ( Host output slave input )
MISO:SPI cable ( Host input and slave output )
IRQ: Break the signal line . Becomes low when interrupted , It becomes lower in the following three cases :Tx FIFO Send and receive ACK( Can make ACK Under the circumstances )、Rx FIFO Receive the data 、 Maximum number of retransmissions reached

There is no need to interrupt here ,Arduino UNO Just let it go 2 Middle fracture , This kind of amateur MCU Inquiry does not delay things

This kind of wireless communication module is usually bound to address , Fixed channel , FM or something , Fixed routine , Here is a simulation SPI The driver , After simple verification, it can send and receive

Attach code :
Here we receive data from the serial port , use NRF Send out , On the other end 2.4g After receiving, the serial port will print , All raw data , No agreement

TX


#include "NRF24L01.h"
String comdata = "";// String function 
#define TX_NAME "32899"



/*  Statement nRF24L01 */
nRF24L01 nrf24;

/*  Create send data cache , Initialize to 0 */
char buf[12] = {
    0};

void setup() {
    
	Serial.begin(9600);
    /*  initialization  nrf24 */         
	nrf24.init();
    /*  Configure data width  */
    nrf24.payload = 10;
    /*  Configure address  */
	nrf24.setTADDR(TX_NAME);
    /*  Write configuration  */
	nrf24.config();

}

void loop() {
    
    recv_uart_cmd();
    if (comdata.length() > 0)// If comdata There's data 
    {
    
      //Serial.println(comdata);// Print comdata data 
      strcpy(buf, comdata.c_str());
      Serial.println(buf);// Print comdata data 
      comdata = "";
      nrf24.send(buf);
      
    }
}

void recv_uart_cmd(void)
{
    
  if (Serial.available() > 0)// Read whether the serial port has data 
  {
    
    comdata = "";// Cache reset 
    while (Serial.available() > 0)// Whether the circular serial port has data 
    {
    
      comdata += char(Serial.read());// Stack data to comdata
      delay(2);// Delay waiting for response 
    }
    //if (comdata.length() > 0)// If comdata There's data 
    //{
    
    // Serial.println(comdata);// Print comdata data 
    //}
  }
}

RX :

#include "NRF24L01.h"


#define RX_NAME "32899"

/*  Statement nRF24L01 */
nRF24L01 nrf24;

/*  Establish receive data cache , Initialize to 0 */
char buf[15];
int  recv_cmd = 0;

void setup() {
    
  Serial.begin(9600);
  nrf24.init();
  nrf24.payload = 10;
  nrf24.setRADDR(RX_NAME);
  nrf24.config();
  /*  Set interrupt  */
  //attachInterrupt(0, readData, LOW) ;
}

void loop() {
    

  readData ();

}

void readData (void)
{
    
  if (nrf24.dataReady())
  {
    
    //Serial.println("Receiving the data!");
    nrf24.getData(buf);
    Serial.print("Data -> ");
    //Serial.print(buf);
    recv_cmd = atoi(buf);
    Serial.print(recv_cmd);
    Serial.println(" ");

  }
}

Finally, the most important thing is to have Driven library support
This folder is in Arduino Under the installation folder , The definition of pins is in
NRF24L01. in

/*  Interrupt state  */
#define RX_IRQ 11
#define TX_IRQ 22
#define MAX_RT_IRQ 33

/*  Default port number  */
#define CE 8 // CE_BIT: Digital Input Chip Enable Activates RX or TX mode
#define CSN 10 // CSN BIT: Digital Input SPI Chip Select
#define SCK 13 // SCK BIT: Digital Input SPI Clock
#define MOSI 11 // MOSI BIT: Digital Input SPI Slave Data Input
#define MISO 12 // MISO BIT: Digital Output SPI Slave Data Output, with tri-state option

 Insert picture description here
Examples It's inside TX RX Of Arduino Program , You can use it directly

Finally, the download link of the library is attached :

https://download.csdn.net/download/weixin_39369053/19619997

click here
over

原网站

版权声明
本文为[Drink more hot water-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140547485800.html