题目:通过串口向
单片机发送十进制数字,将数字十六进制显示在数码管上
我写了下面的程序,可运行的时候第一位数码管永远显示数字3,不知道错在哪,求大佬指点,非常感谢!
#include
***it wela=P2^7;
***it dula=P2^6;
unsigned char x, y, z, n, r, s, flag;
unsigned char data a[6];
unsigned int m;
unsigned char code tablewei[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
unsigned char code tableduan[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
unsigned char transform(unsigned int m);
void display();
void delay(unsigned char z);
void main()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
transform(m);
while(1)
{
if(flag==1)
{
flag=0;
ES=0;
SBUF=m;
while(!ti);//发送完TI会置1
TI=0;//发送中断标志位,软件清零
ES=1;
}
display();
}
}
void ser() interrupt 4
{
RI=0;
m=SBUF;
flag=1;
}
unsigned char transform(unsigned int m)
{
unsigned char i;
i=0;
s=0;
while(m!=0)
{
n=m/16;
r=m%16;
s=s++;
a=r;
i++;
m=n;
}
return s;
}
void display()
{
unsigned char i;
s=transform(m);
for(i=0;i
{
wela=1;
P0=tablewei;
wela=0;
P0=0x00;
dula=1;
P0=tableduan[a[s-i-1]];
dula=0;
delay(5);
}
}
void delay(unsigned char z)
{
for(x=110;x>0;x--)
for(y=z;y>0;y--);
}