【发布时间】:2018-07-04 05:05:40
【问题描述】:
我遇到了this code by Ganssle 关于开关去抖动的问题。代码看起来很高效,我的几个问题可能很明显,但我希望能澄清一下。
- 为什么他检查 10 毫秒按钮按下和 100 毫秒按钮释放。他不能只检查 10 毫秒的新闻和发布吗?
- 从 main 中每 5 毫秒轮询一次此函数是执行它的最有效方式,还是我应该检查引脚中的中断,当有中断时将引脚更改为 GPI 并进入轮询例程,然后我们推断值将引脚切换回中断模式?
#define CHECK_MSEC 5 // Read hardware every 5 msec
#define PRESS_MSEC 10 // Stable time before registering pressed
#define RELEASE_MSEC 100 // Stable time before registering released
// This function reads the key state from the hardware.
extern bool_t RawKeyPressed();
// This holds the debounced state of the key.
bool_t DebouncedKeyPress = false;
// Service routine called every CHECK_MSEC to
// debounce both edges
void DebounceSwitch1(bool_t *Key_changed, bool_t *Key_pressed)
{
static uint8_t Count = RELEASE_MSEC / CHECK_MSEC;
bool_t RawState;
*Key_changed = false;
*Key_pressed = DebouncedKeyPress;
RawState = RawKeyPressed();
if (RawState == DebouncedKeyPress) {
// Set the timer which will allow a change from the current state.
if (DebouncedKeyPress) Count = RELEASE_MSEC / CHECK_MSEC;
else Count = PRESS_MSEC / CHECK_MSEC;
} else {
// Key has changed - wait for new state to become stable.
if (--Count == 0) {
// Timer expired - accept the change.
DebouncedKeyPress = RawState;
*Key_changed=true;
*Key_pressed=DebouncedKeyPress;
// And reset the timer.
if (DebouncedKeyPress) Count = RELEASE_MSEC / CHECK_MSEC;
else Count = PRESS_MSEC / CHECK_MSEC;
}
}
}
【问题讨论】:
标签: c embedded debouncing