Bidirectional communication with NRF24L01[Copy link]
1. Bidirectional communication of NRF24l01 The most basic use of NRF24l01zu is: one NRF24l01 module is used as the data transmission party, and the other NRF24l01 is used as the data receiving party. But sometimes we need two NRF24l01 wireless transmission modules to perform real-time bidirectional communication. What should we do at this time? 1. First, we can set both NRF24L01 wireless transmission modules to Enhanced ShockBurstTM transceiver mode. One of them is initially set as the transmitter, which we can call A, and the other NRF24l01 wireless transmission module is initially set as the receiving end, which we call B. 2. First, end A starts to send data to end B. If end A sends data successfully and end A receives the response signal sent by end B, then end A completes the first transmission and end B completes the first reception. Then let end A be set to receiving mode and end B be set to sending mode. If end B sends data to end A successfully and receives the response signal, it means that end B completes the transmission and end A completes the reception. Then let end A be set as the transmitting end and end B as the receiving end. This cycle will allow two NRF wireless transmission modules to transmit information to each other. The principle is very simple, just like two people communicating using a walkie-talkie. When you talk, I listen carefully, and when I talk, you listen carefully. This way, the communication is completed. 3 But sometimes, due to some reasons such as some interference, a communication fails, so that both ends A and B are set to the same mode at the same time. At this time, it is like two people talking at the same time when using a walkie-talkie, and no one listens to each other, or two people listen to each other at the same time and remain silent, and no one speaks. This will cause the program to get stuck here and not run. The solution to this BUG is also very simple: set the A end to automatically jump to another mode when the data transmission fails or the data reception fails a certain number of times. 4. The following is a simple code. The data to be sent by end A can be updated by using interrupts or buttons in an infinite loop. #define MAX 2 //The maximum number of retries after receiving or sending failures u8 Tx_Cnt=0; //Send count u8 Rx_Cnt=0; //Receive count u8 Mode=1; //Mode is 1 for send mode, 0 for receive mode u8 tmp_buf_Tx[32],tmp_buf_Rx[32]; //Send and receive buffer array NRF24L01_TX_Mode(); while(1) //NRF24L01 works in an infinite loop { if (Mode==1) //In send mode { (NRF24L01_TxPacket(tmp_buf_Tx)==TX_OK) { Tx_Cnt=0; Mode=0; NRF24L01_RX_Mode(); //Once the send is successful, it becomes receive mode; } Tx_Cnt++; if (Tx_Cnt==Max) //If the continuous transmission fails for Max times, switch to receiving mode{ Tx_Cnt=0; Mode=0; NRF24L01_RX_Mode(); } } else //In receiving mode{ if (NRF24L01_RxPacket(tmp_buf_Rx)==0)//Once the reception is successful, it will change to sending mode; { Rx_Cnt=0; Mode=1; NRF24L01_TX_Mode(); } Rx_Cnt++; if (Rx_Cnt==Max)//If the continuous reception fails for Max times, switch to sending mode{ Rx_Cnt=0; Mode=1; NRF24L01_TX_Mode(); } } delay_ms(10); //Large delay means slow transmission and reception, small delay means fast transmission and reception, too small delay may affect communication reliability; } Below is the code for end B u8 Mode=0; //The mode flag is set to 0 Receiver NRF24L01_RX_Mode(); //Set the mode to receive mode while(1) //NRF24L01 works in an infinite loop { if (Mode==1) { /*Data to be sent can be updated here*/ if (NRF24L01_TxPacket(tmp_buf_Tx)==TX_OK)//Data transmission is successful { Mode=0;//Change to receive mode NRF24L01_RX_Mode(); //Once the transmission is successful, it will change to receive mode; } } else { if (NRF24L01_RxPacket(tmp_buf_Rx)==0)//Once the reception is successful, it will change to transmit mode; { Mode=1; NRF24L01_TX_Mode(); } } delay_ms(2); //Large delay means slow transmission and reception, small delay means fast transmission and reception, too small delay may affect communication reliability; }