单片机串口通信协议程序
else
gRecState=R55;
break;
case RLEN:
gRecLen=ch;
gRecCount=0;
gRecState=RDFra Baidu bibliotekTA;
break;
case RDATA:
RecBuf[gRecCount]=ch;
gRecCount++;
if (gRecCount>=gRecLen)
{
gRecState=RCH;
if (RI==1)
{
ch=SBUF;
switch(gRecState)
{
case R55:// wait 0x55
if (ch==0x55)
gRecState=RAA;
break;
case RAA:
if (ch==0xaa)
gRecState=RLEN;
else if (ch==0x55)
gRecState=RAA;
gRecState=R55;
break;
}
RI=0;
}
if (TI==1)
{
}
}
void initcom()
{
//8,n.1
SCON=0x50;
PCON=0x00;
//baud rate
TH1=0xfd;
TL1=0xfd;
//timer1
TMOD=0x20;
TR1=1;
ES=1;
EA=1;
}
void cmdexecute()
}
break;
case RCH:
temp=0;
for(i=0;i<gRecLen;i++)
temp=temp^ RecBuf[i];
if (temp==ch)
gValue=1;
else
gValue=0;
//XBYTE[0xd000]=0x00;
gRecState=R55;
break;
default:
#include <reg52.h>
#include <absacc.h>
#define R55 101
#define RAA202
#define RLEN203
#define RDATA 104
#define RCH105
//#define
unsigned char gRecState=R55;
unsigned char gRecLen;
initcom();
while(1)
{
cmdexecute();;
}
}
{
if (gValue==1)
{
switch(RecBuf[0])
{
case 1:
XBYTE[0xd000]=0x00;
break;
case 2:
XBYTE[0xd000]=0xff;
break;
case 3:
break;
case 4:
break;
}
gValue=0;
}
}
void main()
{
//XBYTE[0xd000]=0x00;
unsigned char gRecCount;
unsigned char RecBuf[30];
unsigned char gValue;
void isr_UART(void) interrupt 4 using 1
{
unsigned char ch;
unsigned char i;
unsigned char temp;