This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

EK-TM4C123GXL的ButtonsPoll例程函数咨询

Other Parts Discussed in Thread: EK-TM4C123GXL

在阅读EK-TM4C123GXL关于FreeRTOS的例程时,有个地方没搞懂。下面这个函数用来检测按键状态,其中ui8SwitchClockA和ui8SwitchClockB的作用是什么?

该函数是否能实现软件去抖动,如何实现的?谢谢指导。

uint8_t
ButtonsPoll(uint8_t *pui8Delta, uint8_t *pui8RawState)
{
uint32_t ui32Delta;
uint32_t ui32Data;
static uint8_t ui8SwitchClockA = 0;
static uint8_t ui8SwitchClockB = 0;

//
// Read the raw state of the push buttons. Save the raw state
// (inverting the bit sense) if the caller supplied storage for the
// raw value.
//
ui32Data = (ROM_GPIOPinRead(BUTTONS_GPIO_BASE, ALL_BUTTONS));
if(pui8RawState)
{
*pui8RawState = (uint8_t)~ui32Data;
}

//
// Determine the switches that are at a different state than the debounced
// state.
//
ui32Delta = ui32Data ^ g_ui8ButtonStates;

//
// Increment the clocks by one.
//
ui8SwitchClockA ^= ui8SwitchClockB;
ui8SwitchClockB = ~ui8SwitchClockB;

//
// Reset the clocks corresponding to switches that have not changed state.
//
ui8SwitchClockA &= ui32Delta;
ui8SwitchClockB &= ui32Delta;

//
// Get the new debounced switch state.
//
g_ui8ButtonStates &= ui8SwitchClockA | ui8SwitchClockB;
g_ui8ButtonStates |= (~(ui8SwitchClockA | ui8SwitchClockB)) & ui32Data;

//
// Determine the switches that just changed debounced state.
//
ui32Delta ^= (ui8SwitchClockA | ui8SwitchClockB);

//
// Store the bit mask for the buttons that have changed for return to
// caller.
//
if(pui8Delta)
{
*pui8Delta = (uint8_t)ui32Delta;
}

//
// Return the debounced buttons states to the caller. Invert the bit
// sense so that a '1' indicates the button is pressed, which is a
// sensible way to interpret the return value.
//
return(~g_ui8ButtonStates);
}

  • 是可以实现软件的去抖动的。这个函数应当是放在定时器中的,具体的算法,我也没看懂。ui8SwitchClockB和ui8SwitchClockA 应当是前后两次扫描到的按键的键值,然后进行的比较吧。


  • 为了便于分析,假定有八个按键。

    这段代码通过抑或求变化状态,比较明显。就不做详细分析,主要是ui32Delta = ui32Data ^ g_ui8ButtonStates这一行代码。


    去抖动的实现通过ui8SwitchClockA 和 ui8SwitchClockB来实现。g_ui8ButtonStates和pui8Delta的最终返回值都需要通过A和B来mask。

    只有当A与B都为0x00时,二者状态才会改变。A和B都为0x00就必须要求ui32Delta=0x00,也就是相邻的采样按键状态保持一致。



  • 赞一个,要弄懂别人的算法,是很不容易的。学习了。