📄 key.c
字号:
//1 2 3 4
//5 6 7 8
//9 10 11 12
//13 14 15 16
#include<SPCE061V004.h>
#define CLK 0x0004 //IOB2
#define DATA 0x0002 //IOB1
#define LOAD 0x0001 //IOB0
#define Set_IOB_Bit(x) (*P_IOB_Data = *P_IOB_Buffer | x)
#define Clear_IOB_Bit(x) (*P_IOB_Data = *P_IOB_Buffer & ~x)
#define ClearWatchdog() *P_Watchdog_Clear = 0x0001
unsigned int KeyCode = 0;
//*********************************************************************//
//函数名称:void KeyInit()
//函数功能:键盘口的初始化
//输入参数:无
//输出参数:无
//********************************************************************//
void KeyInit()
{
*P_IOB_Dir |= CLK + LOAD;
*P_IOB_Dir &= ~DATA;
*P_IOB_Attrib |= CLK + LOAD;
*P_IOB_Attrib &= ~DATA;
*P_IOB_Data |= CLK;
*P_IOB_Data &= ~(DATA + LOAD);
}
//**********************************************************************//
//函数名称:unsigned int KeyScan()
//函数功能:键盘扫描,并去抖
//输入参数:无
//输出参数:是否有键按下标志,1代表无键按下,0代表有键按下
//**********************************************************************//
unsigned int KeyScan()
{
unsigned int i = 0;
unsigned int keytemp = 0;
KeyCode = 0;
Clear_IOB_Bit(LOAD); //拉低LOAD,以锁存按键值
Set_IOB_Bit(LOAD);
for(i=0;i<16;i++) //读取键值
{
KeyCode <<= 1;
if(!(*P_IOB_Data & DATA))
KeyCode += 1;
Set_IOB_Bit(CLK);
Clear_IOB_Bit(CLK);
}
if(KeyCode==0) //如果键值为零,则返回零
return(0);
else //若键值不为零,则等待按键释放
{
keytemp = KeyCode;
while(keytemp)
{
Clear_IOB_Bit(LOAD); //拉低LOAD,以锁存按键值
Set_IOB_Bit(LOAD);
for(i=0;i<16;i++) //读取键值,直到读到键值为零为止
{
keytemp <<= 1;
if(!(*P_IOB_Data & DATA))
keytemp += 1;
Set_IOB_Bit(CLK);
Clear_IOB_Bit(CLK);
}
ClearWatchdog();
}
return(1); //返回有键按下标志位
}
}
//***********************************************************************//
//函数名称:unsigned int GetKey()
//函数功能:得到按下键对应的键值
//输入参数:无
//输出参数:按键值
//***********************************************************************//
unsigned int GetKey()
{
switch(KeyCode)
{
case 0x0001: return(13);
case 0x0002: return(14);
case 0x0004: return(15);
case 0x0008: return(16);
case 0x0010: return(12);
case 0x0020: return(11);
case 0x0040: return(10);
case 0x0080: return(9);
case 0x0100: return(8);
case 0x0200: return(7);
case 0x0400: return(6);
case 0x0800: return(5);
case 0x1000: return(4);
case 0x2000: return(3);
case 0x4000: return(2);
case 0x8000: return(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -