#include<reg52.h>
#include<math.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint i=0,j,URTAReceivedCount=0,n=1;
uchar data Tempdatatable[2];//数据包
sbit led=P2^7;
void Delay_1ms(uint i)//1ms延时
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
void Com_Int(void) interrupt 4
{
uchar temp;
ES=0; //关串口中断
RI=0; //软件清除接收中断
temp=SBUF;
if(temp==0XFF && URTAReceivedCount<3)
{
Tempdatatable[0]==0XFF; //包头
URTAReceivedCount++;
}
else
{
Tempdatatable[n]=temp;
n++;
}
if(URTAReceivedCount==2)//包尾
{
Tempdatatable[0]=0XFF;
Tempdatatable[2]=0XFF;
n=1;
URTAReceivedCount=0; //组包完毕
temp="";
}
ES=1;//开串口中断
}
void main() //程序入口
{
Delay_1ms(200);
TMOD = 0x20;
PCON = 0x80;
SCON = 0x50;
TH1 = 0xfA; //设置波特率 9600
TL1 = 0xfA;
TR1 = 1; //启动定时器1
ET1 =0;
ES = 1; //开串口中断
PS=1; //设计串行口中断优先级
EA = 1; //开总中断
IT0=0;
EX0=1;
while(1)
{
if(Tempdatatable[0]==0XFF && Tempdatatable[2]==0XFF)
{
switch(Tempdatatable[1])
{
case 0X02:
led=0;
P1=0x02;
break;
default : TR0=0;TR2=0;
break;
}
}
}
}
|