#include<reg52.h>
sbit P2_0=P2^0;//1號繼電器正位
sbit P2_1=P2^1;//1號繼電器反位
sbit P2_2=P2^2;//2號繼電器正位
sbit P2_3=P2^3;//2號繼電器反位
sbit P2_4=P2^4;//光電開關
unsigned int a;
typedef unsigned char uchar;
void delay(unsigned int xms) //延時函數
{
unsigned int t1, t2 ;
for(t1=0;t1<xms;t1++)
for(t2=0;t2<110;t2++);
}
void main()
{
SCON = 0x50;//設置串口工作方式1
TMOD = 0x20;//設置計數器工作方式2
PCON = 0x00;//即SMOD=1,波特率不加倍
TH1 = 0xE6;//計數器初值,波特率是1200,晶振為11.0592MHz
TL1 = 0xE6;
ES = 1;//打開接收中斷
EA = 1;//打開總中斷
TR1 = 1;//打開計數器
while(1)
{
if(P2_4==0)
{
a=0x01;
SBUF=a;
while(!TI);
TI=0;
}
}
}
void Usart() interrupt 4
{
uchar receiveData;
if(RI)
{
while(!RI);
receiveData=SBUF;//出去,接,收到的數據
if(receiveData==0xA1)//1號繼電器正位
{
P2_0=0;
P2_1=1;
P2_2=1;
P2_3=1;
delay(10000);
P2_0=1;
}
if(receiveData==0xA2)//1號繼電器反位
{
P2_0=1;
P2_1=0;
P2_2=1;
P2_3=1;
delay(10000);
P2_1=1;
}
if(receiveData==0xB1)//2號繼電器正位
{
P2_0=1;
P2_1=1;
P2_2=0;
P2_3=1;
delay(10000);
P2_2=1;
}
if(receiveData==0xB2)//2號繼電器反位
{
P2_0=1;
P2_1=1;
P2_2=1;
P2_3=0;
delay(10000);
P2_3=1;
}
}
RI = 0;
}
|