如何正确实现STM32的按钮组合?

问题描述 投票:0回答:2

我想用STM32连接一些触觉按钮。然后基于按钮按压组合一段时间,我需要执行不同的功能。

我知道使用HAL_Delays将冻结程序,我不想这样做。我想定时器是要走的路。在那种情况下,我应该使用什么作为时间段。我应该轮询定时器计数器吗?什么是标准和无故障的方法?

c button embedded stm32
2个回答
1
投票

有许多可能性,但一种简单的方法是捕获按钮事件的时间,并将其与当前时间进行比较。

硬件密集型方法是将每个按钮连接到输入捕获计时器。然后将捕获按钮停机时间,并且其保持的时间是当前定时器值减去捕获时间。然后,您的应用程序可以确定每个按钮是否已关闭,这是多长时间。

但是,该方法需要每个按钮一个定时器捕获单元。更便宜的解决方案是将每个按钮连接到GPIO EXTI输入,并且每个按钮捕获按钮按下中断的systick时间。

在任何一种情况下,捕获时间的处理都是相同的。

伪代码:

int downTime( int button_id ) 
{
    int down_time = 0 ;

    // If the button is down, report how long it has been down
    if( buttonDown( button_id ) )
    {
        down_time = buttonTimerNow( button_id ) - buttonTimerCapture( button_id ) ;
    }

    return down_time ;
}

bool pressed( int button_id )
{
    // The button is pressed, if it has been down for 
    // longer than the switch bounce time.
    return downTime( button_id ) > DEBOUNCE_TIME ;
}

bool combinationPressed()
{
    // Test for the required combination of currently 
    // simultaneously pressed buttons.
    return pressed( BUTTON_A ) && pressed( BUTTON_B ) ;
}

© www.soinside.com 2019 - 2024. All rights reserved.