想用红外线遥控和按钮同时控制继电器,但按如下程序如果单独用遥控没问题,单独用按钮也没问题,但不能同时使用,不知为什么?
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit K1=P3^6;
sbit K2=P3^7;
sbit JD1=P1^0;
sbit JD2=P1^1;
sbit JD3=P1^2;
sbit IR=P3^2;
bit kg;
uchar kval;
uchar a[4];
uint lowtime,hightime;
bit decode(void)
{
uchar i,j;
uchar temp;
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
temp=temp>>1;
TH0=0;
TL0=0;
TR0=1;
while(IR==0);
TR0=0;
lowtime=TH0*256+TL0;
TH0=0;
TL0=0;
TR0=1;
while(IR==1);
TR0=0;
hightime=TH0*256+TL0;
if((lowtime<370)||(lowtime>640))
return 0;
if((hightime>420)&&(hightime<620))
temp=temp&0x7f;
if((hightime>1300)&&(hightime<1800))
temp=temp|0x80;
}
a[i]=temp;
}
if(a[2]=~a[3])
{
return 1;
}
}
void int0(void) interrupt 0
{
EX0=0;
TH0=0;
TL0=0;
TR0=1;
while(IR==0);
TR0=0;
lowtime=TH0*256+TL0;
TH0=0;
TL0=0;
TR0=1;
while(IR==1);
TR0=0;
hightime=TH0*256+TL0;
if((lowtime>7800)&&(lowtime<8800)&&(hightime>3600)&&(hightime<4700))
{
if(decode()==1)
{
if(a[2]==0x0c)
{
JD1=0;JD2=1;JD3=1;
}
if(a[2]==0x18)
{
JD1=0;JD2=0;JD3=1;
}
if(a[2]==0x5e)
{
JD1=0;JD2=0;JD3=0;
}
}
}
EX0=1;
}
delay()
{
uchar n,m;
for(n=0;n<200;n++)
for(m=0;m<100;m++);
}
void timeint() interrupt 3
{
TR1=0;
if(K1==0)
{
delay();
if(K1==0)
{
JD1=0;JD2=1;JD3=1;
}
}
if(K2==0)
{
delay();
if(K2==0)
{
JD1=0;JD2=0;JD3=1;
}
}
TH1=0X00;
TL1=0XDC;
TR1=1;
}
void main()
{
EA=1;
EX0=1;
ET0=1;
ET1=1;
IT0=1;
TMOD=0X01;
TR0=0;
TH1=0X00;
TL1=0XDC;
TR1=1;
while(1);
} |