简单的去抖动例程 [英] Simple Debounce Routine

查看:158
本文介绍了简单的去抖动例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是一个没有任何操作系统的简单的裸机系统。



我想避免具有特定计数的循环结构,因为处理器速度可能会波动。

解决方案

我想你可以在这里学到很多东西: http://www.ganssle.com /debouncing.pdf



如果可能,您最好的选择是在硬件中执行此操作,但也有一些软件在这方面的想法。 p>

来自TFA的简单示例代码:

  #define CHECK_MSEC 5 //每5毫秒读取硬件
#define PRESS_MSEC 10 //注册之前的稳定时间
#define RELEASE_MSEC 100 //注册前的稳定时间
//此函数读取从硬件的关键状态。
extern bool_t RawKeyPressed();
//保存键的去抖状态。
bool_t DebouncedKeyPress = false;
//服务例程调用每个CHECK_MSEC到
//去抖两边
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){
//设置允许从当前状态更改的定时器。
if(DebouncedKeyPress)Count = RELEASE_MSEC / CHECK_MSEC;
else Count = PRESS_MSEC / CHECK_MSEC;
} else {
//键已更改 - 等待新状态变得稳定。
if(--Count == 0){
//计时器已过期 - 接受更改。
DebouncedKeyPress = RawState;
* Key_changed = true;
* Key_pressed = DebouncedKeyPress;
//并重置定时器。
if(DebouncedKeyPress)Count = RELEASE_MSEC / CHECK_MSEC;
else Count = PRESS_MSEC / CHECK_MSEC;
}
}

}


Do you have a simple debounce routine handy to deal with a single switch input?

This is a simple bare metal system without any OS.

I would like to avoid a looping construct with a specific count, as the processor speed might fluctuate.

解决方案

I think you could learn a lot about this here: http://www.ganssle.com/debouncing.pdf

Your best bet is always to do this in hardware if possible, but there are some thoughts on software in there as well.

Simple example code from TFA:

#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 allows a change from 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;
        }
    }

}

这篇关于简单的去抖动例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆