#include <reg52.h>
sbit KEY_OUT_1 = P3^7;
sbit KEY_OUT_2 = P3^6;
sbit KEY_OUT_3 = P3^5;
sbit KEY_OUT_4 = P3^4;
sbit KEY_IN_1 = P3^0;
sbit KEY_IN_2 = P3^1;
sbit KEY_IN_3 = P3^2;
sbit KEY_IN_4 = P3^3;
extern void LcdSingleShow(unsigned char x,unsigned char y,unsigned char dat);
unsigned char code KeyCodeMap[4][4] = {
{'1','2','3',0x26},
{'4','5','6',0x25},
{'7','8','9',0x28},
('0',0x1b,0x0d,0x27),
};
unsigned char KeySta[4][4] = {
{1,1,1,1},{1,1,1,1},
{1,1,1,1},{1,1,1,1},
};
extern void KeyAc
tion(unsigned char keycode);
void KeyDriver()
{
unsigned char i,j;
static unsigned char backup[4][4] = {
{1,1,1,1},{1,1,1,1},
{1,1,1,1},{1,1,1,1},
};
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(backup[i][j] != KeySta[i][j])
{
if(backup[i][j] == 1)
KeyAction(KeyCodeMap[i][j]);
backup[i][j] = KeySta[i][j];
}
}
}
}
void KeyScan() //扫描按键状态并消抖
{
unsigned char i;
unsigned char a,b,c,d;
static unsigned char keyout = 0;
static unsigned char keybuf[4][4] = {
{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},
{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},
};
keybuf[0][keyout] = (keybuf[keyout][0]<<1)|KEY_IN_1;
keybuf[1][keyout] = (keybuf[keyout][1]<<1)|KEY_IN_2;
keybuf[2][keyout] = (keybuf[keyout][2]<<1)|KEY_IN_3;
keybuf[3][keyout] = (keybuf[keyout][3]<<1)|KEY_IN_4;
for(i=0;i<4;i++)
{
if((keybuf[i][keyout]&0x0f) == 0x0f)
KeySta[i][keyout] = 1;
else if((keybuf[i][keyout]&0x0f) == 0x00)
KeySta[i][keyout] = 0;
}
keyout++;
keyout &= 0x03;
switch(keyout)
{
case 0:KEY_OUT_1 = 0;KEY_OUT_2 = 1;KEY_OUT_3 = 1;KEY_OUT_4 = 1;break;
case 1:KEY_OUT_2 = 0;KEY_OUT_1 = 1;KEY_OUT_3 = 1;KEY_OUT_4 = 1;break;
case 2:KEY_OUT_3 = 0;KEY_OUT_2 = 1;KEY_OUT_1 = 1;KEY_OUT_4 = 1;break;
case 3:KEY_OUT_4 = 0;KEY_OUT_2 = 1;KEY_OUT_3 = 1;KEY_OUT_1 = 1;break;
default:break;
}
}
为什么只有主对角线上的四个按键可以正常使用 按其它按键都没有反应
在程序中插入Lcd显示 应该是KeyScan()这个函数出错了 但没发现错在哪
-
一周热门 更多>