|
发表于 2013-2-28 19:06:39
|
显示全部楼层
个人更改的简单版矩阵键盘读键程序static BYTE key_state = 0,key_press;
BYTE key_return = key_no,Key_Read_Data = 0;
// 读按键I/O电平
sw5 = 1;
sw6 = 0;
Nop();
Nop();
set_keyport(); //读IO口 矩阵排1
Key_Read_Data = key_port.portbits;
Key_Read_Data = Key_Read_Data<<4;
key_press = Key_Read_Data&key_mask;
//-------------------------------------
sw5 = 0;
sw6 = 1;
Nop();
Nop();
set_keyport(); //读IO口 矩阵排2
Key_Read_Data += key_port.portbits;
key_press = Key_Read_Data&key_mask;
//********************************************
switch (key_state)
{
case key_state_0: // 按键初始态
if (key_press != key_mask) key_state = key_state_1; // 键被按下,状态转换到键确认态
break;
case key_state_1: // 按键确认态
if (key_press == (Key_Read_Data&key_mask)) // 与原电平比较(消抖处理)
{
if(key_press == 0b11111110) key_return = key_1;
if(key_press == 0b11111101) key_return = key_2;
if(key_press == 0b11111011) key_return = key_3;
if(key_press == 0b11110111) key_return = key_4;
if(key_press == 0b11101111) key_return = key_5;
if(key_press == 0b11011111) key_return = key_6;
if(key_press == 0b10111111) key_return = key_7;
if(key_press == 0b01111111) key_return = key_8;
key_state = key_state_2; // 状态转换到判键释放态
}
else
key_state = key_state_0; // 按键已抬起,转换到按键初始态(消抖)
break;
case key_state_2:
if (key_press == key_mask)
key_state = key_state_3;
break;
case key_state_3:
if(key_press == key_mask)
key_state = key_state_0;
break;
}
return key_return; |
|