|
用外部中斷讀取按鍵信號,消抖是個(gè)問題。仿真按鍵不會(huì)抖動(dòng),如果用簡單的延時(shí)消抖方法會(huì)導(dǎo)致動(dòng)態(tài)數(shù)碼管顯示停頓。實(shí)際電路會(huì)因?yàn)榘存I抖動(dòng),cnt會(huì)加不確定的值。給你程序修改了,你試試。
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- //P0為SA-SH P1為C0-C3
- sbit K1=P3^2;
- uchar dis_code[11]={0xc0,0xf9,0xa4,0xb0, // 0, 1, 2, 3
- 0x99,0x92,0x82,0xf8,0x80,0x90, 0xff}; // 4, 5, 6, 7, 8, 9, off
- uchar buf[4];
- uint cnt=0;
-
- void delay(uint ms)
- {
- while(ms--);
- }
- void initial()
- {
- IT0 = 1;
- EX0 = 1;
- EA = 1;
- }
- void main()
- {
- uchar i;
- initial();
- while(1)
- {
- buf[0]=dis_code[cnt/1000%10]; //千位
- buf[1]=dis_code[cnt/100%10];//百位
- buf[2]=dis_code[cnt/10%10];//十位
- buf[3]=dis_code[cnt%10];//個(gè)位
- P1&=0xf0;
- P0=buf[i];
- P1|=0x01<<i;
- if(++i>3)
- i=0;
- delay(100);
- }
- }
- void init0() interrupt 0
- {
- cnt++;
- if(cnt>9999)
- {
- cnt=0;
- }
- }
復(fù)制代碼
|
|