Debouncing the Altera UP1 Board's Pushbutton
The following code will debounce the pushbutton by requiring that
the (active-low) "rawbutton" input be asserted for 4095 clock
periods before asserting the (active-high) "button" signal. At
8.333 MHz this adds about 500 microseconds of delay.
entity lab4 is
port (
rawbutton : in std_logic ; -- un-debounced input
clk : in std_logic -- 8.333 MHz ISA bus clock
) ;
end lab4 ;
architecture rtl of lab4 is
signal rawbuttoncount, nextrawbuttoncount : unsigned(11 downto 0) ;
begin
-- debounce button
nextrawbuttoncount <=
conv_unsigned(0,12) when rawbutton = '1' else
rawbuttoncount when rawbuttoncount = 4095 else
rawbuttoncount + 1 ;
button <=
'1' when rawbuttoncount = 4095 else
'0' ;
process(clk)
begin
if clk'event and clk ='1' then
rawbuttoncount <= nextrawbuttoncount ;
end if ;
end process ;
end rtl ;