添加一個顯示緩存,可以大幅度簡化代碼。
- #include "reg52.h"
- #define uchar unsigned char
- #define uint unsigned int
- uint i,j,k;
- uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//段選數字0-9;
- uchar code wela[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位選開關
- uchar data dis_buf[11];//顯示緩存
- void delay(uint k) //ms延時程序
- {
- uint i,j;
- for(i=k;i>0;i--)
- for(j=120;j>0;j--);
- }
- void main()
- {
- for(i=0;i<10;i++)
- {//將需要顯示的數字0~9按序保存在顯示緩存中
- dis_buf[i]=table[i];
- }
- while(1)
- {
- P0=0xff;//消隱
- P2=wela[j];//送位碼
- P0=dis_buf[j];//送段碼
- j++; //數碼管動態顯示計數
- if(j==8)
- {
- j=0;
- k++; //更新速度計數
- if(k==50)//可以調整顯示更新速度
- {
- k=0;
- dis_buf[10]=dis_buf[0];//將顯示緩存0移動到10
- for(i=0;i<10;i++)
- {
- dis_buf[i]=dis_buf[i+1];//顯示緩存全部移動一位
- }
- }
- }
- delay(2);//控制主循環速度
- }
- }
復制代碼 |