LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_Arith.ALL;
USE IEEE.STD_LOGIC_Unsigned.ALL;
ENTITY debounce IS
GENERIC(KEY_WIDTH:Integer:=8);
PORT(
clk: IN STD_LOGIC; --系统时钟输入
key_in: IN STD_LOGIC_VECTOR(KEY_WIDTH-1 DOWNTO 0); --外部按键输入
key_out:OUT STD_LOGIC_VECTOR(KEY_WIDTH-1 DOWNTO 0) --按键消抖输出
);
END;
ARCHITECTURE one OF debounce IS
SIGNAL dout1,dout2,dout3:STD_LOGIC_VECTOR(KEY_WIDTH-1 DOWNTO 0);
BEGIN
key_out<=dout1 OR dout2 OR dout3;--按键消抖输出
PROCESS(clk)
BEGIN
IF RISING_EDGE(clk)THEN
dout1<=key_in;
dout2<=dout1;
dout3<=dout2;
END IF;
END PROCESS;
END;
消抖电路的采样时钟要实际应用可以灵活改变,因为按键触发的时间一般为几百毫秒,干扰毛剌脉宽一般为几百微秒到几毫秒,所以采样时钟的周期一般为几毫秒。