进来学习,早就该了解这些,本人很懒
#include <reg52.h>
#define c(x) (x*110592/120000)
sbit Ir_Pin = P3^5;
unsigned char Ir_Buf[4]; //用于保存解码结果
// UART初始化
// 波特率:9600
void uart_init(void)
{
unsigned char u;
ET1=0;
TMOD = 0x21; // 定时器1工作在方式2(自动重装)
SCON = 0x50; // 10位uart
TH1 = 0xFD;
TL1 = 0xFD;
u = SBUF;
TR1 = 1;
}
// 获取低电平时间
unsigned int Ir_Get_Low()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (!Ir_Pin && (TH0&0x80)==0);
TR0 = 0;
return (TH0 * 256 + TL0);
}
// 获取高电平时间
unsigned int Ir_Get_High()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (Ir_Pin && (TH0&0x80)==0);
TR0 = 0;
return (TH0 * 256 + TL0);
}
main()
{
unsigned int temp;
char i,j;
uart_init();
while (1)
{
while (Ir_Pin);
temp = Ir_Get_Low();
if (temp < c(8500) || temp > c(9500)) //引导脉冲低电平9000
continue;
temp = Ir_Get_High();
if (temp < c(4000) || temp > c(5000)) //引导脉冲高电平4500
continue;
for (i=0; i<4; i++) //4个字节
{
for (j=0; j<8; j++) //每个字节8位
{
temp = Ir_Get_Low();
if(temp < c(200) || temp > c(800))
continue;
temp = Ir_Get_High();
if(temp < c(200) || temp > c(2000))
continue;
Ir_Buf[i] >>= 1;
if(temp > c(1120))
Ir_Buf[i]|=0x80;
}
}
SBUF = Ir_Buf[2]; // 返回键码
while (TI == 0);
TI = 0;
}
}
我要赚赏金
