//----鍵盤掃描程序------- //----用數(shù)碼管顯示相應(yīng)的鍵值----- //P1.0--P1.3接行------- //P1.4---P1.7接列------- #include 本文引用地址:http://m.butianyuan.cn/article/201611/321876.htmunsigned 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)//延時函數(shù) { 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)//主函數(shù) {
while(1) { getkey(); display();
} }
|
評論