去抖动程序
『壹』 单片机消抖延时程序
#include<reg52.h>
bit KEYFlag = 1; //指示按键状态,'1'为弹起态,'0'按下态
/********************************
按键去抖
*********************************/
void KEYDelay()
{
unsigned char i = 200;
while(i--);
}
/********************************
键翻译
*********************************/
unsigned char KEY_Tran(unsigned char Key)
{
switch(Key)
{
case 0xee: return 0; break; //根据按键编码返回对应的键值
case 0xde: return 1; break;
case 0xbe: return 2; break;
case 0x7e: return 3; break;
case 0xed: return 4; break;
case 0xdd: return 5; break;
case 0xbd: return 6; break;
case 0x7d: return 7; break;
case 0xeb: return 8; break;
case 0xdb: return 9; break;
case 0xbb: return 0x0a;break;
case 0x7b: return 0x0b;break;
case 0xe7: return 0x0c;break;
case 0xd7: return 0x0d;break;
case 0xb7: return 0x0e;break;
case 0x77: return 0x0f;break;
default: break;
}
}
/********************************
键扫描
*********************************/
unsigned char KEY_Scan()
{
unsigned char KEY_Value;
P2 = 0x0f;
KEY_Value = P2;
P2 = 0xf0;
KEY_Value |= P2;
return KEY_Value;
}
/********************************
查键
*********************************/
void KEY()
{
static bit KEYFlag = 1;
static unsigned char KEYCode;
if(KEYFlag)
{
if(KEY_Scan()!=0xff)
{
KEYCode = KEY_Scan();
KEYDelay();//有按键按下延时去抖
if(KEYCode==KEY_Scan())
{
// 键号 = KEY_Tran(KEYCode);
// 此处添加功能模块
KEYFlag = 0;
}
}
}
else
{
if(KEY_Scan()==0xff)
{
KEYDelay();//按键弹起延时去抖
if(KEY_Scan()==0xff)
KEYFlag = 1;
}
}
}