矩阵键盘的扫描实际上就像坐标轴上的xy一样:
unsigned char Key_Read()
{
unsigned char temp = 0;
P44 = 0;P42 = 1;P35 = 1;P34 = 1;
if(P33 == 0) temp = 4;
if(P32 == 0) temp = 5;
if(P31 == 0) temp = 6;
if(P30 == 0) temp = 7;
P44 = 1;P42 = 0;P35 = 1;P34 = 1;
if(P33 == 0) temp = 8;
if(P32 == 0) temp = 9;
if(P31 == 0) temp = 10;
if(P30 == 0) temp = 11;
P44 = 1;P42 = 1;P35 = 0;P34 = 1;
if(P33 == 0) temp = 12;
if(P32 == 0) temp = 13;
if(P31 == 0) temp = 14;
if(P30 == 0) temp = 15;
P44 = 1;P42 = 1;P35 = 1;P34 = 0;
if(P33 == 0) temp = 16;
if(P32 == 0) temp = 17;
if(P31 == 0) temp = 18;
if(P30 == 0) temp = 19;
return temp;
}
首先对P44赋值0,则当按键被按下时,与其连接到相应io口也会变成0,所以可以通过扫描相应的io得知相应的键码
使用时可以在主函数中使用西风的三行消抖代码
unsigned char Key_Val,Key_Down,Key_Old;//定义变量
void Key_Proc()
{
Key_Val=KeyRead();
Key_Down = Key_Val & (Key_Val ^ Key_Old);
Key_Val = Key_Old;
}
个人使用,欢迎指教