当前位置:网站首页>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;}}}
边栏推荐
- golang generics
- 软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
- Android studio connects to MySQL and completes simple login and registration functions
- goroutine (coroutine) in go language
- How Navicat Connects to MySQL
- MySQL 5.7 detailed download, installation and configuration tutorial
- navicat无法连接mysql超详细处理方法
- MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
- Go语学习笔记 - grpc serverclient protobuf 从零开始Go语言
- go项目的打包部署
猜你喜欢

navicat无法连接mysql超详细处理方法

12 reasons for MySQL slow query

测试环境要多少?从成本与效率说起

复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式

MySQL如何创建用户

面试测试工程师一般会问什么?测试主管告诉你
[email protected](使用passwordYES)"/>Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)

C language: Check for omissions and fill in vacancies (3)

Install and use Google Chrome

apisix-入门使用篇
随机推荐
Detailed explanation of interface in Go language
C语言小游戏——扫雷小游戏
Go语学习笔记 - grpc serverclient protobuf 从零开始Go语言
[PSQL] 函数、谓词、CASE表达式、集合运算
navicat无法连接mysql超详细处理方法
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
对node工程进行压力测试与性能分析
关于web应用的目录结构
MySQL 5.7 upgrade to 8.0 detailed process
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
Timing task library in the language use Cron, rounding
Mysql 回表
Use the browser's local storage to realize the function of remembering the user name
go项目的打包部署
浏览器的onload事件
利用浏览器本地存储 实现记住用户名的功能
MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)
面试测试工程师一般会问什么?测试主管告诉你
网安学习-内网渗透4
Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言






