STM32F407 implements NEC protocol infrared decoding

【1】NEC infrared protocol introduction

NEC infrared protocol is a commonly used infrared communication protocol, which is widely used in consumer electronics products, such as TV, DVD player, air conditioner remote control, etc. This protocol defines the physical layer and data link layer specifications of infrared communication to achieve reliable infrared data transmission.

The following is a detailed introduction of the NEC infrared protocol:

(1) Carrier frequency: The carrier frequency used by the NEC infrared protocol is 38kHz. The sender modulates the infrared light signal at a frequency of 38kHz, and the receiver receives the data through demodulation.

(2) Data encoding: NEC infrared protocol uses pulse width encoding (Pulse Width Encoding) to represent binary data. Each data bit consists of a series of pulses, and logic 0 and logic 1 are represented by different pulse widths.

  • Logic 0: After the sender transmits a 530μs carrier, the delay time is 530μs.
  • Logic 1: After the sender transmits a 530μs carrier, the delay time is 1.69ms.

(3) Frame structure: A complete frame of the NEC infrared protocol consists of multiple data bits, including boot code, custom code, data code and inverted code. The frame structure is as follows:

  • Leader Code: It consists of a series of carrier pulses and is used to synchronize the clocks of the receiver and the sender.
  • Custom Code: 8-bit data, used to identify the device type.
  • Data Code (Data Code): 8-bit data, used to transmit specific commands or data.
  • Inverted Data Code: The inverse code of the data code, used to verify the correctness of the data.

(4) Repeat code: In order to improve communication reliability, the NEC infrared protocol also defines a repeat code. When the key is not released, the sender will periodically send a repetition code to ensure that the receiver correctly receives the continuous key data.

(5) The working principle of the receiving end: the receiving end uses the infrared receiving module to receive the infrared signal and convert it into digital data through demodulation. After receiving the boot code, the receiving end starts to analyze the custom code and data code, and perform verification.

The advantage of the NEC infrared protocol is that it is simple, widely used, and widely supported in consumer electronics. It provides a reliable infrared data transmission method, which is suitable for remote control and communication needs.

【2】Hardware connection

38KHZ infrared receiving head hardware connection diagram:

img

img

【3】Source code

Main.c sample code

#include "stm32f4xx.h" // Device header
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include "sys.h"
#include "exti.h"
#include "timer.h"
#include "pwm.h"
#include "ds18b20.h"
#include "infrared.h"
extern u8 InfraredRxBuff[5];
int main(void)
{
    
    
		LED_Init();
		KEY_Init();
		USART1_Init(84,115200);
		KEY_EXTI_Init();
		DS18B20_Init();
		InfraredRxInit();             //红外线解码初始化
	  while(1)
		{
    
    
			  if(InfraredRxBuff[4])
				 {
    
    
						 InfraredRxBuff[4]=0; //清除接收成功标志
						 printf("USER=0x%x\r\n",InfraredRxBuff[0]);
						 printf("KEY=0x%x\r\n",InfraredRxBuff[2]);
						 LED0=0;
						 DelayMs(100);
						 LED0=1;
				 }
		}
}


Infread.c file example

#include "infrared.h"
/*
函数功能:红外线解码初始化
硬件连接:PA8
定 时 器:使用TIM2
*/
void InfraredRxInit(void)
{
    
    
		/*1. 开时钟*/
	  RCC->AHB1ENR|=1<<0;//使能PORTA时钟
	
	  /*2. 配置GPIO口模式*/
	  GPIOA->MODER&=~(0x3<<8*2); //清除模式
		GPIOA->MODER|=0x0<<8*2;    //配置输入模式
		
	  /*3. 开启SYSCFG时钟 */
		RCC->APB2ENR|=1<<14;
		
	  /*4. 开放来自线x上的中断请求*/
	  EXTI->IMR|=1<<8; //中断线8
	
	  /*3. 配置中断线触发边沿*/
	  EXTI->FTSR|=1<<8; //下降沿
	  
	  /*4. 配置产生中断的对应IO口*/
	  SYSCFG->EXTICR[2]&=~(0xf<<0*4);
	  SYSCFG->EXTICR[2]|=0x0<<0*4;
	  
	  /*5. 配置中断优先级*/
		SetNVICPriorityGrouping(EXTI9_5_IRQn,1,1);
	
		/*6. 初始化定时器*/
	  RCC->APB1ENR|=1<<0;     //开启定时器2的时钟
	  RCC->APB1RSTR|=1<<0;    //开启复位时钟  
	  RCC->APB1RSTR&=~(1<<0); //关闭  
	  TIM2->PSC=84;     			//预分频
	  TIM2->ARR=65535;        //重装载寄存器
	  TIM2->CR1&=~(1<<0);     //开启计数器
}

/*
函数功能:获取高电平持续的时间
返 回 值:高电平持续的时间
*/
u32 GetInfraredRxH(void)
{
    
    
	 TIM2->CR1|=1<<0;
	 TIM2->CNT=0;
	 while(INFRARED_RX){
    
    }
	 TIM2->CR1&=~(1<<0);
	 return TIM2->CNT;
}

/*
函数功能:获取低电平持续的时间
返 回 值:低电平持续的时间
*/
u32 GetInfraredRxL(void)
{
    
    
	 TIM2->CR1|=1<<0;
	 TIM2->CNT=0;
	 while(!INFRARED_RX){
    
    }
	 TIM2->CR1&=~(1<<0);
	 return TIM2->CNT;
}


/*
函数功能:外部中断线0中断服务函数
NEC协议解码原理:
1. 先接收引导码:9ms低电平+4.5ms高电平
2. 引导码之后,是连续的32位数据。用户码+用户反码+按键码+按键反码
3. 数据‘0’ :560us低电平+560us高电平
4. 数据‘1’ :560us低电平+1680us高电平
*/
u8 InfraredRxBuff[5]={
    
    0}; //存放红外线接收的数据值,其中[4]表示标志位。=0失败,=1成功
void EXTI9_5_IRQHandler(void)
{
    
    
		u32 time,j,i;
		u8 data=0;
		EXTI->PR|=1<<0;  //清除中断标志位
		/*1. 判断引导码*/
		time=GetInfraredRxL(); //获取低电平的时间
		if(time<5000||time>11000)return;
		time=GetInfraredRxH();
		if(time<2500||time>5500)return;
	
	  /*2. 接收用户码和按键码*/
		for(i=0;i<4;i++)
	  {
    
    
				for(j=0;j<8;j++)
			  {
    
    
						time=GetInfraredRxL(); //获取低电平的时间
					  if(time<360||time>660)return;
						
						time=GetInfraredRxH(); //获取高电平的时间
					  //560us高电平  0  、 1680us高电平 1
						if(time>360&&time<660)
						{
    
    
							data>>=1;
						}
					  else if(time>1480&&time<1880)
						{
    
    
							 data>>=1;
							 data|=0x80; //1000 0000
						}
				}
				InfraredRxBuff[i]=data;
		}
		InfraredRxBuff[4]=1; //标志红外线解码成功
}


Infread.h file example

#ifndef _INFRARED_H
#define _INFRARED_H
#include "stm32f4xx.h"
#include "sys.h"
void InfraredRxInit(void);
u32 GetInfraredRxH(void);
u32 GetInfraredRxL(void);
#define INFRARED_RX PAin(8)  //红外线的接收口
#endif

Screenshot example:

img

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/131458658