STM32CubeMX-Keil_uVision5/exp4/Core/Src/DirectKey.c
423A35C7 219ac4be0e 第四次实验-上课示例
应该算完成第四次实验了。
2024-11-03 21:54:57 +08:00

42 lines
884 B
C

#include "directkey.h"
#include "main.h"
uint8_t ReadDirectKey(void) {
static uint8_t KeyState = State0;
static uint8_t KeyOld;
uint8_t KeyPress;
uint8_t KeyValue = NoKey;
KeyPress = KeyPort->IDR & 0x000F; // 读按键
switch (KeyState) {
case State0:
if (KeyPress != NoKey) {
KeyOld = KeyPress; // 保存原来的键
KeyState = State1; // 切换状态
}
break;
case State1:
if (KeyPress == KeyOld) {
KeyState = State2;
// KeyValue = KeyOld; // 按下有效
} else {
KeyState = State0;
}
break;
case State2:
if (KeyPress == NoKey) {
KeyState = State0;
KeyValue = KeyOld; // 抬起有效
} else {
}
break;
default:
break;
}
return KeyValue;
}