#include <reg52.H>
typedef unsigned int uint ;
typedef unsigned char uchar ;
sbit RF=P3^2;
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
uchar mma1,mma2,mma3,mma4; //第一次遥控编码
uchar rf_ok1; //第一次遥控编码赋值成功
uchar mmb1,mmb2,mmb3,mmb4; //第二次遥控编码
uchar rf_ok2; //第二次遥控编码赋值成功
uchar bma1,bma2,bma3,bma4;//临时遥控编码
uchar last_state; //上一个编码状态,0为低,1为高
uint hh_w=0,ll_w=0; //高低电平宽度初始化
uchar tb_ok; //同步码标志位,置1表示已经收到同步码,置0表示未收到同步码
uchar rf_data[4]; //最后收到的遥控编码
uchar ma_x; //接收到第几位编码
uchar s; //收到第一个码和第二个码之间不能超过S个周期
uchar rf_ok;
uchar decode_ok;
void ledinit();
void ex0init();
void ledinit()
{
led1=1;
led2=1;
led3=1;
led4=1;
}
void main()
{
ledinit();
ex0init();
TMOD=0X01;
TH0=0X00;
TL0=0X00;
while(1)
{
if(decode_ok==1)
{
switch(rf_data[3])
{
case 0xc8:led1=0;break;
case 0xc4:led2=0;break;
case 0xc2:led3=0;break;
case 0xc1:led4=0;break;
P1=rf_data[3];
}
}
}
void ex0init()
{
EX0=1;
IT0=1;
EA=1;
}
void ex0() interrupt 0
{
uchar rf_tmp=0;
EX0=0;
TH0=0;
TL0=0;
TR0=1;
if(RF==1)
{
TR0=0;
hh_w=TH0*256+TL0;
TH0=0;
TL0=0;
TR0=1;
}
else
{
TR0=0;
ll_w=TH0*256+TL0;
last_state=0;
}
if(!last_state)
{
if(((hh_w>=300)&&(hh_w<=400))&&((ll_w>=10000)&&(ll_w<=10500)))
{
tb_ok=1;
ma_x=0;
bma1=0;bma2=0;bma3=0;bma4=0;
TH0=0;
TL0=0;
TR0=1;
if(RF==0)
{
TR0=0;
ll_w=TH0*256+TL0;
last_state=0;
}
}
else if ((tb_ok)&&(ll_w>=900)&&(ll_w<1000))
{
ma_x++;
if(ma_x>23)
{
if(!rf_ok1)
{
mma1=bma1;
mma2=bma2;
mma3=bma3;
mma4=bma4;
rf_ok1=1;
tb_ok=0;
s=1300;
}
else{
mmb1=bma1;
mmb2=bma2;
mmb3=bma3;
mmb4=bma4;
rf_ok2=1;
tb_ok=0;
TH0=0;
TL0=0;
TR0=1;
if(RF==0)
{
TR0=0;
ll_w=TH0*256+TL0;
last_state=0;
}
}
}
}
else if((tb_ok)&&((ll_w>=300)&&( ll_w<=400)))
{
switch(ma_x)
{
case 0 : { bma1=bma1 | 0x80;
break; } //遥控编码第1位
case 1 : { bma1=bma1 | 0x40;
break; }
case 2 : { bma1=bma1 | 0x20;
break; }
case 3 : { bma1=bma1 | 0x10;
break; }
case 4 : { bma1=bma1 | 0x08;
break; }
case 5 : { bma1=bma1 | 0x04;
break; }
case 6 : { bma1=bma1 | 0x02;
break; }
case 7 : { bma1=bma1 | 0x01;
break; }
case 8 : { bma2=bma2 | 0x80;
break; }
case 9 : { bma2=bma2 | 0x40;
break; }
case 10: { bma2=bma2 | 0x20;
break; }
case 11: { bma2=bma2 | 0x10;
break; }
case 12: { bma2=bma2 | 0x08;
break; }
case 13: { bma2=bma2 | 0x04;
break; }
case 14: { bma2=bma2 | 0x02;
break; }
case 15: { bma2=bma2 | 0x01;
break; }
case 16: { bma3=bma3 | 0x80;
break; }
case 17: { bma3=bma3 | 0x40;
break; }
case 18: { bma3=bma3 | 0x20;
break; }
case 19: { bma3=bma3 | 0x10;
break; }
case 20: { bma4=bma4 | 0x08;
break; }// 按键状态第1位
case 21: { bma4=bma4 | 0x04;
break; }
case 22: { bma4=bma4 | 0x02;
break; }
case 23: { bma4=bma4 | 0x01;
if(!rf_ok1)
{
mma1=bma1;
mma2=bma2;
mma3=bma3;
mma4=bma4;
rf_ok1=1;
tb_ok=0;
s=1300;
}
else
{
mmb1=bma1;
mmb2=bma2;
mmb3=bma3;
mmb4=bma4;
rf_ok2=1;
tb_ok=0;
break;
}
}
}
ma_x++;
}
else {
ma_x=0;tb_ok=0;bma1=0;bma2=0;bma3=0;bma4=0;hh_w=0;ll_w=0;
}
last_state=1;
}
if(rf_ok1)
{
s--;
if(!s)rf_ok1=0;
if(rf_ok2)
{
if((mma1==mmb1)&&(mma2==mmb2&&(mma3==mmb3)&&(mma4==mmb4)))
{
rf_ok=1;
rf_ok1=0;
rf_ok2=0;
}
else
{
rf_ok=0;
rf_ok1=0;
rf_ok2=0;
}
}
}
if((rf_ok))
{
rf_ok=0;
rf_data[0]=mma1;
rf_data[1]=mma2;
rf_data[2]=mma2;
rf_data[3]=mma3;
decode_ok=1;
}
} |