同样的数字,四位数码管正常,二位数码管有乱码,同时,显示的数字顺序与实际要显示的数字顺序不符合,单片机代码如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar code table1[]={50,3,25};
- uchar code table2[]={55,20,3};
- uchar a=50,b=55,n=0,m=0;
- uchar j,k,c3,c2,c1,c0;
- uchar pp;
- void delay(unsigned int i)
- {
- int j;
- for(i;i>0;i--)
- for(j=20;j>0;j--);
- }
- void display(uchar x,uchar y,uchar o,uchar p)
- {
- P2=0x01;
- P0=table[p];
- delay(10);
-
- P2=0x04;
- P0=table[o];
- delay(10);
- P2=0x08;
- P0=table[y];
- delay(10);
- P2=0x02;
- P0=table[x];
- delay(10);
- }
- void main()
- {
- TMOD=0x01;
- TH0=(65536-46080)/256;
- TL0=(65536-46080)%256;
- ET0=1;
- EA=1;
- TR0=1;
- while(1)
- {
- if(TF0==1)
- {
- TF0=0;
- pp++;
- TH0=(65536-46080)/256;
- TL0=(65536-46080)%256;
- }
- if(pp==20)
- {
- pp=0;
- a--;
- b--;
- if(a==0)
- {
- n++;
- a=table1[n];
- if(n==2)
- {
- n=-1;
- }
- }
- if(b==0)
- {
- m++;
- b=table2[m];
- if(m==2)
- {
- m=-1;
- }
- }
- }
- c0=a%10;
- c1=a/10;
- c2=b%10;
- c3=b/10;
- display(c1,c0,c2,c3);
- }
- }
复制代码
|