本文主要是介绍2024/4/25 红外遥控代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
51完结撒花!!!
这块如果IR听不懂可以看看那个状态机的相关视频。
Int0.c
#include <REGX52.H>void Int0_Init(void)
{IT0=1;IE0=0;EX0=1;EA=1;PX0=1;
}//void Int0_Routine(void) interrupt 0
//{
// Num++;
//}
Timer0.c
#include <REGX52.H>/*** @brief 定时器0初始化,1毫秒@12.000MHz* @param 无* @retval 无*/
void Timer0_Init(void) //10us
{TMOD &= 0xF0; //设置定时器模式TMOD |= 0x01; //设置定时器模式TL0 = 0xA4; //设置定时初值TH0 = 0xFF; //设置定时初值TF0 = 0; //清除TF0标志TR0 = 1; //定时器0不计时
}void Timer0SetCounter(unsigned int Value)//把传过来的十六位数据拆成两个八位存进寄存器,设置初始值(要拆掉才能存)
{TH0=Value/256;TL0=Value%256;
}unsigned int Timer0_GetCounter(void)//把定时器里变化后的数据拿出来,得到时间差
{return (TH0<<8)|TL0;
}void Timer0_Run(unsigned char Flag)//主函数里启动定时器的开关
{TR0=Flag;
}
IR.c
#include <REGX52.H>
#include "Timer0.h"
#include "Int0.h"unsigned int IR_Time;
unsigned char IR_State;unsigned char IR_Data[4];
unsigned char IR_pData;unsigned char IR_DataFlag;
unsigned char IR_RepeatFlag;
unsigned char IR_Address;
unsigned char IR_Command;void IR_Init(void)
{Timer0_Init();Int0_Init();
}unsigned char IR_GetDataFlag(void)
{if(IR_DataFlag){IR_DataFlag=0;return 1;}return 0;
}unsigned char IR_GetRepeatFlag(void)
{if(IR_RepeatFlag){IR_RepeatFlag=0;return 1;}return 0;
}unsigned char IR_GetAddress(void)
{return IR_Address;
}unsigned char IR_GetCommand(void)
{return IR_Command;
}void Int0_Routine(void) interrupt 0
{if(IR_State==0){Timer0SetCounter(0);Timer0_Run(1);IR_State=1;}else if(IR_State==1){IR_Time=Timer0_GetCounter();Timer0SetCounter(0);if(IR_Time>12442-500&&IR_Time<12442+500){IR_State=2;}else if(IR_Time>10368-500&&IR_Time<10368+500){IR_RepeatFlag=1;Timer0_Run(0);IR_State=0;}else{IR_State=1;}}else if(IR_State==2){IR_Time=Timer0_GetCounter();Timer0SetCounter(0);if(IR_Time>1032-500&&IR_Time<1032+500){IR_Data[IR_pData/8]&=~(0x01<<(IR_pData%8));IR_pData++;}else if(IR_Time>2074-500&&IR_Time<2074+500){IR_Data[IR_pData/8]|=(0x01<<(IR_pData%8));IR_pData++;}else{IR_pData=0;IR_State=1;}if(IR_pData>=32){IR_pData=0;if((IR_Data[0]==~IR_Data[1])&&(IR_Data[2]==~IR_Data[3])){IR_Address=IR_Data[0];IR_Command=IR_Data[2];IR_DataFlag=1;}Timer0_Run(0);IR_State=0;}}
}
main.c
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
#include "IR.h"unsigned int Num;
unsigned char Address;
unsigned char Command;
void main()
{LCD_Init();LCD_ShowString(1,1,"ADDR CMD NUM");LCD_ShowString(2,1,"00 00 000");IR_Init();while(1){if(IR_GetDataFlag()|| IR_GetRepeatFlag()){Address=IR_GetAddress();Command=IR_GetCommand();LCD_ShowHexNum(2,1,Address,2);LCD_ShowHexNum(2,7,Command,2);if(Command==IR_VOL_MINUS){Num--;}if(Command==IR_VOL_ADD){Num++;}LCD_ShowNum(2,12,Num,3);}}
}
这篇关于2024/4/25 红外遥控代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!