ESP32 Development Notes (3) Source code example 12_IR_Rev_RMT Use RMT to realize infrared remote control receiving and decoding (NEC encoding)

Development board purchase link

https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.50111deb2Ij1As&ft=t&id=626366733674

Introduction to the development board
development environment to build the windows
source code example:
    0_Hello Bug (ESP_LOGX and printf)     project template/print debugging output
    1_LED                                                     LED on and off control       
    2_LED_Task                                           use task mode to control LED
    3_LEDC_PWM                                       use LEDC to control LED to achieve breathing light effect
    4_ADC_LightR                                       use ADC to read photosensitive Resistance to achieve light sensing
    5_KEY_Short_Long                               button long press and short press to achieve
    6_TouchPad_Interrupt                           Capacitive touch interrupt to achieve
    7_WS2812_RMT                                   RGB_LED rainbow color change example
    8_DHT11_RMT                                     using RMT to achieve reading DHT11 temperature and humidity sensor
    9_SPI_SDCard                                     uses SPI bus to implement TF card file system example
    10_IIC_ADXL345                                 uses IIC bus to implement reading ADXL345 angular acceleration sensor
    11_IIC_AT24C02                                  uses IIC bus to implement small-capacity data storage test
    12_IR_Rev_RMT uses RMT to implement infrared remote control receiving and decoding (NEC encoding)
    13_IR_Send_RMT uses RMT to implement infrared data transmission (NEC)
    14_WIFI_Scan Example of nearby WIFI signal scanning
    15_WIFI_AP Example of     creating soft AP
    16_WIFI_AP_TCP_Server Implementing TCP server
    in soft AP mode
    17_WIFI_AP_TCP_Client Implementing TCP client in soft AP mode 18_WIFI_AP_UDP Implementing UDP communication in soft AP mode
    19_WIFI_STA Create STA station model
    20_WIFI_STA_TCP_Server Implement TCP server
    in station mode STA
    21_WIFI_STA_TCP_Client Implement TCP client in station mode STA
    22_WIFI_STA_UDP Implement UDP communication in station mode STA 23_LVGL_Test LVGL graphics library simple example

Introduction to Infrared

Remote control technology, also known as remote control technology, refers to the realization of remote control of the controlled target, and is widely used in the fields of industrial control, aerospace, and home appliances.

Infrared remote control is a wireless, non-contact control technology. It has significant advantages such as strong anti-interference ability, reliable information transmission, low power consumption, low cost, and easy realization. It is widely used by many electronic devices, especially household appliances, and is becoming more and more popular. It is widely used in computer and mobile phone systems.

        Infrared is also called infrared light wave. In the electromagnetic spectrum, the wavelength range of light wave is 0.01um~1000um. According to the different wavelengths, it can be divided into visible light and invisible light. Light waves with a wavelength of 0.38um~0.76um can be visible light, followed by seven colors of red, orange, yellow, green, blue, blue and purple. The light wave with a light wave of 0.01um~0.38um is ultraviolet light (line), and the light wave with a wavelength of 0.76um~1000um is infrared light (line). Infrared light is divided into 4 types of near infrared, mid-infrared, far infrared, and extreme infrared according to the wavelength range. Infrared remote control uses near-infrared light to transmit remote control commands, with a wavelength of 0.76um~1.5um. Near-infrared is used as the remote control light source because the peak wavelengths of the infrared emitting devices (infrared light-emitting tubes) and infrared receiving devices (photodiodes, triodes and photocells) are generally 0.8um~0.94um. In the near-infrared light band, The spectra of the two coincide exactly, can be well matched, and can obtain higher transmission efficiency and higher reliability

In the actual communication field, the signal sent out generally has a wider frequency spectrum and a large amount of energy is distributed in a relatively low frequency range, so it is called a baseband signal. This signal is not suitable for direct transmission in the channel. . In order to facilitate transmission, improve anti-interference ability and effectively utilize bandwidth, it is usually necessary to modulate the signal to a frequency range suitable for the channel and noise characteristics for transmission, which is called signal modulation. At the receiving end of the communication system, the received signal must be demodulated to recover the original baseband signal. You can understand the content of this part of the communication principle.

  The infrared communication in the infrared remote control that we usually use is usually modulated with a carrier of about 38K. Below I will introduce the principle to you. To understand, let's look at the principle of the sending part.

  Modulation: The process of using the signal to be transmitted to control the amplitude, phase, frequency and other parameters of a high-frequency signal, that is, using one signal to load another signal. For example, when our infrared remote control signal is to be sent, it is first modulated by 38K, as shown in the figure

The original signal is a data "0" bit or a data "1" bit we want to send, and the so-called 38K carrier is a square wave signal with a frequency of 38K, and the modulated signal is the final waveform we transmit. We use the original signal to control the 38K carrier. When the signal is data "0", the 38K carrier is sent out without reservation. When the signal is data "1", no carrier signal is sent. The signal waveform after modulation as shown in the figure above.

The following figure shows the waveform timing in the NEC code in the waveform

A complete segment of NEC coded waveform

There are many types of infrared receivers. The infrared receiver used on the development board is KMS183. The integrated infrared receiver can decode the carrier infrared signal into high and low level signals, which is convenient for the MCU to parse infrared commands, as shown in the figure below:

Introduction to RMT

RMT (Remote Control) module driver can be used to send and receive infrared remote control signals. Due to the flexibility of the RMT module, the driver can also be used to generate or receive many other types of signals.

The signal consists of a series of pulses, which are generated by the RMT transmitter based on a list of values. These values ​​define the pulse duration and binary level, see below. The transmitter can also provide a carrier wave and modulate it with the provided pulse.

Send modulation diagram:

In the receiver, a series of pulses is decoded into a list of values ​​containing pulse duration and binary level. Filters can be applied to remove high frequency noise from the input signal.

Receiving modulation diagram:

1. Hardware design/principle

Check the schematic diagram of the development board, the signal pin of the KMS183 integrated infrared receiver is connected to the GPIO35 pin of the main control, and the infrared emission pin is connected to the GPIO7 pin of the main control. You can proceed according to the introduction of RMT and infrared above. The code is written.

2. Program design

First quote the necessary header files

// IR_Rre Example

#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "IR_Rev.h"
#include <esp_log.h>
#include "driver/rmt.h"

By default, the development board decodes NEC infrared encoding, which is also the most common infrared encoding. It can be tested with the remote control of other small appliances such as TVs, set-top boxes, etc., if you need to change the encoding, you only need to change the encoding timing in the code. One click

NEC infrared decoding timing

#define NEC_BITS          32
#define NEC_HDR_MARK    9000
#define NEC_HDR_SPACE   4500
#define NEC_BIT_MARK     560
#define NEC_ONE_SPACE   1690
#define NEC_ZERO_SPACE   560
#define NEC_RPT_SPACE   2250

Pin definition and main function

const static char *TAG = "IR_Rre Demo";


#define RECV_PIN		35	// 一体化红外接收头GPIO
uint8_t command = 0;		// 接收到的ENC红外指令
void app_main()
{
	ESP_LOGI(TAG, "APP Start......");

	IRrecvInit(RECV_PIN, 3);
	while(1){
		command = IRrecvReadIR();
		if (command != 0){
			printf("IR Command is 0x%02X\n", command);
		}
	}
}

Infrared decoding RMT initialization

void IRrecvInit(uint8_t pin, uint8_t port)
{
	IRrecv_Pin = pin;
	IRrecv_Chanel = port;
	rmt_config_t config;
	config.rmt_mode = RMT_MODE_RX;
	config.clk_div = CLK_DIV;
	config.channel = (rmt_channel_t)IRrecv_Chanel;
	config.gpio_num = (gpio_num_t)IRrecv_Pin;
	config.mem_block_num = 2;
	config.rx_config.filter_en = 1;
	config.rx_config.filter_ticks_thresh = 100;
	config.rx_config.idle_threshold = TICK_10_US * 100 * 20;
	ESP_ERROR_CHECK(rmt_config(&config));
	ESP_ERROR_CHECK(rmt_driver_install(config.channel, 5000, 0));
	rmt_get_ringbuf_handle(config.channel, &ringBuf);
	rmt_rx_start(config.channel, 1);
}

Receive infrared code

uint8_t IRrecvReadIR(void)
{
	size_t itemSize;
	uint8_t command = 0;
	rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive((RingbufHandle_t)ringBuf, (size_t *)&itemSize, (TickType_t)portMAX_DELAY);
	int numItems = itemSize / sizeof(rmt_item32_t);
	int i;
	rmt_item32_t *p = item;
	for (i=0; i<numItems; i++) {
		p++;
	}
	command = IRrecvDecode(item, numItems);
	vRingbufferReturnItem(ringBuf, (void*) item);
	return command;
}

Decode the received level array into infrared encoding function


uint8_t IRrecvDecode(rmt_item32_t *data, int numItems)
{
	printf("IRrecvDecode->Levels Count:%d   Start Decode.....\n", numItems*2);
	// 检查协议头,9ms/4.5ms,检查误差300us
	if(!IRrecvIsInRange(data[0], NEC_HDR_MARK, NEC_HDR_SPACE, 300)){
		uint32_t lowValue = data[0].duration0 * 10 / TICK_10_US;
		uint32_t highValue = data[0].duration1 * 10 / TICK_10_US;
		printf("IRrecvIsInRange->Error   lowValue %d	highValue %d\n", lowValue,highValue);
		return 0;
	}
	int i;
	uint8_t address = 0, notAddress = 0, command = 0, notCommand = 0;
	int accumCounter = 0;
	uint8_t accumValue = 0;
	for(i=1; i<numItems; i++){// 第1组为协议头,从第2组开始
		if(IRrecvIS0(data[i])){// 检查是否为0
			accumValue = accumValue >> 1;
		}else if(IRrecvIS1(data[i])){// 检查是否为1
			accumValue = (accumValue >> 1) | 0x80;
		}
		if(accumCounter == 7){// 检查了8Bit
			accumCounter = 0;
			if(i==8){
				address = accumValue;		// 第1字节为地址
			}else if (i==16){
				notAddress = accumValue;	// 第2字节为地址取反
			}else if (i==24){
				command = accumValue;		// 第3字节是命令
			}else if (i==32){
				notCommand = accumValue;	// 第4字节是命令取反
			}
			accumValue = 0;
		}else{
			accumCounter++;
		}
	}
	// 检查取反数据是否正确
	if(address != (notAddress ^ 0xff)){
		printf("IRrecvDecode->Address Overturn Check Error   address %02X	notAddress %02X\n", address,notAddress);
		return 0;
	}
	if(command != (notCommand ^ 0xff)){
		printf("IRrecvDecode->Command Overturn Check Error   command %02X	notCommand %02X\n", command,notCommand);
		return 0;
	}
	return command;
}

The received timing is biased, so make a judgment

//高低电平结构体,低电平检测时长,高电平检测时长,公差
bool IRrecvIsInRange(rmt_item32_t item, int lowDuration, int highDuration, int tolerance)
{
	uint32_t lowValue = item.duration0 * 10 / TICK_10_US;
	uint32_t highValue = item.duration1 * 10 / TICK_10_US;
	if(lowValue<(lowDuration-tolerance)||lowValue>(lowDuration+tolerance)||(highValue!=0&&(highValue<(highDuration-tolerance)||highValue>(highDuration+tolerance))))
	{
		return false;
	}
	return true;
}

Three, download test

Open ESP-IDF Command Prompt

cd command to enter this project directory

cd F:\ESP32_DevBoard_File\12_IR_Rev_RMT

View the serial port number of the development board in the computer device manager

Execute idf.py -p COM9 flash monitor to download from the serial port 9 and run it. Open the port and display the debugging information of the device. Ctrl+c to exit the operation. You can use your home remote control to press several buttons against the infrared receiver on the development board and observe the serial port printing .

Guess you like

Origin blog.csdn.net/cnicfhnui/article/details/108508379