遥控器按同一个键,每次接收到的值都不样,是怎么回事,还有如何检测按键抬起
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit infrared_in=P3^3;
uchar lamp_cache[4]=0xff;
uint t0_500vs;
#define T0 TH0=0xfe,TL0=0x33;
ys(uint y)
{
while(y--);
}
time0()interrupt 1
{
T0;
t0_500vs++;
}
main()
{
uchar i,j,temp;
TMOD=0x01;
T0;
EA=1;
ET0=1;
TR0=1;
while(1)
{
while(infrared_in == 1);
//初始状态下,红外输出脚一直是高电平,等待遥控按键
while(infrared_in== 0);
t0_500vs=0;
while(t0_500vs<=17); //等待9毫秒
t0_500vs=0;
while(infrared_in==1);
if(t0_500vs>5) //超过0.5*5 = 2.5毫米,检查一下遥控命令是不是连发,不是则执行下面的程序
{
t0_500vs=0;
lamp_cache[0]=0;
lamp_cache[1]=0;
lamp_cache[2]=0;
lamp_cache[3]=0;
for(i =0;i<4;i++)
{
for(j=0;j<8;j++)
{
while(infrared_in==0);
while(infrared_in==1);
if(t0_500vs>2) //根据波形长度判定码值为0或1
temp=0;
else
temp=1;
lamp_cache
|=(temp<<j);
t0_500vs=0;
}
}
while(infrared_in==0);
P1=lamp_cache[2];
}
}
}
此帖出自小平头技术问答
一周热门 更多>