im in internship and my company wants that i learn vhdl for fpga. I'm using lattice diamond to code and compile my project and questasim for simulation. I also have a little board for training : MachXO3LF by Lattice.
I did a project : when i push a button a led is lighting for 2s and then fade. (works in simulation but but with the card because of the bouncing effect.
My mentor gave me a new project : write a code and a test bench to avoid the boucing effect and when i push the button, the led is lighting for 2s and when i push again the button there is no action for 10s.
I dont know how to avoid the bouncing effect with my starting code.... The bouncing effect is the results of false triggering or multiple triggering like the button is pressed multiple times
Here is my code
use ieee.std_logic_1164.ALL;
use ieee.numeric_std.ALL;
entity bouton_led_debounce is
Port (btn : in std_logic;
reset : in std_logic;
clock : in std_logic;
led : out std_logic
);
end bouton_led_debounce;
architecture Behavioral of bouton_led_debounce is
signal m1 : std_logic := '1';
signal m2 : std_logic:= '1';
signal int : std_logic:= '1';
signal buffer_led : std_logic := '1';
signal cptr : std_logic_vector (27 downto 0) := (others => '0');
begin
process(clock,reset)
begin
if(reset = '0') then
buffer_led <= '1';
m1 <= '1';
m2 <= '1';
int <= '1';
cptr <= (others => '0');
elsif rising_edge(clock) then
buffer_led <= '1';
m1 <= btn;
m2 <= m1;
int <= m2;
if( (int = '1') and (m2 = '0') ) then
cptr <= (others => '0');
cptr(0) <= '1';
buffer_led <= '0';
end if;
if( unsigned(cptr) /= 0) then
cptr <= std_logic_vector(unsigned(cptr)+1);
buffer_led <= '0';
end if;
if( unsigned (cptr) = (24000000-1) )then
cptr <= (others => '0');
end if;
end if;
end process;
led <= buffer_led;
end Behavioral;
And my test bench :
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity bouton_led_debounce_tb is
end bouton_led_debounce_tb;
architecture Behavioral of bouton_led_debounce_tb is
component bouton_led_debounce is
port( btn : in STD_LOGIC;
reset : in STD_LOGIC;
clock : in STD_LOGIC;
led : out STD_LOGIC);
end component;
signal btn : STD_LOGIC := '1' ;
signal led : STD_LOGIC;
signal reset : STD_LOGIC := '1';
signal clock : STD_LOGIC := '0';
begin
uut: bouton_led_debounce
port map(
btn => btn,
led => led,
clock => clock,
reset => reset);
btn_sti : process
begin
btn <= '1';
wait for 500 ns;
btn <= '0';
wait for 50 ns;
btn <= '1';
wait for 30 ns;
btn <= '0';
wait for 50 ns;
btn <= '1';
wait for 30 ns;
btn <= '0';
wait for 30 ns;
btn <= '1';
wait;
end process btn_sti;
rst_sti : process
begin
reset <= '1';
wait for 1 us;
reset <= '0';
wait for 1 us;
reset <= '1';
wait;
end process rst_sti;
clock_sti : process
begin
clock <= '0';
wait for 83.33 ns;
clock <= '1';
wait for 83.33 ns;
end process clock_sti;
end Behavioral;