简介:1*10个按键,按下其中任何一个都要产生一个100ms长的控制电平,无论高低,只做为下一级的时长控制,我用它来控制发射信号的脉宽。
思考:因为按下任何一个按键,考虑抖动和人的机械动作,很容易超过100ms,所以只能通过按键按下 或弹起的下降或上升沿来 产生这个100ms的控制电平,但是按键 的按下或弹起 都会有大量的抖动,因此如果能将有抖动的下降沿 变为干净的下降沿 就能保证控制电平的产生,并严格控制为100ms,所以对按下按键过程的消抖 就很重要。
大家帮我看看 我写的代码 可行否 或者 我哪儿考虑欠妥!谢谢
library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity xiaodou is port( clk: in std_logic; reset : in std_logic; din : in std_logic; dout : out std_logic ); end; architecture one of xiaodou is type state is(s0,s1,s2,s3,s4,s5); signal current_state,next_state : state :=s0;
signal key_confirm : std_logic := '1'; signal delay_end : std_logic; signal cnt : integer :=0; begin process(reset,clk) begin if reset = '0' then current_state <= s0; elsif rising_edge(clk) then current_state <= next_state; else null; end if; end process;
process(current_state,next_state,din,clk) begin case current_state is when s0=>if din = '1' then --采到为高继续采 next_state <= s0; elsif din = '0' then--采到为低 转入下个状态 next_state <= s1; end if;
when s1=>if rising_edge(clk) then if cnt = 80 then--delay 8 ms clk is 1M cnt <= 0; delay_end <= '1'; else cnt <= cnt + 1; delay_end <= '0'; end if; end if; if delay_end = '1' then--开始延时 next_state <= s2; else next_state <= s1;--延时结束,转入下个状态 end if;
when s2=>if din = '0' then --第一次采到为低 转入下个状态 next_state <= s3; elsif din = '1' then next_state <= s0; end if;
when s3=>if din = '0' then --第二次采到为低 转入下个状态 next_state <= s4; elsif din='1' then next_state <= s0; end if;
when s4=>if din = '0' then --第三次采到为低 转入下个状态 next_state <= s5; elsif din='1' then next_state <= s0; end if; when s5=>key_confirm<='0'; if din = '0' then --按键未释放 next_state <= s5; elsif din='1' then--按键已释放 next_state <= s0; key_confirm<='1'; end if; end case; end process;
dout <= key_confirm;
end one;
功能仿真 din的输入模拟 抖动 但是dout的输出 并未出现下降沿 问题出在哪儿 我也不知道 望大家帮忙指点。谢谢
|