关于串口收发数据的讨论
时间:10-02
整理:3721RD
点击:
单片机从串口助手接收数据,再输出串口助手,每次输出把数据的时候,最后一位都是在下一次输出的时候才会输出,详情如图。请高手帮我指出程序的错误之处,感激不尽
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar flag0=0,flag1,j=0,m=0;
uchar zifu[10];
uchar zifu1[]={"Plaes enter shuzi:"};
uchar zifu2[]={"You enter:"};
uchar rev;
void delay(uchar i)
{
for(;i>0;i--)
for(j=200;j>0;j--);
}
void es0()interrupt 4
{
uchar i;
if(flag0==0)//????ê?3?±ê??
{
if(TI==1)
{
for(i=0;i<18;i++)//ê?3??°Please enter shuzi:?±
{
SBUF=zifu1[i];
delay(100);
}
}
TI=0;//?áê??D??
RI=1;//??è?display()oˉêy
}
flag0=1;
if(flag1==1)//????ê?3?±ê??
{
if(TI==1)
{
for(i=0;i<10;i++)//ê?3??°zifu2[]?±
{
SBUF=zifu2[i];
delay(100);
}
for(m=0;m<10;m++)//ê?3??°zifu[]?±£??°′??ú?óê?μ?μ??úèY
{
SBUF=zifu[m];
delay(100);
}
}
flag0=0;
j=0;
}
flag1=0;
}
void display()
{
if(RI==1)
{
ES=0;
zifu[j++]=SBUF;//?óê?′??úêy?Y
RI=0;
if(j==10)
{
flag1=1;
TI=1;
ES=1;
}
}
}
void main()
{
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
PCON=0x00;
EA=1;
ES=1;
TI=1;
while(1)
{
display();
}
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar flag0=0,flag1,j=0,m=0;
uchar zifu[10];
uchar zifu1[]={"Plaes enter shuzi:"};
uchar zifu2[]={"You enter:"};
uchar rev;
void delay(uchar i)
{
for(;i>0;i--)
for(j=200;j>0;j--);
}
void es0()interrupt 4
{
uchar i;
if(flag0==0)//????ê?3?±ê??
{
if(TI==1)
{
for(i=0;i<18;i++)//ê?3??°Please enter shuzi:?±
{
SBUF=zifu1[i];
delay(100);
}
}
TI=0;//?áê??D??
RI=1;//??è?display()oˉêy
}
flag0=1;
if(flag1==1)//????ê?3?±ê??
{
if(TI==1)
{
for(i=0;i<10;i++)//ê?3??°zifu2[]?±
{
SBUF=zifu2[i];
delay(100);
}
for(m=0;m<10;m++)//ê?3??°zifu[]?±£??°′??ú?óê?μ?μ??úèY
{
SBUF=zifu[m];
delay(100);
}
}
flag0=0;
j=0;
}
flag1=0;
}
void display()
{
if(RI==1)
{
ES=0;
zifu[j++]=SBUF;//?óê?′??úêy?Y
RI=0;
if(j==10)
{
flag1=1;
TI=1;
ES=1;
}
}
}
void main()
{
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
PCON=0x00;
EA=1;
ES=1;
TI=1;
while(1)
{
display();
}
}