单片机串口程序。p2控制数码管显示位,P0口数码管显示内容。

要求是:通过串口程序,写入数据,然后在数码管上显示出来。
但是结果:送入十六进制的0——f,数码管显示0--15.正确
送入十六进制10以上的,数码管显示的,就不正确了。10显示48,11显示49,20显示64,30显示112,这是为什么呢?
程序如下:#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uint shuma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uint wei[]={0,1,2,3,4,5,6,7};
uchar temp,temp1;
int a,b,c;
void delay(unit);
void display();
void ck_csh();
void main()
{while(1)
{ck_csh();
display();
}
}
void display()
{
P2=wei[0];
P0=shuma[a];
delay(5);
P2=wei[1];
P0=shuma[b];
delay(5);
P2=wei[2];
P0=shuma[c];
delay(5);
}
/******************毫秒延时********************************************/
void delay(uint xms)
{ int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
/***********************串口初始化*********************/
void ck_csh()
{SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
EA=1;
ES=1; }
/*****************串口中断服务子程序**********************/
void sr() interrupt 4
{
if(RI)
{RI=0;
temp=SBUF;
P1=temp;
a=temp/100;
temp1=temp%100;
b=temp1/10;
c=temp1%10;
SBUF=temp;
if(TI)
{TI=0;}
}
}

第1个回答  2011-12-02
uint shuma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
数码管显示段码表里只有0~9,怎么能显示 16进制的A~F呢?
a=temp/100;
temp1=temp%100;
b=temp1/10;
c=temp1%10;
中断里对对到数据的处理也是按10进制的,应该改为16进制的。
a=temp&0x0f;
b=(temp1&0xf0)>>4;
一个字节用两位16进制显示就OK了。
第2个回答  2011-12-02
为什么那么多人犯这样的错误啊,你在中断程序里面又发送数据,这样又触发了新的中断,而且收和发是公用同一个中断,中断程序里面把自己又中断了。建议在中断前后加上中断保护
第3个回答  2011-12-02
unsigned char wei[]={1,2,4,8,10,20,40,80};//16进制
void main()
{
ck_csh();
while(1)
{ display();
}
}
void display()
{
P2=wei[0];
P0=shuma[a];
delay(5);
P2=wei[1];
P0=shuma[b];
delay(5);
P2=wei[2];
P0=shuma[c];
delay(5);
}
另外,不要在中断中进行数据处理,设置一个标志,在中断中设置标志,在主程序中判断标志,根据标志进行处理
延时程序最好用计时器完成,实际应用的程序一般不用软件延时本回答被提问者采纳
第4个回答  2011-12-02
爱情公寓第2季13 14集关谷的衣服什么牌子的 哪有卖的
相似回答