VHDL:Mealy状态机内的按钮反跳 [英] VHDL: button debounce inside a Mealy State Machine

查看:119
本文介绍了VHDL:Mealy状态机内的按钮反跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VHDL来实现一台粉餐机,但是我需要取消按钮的弹跳动作。我的问题是我不确定应该在哪里实施防抖。我当前的工作是这样的:

Hi I'm trying to implement a mealy machine using VHDL, but I'll need to debounce the button press. My problem is I'm not sure where should I implement the debouncing. My current work is like this:

process(clk)
begin
    if(clk' event and clk = '1') then
        if rst = '1' then
            curr_state <= state0;
        else
            curr_state <= next_state;
        end if;
    end if;
end process;

process(curr_state, op1,op0,rst)  --here op1,op0 and rst are all physical buttons and I need to debounce op1 and op0
begin
    if rst = '1' then
        ...some implementation
    else
        ...implement the debounce logic first
        ...process some input
        case curr_state is
            when state0=>...implementation
            ...similar stuff
        end case; 
end process;

我不确定自己的做法是否正确。在第二个过程中,应该像这样放置第一个处理,还是应该在state0块放置在里面?另外,由于反跳处理需要计数,因此我是否将其放在大小写框之外?谢谢!

I'm not sure whether I'm doing in the right way or not. In the second process, should I put the rst processing like this, or should I put it inside when state0 block? Also, as the processing of debounce requires counting, do I put it outside the case block like this? Thank you!

推荐答案

我将使用一个完全独立的代码块来消除任何按钮信号的反跳,从而使您的状态机过程能够集中精力

I would use a completely separate block of code to debounce any button signals, allowing your state machine process to focus on just the state machine, without having to worry about anything else.

您可以使用这样的过程来消除输入的抖动。在此示例中,您当然可以交换信号变量(以及相关的赋值运算符替换)。

You could use a process like this to debounce the input. You could of course exchange variables for signals in this example (with associated assignment operator replacements).

process (clk)
  constant DEBOUNCE_CLK_PERIODS : integer := 256;  -- Or whatever provides enough debouncing
  variable next_button_state : std_logic := '0';  -- Or whatever your 'unpressed' state is
  variable debounce_count : integer range 0 to DEBOUNCE_CLK_PERIODS-1 := 0;
begin
  if (rising_edge(clk)) then
    if (bouncy_button_in /= next_button_state) then
      next_button_state := bouncy_button_in;
      debounce_count := 0;
    else
      if (debounce_count /= DEBOUNCE_CLK_PERIODS-1) then
        debounce_count := debounce_count + 1;
      else
        debounced_button_out <= next_button_state;
      end if;
    end if;
  end if;
end process;

另一种选择是对 bouncy_button_in 进行采样

Another option would be to sample the bouncy_button_in at a slow rate:

process (clk)
  constant DEBOUNCE_CLK_DIVIDER : integer := 256;
  variable debounce_count : integer range 0 to DEBOUNCE_CLK_DIVIDER-1 := 0;
begin
  if (rising_edge(clk)) then
    if (debounce_count /= DEBOUNCE_CLK_DIVIDER-1) then
      debounce_count := debounce_count + 1;
    else
      debounce_count := 0;
      debounced_button_out <= bouncy_button_in;
    end if;
  end if;
end process;

第一种方法的优点是它将拒绝输入中的毛刺。无论哪种情况,您都可以在州内使用 debounced_button_out (或任何您想称呼的东西,也许是 rst

The advantage of the first method is that it will reject glitches in the input. In either case, you would use the debounced_button_out (or whatever you want to call it, perhaps rst) in your state machine, whose code then contains only the core state machine functionality.

如果您想进行更多的反跳操作,则可以使用另一个计数器为上述进程创建启用信号,有效地降低时钟频率。这可能比将除数常量设置为一个很高的数字更好,因为如果计数器超过一定大小,您可能无法满足计时要求。

If you wanted even more debouncing, you could use another counter to create an enable signal for the processes above, to effectively divide down the clock rate. This could be better than setting the division constant to a very high number, because you may not be able to meet timing if the counter gets beyond a certain size.

您可以甚至在单独的文件中创建防抖动实体,可以为每个按钮实例化该实体。

You could even create a debounce entity in a separate file, which could be instantiated for each button. It could have a generic for the constant in the above process.

也有硬件反弹功能,但我想这不在这个问题的范围内。

There's also hardware debouncing, but I suppose that's outside the scope of this question.

这篇关于VHDL:Mealy状态机内的按钮反跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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