//----键盘扫描程序------- //----用数码管显示相应的键值----- //P1.0--P1.3接行------- //P1.4---P1.7接列------- #include unsigned char code table[ ]={0x3F,0x06,0x5B,0x4F, 0x66,0x6D,0x7D,0x07, 0x7F,0x6F,0x77,0x7C, 0x39,0x5E,0x79,0x71};//0到F的16个键植 unsigned char dis[4]={1,2,3,4}; unsigned int h=0; unsigned long f=0;
void delayt(unsigned char t)//延时函数 { unsigned char a; while( --t != 0) { for(a=0;a<125;a++); } } bit pkey(void)//判断键的否被按下,通过返回值确定 { P1=0xf0; if(P1!=0xf0) { delayt(20); if(P1 != 0xf0) return 1; else return 0; } else return 0; } void getkey() { unsigned char key, j ,k, s; if(pkey()== 1)//确定键被按下 { P1=0xfe; k=0xfe; for(j=0;j<4;j++) { s=P1&0xf0; switch(s) { case 0xe0: key=4*j+0; break; case 0xd0: key=4*j+1; break; case 0xb0: key=4*j+2; break; case 0x70: key=4*j+3; break; default: break; } k=(k<1)|0x01; P1=k; } } if(key==11) { dis[0]=0; dis[1]=0; dis[2]=0; dis[3]=0; f=0; } else if(key==10) { f=1000*dis[3]+100*dis[2]+10*dis[1]+dis[0]; } else if(key<10) { h=h+1; if(h==1) { dis[0]=key; } else if(h==2) { dis[1]=dis[0]; dis[0]=key; } else if(h==3) { dis[2]=dis[1]; dis[1]=dis[0]; dis[0]=key; } else if(h==4) { dis[3]=dis[2]; dis[2]=dis[1]; dis[1]=dis[0]; dis[0]=key; } else if(h>4) { h=0; dis[0]=0; dis[1]=0; dis[2]=0; dis[3]=0; }
} } void display() { P0=table[dis[0]]; P2=0x01; delayt(100);
P0=table[dis[1]]; P2=0x02; delayt(100);
P0=table[dis[2]]; P2=0x04; delayt(100);
P0=table[dis[3]]; P2=0x08; delayt(100); } void main(void)//主函数 {
while(1) { getkey(); display(); } }
|