📄 key.c
字号:
#include "type.h"
#include "reg52.h"
#include "common.h"
#include "key.h"
typedef struct tagKey
{
S_BYTE KeyCode : 5;
S_BYTE pressed : 1;
S_BYTE reserved : 2;
S_BYTE counter;
}
S_KEY;
#define KEY_SHORT_COUNT_MAX 50
#define KEY_LONG_COUNT_MAX 250
#define NO_KEY_PRESSED 0x00
#define KEY_MASK 0xFC
static S_KEY idata gkey;
S_BYTE code KeyMask[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04};
static S_BYTE GetKeyPins(void)
{
// P0 = KEY_MASK;
return (~P2 & KEY_MASK);
}
static void KeyScan(void)
{
S_BYTE i;
S_BYTE key;
if (gkey.pressed)
return;
key = GetKeyPins();
if (key == 0)
{
if (gkey.counter == 0)
DelayMs(1);
else if (gkey.counter < KEY_SHORT_COUNT_MAX)
gkey.KeyCode = KEY_NULL;
else if (gkey.counter < KEY_LONG_COUNT_MAX)
gkey.pressed = 1;
else
{
gkey.pressed = 1;
gkey.KeyCode += KEY_SHORT_END;
}
gkey.counter = 0;
return;
}
for (i = 0; i < sizeof(KeyMask); i++)
if (key == KeyMask[i])
break;
if (i >= sizeof(KeyMask))
return;
key = i + KEY_SHORT_BEGIN;
if (gkey.KeyCode == KEY_NULL)
gkey.KeyCode = key;
else if (gkey.KeyCode != key)
{
gkey.KeyCode = key;
gkey.counter = 0;
}
if (gkey.counter <= KEY_LONG_COUNT_MAX)
{
gkey.counter += 1;
DelayUs(500);
}
return;
}
static S_BYTE _GetKey_(void)
{
S_BYTE key;
if (gkey.pressed)
{
key = gkey.KeyCode;
gkey.KeyCode = KEY_NULL;
gkey.pressed = 0;
gkey.counter = 0;
return key;
}
return KEY_NULL;
}
S_BYTE GetKey(void)
{
KeyScan();
return _GetKey_();
}
S_BOOL PutKey(S_BYTE key)
{
if (gkey.pressed)
return S_FALSE;
gkey.pressed = 1;
gkey.KeyCode = key;
return S_TRUE;
}
S_BYTE WaitKey(void)
{
S_BYTE key;
while (1)
{
key = GetKey();
if (key)
return key;
DelayMs(3);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -