Realize the automatic frequency pairing function of NRF24L01

We all know that this module has only 128 channels, which means that there can only be up to 128 devices in the same vicinity, but it is difficult for such a phenomenon to exist at the same time in this environment.
If you want to make many such devices, each device needs to work On different frequencies, then each device needs to have a different code (because the channel needs to be modified). It's scary to think about.
It's not something a programmer does either.
I searched the Internet for a few days and couldn't find the relevant technology. It may be because of commercial reasons, so I can only figure out the realization ideas myself.
In the past 2 days, I have made an idea of ​​automatic frequency. Because of too much code, many files were written by previous blogs and will not be uploaded. The key is to understand the mind.


Ideas:
Receiving end:
The receiver presses the button to enter the frequency mode (running on the specified channel)
Randomly get the number of channels
Send channel number to transmitter
Wait for transmitter ACK
switch to this channel (random channel)
communicate
save random channel to internal flash


The transmitting end:
Press the button on the transmitter to enter the frequency mode
Receive the channel sent by the receiver and verify it
Send and receive ACK
switch to this channel (random channel)
communicate
save random channel to internal flash


The whole idea is like an appeal.


This program needs to use NRF basic communication function, random number function, flash function. These functions have been mentioned in the previous blog.


Development environment: keil 4
Development language: C/C++
Development platform: STM32


Upload the code below

dui_pin.h

/*
Date: 18-1-18
Author: HES

Function: Realize the automatic frequency matching function of NRF24l01

*/


#ifndef _DUI_PIN_H
#define _DUI_PIN_H
#include "nrf_info.h"
#include "sys.h"
#include "24l01.h"

#define DUI_PIN_LOG 1 //Serial log switch

#define DUI_PIN_CH 00 //Use the channel for pairing

#define NULL 0

#define SEND_ACK 5 // ACK sent
#define RECV_ACK 5 //Received ACK

#define STEP1 1 //Various frequency pairing states
#define	 STEP2 2
#define	 STEP3 3
#define	 STEP4 4

#define DUI_PIN_ID 99 //pair ID

#define DUI_PIN_FLAG 9 //pairing flag

class DUI_PIN:public NRF_INFO //Inherit NRF function
{
	public:
				u8 CH; // pair frequency channel
				u8 state; // pair frequency state
				u8 flash_buf[2]; //Used to save channel data to flash
				void RX_start_dui_pin(); //RX pair frequency
				void TX_start_dui_pin();		//TX对频
				void save_ch_flash(); //Save the channel to flash
				void read_ch_flash(); //To the channel in the flash
};

#endif
dui_pin.c

/*
Date: 18-1-18
Author: HES

Function: Realize the automatic frequency matching function of NRF24l01

*/

#include "dui_pin.h"
#include "serial.h"
#include "delay.h"
#include "time.h"
#include "timer.h"
#include "stdlib.h"
#include "flash.h"


/*
Date: 18-1-18
Author: HES

Function: realize the automatic frequency matching function of the receiving end

*/
void DUI_PIN::RX_start_dui_pin()
{
	
					u8 num=0;
			#if DUI_PIN_LOG
					printf("<<---Enter frequency pairing mode--->>\r\n");
			#endif
					NRF24L01_TX_Mode(1,DUI_PIN_CH);
					delay_ms(10);
					
					do
					{
						srand(Timer_Count_Value); //The random seed Timer_Count_Value is the count value of the timer,
						
						num = rand() % 128; //Generate random integers from 0 to 128,
				#if DUI_PIN_LOG
						printf("<<---Generate channel:%d--->>\r\n",num);
				#endif		
						state=STEP1;
					}while(num==DUI_PIN_CH); //The channel is equal to the paired channel, get it again
					
					while(1)
					{
						state=STEP2;
						NRF24L01_TX_Mode(1,DUI_PIN_CH);
						delay_ms(20);
						send_mes(num,DUI_PIN_FLAG,NULL,NULL,NULL); //Send random channel and pairing ID
						delay_ms(10);
					
						
#if 1				
						NRF24L01_RX_Mode(1,DUI_PIN_CH);
						delay_ms(20);
						if(recv_mes())
						{
							#if DUI_PIN_LOG
							printf("<<---Data received--->>\r\n");
							#endif
							//TX returns ACK
							if(ID==RECV_ACK)
							{		
									CH=num;
									break;
							}
						}
#endif						
							#if DUI_PIN_LOG
						printf("<<---During frequency pairing--->>\r\n");
				#endif		
						
					}
				#if DUI_PIN_LOG
						printf("<<---TX receiving channel data completed:%d--->>\r\n",CH);
				#endif
					
					while(1)
					{
						state=STEP3;
						NRF24L01_TX_Mode(1,CH);
						delay_ms(10);
						send_mes(SEND_ACK,NULL,NULL,NULL,NULL);
						delay_ms(10);
						NRF24L01_RX_Mode(1,CH);
						delay_ms(10);
						if(recv_mes())
						{
							//TX returns ACK
							if(ID==RECV_ACK)	
							break;
						}
						#if DUI_PIN_LOG
						printf("<<---Frequency verification:%d--->>\r\n",CH);
						#endif
						
					}
					
				#if DUI_PIN_LOG
						printf("<<--- Frequency matching completed:%d--->>\r\n",CH);
				#endif
					
					save_ch_flash();
					state=STEP4;
}

/*
Date: 18-1-18
Author: HES

Function: Realize the automatic frequency matching function of the sender

*/

void DUI_PIN::TX_start_dui_pin()
{
					u8 n=0;
					u8 num=0;
			#if DUI_PIN_LOG
					printf("<<---Enter frequency pairing mode--->>\r\n");
			#endif
					NRF24L01_RX_Mode(1,DUI_PIN_CH);
					delay_ms(10);
					
				
						state=STEP1;
					
					
					while(1)
					{
						state=STEP2;
						NRF24L01_RX_Mode(1,DUI_PIN_CH);
						delay_ms(20);
						if(recv_mes())
						{
							
							if(TX_voltage!=DUI_PIN_FLAG)continue; //Continue to wait if it is not a frequency identification
							
							#if DUI_PIN_LOG
							printf("<<---Received frequency data--->>\r\n");
							#endif	
							
							//TX returns ACK
							
									CH=ID;
									NRF24L01_TX_Mode(1,DUI_PIN_CH);
									delay_ms(20);
									n=10;
									while(n--!=1) //Send 10 ACKs
									{
										send_mes(RECV_ACK,NULL,NULL,NULL,NULL);		
										delay_ms(100);
									}
									break;
							}
							#if DUI_PIN_LOG
							printf("<<---During frequency pairing--->>\r\n");
							#endif	
						}
						
					
				#if DUI_PIN_LOG
						printf("<<---TX receiving channel data completed:%d--->>\r\n",CH);
				#endif
					
					while(1)
					{
						state=STEP3;
						NRF24L01_RX_Mode(1,CH);
						delay_ms(20);
						if(recv_mes())
						{
							//TX returns ACK
							if(ID==RECV_ACK)	
							break;
						}
					#if DUI_PIN_LOG
						printf("<<---Frequency verification:%d--->>\r\n",CH);
					#endif
					}
					
									NRF24L01_TX_Mode(1,CH);
									delay_ms(20);
									n=10;
									while(n--!=1)
									{
										send_mes(RECV_ACK,NULL,NULL,NULL,NULL);
										delay_ms(20);
									}
					
				#if DUI_PIN_LOG
						printf("<<--- Frequency matching completed:%d--->>\r\n",CH);
				#endif
					
				save_ch_flash();
									
				state=STEP4;					
					
}
/*
Date: 18-1-18
Author: HES

Function: Realize saving channel to internal flash

*/
void DUI_PIN::save_ch_flash()
{
	flash_buf[0]=CH;
	flash_buf[1]=1;
	
	FLASH_WriteByte(ADDR,flash_buf,2);
	
}
/*
Date: 18-1-18
Author: HES

Function: Realize reading channel in internal flash

*/
void DUI_PIN::read_ch_flash()
{
	
	FLASH_ReadByte(ADDR,flash_buf,2);
	
	CH=flash_buf[0];
	
}
Main function test:

	Receiver main function call test:

	DUI_PIN dui_pin //Define function object

	main()
	{
		/*
			Be sure to initialize the NRF basic communication function, random number function, and flash function before. The three functions

		 */
		dui_pin.RX_start_dui_pin();
		printf("RX frequency complete\r\n");
	}

	Transmitter main function call test:

	DUI_PIN dui_pin //Define function object

	main()
	{
		/*
			Be sure to initialize the NRF basic communication function, random number function, and flash function before. The three functions

		 */
		dui_pin.TX_start_dui_pin();
		printf("TX frequency complete\r\n");
	}
Receiver effect:

<<---On frequency--->>
<<---On frequency--->>
<<---On frequency--->>
<<---Data received--->>
<<---TX receiving channel data completed: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<---Frequency verification: 95--->>
<<--- Frequency pairing completed: 95--->>





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586042&siteId=291194637