?? matrix_key.c
字號:
#include "Matrix_Key.h"
/*
* 矩陣鍵盤通用程序(引腳輸入輸出方向不切換)
*/
//KEY_PINS 低4位為ROW(監聽) 高4位為COL(輸出,低電平)
// 7 6 5 4 3 2 1 0
// Co4 Co3 Co2 Co1 Ro4 Ro3 Ro2 Ro1
/* COL 4 3 2 1
ROW 4
3
2
1
*/
unsigned char row=0,col=0;
//<返回其列號(col) 需要事先準備其行號掩碼>
static unsigned char Matrix_Key_Scan_Col(const unsigned char rowbitmask)
{
unsigned char i=0;
unsigned char colmask;
KEY_PINS = 0x0f;
colmask=0x0f;
while ( (KEY_PINS & rowbitmask)==0)
{
colmask = colmask<<1;
KEY_PINS = colmask | 0x0f;
i++;
}
return i;
}
//<檢測是否有按鍵按下,并返回其行號(row)>
static unsigned char Matrix_Key_Scan_Row()
{
KEY_PINS = 0x0f;
if (ROW1 == 0)
{
return 1;
}
else if (ROW2 == 0)
{
return 2;
}
else if (ROW3 == 0)
{
return 3;
}
else if (ROW4 == 0)
{
return 4;
}
return 0;
}
unsigned char Matrix_Key_Detect() //行/列
{
static unsigned char keep_time;
static unsigned char keep_row;
static unsigned char keep_col;
static unsigned char state = 0;
unsigned char uScanReturn;
switch (state)
{
case 0: //檢測是否有按鈕按下
if (Matrix_Key_Scan_Row() != 0 )
{ //<初始化>
keep_time = 0;
keep_row = 0;
keep_col = 0;
state++;
}
break;
case 1:
uScanReturn = Matrix_Key_Scan_Row();
if (uScanReturn != 0 ) //按鍵按下姿態
{
if (keep_time<250) keep_time++;
if (keep_row == 0)
{
keep_row = uScanReturn;
}
if (keep_row != 0 && keep_col == 0)
{
keep_col = Matrix_Key_Scan_Col( 0x01<<(keep_row-1) );
}
}
else //按鍵松起姿態
{
if (keep_time>5) //檢測到有效按鍵
{
keep_time = 0;
state++;
}
else //按鍵無效
{
state = 0;
}
}
break;
case 2:
if (Matrix_Key_Scan_Row() == 0 ) //按鍵松起姿態(保持)
{
if (keep_time<250) keep_time++;
if (keep_time>5) //檢測到有效松起
{
/* 返回信息:檢測到有效按鍵,并可以執行對應操作 */
state = 0;
row = keep_row;
col = keep_col;
return 1;
}
}
else //按鍵被重新按下
{
keep_time=0; //重新計時
}
break;
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -