我想把一些触觉按钮和STM32连接起来。然后基于某个时间段的按钮按下组合,我需要执行不同的功能。
我知道使用HAL_Delays会冻结程序,我不想这么做。我认为定时器是最好的选择。在这种情况下,我应该使用什么作为时间段。我应该轮询计时器计数器吗?做这件事的标准和无错误的方法是什么?
发布于 2019-04-16 20:19:20
有许多可能性,但一个简单的方法是捕获按钮按下事件的时间,并将其与当前时间进行比较。
硬件密集型方法是将每个按钮连接到输入捕获计时器。然后将捕获按钮按下时间,并且其被保持的时间是当前计时器值减去捕获时间。然后,您的应用程序可以确定按下的每个按钮的持续时间。
然而,对于每个按钮,该方法需要一个定时器捕获单元。另一种成本较低的解决方案是将每个按钮连接到GPIO EXTI输入,并针对每个按钮捕获按下中断时的系统操纵杆时间。
在这两种情况下,对捕获时间的处理都是相同的。
伪码:
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 ) ;
}发布于 2019-04-17 06:12:32
https://stackoverflow.com/questions/55692049
复制相似问题