📄 key.cpp
字号:
#include "includes.h"
#include "Key.h"
void InitKey(int iFlag)
{
if(iFlag)
{
//设置状态为:行信号输入,列信号输出低电平
//行信号 GPD[0..3] 输入(外部上拉)
rPCOND &= 0xff00; //PCOND[7..0]=00000000
//列信号 GPF[0..5] 输出低电平
rPCONF &= 0xfffff000;
rPCONF |= 0x0555; //PCONF[12..0]=0010101010101
rPDATF &= 0xffc0; //PDATF[5..0]=000000
}
else // col input,row output
{
//设置状态为:列信号输入,行信号输出低电平
//列信号 GPF[0..5] 输入(外部上拉)
rPCONF &= (0xff<<12); //PCONF[12..0]=0000000000000
//行信号 GPD[0..3] 输出低电平
rPCOND &= (0xFF<<8);
rPCOND |= 0x0055; //PCOND[7..0]=01010101
rPDATD &= 0xF0; //PDATD[3..0]=0000
}
}
void SetKeyInt()
{
rPCOND &= ~(0x300);
rPCOND |= 0x100;
rPDATD |= 0x10;
(*(volatile unsigned int *)( 0x1d20040))&=~0x03;
}
//功能:检查是否有键按下
int iIfkbhit(void)
{
unsigned int uiCount;
if(!(rPDATG&0x01))
{
for(uiCount=0;uiCount<0x3500;uiCount++);
//vDelayMs(10);//延时10Ms
if(!(rPDATG&0x01))
{
return 1;
}
}
return 0;
}
unsigned char ReadKey()
{
unsigned char ucKeyA,ucKeyB;
unsigned char ucTmp;
InitKey(1);
//当前状态:列信号输入,行信号输出低电平
//读入列信号
ucTmp = (rPDATD & 0x0f);
//printf("读入列信号x=%x\n",ucTmp);
switch(ucTmp)
{
case 0x0e :
ucKeyA = 0x00; break;
case 0x0d :
ucKeyA = 0x01; break;
case 0x0b :
ucKeyA = 0x02; break;
case 0x07 :
ucKeyA = 0x03; break;
default :
ucKeyA = 0xff;
return ucKeyA;
}
InitKey(0);
//读入列信号
ucTmp = (rPDATF & 0x3f);
switch(ucTmp)
{
case 0x3e :
ucKeyB = 0x05; break;
case 0x3d :
ucKeyB = 0x04; break;
case 0x3b :
ucKeyB = 0x03; break;
case 0x37 :
ucKeyB = 0x02; break;
case 0x2f :
ucKeyB = 0x01; break;
case 0x1f :
ucKeyB = 0x00; break;
case 0x3c : //
if(ucKeyA==3)
ucKeyB = 0x05; break;//双键(确认)
default :
ucKeyB = 0xff;
break;
}
//恢复键盘端口(列信号输入,行信号输出低电平)设置,为下次读入键值做准备
return ucKeyA;
}
unsigned char GetKey()
{
unsigned char ucKey;
while(!iIfkbhit());
ucKey = ReadKey();
//当按键放开后,返回键值
while(!(rPDATG & 0x01));
return ucKey;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -