代码:
#include<reg51.h>
#define uchar unsigned char
void delay0_5s()
{
uchar i;
for(i=0;i<0x0a;i++)
{
TH0=0x3c;
TL0=0xb0;
TR0=1;
while(!TF0);
TF0=0;
}
}
void delay_t(uchar t)
{
uchar i;
for(i=0;i<t;i++)
delay0_5s();
}
void serial()interrupt 4 using 2
{
uchar i;
EA=0;
if(RI==1)
{
RI=0;
if(SBUF==0x01)
{
SBUF=0x01;
while(!TI);
TI=0;
i=P1;
P1=0xe4;
while(SBUF!=0x02);
{
while(!RI);
RI=0;
}
SBUF=0x02;
while(!TI);
TI=0;
P1=i;
EA=1;
}
else{EA=1;}
}
}
void main()
{
uchar k;
TMOD=0x21;
TH1=0xf4;
TL1=0xf4;
TR0=1;
TR1=1;
SCON=0x50;
PCON=0x00;
EA=1;
ES=1;
while(1)
{
P1=0xcc;
delay_t(10);
for(k=0;k<3;k++)
{
P1=0xd4;
delay0_5s();
P1=0xc4;
delay0_5s();
}
P1=0xe1;
delay_t(10);
for(k=0;k<3;k++)
{
P1=0xe2;
delay0_5s();
P1=0xe0;
delay0_5s();
}
}
}
效果:
串口发送01双向禁行,发送02恢复正常状态