当前位置:网站首页>C51 minimum system board infrared remote control LED light on and off
C51 minimum system board infrared remote control LED light on and off
2022-06-30 07:26:00 【Three stinky ginger】
C51 Minimum system board infrared remote control led The light goes on and off
One 、 Preface
Because I haven't touched the SCM for a long time , And the previous code has not been well preserved , So we can't find a suitable one to refer to when we need to use infrared remote control . stay b Station to see a good effect video , But you need to add groups , After adding the group, I asked the group leader for code reference , The result is a fee , Because I am used to whoring for nothing , And the infrared remote control itself is not difficult , So I asked my classmates for the code of the infrared remote control car , I modified it , Change to control led Light out , It's basically the simplest one , If you don't understand the principle of infrared, you can use it easily . I am writing this article to facilitate migration to other projects .
Two 、 explain
1. This procedure does not include the acquisition of the key value of the infrared remote control , When buying a treasure, the merchant usually gives the key value .
2. I used a bread board to light up led Circuit , A resistor is used , The resistance value is 1k.
3. The code is not explained here , Will tell you where you can modify it and you can use it directly .
3、 ... and 、 connection
The wiring should be simple , I got the signal pin of the infrared receiver here P3^2 mouth . If you are not clear, you can comment and ask me .
Effect demonstration video :https://www.bilibili.com/video/BV1j44y1p7Nq
Four 、 Code
#include<reg52.h>
/* C51 Infrared remote control led The light goes on and off (51 Minimum system board ) Wiring instructions : P1^0 -- led P3^2 -- Signal terminal of infrared receiving module */
// Function definition
void delay(unsigned int z);
void delay_us(unsigned int aa);
/*==================================== Custom type name ====================================*/
typedef unsigned char INT8U;
typedef unsigned char uchar;
typedef unsigned int INT16U;
typedef unsigned int uint;
/*==================================== Hardware interface bit declaration ====================================*/
sbit IR = P3^2; // Define the infrared pulse data interface External interrupt O Input port
sbit led = P1^0;
uchar IRtime; // Detect the duration of infrared high level ( Pulse width )
uchar IRcord[4]; // This array is used to store the separated 4 Bytes of data ( User code 2 Bytes + Key value code 2 Bytes )
uchar IRdata[33]; // This array is used to store infrared 33 Bit data ( The first digit is the guide code and user code 16+ Key value code 16)
bit IRpro_ok, IRok; // The first one is for infrared reception 4 Bytes complete .IRok Used to detect pulse width
void init() // Initialize the timer 0 And external interruptions 0
{
TMOD = 0x02; // Timer 0 Operation mode 2,8 Bit auto reassembly
TH0 = 0x00; // high 8 Bit loading 0 Then the time that the timer overflows once is 256 Machine cycles
TL0 = 0x00;
EA = 1; // Total interruption
ET0 = 1; // Timer 0 interrupt
TR0 = 1; // Start timer 0
IT0 = 1; // Set external interrupt 0 It is the trigger mode of jump edge , A falling edge triggers once
EX0 = 1; // Start external interrupt 0
}
void time0() interrupt 1 // Define timer 0
{
IRtime++; // Detect pulse width ,1 Next is 278us
}
void int0() interrupt 0 // Define external interrupts 0
{
static uchar i; // Declare static variables ( After jumping out of the function, the value will not be lost when it comes back to execution )i Used for holding 33 The duration of the sub high level is stored in IRdata
static bit startflag; // Start storing the pulse width flag bit
if(startflag) // Start receiving pulse width detection
{
if( (IRtime < 53) && (IRtime >= 32) ) /* Determine whether it is a boot code , Bottom level 9000us+ high 4500us I can count myself as 11.0592 Come on NEC The boot code of the protocol is low 8000-10000+ high 4000-5000 If the boot code has been received, then i Will not be set 0 Will start to store the pulse width in turn */
i = 0; // If it is a boot code, execute i=0 Save him to IRdata The first bit of
IRdata[i] = IRtime; // With T0 To calculate the pulse width , Save this time in the array and judge later
IRtime = 0; // Counter reset , The next falling edge is stored in the pulse width
i++; // Count the number of times the pulse width is stored
if(i == 33) // If deposited 34 Time The subscript of an array is from 0 Start i be equal to 33 It means that 34 Time
{
IRok = 1; // Then it means that the pulse width detection is completed
i = 0; // Clear the pulse width count to prepare for the next storage
}
}
else
{
IRtime = 0; // The pilot code starts to enter. Clear the pulse width count to start counting
startflag = 1; // Start processing flag position 1
}
}
void IRcordpro() // Extract its 33 Second pulse width for data decoding
{
uchar i, j, k, cord, value; /*i Used for processing 4 Bytes ,j Used to process each bit in a byte ,k be used for 33 Which one of the second pulse width cord The time used to take out the pulse width is used to judge whether it meets 1 Pulse width time */
k = 1; // Take... From the first pulse width , Discard the pilot code
for(i = 0; i < 4; i++)
{
for(j = 0; j < 8; j++)
{
cord = IRdata[k]; // Store the pulse width in cord
if(cord > 5) // If the pulse width is greater than me 11.0592 Of t0 The overflow rate is about 278us*5=1390 Then judge as 1
value = value | 0x80; /* When receiving, the lowest bit is received first , Put the lowest position first value The highest position is in and 0x08 Press or click It won't change valua The value of other bits of will only make his highest position 1*/
if(j < 7)
{
value = value >> 1; //value Bits are shifted to the left and received in turn 8 Bit data .
}
k++; // Every time the pulse width bit is added 1
}
IRcord[i] = value; // After each byte is processed, put it into IRcord Array .
value = 0; // Zero clearing value It is convenient to store data next time
}
IRpro_ok = 1; // After receiving 4 After the bytes IRpro ok Set up 1 Indicates that infrared decoding is complete
}
/******************* The main function **************************/
void main()
{
init(); // Execute initialization timer 0 And external interruptions 0
while(1)
{
if(IRok) // Judge whether the pulse width is detected
{
IRcordpro();// Decode according to the pulse width 4 Bytes of data
IRok = 0; // Wait for pulse width detection again
if(IRpro_ok) // Determine whether the decoding is complete
{
switch(IRcord[2])
{
case 0x45: led = 1; break; // Press "1" Light on
case 0x46: led = 0; break; // Press "2" The light goes out
default:break;
}
IRpro_ok = 0;
}
}
}
}
/******************z Second delay function *************************/
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/**************** Subtle delay ******************************/
void delay_us(unsigned int aa)
{
while(aa--);
}
Modify the main function case The value after the statement is the key value corresponding to your remote control . as follows :
The Baidu network disk link of the whole project is as follows :
link :https://pan.baidu.com/s/1VNgm2WKdiygarchKKM97Rg
Extraction code :2022
– From Baidu network disk super member V4 The share of
边栏推荐
- RT thread kernel application development message queue experiment
- Thread network
- Keil plug-in Usage Summary
- Cubemx completes STM32F103 dual serial port 485 transceiver transmission
- QT generate random number qrandomgenerator
- Lt268 the most convenient TFT-LCD serial port screen chip in the whole network
- 【SemiDrive源码分析】【X9芯片启动流程】33 - Display模块 相关概念解析
- The class imported by idea import clearly exists, but it is red?
- Record the problem that the system file cannot be modified as an administrator during the development process
- QT wmic command obtains some hardware information
猜你喜欢

Network security - routing principle

Use of ecostruxure (3) creating composite function blocks

Next initializesecuritycontext failed: unknown error (0x80092012) - the revocation function cannot check whether the certificate is revoked.

我今年毕业,但我不知道我要做什么

Install go language development tools

动态内存管理

Stm32g0 Tim interrupt use

Dynamic memory management

Can introduction

SwiftUI打造一款美美哒自定义按压反馈按钮
随机推荐
【最全】linux服务器上安装Mysql
FreeRTOS timer group
Network security ARP protocol and defense
手机开户股票开户安全吗?开户需要准备什么?
Install go language development tools
【SemiDrive源码分析】【X9芯片启动流程】33 - Display模块 相关概念解析
How crazy are young people in sideline industry: 3000 monthly salary and 3W sideline income
app quits unexpectedly
Linux server installation redis
期末复习-PHP学习笔记11-PHP-PDO数据库抽象层.
Embedded test process
[semidrive source code analysis] [x9 chip startup process] 34 - RTOS side display module SDM_ display_ Init display initialization source code analysis
I graduated this year, but I don't know what I want to do
Error reporting record
Experiment 1: comprehensive experiment [process on]
Private method of single test calling object
The class imported by idea import clearly exists, but it is red?
【已解决】MySQL异常:ERROR 1045 (28000): Unknown error 1045,忘记初始密码
The first up Master of station B paid to watch the video still came! Price "Persuading" netizens
next InitializeSecurityContext failed: Unknown error (0x80092012) - 吊销功能无法检查证书是否吊销。