uas_arduino

Debouncing - Not Working Properly

May 16th, 2014
430
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.NUMERIC_STD.ALL;
  4.  
  5. entity LedCounter is
  6.     Port ( clk : in  STD_LOGIC;
  7.            rst : in  STD_LOGIC;
  8.               button : in STD_LOGIC;
  9.            leds : out  STD_LOGIC_VECTOR (7 downto 0));
  10. end LedCounter;
  11.  
  12. architecture Behavioral of LedCounter is
  13.  
  14.     -- Clocks since the button was last pressed
  15.     signal csp : unsigned(31 downto 0) := (others => '0');
  16.     -- Holds the value that will be assigned to the LEDs
  17.     signal leds_counter : unsigned(leds'range) := (others => '0');
  18.     -- Last sampled value of the button
  19.     signal last_button_value : std_logic := '0';
  20. begin
  21.  
  22.     -- Always assign LEDs the value of leds_counter
  23.     leds <= std_logic_vector(leds_counter);
  24.  
  25.     process(clk, rst)
  26.     begin
  27.         -- clear things
  28.         if(rst = '1') then
  29.             leds_counter <= (others => '0');
  30.             csp <= (others => '0');
  31.             -- Assume the button has not been pressed
  32.             last_button_value <= '0';
  33.         elsif(rising_edge(clk)) then
  34.             -- Check to make sure some time has passed since the button was pressed
  35.             if(csp > 100_000_000 / 5) then
  36.                 -- Check to see if the button is on the rising edge
  37.                 if(button = '1' and last_button_value = '0') then
  38.                     -- Increment the counter and reset the counts since last pressed
  39.                     leds_counter <= leds_counter + 1;
  40.                     csp <= (others => '0');
  41.                 end if;
  42.             else
  43.                 -- The max count has not been reached.  Increment.
  44.                 csp <= csp + 1;
  45.             end if;
  46.            
  47.             -- Always update the last value signal with the current value
  48.             last_button_value <= button;
  49.         end if;
  50.     end process;
  51.  
  52.  
  53.  
  54. --  process(clk, rst)
  55. --      variable c : unsigned(31 downto 0) := (others => '0');
  56. --  begin
  57. --      if(rst = '1') then
  58. --          c := (others => '0');
  59. --      elsif(rising_edge(clk)) then
  60. --          if(c > 100_000_000 / 10 and button = '1' and last_button_value = '0') then
  61. --              leds_counter <= leds_counter + 1;
  62. --              c := (others => '0');
  63. --          else
  64. --              if(c <= 100_000_000 / 4) then
  65. --                  c := c + 1;
  66. --              end if;
  67. --          end if;
  68. --      end if;
  69. --     
  70. --      last_button_value <= '0';
  71. --  end process;
  72.  
  73. end Behavioral;
RAW Paste Data