当前位置:网站首页>51 MCU Peripherals: Infrared Communication
51 MCU Peripherals: Infrared Communication
2022-08-02 06:19:00 【Luxi Feixi】
Infrared Remote Control
Human-machine interface
Face-to-face operation: buttons, knobs/touch buttons, touch screen
Remote operation: infrared remote control, 433M-2.4G wireless communication, Bluetooth-WIFI-Zigbee-4G-LoRa and other wireless networksInfrared remote control is a device that uses infrared light to communicate. The modulated signal is sent out by an infrared LED, and is demodulated and output by a dedicated infrared receiver.
Communication mode: simplex, asynchronous;
Infrared LED wavelength: 940nm;
Communication protocol standard: NEC standard (commonly used)
Schematic diagram of the receiving end:
The integrated receiver head has built-in infrared carrier demodulation function, and the binary signal of communication comes out from the IRD pin.Therefore, the IO of the microcontroller can be directly connected to the IRD pin to read the communication information in the infrared signal.So the modulation and demodulation process of the 38KHz carrier is transparent to programming.
The infrared receiving head itself has an inversion, which means that when the sender does not send a signal, it usually receives 1, and when the sender sends a carrier, the IRD pin of the receiving head outputs 0, which is very important to us.Writing code has an impact, and it is just the opposite when looking at the protocol sequence diagram later.
Principle overview
Infrared communication consists of two parts: a transmitter and a receiver. The transmitter encodes the data, modulates it into a series of pulse signals, and then sends the pulse signals, that is, infrared signals, through a transmitter circuit with an infrared transmitter.The receiving end completes the reception, amplification, detection and shaping of the pulse signal, and then demodulates the encoded signal, and decodes it to obtain the transmitted data.The details are shown in the following figure:
NEC Protocol
The representation of logic 1 and logic 0 of NEC protocol is shown in the following figure:
– Logic 1 is 2.25ms, pulse time is 560us.
– Logic 0 is 1.12ms, pulse time is 560us.Decode according to the pulse duration.The recommended carrier duty cycle is 1/3 to 1/4.NEC protocol format as shown below:
In the NEC protocol, the first is a high-level pulse of 9ms, followed by a low-level pulse of 4.5ms, followed by an 8-bit address code (sent from the low significant bit), and then the inverse of the 8-bit address codecode (mainly used to check whether there is an error).Then there is the 8bit command code (also sent from the low significant bit), and then the inverse of the 8bit command code.
The above is a sequence of command codes, but when you hold down the remote control button for a long time, in this case, the infrared remote control using the NEC protocol will transmit a repeating code with a cycle of 110ms.That is to say, every time the user presses the button of the remote control, after the remote control sends the command code once, it will not send the command code again, but send a repeated code.As shown below:
The repetition code consists of a high level of 9ms, a low level of 2.25ms and a high level of 560us, as shown in the following figure:
It should be noted that the 1838 infrared receiver is to improve the receiving sensitivity.The input is high, and the output is the opposite low.
Code implementation
#include "ir.h"// The global variable is defined in which C file is used in which C file// cannot be defined in a header file// If the same global variable is used in multiple C files, it should be defined in a main C file// Then the extern declaration in other C files can besbit IRIN = P3^2;unsigned char IrValue[5]; // 0-3 of IrValue is used to put the original data, 4 is used to put the key value that has been verified and confirmed to be correctunsigned char Time;void DelayMs(unsigned int x) //0.14ms error 0us{unsigned char i;while(x--){for (i = 0; i<13; i++){}}}void IrInit(void){IT0=1;//Falling edge triggerEX0=1;//Enable interrupt 0EA=1; //Open the total interruptIRIN=1;//Initialize the port}void ReadIr() interrupt 0{unsigned char j,k;unsigned int err;Time = 0;DelayMs(40); // 136us*40if (IRIN == 0) //Confirm if the correct signal is really received{err = 1000; //1000*10us=10ms, more than that means an error signal is received/* Loop when both conditions are true, if one of the conditions is false, jump out of the loop to avoid program errorsHou, the program dies here */while ((IRIN==0) && (err>0)) //Wait for the low level of the previous 9ms to pass{DelayMs(1); // 136userr--;}if (IRIN == 1) //if correct wait until 9ms low{err = 500;while ((IRIN==1) && (err>0)) //Wait for the initial high level of 4.5ms to pass{DelayMs(1);err--;}for (k=0; k<4; k++) //There are 4 sets of data{for (j=0; j<8; j++) //Receive a set of data{err = 60;while ((IRIN==0) && (err>0))//Wait for the 560us low level in front of the signal to pass{DelayMs(1);err--;}err = 500;while ((IRIN==1) && (err>0)) //Calculate the duration of high level.{DelayMs(1);//0.14msTime++;err--;if (Time > 30){EX0 = 1;return;}}IrValue[k] >>= 1; //k represents which group of dataif (Time >= 8) //If the high level is greater than 565us, then it is 1{IrValue[k] |= 0x80;}Time = 0; //Reassign the time when the time is used up}}}if (IrValue[2] == ~IrValue[3]){IrValue[4] = IrValue[2];return;}}}
边栏推荐
- 【解决】RESP.app 连接不上redis
- 区块元素、内联元素(<div>元素、span元素)
- ATM系统
- 18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
- MySQL 5.7详细下载安装配置教程
- MySQL 5.7 detailed download, installation and configuration tutorial
- Introduction to Grid Layout
- 公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
- JUC(二)原子类:CAS、乐观锁、Unsafe和原子类
- 构造方法、成员变量、局部变量
猜你喜欢
随机推荐
MySQL 5.7 detailed download, installation and configuration tutorial
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
Go语言中定时任务库Cron使用详解
navicat无法连接mysql超详细处理方法
Introduction to Grid Layout
[PSQL] 窗口函数、GROUPING运算符
How much does a test environment cost? Start with cost and efficiency
51单片机外设篇:DS18B20
MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
100 latest software testing interview questions in 2022, summary of common interview questions and answers
Mysql存储json格式数据
Grid布局介绍
Android Studio 实现登录注册-源代码 (连接MySql数据库)
Navicat如何连接MySQL
Mysql common commands
12个MySQL慢查询的原因分析
Redis集群模式
Install and use Google Chrome
MySQL 字符串拼接 - 多种字符串拼接实战案例
MySQL 8.0.29 set and modify the default password