|
这次介绍的是按键防抖动对应的真值表。
volatile unsigned char Trg; //表示按键按下触发
volatile unsigned char Release; //表示按键释放触发
volatile unsigned char LastKey;//表示上次按键键值
volatile unsigned char CurrentKey;//表示本次按键键值
volatile unsigned char LastStatus;//表示上次按键状态
volatile unsigned char CurrentStatus;//表示本次按键状态
CurrentKey | LastKey | LastStatus | CurrentStatus | Trg | Release |
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 |
1 | 1 | 0 | 1 | 1 | 0 |
1 | 1 | 1 | 1 | 0 | 0 |
0 | 1 | 1 | 1 | 0 | 0 |
0 | 0 | 1 | 0 | 0 | 1 |
0 | 0 | 0 | 0 | 0 | 0 |
|
|
|
|
|
|
由此真值表可以得出:
CurrentStatus = CurrentKey& LastKey&(~ LastStatus)+ CurrentKey& LastKey& LastStatus+ (~CurrentKey& LastKey&LastStatus)
= CurrentKey& LastKey&(~ LastStatus)+ LastKey& LastStatus;
= CurrentKey& LastKey&(~ LastStatus); // 2 得到按下触发值
Release =( ~CurrentKey)& (~LastKey)&LastStatus& (~CurrentStatus);
=( ~CurrentKey)& (~LastKey)&LastStatus;
void KeyRead( void )
{
unsigned char CurrentKey = (~PINB)&0xff; // 1 读键值
Trg = CurrentKey& LastKey&(~ LastStatus); // 2 得到按下触发值
Release =( ~CurrentKey)& (~LastKey)&LastStatus& (~CurrentStatus)=( ~CurrentKey)& (~LastKey)&LastStatus; // 3 得到释放触发值
LastKey = CurrentKey; // 4 得到所有未释放的键值
LastStatus = CurrentStatus;//5 得到上次按键状态
}