求教51单片机串口通信PC机为什么接收不到数据?
2019-07-15 07:18发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void sand1(uchar *receive);
uchar buf;
uchar flag = 0;
uchar i=0;
uchar Receive[20];
void main()
{
SCON = 0X50;//串口方式1,允许接收
PCON = 0X00;//波特率不加倍
TMOD = 0X20;//定时器方式2
EA = 1;
ES = 1;
TH1 = 0XFD;//波特率9600
TL1 = 0XFD;
TR1 = 1;
if(flag == 1)
{
for(i = 1;(i<21)&&(buf!=-1); i++)//-1表示写入字符串的结束标志
{
Receive[i-1]=buf;
flag = 0;
}
}
while(1)
{
sand1(Receive);
}
}
//下位机串口接收中断
void shou_xia() interrupt 4
{
ES = 0;
if(RI == 1)
{
RI = 0;
buf = SBUF;
}
flag = 1;
ES = 1;
}
void sand(uchar dat)
{
SBUF=dat;//向pc发送单个字符
while(ti == 0);//等待发送
TI = 0;
}
void sand1(uchar *receive)
{
while(*receive!=' ')
sand(*receive++);
}
一周热门 更多>