#include <REG52.h>
#include <string.h>
#include <stdio.h>
#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<ms;i++)
for(j=0;j<10;j++);
} |