#include
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define ushort unsigned short
#define MAX_DATA_LENGTH 32
typedef struct//界说数据发送包
{
uchar head;//包头
uchar length;//数据长度
uchar cmd;//指令
uchar sData[6];//数据
uchar crc;//校验
uchar end;//包尾
} Packet,*PT;
typedef struct//界说接纳缓冲区
{
uchar iCnt;
uchar sData[MAX_DATA_LENGTH];
} RecvBuf,*RB;
void SendD(PT addrBuf,uint cnt);//界说发送函数
void Sleep(uint ms);//界说延时子程序
void init();//C51初始化程序
bit Judge(RB addrBuf);
void Crc(PT addrBuf);
Packet pT;
RecvBuf rB;
void main()
{
memset(&pT,0x00,MAX_DATA_LENGTH+5);
memset(&rB,0x00,MAX_DATA_LENGTH+1);
pT.head=0x02;
pT.length=0x0b;
pT.cmd=0x44;
pT.end=0x03;
sprintf(pT.sData,”%s”,”080327″);
pT.crc=0x056;
init();
while(1)
{
Sleep(10000);
}
}
void serial(void) interrupt 4//界说串行口中止处理子程序
{
ES=0;
while(RI)
{
rB.sData[rB.iCnt]=SBUF;
rB.iCnt++;
RI=0;
}
if(Judge(&rB))
{
memset(&rB,0x00,MAX_DATA_LENGTH+1);
SendD((uchar*)&pT,pT.length);
}
ES=1;
}
bit Judge(RB addrBuf)
{
if(addrBuf->sData[0]==0x02&&addrBuf->sData[addrBuf->sData[1]-1]==0x03)
return 1;
else
return 0;
}
void init()//完结51初始化作业
{
TMOD=0x20;//初始化51
TH1=0xfd;
TL1=0x0Fd;
TR1=1;
SCON=0x50;
PCON=0x00;
EA=1;
ES=1;
}
void SendD(uchar *addrBuf,uchar cnt)
{
int i=0;
T0=0;
while(++i<=cnt)
{
SBUF=addrBuf[i-1];
while(!TI);
TI=0;
}
}
void Sleep(uint ms)//界说延时子程序
{
int i,j;
for(i=0;i
for(j=0;j<10;j++);
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/255858.html