#include <reg52.h>
unsigned char zimo[10]={0xc0,0xf9,0xa4,0xb0, // 0, 1, 2, 3 0x99,0x92,0x82,0xf8,0x80,0x90};// 4, 5, 6, 7, 8, 9 unsigned char disflag[4]={0xfe,0xfd,0xfb,0xf7}; unsigned char disbuf[4]; unsigned char n; unsigned char flag; unsigned char key; unsigned char KeyDelaytime; unsigned char Key_v; unsigned char Keystate; #define KEY_DELAY_TIME 10 #define KEY_IDEL 0x00 #define KEY_DELAY 0x01 #define WAIT_KEY_OPEN 0x02 sbit AN1 = P3^2; sbit AN2 = P3^3;
void scan_key() { switch(Keystate) { case KEY_IDEL : P3=0xff; Key_v=AN1; if((Key_v&0x01)==0) { Keystate=KEY_DELAY; KeyDelaytime=0; } break; case KEY_DELAY : if(KeyDelaytime>=KEY_DELAY_TIME) { P3=0xff; Key_v=AN1; if((Key_v&0x01)==0) { key++; if(key==6)key=0; Keystate=WAIT_KEY_OPEN; } else { Keystate=KEY_IDEL; } } break; case WAIT_KEY_OPEN : P3=0xff; Key_v=AN1; if(Key_v&0x01)Keystate=KEY_IDEL; break; } } int main() { unsigned char i; P0 = 0xff; P2 = 0xff; TMOD = 0x01; TH0 = 0xfc; TL0 = 0x18; IE = 0x82; key=0; flag=0; n=0; for(i=0;i<4;i++) { disbuf=zimo; } TR0=1; while(1) { scan_key(); } return 0; }
void timer0(void) interrupt 1 { TH0=0xfc; TL0=0x18; KeyDelaytime++; if(flag<key+1) { P0=disbuf[n]; P2=disflag[n]; } else { P2=0xff; P0=0; } flag++; if(flag==6) { n++; if(n==4) { n=0; } flag=0; } } 来个我的程序,中断扫描6个数码管,可调节亮度.20MS扫描8个太慢了,肯定会出现闪烁的情况 |