有点长,请高手别介意:dizzy:
第七课的第三题:
以16进制发送一个0-65536之间的任一数,当单片机收到后在数码管上动态显示出来,波特率自定。
这是我写的,发送数据数码管显示不了前三位,为毛?#include<reg52.h>
unsigned int num,aa;
unsigned char flag,num1,num2,num3,num4,num5,num6;
sbit dula=P2^6;
sbit wela=P2^7;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76,0x79,0x38,0x3f};
void led_dispaly(unsigned int num);
void delay(unsigned int z);
void ser_init()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
flag=0;
num=0;
aa=0;
}
void main()
{
ser_init();
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
SBUF=aa;
while(!TI);
TI=0;
ES=1;
while(RI==0)
{
led_dispaly(num);
}
}
}
}
void ser() interrupt 4
{
RI=0;
flag=1;
num=SBUF;
aa=SBUF;
}
void led_dispaly(unsigned int num0)
{
num1=num0/100000%10;
num2=num0/10000%10;
num3=num0/1000%10;
num4=num0/100%10;
num5=num0/10%10;
num6=num0%10;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[num1];
dula=0;
delay(1);
P0=0xff;
P0=0xfd;
wela=0;
dula=1;
P0=table[num2];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[num3];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[num4];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xef;
wela=0;
dula=1;
P0=table[num5];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
dula=1;
P0=table[num6];
dula=0;
delay(1);
}
void delay(unsigned int z) //延时函数,delay(1);延时1ms 11.0592MHZ
{
unsigned int i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
|