簡(jiǎn)單的鍵盤(pán)讀寫(xiě)程序
電路說(shuō)明:
鍵盤(pán)由P1口引出,123是列線(xiàn),4567是行線(xiàn)。P2和P3口的LED是檢測(cè)按鍵的。
程序如下:
#include
#include
#define uchar unsigned char
uchar key;
delay (void)
{
unsigned int i=1000;
while(i--);
}
output (void)
{
switch(key)
{
case 0: P2=0xfe; break;
case 1: P2=0xfd; break;
case 2: P2=0xfb; break;
case 3: P2=0xf7; break;
case 4: P3=0xfe; break;
case 5: P3=0xfd; break;
case 6: P3=0xfb; break;
case 7: P3=0xf7; break;
case 8: P3=0xef; break;
case 9: P3=0xdf; break;
case 10: P3=0xbf; break;
case 11: P3=0x7f; break;
case 12: P3=0xff; break;
}
}
keyscan (void)
{
uchar temp=0;
P1=0xF0;
delay ();
if(P1!=0xF0)
{
P1=0xF0;
delay();
temp=P1;
temp=temp&0xF0;
temp=~((temp>>4)|0xF0);
if(temp==1)key=0;
else if(temp==2) key=1;
else if(temp==4) key=2;
else if(temp==8) key=3;
else key=12;
P1=0x0F;
delay();
temp=P1;
temp=temp&0x0F;
temp=~(temp|0xF0);
if(temp==2) key=key+0;
else if(temp==4) key=key+4;
else if(temp==8) key=key+8;
else key=12;
output ();
}
else return;
}
main (void)
{
keyscan ();
}
程序說(shuō)明:
函數(shù)定義很清楚,吃飯了,不多說(shuō)了。
評(píng)論