📄 key.c
字号:
#include "44blib.h"
#include "key.h"
#include "uart_lib.h"
//**********************************************//
// 读矩阵键盘键盘扫描码 Matrix_GetScanCode
//**********************************************//
// 函数说明:
// 读取矩阵键盘当前按键的扫描码
// 入口参数:
// 无
// 返回值:
// 按键的16位扫描码
// BIT[7:0] 键盘编码。0~63 为有效键值,128代表没有按键
// BIT[ 8] 特殊键1。1表示该键按下,0表示该键没有按下
// BIT[ 9] 特殊键2。1表示该键按下,0表示该键没有按下
// BIT[10] 特殊键3。1表示该键按下,0表示该键没有按下
// BIT[11] 特殊键4。1表示该键按下,0表示该键没有按下
// BIT[13:12] 保留。固定0
// BIT[14] 按键状态1。1表示该键值尚未读,0表示该键值前次已读过
// BIT[15] 按键状态2。1表示矩阵键盘有键按下,0表示没有键按下(特殊键除外)
//
//----------------------------------------------//
unsigned short Matrix_GetScanCode(void)
{
return FPGA_Addr_KEY;
}
//**********************************************//
// 矩阵键盘读按键函数 Matrix_GetKey
//**********************************************//
// 函数说明:
// 读取矩阵键盘当前的按键值
// 入口参数:
// 无
// 返回值:
// 0 没有按键
// 其它 当前按键的编码(1~64)
//
//----------------------------------------------//
char Matrix_GetKey(void)
{
static unsigned short Key_inhibit;
unsigned short Key_Code;
char Key;
Key_Code = FPGA_Addr_KEY;
FPGA_Addr_KEY = 0; // clear key
//UART0_Printf( "Key code = %x\n",Key_Code) ;
//Delay(2000);
if(INHIBIT_AUTO_KEY_INPUT == 1)
{
if( Key_inhibit==0)
{
if(Key_Code>>7 & 1) // Bit7==1 : No key
Key = 0;
else
{
Key = (Key_Code & 0x007f)+1;
if(Key_Code>>8 & 1) // Bit8:shift1
Key+=64;
else if(Key_Code>>9 & 1) // Bit9: shift2
Key+=128;
//UART0_Printf( "Key %d Press!\n",Key) ;
Key_inhibit = 1;
}
}
else
{
Key = 0;
if((Key_Code >>15 & 1)==0) // key up
Key_inhibit = 0;
}
}
else
{
if(Key_Code >> 14 & 1) // Bit14:New_Key
{
Key = (Key_Code & 0x007f)+1;
if(Key_Code>>8 & 1)
Key+=32;
else if(Key_Code>>9 & 1)
Key+=64;
//UART0_Printf( "Key %d Press!\n",Key) ;
}
else
Key = 0;
}
return Key;
}
//**********************************************//
// 矩阵键盘读字符函数 Matrix_GetChar
//**********************************************//
// 函数说明:
// 等待用户从矩阵键盘输入一个字符
// 入口参数:
// 无
// 返回值:
// 用户输入按键的编码(1~64)
//
//----------------------------------------------//
char Matrix_GetChar(void)
{
char Key;
Matrix_GetKey();
do
Key=Matrix_GetKey();
while(!Key);
return Key;
}
void KeyPad_test(void)
{
char ch;
Uart_Printf("---矩阵式键盘测试开始---\n");
while(1)
{
ch = Matrix_GetKey();
if(ch!=0)
Uart_Printf("Key=%02xh\n",ch);
if(ch==0x0f)
break;
}
Uart_Printf("---矩阵式键盘测试结束---\n\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -