- #include<reg51.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit key1=P3^0;
- sbit key2=P3^1;
- uchar code table[]={ //共陰數碼管段碼0~F
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- uchar code table1[]={0xff,0xfe,0xfc,0xf8,0xf0,0xe0};//LED低電平亮
- uchar num=0; //鍵值變量
- uchar count=0; //消抖計數變量
- bit flag=0; //按鍵狀態標志
- void main(void)
- {
- P2=table[num];
- P1=table1[num];
- while(1)
- {
- if(!key1||!key2)
- {
- if(++count>=100 && flag==0)
- {
- flag=1;
- if(!key1)
- {
- if(num<5)
- num++;
- }
- if(!key2)
- {
- if(num>0)
- num--;
- }
- P2=table[num];
- P1=table1[num];
- }
- }
- else
- {
- count=0;
- flag=0;
- }
- }
- }
復制代碼
|