当前位置:网站首页>STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)

STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)

2022-07-07 11:22:00 Hua Weiyun

One 、 Introduction to the environment

MCU: STM32F103ZET6

Programming software environment : keil5

Infrared transmission protocol : NEC agreement —38KHZ carrier :.NEC Protocol is a common infrared remote control protocol .

Decoding ideas : External interrupt + Timer mode

Code style. : Modular programming , Register direct operation mode

Two 、NEC Introduction to protocol and decoding ideas

2.1 Relevant hardware used

chart 1: This is a NEC Protocol infrared remote control : If your mobile phone doesn't have the function of infrared remote control , You can buy a small remote control on Taobao to learn and test , The cost is not high , This remote control can also be made by yourself , Can decode, of course, can also encode and send , Just need an infrared light emitting tube .
image.png

chart 2: This is the infrared receiver module . If your own development board does not have this receiver , Then buy a separate receiver module , Use DuPont cable to connect to the development board IO It can be used to test learning , Wiring is very convenient .
image.png
image.png

chart 3: This is an infrared emitting tube , If you want to be the transmitter of the remote control , Make your own remote control , Then you can buy this module directly .
image.png

2.2 Introduction to infrared protocol

In the spectrum, the wavelength is from 760nm to 400um The electromagnetic wave is called infrared , It's an invisible light . The example of infrared communication should be familiar to all of us , At present, almost all commonly used household appliances can be remotely controlled by infrared remote control , Like the TV set 、 Air conditioner 、 Projector, etc , You can see the shadow of infrared remote control . This technology is widely used , The corresponding application devices are very cheap , Therefore, infrared remote control is an ideal way for our daily equipment control .

Infrared communication principle : Infrared light is emitted in the form of specific frequency pulses , After the receiving end receives the signal , Decode according to the agreed protocol , Complete the data transfer , In consumer electronics , Pulse frequency is commonly used 30KHz To 60KHz This frequency band ,NEC The frequency of the agreement is 38KHZ. This emission at a specific frequency can actually be understood as lighting , Don't be baffled by complex words , Is to control the flashing frequency of the light ( Light out ), It means the same as just learning MCU to complete flash , It's just a different type of lamp , It's all lights . The principle of the receiver : The chip at the receiving end is sensitive to this infrared light , It can output high and low levels according to whether there is light , If the flicker frequency at the transmitter is regular , The high level and low level output by the receiving end also correspond regularly , In this way, as long as the sender and receiver agree , Then you can do data transmission .

Infrared transmission protocol is the lowest cost of all wireless transmission protocols , The most convenient transmission protocol , But there are drawbacks , The distance is not long enough , Not fast enough ; Of course , The environment of each transport protocol application is different , Different positioning , There is no comparison between good and bad , It depends on your actual situation and choose the appropriate communication mode .

2.3 NEC Protocol is introduced

NEC Protocol is one of many infrared protocols ( The protocol here is that their data frame format definitions are different , The principle of data transmission is the same ), The external energy remote control we bought 、 Taobao bought mini The remote control 、 The TV 、 Projectors are almost NEC agreement . Like Gree air conditioner 、 Midea air conditioners use other protocol formats , No NEC agreement , But just learn a protocol parsing method , Understand the principle of infrared transmission , Other remote control protocols can be solved .

The picture below is NEC The complete format of data transmitted once by the protocol :
image.png

NEC A complete transmission of the protocol includes : Pilot code 、8 Bit user code 、8 Bit user inverse code 、8 Bit data code 、8 Bit data inverse code .

( Be careful : The following explanations are explained from the perspective of the infrared receiving end , It's the angle of the decoder )

Pilot code : from 9ms High level of +4.5ms Low level composition of .

4 Bytes of data : User code + User inverse code + Data code + Data inversion . The inverse code here can be used to verify whether the data is transmitted correctly , Have you lost your bag .

a key : NEC When the protocol transmits data bits ,0 and 1 The distinction depends on the high 、 The duration of low level to distinguish — This is the key to decoding .

Standard interval :0.56ms

Received data bits 0: 0.56ms

Received bit 1: 1.68ms

therefore , The complete time of receiving a data bit is expressed as follows :

Received data bits 0: 0.56m Low level + 0.56ms High level of

Received data bits 1: 0.56ms Low level +1.68ms High level of

Principle of output level of infrared receiver module : The infrared receiving head senses the presence of infrared light and outputs a low level , Output high level without sensing infrared light .

This is to use logic analysis to collect the signal output by the infrared receiving head :
image.png

This is on the infrared remote control LED Lamp output level timing diagram , Just opposite to the receiving end :
image.png

When the MCU writes the decoding program , A common way is to use external interrupts + Timer mode for analysis , The interrupt can be set to trigger at low level , Because the receiving head does not sense infrared light, it outputs high level by default , If you receive NEC Pilot code , It will output low level , Enter the interrupt service function , Complete decoding , During decoding, start the timer to record the high level of each segment 、 The duration of the low level , according to NEC Agreement to judge , Complete final decoding .

STM32 Decoding can be done using input capture , In fact, input capture is an external interrupt + The combination of timers , nothing but STM32 The interior is encapsulated with a layer .

The decoding program in the external interrupt server is as follows ( The idea is the same on other microcontrollers ):

/* The functionality :  External interrupt line 9_5 Service functions */void EXTI9_5_IRQHandler(void){    u32 time;    u8 i,j,data=0;      // Clear the interrupt line 9 Interrupt request on 		EXTI->PR|=1<<9;      time=Infrared_GetTime_L();       // Get the low level time     if(time<7000||time>10000)return; // Standard time : 9000us    time=Infrared_GetTime_H();       // Get high level time     if(time<3000||time>5500)return;  // Standard time 4500us        // Officially decode NEC agreement     for(i=0;i<4;i++)    {        for(j=0;j<8;j++)        {             time=Infrared_GetTime_L();       // Get the low level time              if(time<400||time>700)return;    // Standard time : 560us                          time=Infrared_GetTime_H();       // Get high level time              if(time>1400&&time<1800)         // data 1 1680us             {                data>>=1;                data|=0x80;             }             else if(time>400&&time<700)   // data 0 560us             {                data>>=1;             }             else return;        }        InfraredRecvData[i]=data; // Store the value of successful decoding       }          // Decoding successful       InfraredRecvState=1;}

3、 ... and 、 Core complete code

image.png
image.png

The decoding idea of this program is : Connect the output pin of the infrared receiving module to STM32 Of PB9 On , To configure STM32 Of PB9 External interrupt mode , Falling edge level trigger ; If the infrared signal is received, it will enter the interrupt service function to decode , If it is found that the data does not meet the requirements during decoding, the decoding will be terminated , If all the data meets the requirements, it will be received according to the protocol , Until decoding is complete , Set flag bit , stay main Function to print and decode the data .

The code is modular programming , It's also easy to read .

3.1 Infrared decoding .c

#include "nec_Infrared.h"u8 InfraredRecvData[4]; // Store the data received by infrared decoding u8 InfraredRecvState=0; //0 Indicates that no data has been received ,1 Indicates received data  /* The functionality :  Infrared decoding initialization ( receive )*/void Infrared_RecvInit(void){    Infrared_Time6_Init(); // Timer initialization         /*1.  To configure GPIO mouth */    RCC->APB2ENR|=1<<3; //PB    GPIOB->CRH&=0xFFFFFF0F;    GPIOB->CRH|=0x00000080;    GPIOB->ODR|=1<<9;      /*2.  Configure external interrupts */    EXTI->IMR|=1<<9; // External interrupt line 9, Interrupt request function of open interrupt line     EXTI->FTSR|=1<<9; // trunk 9_ Falling edge       RCC->APB2ENR|=1<<0; // Turn on AFIO The clock     AFIO->EXTICR[2]&=~(0xF<<1*4);    AFIO->EXTICR[2]|=0x1<<1*4;    STM32_NVIC_SetPriority(EXTI9_5_IRQn,1,1);}/* The functionality :  Initialize the timer , For infrared decoding */void Infrared_Time6_Init(void){    RCC->APB1ENR|=1<<4;		RCC->APB1RSTR|=1<<4;		RCC->APB1RSTR&=~(1<<4);    TIM6->PSC=72-1; // Preassigned frequency counter     TIM6->ARR=65535;   // Reload register     TIM6->CR1|=1<<7; // Enable caching 	  //TIMx->CR1|=1<<0; // Turn on timer }/* The functionality :  Measure the duration of high level */u32 Infrared_GetTime_H(void){    TIM6->CNT=0;    TIM6->CR1|=1<<0;    // Turn on timer     while(NEC_IR){}     // Wait for the high level to end     TIM6->CR1&=~(1<<0); // off timer     return TIM6->CNT;}/* The functionality :  Measure the duration of low level */u32 Infrared_GetTime_L(void){    TIM6->CNT=0;    TIM6->CR1|=1<<0;    // Turn on timer     while(!NEC_IR){}     // Wait for the low level to end     TIM6->CR1&=~(1<<0); // off timer     return TIM6->CNT;}/* The functionality :  External interrupt line 9_5 Service functions */void EXTI9_5_IRQHandler(void){    u32 time;    u8 i,j,data=0;      // Clear the interrupt line 9 Interrupt request on 		EXTI->PR|=1<<9;      time=Infrared_GetTime_L();       // Get the low level time     if(time<7000||time>10000)return; // Standard time : 9000us    time=Infrared_GetTime_H();       // Get high level time     if(time<3000||time>5500)return;  // Standard time 4500us        // Officially decode NEC agreement     for(i=0;i<4;i++)    {        for(j=0;j<8;j++)        {             time=Infrared_GetTime_L();       // Get the low level time              if(time<400||time>700)return;    // Standard time : 560us                          time=Infrared_GetTime_H();       // Get high level time              if(time>1400&&time<1800)         // data 1 1680us             {                data>>=1;                data|=0x80;             }             else if(time>400&&time<700)   // data 0 560us             {                data>>=1;             }             else return;        }        InfraredRecvData[i]=data; // Store the value of successful decoding       }          // Decoding successful       InfraredRecvState=1;}

3.2 The main function .c

#include "stm32f10x.h"#include "led.h"#include "delay.h"#include "key.h"#include "usart.h"#include "at24c02.h"#include "W25Q64.h"#include "spi.h"#include "nec_Infrared.h"int main(){	LED_Init();	BEEP_Init();	KeyInit();  USARTx_Init(USART1,72,115200);  IIC_Init();    W25Q64_Init();    printf(" chip ID Number :0x%X\n",W25Q64_ReadID());    Infrared_RecvInit();  	while(1)	{		if(InfraredRecvState)    {        InfraredRecvState=0;        printf(" User code :%d, Key code :%d\n",InfraredRecvData[0],InfraredRecvData[2]);        printf("user Inverse code :%d,key Inverse code :%d\n",(~InfraredRecvData[1])&0xFF,(~InfraredRecvData[3])&0xFF);        BEEP=!BEEP;        LED0=!LED0;    }	}}

Four 、 Expand and improve

If the top NEC The decoding idea has been seen , The program can already be written by itself , You can try to use STM32 Input capture for + Write a version of decoding code in timer mode , Can be more familiar with NEC agreement 、 You can also learn STM32 Timer capture usage ; You can also do some small things to exercise , such as : Infrared remote control car 、 The music player supports infrared remote control to cut songs , Switch of motor 、 Light switch, etc .

After decoding the protocol , Our next step is to complete the custom NEC Protocol infrared production , use STM32 Simulate a universal infrared remote control .

In the spectrum, the wavelength is from 760nm to 400um The electromagnetic wave is called infrared , It's an invisible light . At present, almost all video and audio devices can be remotely controlled by infrared remote control , Like the TV set 、 Air conditioner 、 DVD player, etc , You can see the shadow of infrared remote control . This technology is widely used , The corresponding application devices are very cheap , Therefore, infrared remote control is an ideal way for our daily equipment control .

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070921201961.html