51单片机串口通信程序(需要判断)
发布网友
发布时间:2022-04-20 04:00
我来回答
共1个回答
热心网友
时间:2022-07-10 01:35
#include<reg51.h>
#define uchar unsigned char
uchar com1[]={0xef,0x01,0xff,0xff,0xff,0xff,0x01,0x00,0x03,0x01,0x00,0x05};
uchar red1[]={0xef,0x01,0xff,0xff,0xff,0xff,0x07,0x00,0x03,0x02,0x00,0x0c};
uchar red2[]={0xef,0x01,0xff,0xff,0xff,0xff,0x07,0x00,0x03,0x02,0x00,0x0a};
uchar red3[]={0xef,0x01,0xff,0xff,0xff,0xff,0x07,0x00,0x03,0x02,0x80,0x0a};
sbit p17=P1^7;
sbit p10=P1^0;
sbit p11=P1^1;
sbit p12=P1^2;
main()
{
uchar i,rdat[12];
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SCON=0x50;
while(1)
{
for(i=0;i<12;i++)
{
SBUF=com1[i];
while(!TI);
TI=0;
}
i=0;
while(i<12)
{
if(RI)
{
RI=0;
rdat[i]=SBUF;
i++;
}
}
for(i=0;i<12;i++)
{
if(rdat[i]!=red1[i])p17=1;
}
for(i=0;i<12;i++)
{
SBUF=com1[i];
while(!TI);
TI=0;
}
i=0;
while(i<12)
{
if(RI)
{
RI=0;
rdat[i]=SBUF;
i++;
}
for(i=0;i<12;i++)
{
if(rdat[i]!=red2[i])p10=1;
else if(rdat[i]!=red2[i])p11=1;
else p12=1;
}
}
}
}