#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define ushort unsigned short
#define MAX_DATA_LENGTH 32
typedef struct//定義數(shù)據(jù)發(fā)送包
{
uchar head;//包頭
uchar length;//數(shù)據(jù)長度
uchar cmd;//命令
uchar sData[6];//數(shù)據(jù)
uchar crc;//校驗(yàn)
uchar end;//包尾
} Packet,*PT;
typedef struct//定義接收緩沖區(qū)
{
uchar iCnt;
uchar sData[MAX_DATA_LENGTH];
} RecvBuf,*RB;
void SendD(PT addrBuf,uint cnt);//定義發(fā)送函數(shù)
void Sleep(uint ms);//定義延時(shí)子程序
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)//定義延時(shí)子程序
{
int i,j;
for(i=0;i
for(j=0;j<10;j++);
}
評(píng)論