仿真時數碼管亂碼的主要原因是消隱方法錯誤,在送段碼與位碼之間加P0=0xff;毫無用處,因為沒有打開鎖存器,P0信號送不出去,鎖存器還是維持原來數據。起不到消隱作用。再者read_num();處理數據的方法超過3ms,致使實際電路末位數碼管亮度與其它不均勻。按鍵使用死循環等待松手,會導致數碼管掃描暫停。另外,以按鍵次數計數與定時器外部計數模式毫無關系,開定時器完全多余。修改如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- unsigned long count;
- sbit key1=P3^4;
- sbit key2=P3^5;
- sbit dula=P2^6;
- sbit wela=P2^7;
- //uchar ge,shi,bai,qian,wan,sw;
- //void init();
- void read_num(void);
- void display();
- uchar code table[]={//段碼
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- uchar weia[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//位碼
- uchar buf[6];//顯示數據緩存
- bit k=0;//按鍵狀態記憶
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--);
- for(y=110;y>0;y--);
- }
- void read_num(void)
- {
- unsigned long j;
- uchar i;
- if (count>999999)
- {
- // TH0=0x00;
- // TL0=0x00;
- count=0;
- }
- j=count;
- for(i=6;i>0;i--)
- {
- buf[i-1]=table[j%10];//由低位到高位分解保存在緩存中
- j/=10;
- }
- }
- void main()
- {
- // P3=0xff;
- // P0=0xff;
- // P2=0xff;
- // init();
- count=543210;//測試顯示,可以刪除
- while(1)
- {
- if(key1==0)
- {
- delay(10);
- if(key1==0&&k==0)
- {
- k=1;
- count++;
- }
- }
- else k=0;//松手
- if(key2==0)
- count=0;
- read_num();
- display();
- }
- }
- void display()
- {
- static uchar i;
- P0 = 0x00; dula=1;dula=0;//送段消隱
- P0=weia[i];wela=1;wela=0;//送位碼
- P0= buf[i];dula=1;dula=0;//送段碼
- i++;i%=6;//逐位顯示計數
- }
- /*
- void init()
- {
- TMOD=0x05;
- TH0=0x00;
- TL0=0;
- TR0=1;
- }*/
復制代碼 |