#include <AT89x051.H>
#define led1 P3_0
#define led2 P3_1
#define led3 P1_6
#define led4 P1_5
unsigned char k;
unsigned char data psCount; //定时器计数器
unsigned char data i;
unsigned char data j;
unsigned char data temp;
unsigned char data dp[4]; //用来保存红外遥控的码值
main()
{
led1=0;
led2=0;
led3=0;
led4=1;
k=0;
dp[0]=0;
dp[1]=0;
dp[2]=0;
dp[3]=0;
TMOD=0X11;
TH0 = 254; //定时500uS
TL0 = 12;
TCON=0X05;
PX1=0;
PT0=1;
IE=0X80;
TR0=1;
EX1=1;
while(1)
{
if(dp[2]==0Xf3)
{
dp[2]=0;
led1=1;
led2=1;
}
if(dp[2]==0Xff)
{
dp[2]=0;
led1=1;
led2=0;
}
if(dp[2]==0Xfb)
{
dp[2]=0;
led1=0;
led2=1;
}
if(dp[2]==0xf7)
{
dp[2]=0;
k=k+1;
if(k==3)
{
k=1;
}
//led1=1;
//led2=0;
}
if(k==1)
{led3=1;
led4=0;
}
if(k==2)
{
led3=0;
led4=1;
}
if(k==1)
{
if(P1_4==1)
{
led1=1;
led2=0;
}
if(P1_4==0)
{
led1=0;
led2=1;
}
}
}
}
void timer0() interrupt 1
{
psCount++;
TH0 = 254;//(65536-500)/256; //定时500uS
TL0 = 12;//(65536-500)%256;
}
void INT1_ISR() interrupt 2 using 2
{
ET0=0;
psCount=0;
//while(P3_2==1);//判断是否有键被按下
ET0=1;
while(P3_3 == 0);
if( psCount > 17)//0.5* 17 = 8.5ms约为9.0ms
{
ET0 = 0;
psCount = 0;
ET0 = 1;
while(P3_3 == 1);
if(psCount > 5)//超过0.5*5 = 2.5毫秒,是起始位,小于2.5MS是停止位,检查一下遥控命令是不是连发,不是则执行下面的程序
{
ET0 = 0;
psCount = 0;
dp[0] = 0x0;
dp[1] = 0x0;
dp[2] = 0x0;
dp[3] = 0x0;
//数据
for(i = 0 ; i < 4; i++)
{
for(j = 0; j < 8; j++)
{
while(P3_3 == 0);
ET0 = 0;
psCount = 0;
ET0 = 1;
while(P3_3 == 1);
if(psCount > 2)//根据波形长度判定码值为0或1
temp = 0;
else
temp = 1;
dp[i] |= (temp<<j);//保存键值
}
}
}
}
// if(dp[2]==0Xf3)
// {
// led1=0;
// led2=1;
// }
// if(dp[2]==0Xff)
// {
// led1=1;
// led2=1;
// }
// if(dp[2]==0Xfb)
// {
// led1=1;
// led2=0;
// }
// if(dp[2]==0Xf7)
// {
// k=k+1;
// if(k==3)
// {
// k=1;
// }
// //led1=1;
// //led2=0;
//
// }
}
评论0