论坛上很多大侠的读按键程序,特别是傻孩子大侠的, 可惜我看不懂, 我发一个我用了几年的按键, 不用对列, 占用资源较少, 不知是否有人读按键思路是否和我的一样? 让高手见笔了
//==========================================
//共四个按键
typedef union
{
unsigned char byte;
struct
{
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
}bits;
}uByte;
#define KEY_NULL 0b00001111
#define KEY_SHORT_TM 2
#define KEY_LONG_TM (2500/10)
#define KEY_CT_TM_START (950/10)
#define KEY_CT_TM_ERR (240/10)
#define KEY_CT_TM_START1 (2050/10)
#define KEY_CT_TM_ERR1 (400/10)
uByte vFlag,vFlag1;
#define bKeyLock vFlag.bits.b2
#define bKeyCtLock vFlag.bits.b3
#define bKeyLock1 vFlag.bits.b4
#define bKeyLock2 vFlag.bits.b5
#define bKeyLock3 vFlag.bits.b6
unsigned char vKCodeBuf,vKCodeCur,vKCodeOld,vKCodeOldBak;
unsigned char vKeyTm,vKeyTmContinue,vKeyTmContinue1,vKeyTmOld;
unsigned char vKeyVal;
//========================================================
void GET_KEY(void)
{
vKeyVal = KEY_NUSE;
if(vKCodeCur == vKCodeOld)
{
if(vKeyTm < 0xff)
{
vKeyTm++;
}
if(vKeyTmContinue < 0xff)
{
vKeyTmContinue++;
}
if(vKeyTmContinue1 < 0xff)
{
vKeyTmContinue1++;
}
}
else
{
vKCodeOldBak = vKCodeOld;
vKCodeOld = vKCodeCur;
vKeyTmOld = vKeyTm;
vKeyTm = 0;
vKeyTmContinue = 0;
vKeyTmContinue1 = 0;
}
if(vKCodeCur == KEY_NULL)
{
if(vKeyTm == KEY_SHORT_TM)
{
if(vKeyTmOld >= KEY_SHORT_TM)
{
switch(vKCodeOldBak)//松开按键,得到对应的键值
{
case 0b1110:
vKeyVal = 1;
break;
case 0b1101:
vKeyVal = 2;
break;
case 0b1011:
vKeyVal = 3;
break;
case 0b0111:
vKeyVal = 4;
break;
default:
break;
}
}
}
else if(vKeyTm == KEY_SHORT_TM+1) //松开后,去掉所有加锁互斥的标志.
{
bKeyLock = 0;
bKeyCtLock = 0;
bKeyLock1 = 0;
bKeyLock2 = 0;
bKeyLock3 = 0;
// bKeyLock4 = 0;
}
}
else
{
if((vKeyTm == KEY_SHORT_TM) && (bKeyLock == 0)) //短按,得到立即键值
{
bKeyLock = 1;
switch(vKCodeCur)
{
case 0b1110:
vKeyVal = 5;
break;
case 0b1101:
vKeyVal = 6;
break;
case 0b1011:
vKeyVal = 7;
break;
case 0b0111:
vKeyVal = 8;
break;
default:
break;
}
}
if(vKeyTmContinue >= (KEY_CT_TM_START+KEY_CT_TM_ERR))//连续按住,类似连加
{
bKeyLock1 = 1;
vKeyTmContinue = KEY_CT_TM_START; //按住一秒的时候得到一个键值,以后每隔200毫秒得到一个键值
switch(vKCodeCur)
{
case 0b1110:
vKeyVal = 9;
break;
case 0b1101:
vKeyVal = 10;
break;
case 0b1011:
vKeyVal = 11;
break;
case 0b0111:
vKeyVal = 12;
break;
default:
break;
}
}
if(vKeyTmContinue1 >= (KEY_CT_TM_START1+KEY_CT_TM_ERR1))
{
vKeyTmContinue1 = KEY_CT_TM_START1;
switch(vKCodeCur)
{
case 0b1110:
vKeyVal = 13;
break;
case 0b1101:
vKeyVal = 14;
break;
case 0b1011:
vKeyVal = 15;
break;
case 0b0111:
vKeyVal = 16;
break;
default:
break;
}
}
if(vKeyTm == KEY_LONG_TM) //长按3秒得到键值.
{
bKeyLock1 = 1;
bKeyLock3 = 1;
switch(vKCodeCur)
{
case 0b1110:
vKeyVal = 17;
break;
case 0b1101:
vKeyVal = 18;
break;
case 0b1011:
vKeyVal = 19;
break;
case 0b0111:
vKeyVal = 20;
break;
default:
break;
}
}
}
}转载
|