4×4矩陣式鍵盤(非中斷)源程序
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
uchar temp;
uchar key;
uchar k;
uchar code_h;
//延時(shí)函數(shù)
void mDelay(uchar Delay)
{ uchar i;
for(;Delay>0;Delay--)
{ for(i=0;i<124;i++)
{;}
}
}
void keyc(void);
//主函數(shù)
void main(void)
{
P2_0=0;
while(1)
{
code_h=0xef;
P3=code_h;
temp=P3;
while(temp==code_h)//檢測(cè)鍵盤是否有按鍵按下
{
code_h=(code_h<<1)|0x01;
if(code_h==0xff)
{code_h=0xef;}
P3=code_h;
temp=P3;
}
mDelay(5); //延時(shí)去抖
if(P3==temp)
{
keyc();
while(temp==P3) //等待按鍵放開
{;}
}
}
}
//鍵盤操作
void keyc(void)
{
switch(temp)
{
case 0xee:
key=10;
break;
case 0xed:
key=0;
break;
case 0xeb:
key=11;
break;
case 0xe7:
key=12;
break;
case 0xde:
key=1;
break;
case 0xdd:
key=2;
break;
case 0xdb:
key=3;
break;
case 0xd7:
key=13;
break;
case 0xbe:
key=4;
break;
case 0xbd:
key=5;
break;
case 0xbb:
key=6;
break;
case 0xb7:
key=14;
break;
case 0x7e:
key=7;
break;
case 0x7d:
key=8;
break;
case 0x7b:
key=9;
break;
case 0x77:
key=15;
break;
}
P0=table[key];
}