📄 独特的键盘扫描程序.txt
字号:
李杰老师的独特键盘扫描程序
/*****************************************************
键盘的键值定义
******************************************************/
#define DubClick 0x40 //连击
#define HaveKey 0x80 //有键
#define Ka (0x01|HaveKey)
#define Kb (0x02|HaveKey)
#define Kc (0x03|HaveKey)
#define Ka_Dub (Ka | DubClick | HaveKey) //双击
#define Kb_Dub (Kb | DubClick | HaveKey)
#define Kc_Dub (Kc | DubClick | HaveKey)
#include "Use.h"
#include "Ver.H"
#define Timer20ms (30/TK) //延时时间20mS
#define Timer3S (1000/TK) //延时时间2S
#define Timer100ms (900/TK) //延时时间100mS
//#define Port P1 //键盘口
//extern unsigned char ChangKey(unsigned char pr);
extern unsigned char BeepPower;
extern void SendTo595(unsigned char *p);
//**********************************************************
KEY idata KeyDat; //定义数据结构
//**********************************************************
sbit KeyIn=P1^5;
//*********************************************
unsigned char ReadKey(void){
unsigned char Buf[2];
Buf[0]=0xff; //关闭
Buf[1]=0x7f; // 0111,1111=7f 2005-3-22
KeyIn=1;
SendTo595(Buf);
if (!KeyIn) return 1; //Ka
Buf[0]=0xff; //关闭
Buf[1]=0xbf; //1011,1111 =bf
SendTo595(Buf);
if (!KeyIn) return 2; //kb
Buf[0]=0xff; //关闭
Buf[1]=0xdf; //1101,1111=df
SendTo595(Buf);
if (!KeyIn) return 3; //kc
return 0;
}
/************************************
判别是否有键
*************************************/
void GetKey (void){
if (ReadKey()!=0) {
KeyDat.KeyDog=Timer20ms;
KeyDat.KeyPower++;
}
}
/**************************************
延时去抖动
***************************************/
void KeyDog(void){
if (KeyDat.KeyDog-- == 0){
KeyDat.KeyData=ReadKey(); //读键盘
if (KeyDat.KeyData!=0){
KeyDat.KeyPower++;
KeyDat.KeyDog=Timer3S;
//*****************************************
BeepPower=1;//蜂鸣器短鸣
//******************************************
}else{
KeyDat.KeyPower=0;
KeyDat.KeyData=0; //返回
}
}
}
/*********************************
判别是否松开
**********************************/
void KeyOff1(void){
if (ReadKey()==0){ //松开按键
KeyDat.KeyPower=0;
KeyDat.KeyData |=HaveKey; //定义标志
}else{
if (KeyDat.KeyDog-- == 0){ //3秒延时到
KeyDat.KeyDog=Timer100ms;
KeyDat.KeyPower++;
}
}
}
/**********************************
连击是否松开
**********************************/
void KeyOff2(void)
{
if (ReadKey()!=0){
if (KeyDat.KeyDog-- == 0){
KeyDat.KeyData |=DubClick | HaveKey ; //连击标志
KeyDat.KeyDog=Timer100ms;
//*****************************************
BeepPower=1;//蜂鸣器短鸣
//******************************************
}
}else{
KeyDat.KeyData =0; //?|=HaveKey; //一次按键
KeyDat.KeyPower =0;
}
}
/**********************************
函数指针定义
***********************************/
code void (code *SubKey[])()={
GetKey,KeyDog,KeyOff1,KeyOff2
};
/*****************************
键处理程序
******************************/
void KeyBord(void){
(*SubKey[KeyDat.KeyPower])();
}
/**************************************
键处理后清除键值?
***************************************/
unsigned char JB_KeyData(void){
unsigned char i=0;
if (KeyDat.KeyData > DubClick){
i=KeyDat.KeyData;
KeyDat.KeyData=0;
}
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -