我用51单片机串口与电脑通信,电脑用串口调试软件,结果发现当电脑发送的数据小于15时准确率很高,发送16时单片机接收到数据为32,而发送的数据大于32小于64时单片机接收到数据都加了32.
例如我发了个10(16进制)每次返回都是48。不知道为什么?哪位大哥能帮帮忙啊?
虽然我自己觉得源程序没问题还是附上程序,如下:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit duan=P1^0;
sbit wei=P1^1;
int m=0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
uchar code table2[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //位选
void delay(uchar n)
{
uchar i,j;
for(i=n;i>0;i--)
for(j=0;j<85;j++);
}
void display()
{
char a[3],temp=0,c=2;
char b;
a[0]=m/100; //百位
a[1]=m%100/10; //十位
a[2]=m%10; //个位
if(a[0])
b=3;
else if(a[1])
b=2;
else
b=1;
temp=0;
c=2;
for(b;b>0;b--)
{
wei=0;
duan=1;
P0=0x00;
P0=table[a[c--]];
duan=0;
wei=1;
P0=0xff;
P0=table2[temp++];
delay(1); //延时
}
if(m<=8)
{
switch(m)
{
case 0x00: P2=255; break;
case 0x01: P2=254; break;
case 0x02: P2=252; break;
case 0x03: P2=248; break;
case 0x04: P2=240; break;
case 0x05: P2=224; break;
case 0x06: P2=192; break;
case 0x07: P2=128; break;
case 0x08: P2=0; break;
}
}
if(m>10)
{
switch(m)
{
case 0x0b: P2 &=0xfe; break;
case 0x0c: P2 |=0x01; break;
case 0x0d: P2 &=0xfd; break;
case 0x0e: P2 |=0x02; break;
case 0x0f: P2 &=0xfb; break;
case 0x10: P2 |=0x04; break;
case 0x11: P2 &=0xf7; break;
case 0x12: P2 |=0x08; break;
case 0x13: P2 &=0xef; break;
case 0x14: P2 |=0x10; break;
case 0x15: P2 &=0xdf; break;
case 0x16: P2 |=0x20; break;
case 0x17: P2 &=0xbf; break;
case 0x18: P2 |=0x40; break;
case 0x19: P2 &=0x7f; break;
case 0x1a: P2 |=0x80; break;
}
}
}
main()
{
SCON=0x50; //设置串行口工作模式为方式一,允许接收
PCON=0x00; //设置波特率
TMOD=0x20; //设置波特率
TH1=0xfd; //设置波特率
TL1=0xfd; //设置波特率
EA=1;
TR1=1;
RI=0;
ES=1; //打开串行口中断
TI=1;
while(1)
{
display();
}
}
void Chuankou(void) interrupt 4 //串口中断
{
if(RI==1) //若接收完毕则开始取数据
{
RI=0;
m=0;
m=SBUF;
TI=0;
SBUF=m;
while(!TI);
}
} |