简介:本文主要为51矩阵键盘程序及电路连接图,一起来学习下:
51矩阵键盘程序及电路连接图
//按键码表
uchar code KeyCodeTable[]=
{
0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88
};
//延迟小程序,一般20ms
void Delay(void)
{
uchar i;
for(i=0;i200;i++);
}
//按键检测程序
uchar Keys_Scan(void)
{
uchar sCode,kCode,i,k;
P1 = 0xf0;
//是否有键被按下
if((P1&0xf0)!=0xf0)
{
Delay();
if((P1&0xf0)!=0xf0)
{
sCode = 0xfe;
for(k=0;k4;k++)
{
P1 = sCode;
if((P1&0xf0)!=0xf0)
{
kCode = ~P1;
for(i=0;i16;i++)
{
if(kCode == KeyCodeTable)
return i;
}
}
else
{
sCode = _crol_(sCode,1);
}
}
}
}
return -1;
}
51矩阵键盘程序及电路连接图