【问题标题】:Switch case needs a little update开关盒需要一点更新
【发布时间】:2020-04-20 07:06:03
【问题描述】:

我正在使用开关盒构建一个去抖动计时器。

我输入了“案例 1”,但它在 if (in_1_timerTimeout) 之后不再继续,我不知道为什么。有人能弄清楚缺少什么吗?

到目前为止,我已经检查过代码存在“case 0”并输入“case 1”,但它停在那里。我感觉我可能将if (in_1_timerTimeout) 解释错了,所以我没有正确使用它。

#include "mcc_generated_files/mcc.h"
#include "stdbool.h"

unsigned long in_1_timer = 0;
unsigned long in_1_state = 0;

bool in_1_timerTimeout = false;
bool in_1 = false;


void myInterruptHandler(void)
{

}

void handleInputFilter(void)
{
    switch (in_1_state)
    {
        case 0:
            if (!in_button_GetValue())
            {
                in_1_timer        = 1000;
                in_1_timerTimeout = false;
                in_1_state        = 1;
            }
            break;

        case 1:
            if (!in_button_GetValue())
            { 
                //programmet kører hertil, men kommer ikke videre til næste "if"
                if (in_1_timerTimeout)
                { 
                    in_1_state       = 2;
                    in_1             = true;
                    //out_LED_SetHigh();
                }
            } else in_1_state     = 0;
            break;

        case 2:

            if (in_button_GetValue())
            {
                in_1_timer        = 1000;
                in_1_timerTimeout = false;
                in_1_state        = 3;
            }
            break; 

        case 3:

            if (in_button_GetValue())
            {
                if (in_1_timerTimeout)
                {
                    in_1_state        = 0;
                    in_1              = false;
                    //out_LED_SetLow();
                }
            } else in_1_state     = 2;
            break;
    }
}

void main(void)
{
    SYSTEM_Initialize();
    while (1)
    {
        handleInputFilter();
        // Add your application code
    }
}

【问题讨论】:

  • 在给定的上下文中,我看不到任何将 in_1_timerTimeout 设置为 true 的地方,因此,它总是会跳过 if (in_1_timerTimeout),因为它保持为 false。
  • 是的,我想通了,我没有完成整个计时器,所以正如你所说,我从来没有让 in_1_timerTimeout 真正中断。谢谢!
  • 很高兴能帮上忙,我会从我的评论中回答。

标签: switch-statement


【解决方案1】:

在给定的上下文中,我看不到任何将 in_1_timerTimeout 设置为 true 的地方。

因此,它将始终跳过 if (in_1_timerTimeout),因为它保持为 false。

我没有发现你的 switch 语句有任何问题,它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2023-03-16
    • 2012-04-23
    • 2017-04-06
    相关资源
    最近更新 更多