新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 通信波特率115.2K的串行通信C语言示例

通信波特率115.2K的串行通信C语言示例

作者:时间:2012-02-19来源:网络收藏

//115.2k程序,校验方式为简单双向校验,
//而且是必要的,因为任何采用中断、crc等的方法
//均很难保证速度和避免错误.
//总体速度5KB/秒

本文引用地址:http://www.eepw.com.cn/article/172057.htm

//单片机程序
//使用22.1184晶振
unsigned char time,b_break,b_break_3;
//35.555ms
void int_t0(void) interrupt 1 {
TH0=0;
if ((++b_break_3)>2) b_break=1;
time++;
}

//

unsigned char rec(void) {
TH0=0;b_break_3=0;b_break=0;
while (RI==0) {
if (b_break) return(1); //等待pc发送100ms超时
}
RI=0;
SBUF=SBUF;
return(SBUF);
}
/*返回1表示失败*/
bit sen(unsigned char d) {
SBUF=d;
TH0=0;b_break_3=0;b_break=0;
while (RI==0) {
if (b_break) return(1); //等待pc送回相同字节数据,100ms超时
}
RI=0;
if (SBUF!=d) return(1);
return(0);
}

void main(void) {
unsigned int i;
unsigned char d;
EA=1;
SCON=0xd8;PCON=0x80;
TMOD=0x21;
TL1=TH1=0xff;TR1=1;
TH0=0;ET0=TR0=1;
while (1) {
d=rec();
if ((!b_break)(d==0x55)) {
d=rec();
if (!b_break) {
if (d==0xaa) {
//接收到pc送来的要求发送指令0xaa55
for (i=0;i1000;i++) {
if (sen(i)) {
//发送错误
break;
}
}
} else if (dd==0xaa) {
//接收到pc送来的要求接收指令0xaaaa
for (i=0;i1000;i++) {
d=rec();
if (!b_break) {
//正确接收处理
} else {
//接收错误处理
break;
}
}
}
}
}
}
}

//电脑程序tc2.0
#i nclude stdio.h
#i nclude dos.h
#i nclude process.h
#i nclude string.h
#i nclude conio.h
#i nclude dir.h>


上一页 1 2 下一页

评论


相关推荐

技术专区

关闭