使用按钮

时间:2015-04-24 13:18:00

标签: button vhdl debouncing

我是VHDL的新手,目前正致力于生成两种不同时钟速度的时钟发生器。 一切正常,除了在speed_status的慢速和快速之间切换。 "速度按钮"似乎存在问题。因为有时我不得不多次按它来改变当前的设定速度。 "重置按钮"总是按预期工作。我的VHDL代码有问题还是我需要添加某种软件去抖动?如果是这样,为什么重置按钮有效? 你能给我一些建议,我可以改进我的时钟发生器的哪些部分(代码/逻辑)?

entity clk_gen is
Port ( clk_in : in  STD_LOGIC;
       clk_btn_in : in STD_LOGIC_VECTOR (3 DOWNTO 0);
       clk_out : out  STD_LOGIC);
end clk_gen;

architecture Behavioral of clk_gen is
    signal temp : STD_LOGIC := '0';
    begin
    clock: process(clk_in,clk_btn_in)
        variable counter : integer range 0 to 49999999 := 0;
        constant freq_cnt_slow : integer := 49999999;
        constant freq_cnt_fast : integer := 4999999;
        type speed is (slow, fast);
        variable speed_status : speed := slow;
        begin
            if rising_edge(clk_in) then
                -- RESET BUTTON PRESSED
                if (clk_btn_in = "1000") then
                    temp <= '0';
                    counter := 0;
                    speed_status := slow;

                -- SPEED BUTTON
                elsif (clk_btn_in = "0100") then
                    if (speed_status = fast) then
                    speed_status:= slow;
                elsif (speed_status = slow) then
                     speed_status := fast;
                end if;
            end if;

            if ((counter = freq_cnt_fast) and (speed_status = fast)) then
                temp <= NOT(temp);
                counter := 0;
            elsif ((counter = freq_cnt_slow) and (speed_status = slow)) then
                temp <= NOT(temp);
                counter := 0;
            else
                counter := counter + 1;
            end if;

        end if;
    end process clock;

    clk_out <= temp;
end Behavioral;

我使用Xilinx ISE 13.4和基于Xilinx Virtex 5的XC5VLX110T。

1 个答案:

答案 0 :(得分:2)

看起来您的速度模式会在按钮按下的任何时候切换到&#39;州。除非你能保证你的按钮只是按下了“按钮”。在一个时钟周期内,状态很可能会多次切换,使按下按钮后的状态基本上是随机的(取决于按下按钮的确切时间)。

首先,您需要按钮上的去抖动电路。这可以在外部实现,也可以在FPGA内实现。我不会在这里详细介绍开关去抖动,但您可以在其他地方轻松找到相关信息。其次,您需要将按钮转换为同步信号,即与时钟具有固定关系的信号。您的示例的示例同步器将是:

signal button_reg1 : std_logic_vector(3 downto 0) := (others => '0');
signal button_reg2 : std_logic_vector(3 downto 0) := (others => '0');

...

process (clk_in)    
begin
    if (rising_edge(clk_in)) then
        button_reg2 <= button_reg1;
        button_reg1 <= clk_btn_in;
    end if;
end process;
然后

button_reg2将与时钟有固定的关系。 如果没有这个,你可能会违反speed_status寄存器上的建立/保持约束。最后,您需要将按下按钮转换为一个时钟周期的脉冲。例如:

signal speed_button_reg : std_logic := '0';
signal speed_button_pressed : std_logic := '0';

...

process (clk_in)    
begin
    if (rising_edge(clk_in)) then
        speed_button_reg <= button_reg2(2);
        if (speed_button_reg = '0' and button_reg2(2) = '1') then
            -- The button has just been pressed (gone from low to high)
            -- Do things here, or set another signal e.g.
            speed_button_pressed <= '1';
        else
            speed_button_pressed <= '0';
        end if;
    end if;
end process;
相关问题