|
发表于 2020-7-6 19:54:02
|
显示全部楼层
我们一般将键盘设计成一个独立模块,产生单击、延时击、延时后的快击,几种状态码,然后到相应的任务模块里调看键值,简单可靠。尽量不用定时器去延时,只用主程序执行的周期做计时,对于按键的去抖延时20-40毫秒,均无防。这样可以减少定时器的支出。
给你一段代码:
#include "Use.h"
#include "Ver.H"
#define Timer20ms (80/TK_MAIN) //ÑÓʱʱ¼ä20mS
#define Timer3S (1500/TK_MAIN) //ÑÓʱʱ¼ä2S
#define Timer100ms (200/TK_MAIN) //ÑÓʱʱ¼ä100mS
//**********************************************************
KEY KeyDat; //¶¨ÒåÊý¾Ý½á¹¹
//**********************************************************
sbit KeyIn=P1^6;
sbit Clk_Key595=P2^7;
sbit Rck_Key595=P0^2;
sbit Dat_Key595=P0^3; //
//*********************************************
void SenDat_Key595(unsigned char Dat){
unsigned char j=8;
Rck_Key595=0;
for (j=0;j<8;j++){
Clk_Key595=0;
Dat_Key595=(bit)(Dat & 0x80);
Clk_Key595=1;
Dat<<=1; //×óÒÆÊäÈë
}
Rck_Key595=1;
}
//*********************************************
unsigned char ReadKey(void){
unsigned char i,j=0;
j =~HMLCtrl;
j &=0x0f;
j<<=1;
if (fBZ==0) j |=0x01;
//*****************************************
i =0x60 | j; // 0111,1111=7f 2005-3-22
KeyIn=1;
SenDat_Key595(i);
if (!KeyIn) return 1; //Ka
i=0xa0 | j; //1011,1111 =bf
SenDat_Key595(i);
if (!KeyIn) return 2; //kb
i=0xc0 | j; //1101,1111=df
SenDat_Key595(i);
if (!KeyIn) return 3; //kc
//***************************** KFo KFs
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;
}else{
KeyDat.KeyPower=0;
KeyDat.KeyData=0; //·µ»Ø
}
}
}
/*********************************
ÅбðÊÇ·ñËÉ¿ª
**********************************/
void KeyOff1(void){
unsigned char i;
i=ReadKey();
if (i==0){ //ËÉ¿ª°´¼ü
KeyDat.KeyPower=0;
KeyDat.KeyData |=HaveKey; //¶¨Òå±êÖ¾
//*****************************************
Beep_Start(0); //·äÃùÆ÷¶ÌÃù
//******************************************
}else{
if (--KeyDat.KeyDog == 0){ //3ÃëÑÓʱµ½
//*****************************************
Beep_Start(0);//·äÃùÆ÷¶ÌÃù
//******************************************
KeyDat.KeyData |=HaveKey | DubClick; //3SºóËÉ¿ªµÃµ½³¤ÑÓʱ¼ä¶¨Òå±êÖ¾
KeyDat.KeyDog=3*Timer100ms;
KeyDat.KeyPower++;
}
}
}
/**********************************
Á¬»÷ÊÇ·ñËÉ¿ª
**********************************/
void KeyOff2(void){
unsigned char i;
i=ReadKey();
if (i!=0){
if (--KeyDat.KeyDog == 0){
KeyDat.KeyData =i | HaveKey | AddClick; //¼ÓËÙ»÷±êÖ¾
KeyDat.KeyDog=Timer100ms;
//*****************************************
Beep_Start(0);//·äÃùÆ÷¶ÌÃù
//******************************************
}
}else{
KeyDat.KeyData =0;
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;
}
|
|