Im trying to use a debouncer for my project but when i test it out and click on a key it send a pulse when i click down and when i release a button. How can i fix this?
Here’s the code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity PulseDeBounce is
Port (
clk : in STD_LOGIC;
din : in STD_LOGIC;
dout : out STD_LOGIC
);
end PulseDeBounce;
architecture Behavioral of PulseDeBounce is
constant CYCLES : integer := 250_000; -- number of clock cycles for debouncing
signal stable_counter : integer := 0;
signal prev_din : STD_LOGIC := '0';
signal stable_din : STD_LOGIC := '0';
signal pulse : STD_LOGIC := '0';
signal pulse_gen : STD_LOGIC := '0';
begin
process(clk)
begin
if rising_edge(clk) then
if din = prev_din then
if stable_counter < CYCLES then
stable_counter <= stable_counter + 1;
end if;
else
stable_counter <= 0;
end if;
if stable_counter = CYCLES then
if din /= stable_din then
stable_din <= din;
pulse_gen <= '1';
else
pulse_gen <= '0';
end if;
else
pulse_gen <= '0';
end if;
prev_din <= din;
end if;
end process;
process(clk)
begin
if rising_edge(clk) then
if pulse_gen = '1' then
pulse <= '1';
else
pulse <= '0';
end if;
end if;
end process;
dout <= pulse;
end Behavioral;
I tried to increment/decrement the number of cycles but it does the same thing.
New contributor
Raquel Meira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.