上位机输入一个数然后在单片机上显示,可是无论怎样改数码管始终乱码,而且从串口调试助手上看,我发送一个数据,,他却在不停的接收,求大神解答
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit we=P2^6;
sbit du=P2^7;
uchar shu1,shu2,ge,shi,bai,flag,b,i;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void init()
{
TMOD=0x20;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
TH1=0xf4;
TL1=0xf4;
TR1=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar bai,uchar shi,uchar ge)
{
we=1;
P0=0xfe;
we=0;
du=1;
P0=table[bai];
du=0;
P0=0xff;
delay(5);
we=1;
P0=0xfd;
we=0;
du=1;
P0=table[shi];
du=0;
P0=0xff;
delay(5);
we=1;
P0=0xfb;
we=0;
du=1;
P0=table[ge];
du=0;
P0=0xff;
delay(5);
}
void main()
{
init();
while(1)
{
if(flag==1)
{
if(b>=300)
b=0;
flag=0;
ES=0;
TI=1;
SBUF=b;
while(!TI);
TI=0;
ES=1;
}
ge=b%10;
shi=b%100/10;
bai=b/100;
display(bai,shi,ge);
}
}
void timer() interrupt 4
{
RI=0;
i++;
if(i==1) //SBUF一次只能读一个值,,我就用了两个shu1和shu2来存了;
{
shu1=SBUF;
}
else if(i==2)
{
i=0;
shu2=SBUF;
b=shu1*256+shu2;
flag=1;
}
else;
}
|