标题:
51单片机串口通信-握手协议
[打印本页]
作者:
远征之路
时间:
2018-11-19 10:54
标题:
51单片机串口通信-握手协议
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
uchar code askconfig[]= {0xcc,0x99,0x01,0x02,0x03,0xdd};
uchar code Ackconfig[]= {0xaa,0x66,0x99,0x16,0x91,0x12,0xd8,0x56,0xbb};
uchar chuli[29];
void System_Init()
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xfc;
TL1 = 0xfc;
PCON = 0x00;
TR1 = 1;
}
void Delay (uint Milliseconds)//延时
{
uchar a,b;
for(a=3;a>0;a--)
{
for(b=245;b>0;b--)
for (; Milliseconds > 0; Milliseconds--);
}
}
void SendChar (uchar chr)//发送一个字符
{
SBUF = chr;
while(!TI);
TI=0;
}
char Handshake (void)
{
uchar i,j,k;
Delay(50000);
Delay(50000);
for(i=0;i<6;i++)
{
SendChar(askconfig[ i]);[ i]
}
while(RI)
{
chuli[j]=SBUF;
RI=0;
j++;
if(j==9)
{
for(k=0;k<10;k++)
{
if(chuli[k]!=Ackconfig[k])
break;
}
if(k!=10)
return 0;
else
return -1;
}
}
}
void main()
{
System_Init();
Delay(200);
while(1)
{
if(Handshake ())
{
Delay(2000);
Delay(2000);
SendChar(0xff);
}
}
}
复制代码
作者:
renshanwu
时间:
2020-8-15 12:57
41行运行有错误啊
作者:
lzj23642008
时间:
2022-6-20 15:24
renshanwu 发表于 2020-8-15 12:57
41行运行有错误啊
41可能运行不了
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1