Advertisement
uas_arduino

Debouncing - Testing

May 19th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 7.73 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_misc.all;
  4. use IEEE.NUMERIC_STD.ALL;
  5.  
  6. entity LedCounter is
  7.     Port ( clk : in  STD_LOGIC;
  8.            rst : in  STD_LOGIC;
  9.               button : in STD_LOGIC;
  10.            leds : out  STD_LOGIC_VECTOR (7 downto 0));
  11. end LedCounter;
  12.  
  13. architecture Behavioral of LedCounter is
  14.  
  15.     -- Clocks since the button was last pressed
  16.     signal csp : unsigned(31 downto 0) := (others => '0');
  17.     -- Holds the value that will be assigned to the LEDs
  18.     signal leds_counter : unsigned(6 downto 0) := (others => '0');
  19.     -- Last sampled value of the button
  20.     signal last_button_value : std_logic := '0';
  21.     signal countdown : unsigned(31 downto 0) := (others => '0');
  22.    
  23.     signal ffs : std_logic_vector(1 downto 0);
  24.     signal result : std_logic;
  25. begin
  26.  
  27.     process(result)
  28.     begin
  29.         if(rising_edge(result)) then
  30.             leds_counter <= leds_counter + 1;
  31.         end if;
  32.     end process;
  33.  
  34.     -- Always assign LEDs the value of leds_counter
  35.     leds(6 downto 0) <= std_logic_vector(leds_counter);
  36.     leds(7) <= '0';
  37. --  process(clk, rst)
  38. --      variable sr : std_logic_vector(3 downto 0) := (others => '0');
  39. --      constant sr_chk : std_logic_vector(sr'high-1 downto 0) := (others => '1');
  40. --  begin
  41. --      if(rst = '1') then
  42. --          leds_counter <= (others => '0');
  43. --          sr := (others => '0');
  44. --      elsif(rising_edge(clk)) then
  45. --          sr(sr'high downto 1) := sr (sr'high-1 downto 0);
  46. --          sr(0) := button;
  47. --          --if(sr(sr'high downto 1) = sr_chk and sr(0) = '0') then
  48. --          --if(sr(19 downto 0) = sr_chk and sr(20) = '0') then
  49. --          if(sr(sr'high-1 downto 0) = sr_chk and sr(sr'high) = '0') then
  50. --              leds_counter <= leds_counter + 1;
  51. --          end if;
  52. --      end if;
  53. --  end process;
  54.    
  55. --  process(clk)
  56. --      variable ffs : std_logic_vector(9 downto 0) := (others => '0');
  57. --      variable co : unsigned(10 downto 0) := (others => '0');
  58. --      variable a : std_logic;
  59. --  begin
  60. --      if(rising_edge(clk)) then
  61. --          ffs(ffs'high downto 1) := ffs(ffs'high - 1 downto 0);
  62. --          ffs(0) := button;
  63. --          a := xor_reduce(ffs);
  64. --          if(a = '1') then
  65. --              co := (others => '0');
  66. --          elsif(co(co'high) = '0') then
  67. --              co := co + 1;
  68. --          else
  69. --              if(ffs(1) = '1') then
  70. --                  leds_counter <= leds_counter + 1;
  71. --              end if;
  72. --              --result <= ffs(1);
  73. --          end if;
  74. --      end if;
  75. --  end process;
  76.  
  77. --  process(clk, rst)
  78. --      variable counter : unsigned(31 downto 0) := (others => '0');
  79. --      variable flipflops : std_logic_vector(3 downto 0) := (others => '0');
  80. --      constant max : unsigned(counter'range) := to_unsigned(100_000_000 / 100, counter'length);
  81. --  begin
  82. --      if(rst = '1') then
  83. --          counter := (others => '0');
  84. --          flipflops := (others => '0');
  85. --      elsif(rising_edge(clk)) then
  86. --          flipflops(flipflops'high downto 1) := flipflops(flipflops'high-1 downto 0);
  87. --          flipflops(0) := button;
  88. --         
  89. --          if(and_reduce(flipflops) = '1') then
  90. --              counter := to_unsigned(100_000_000 / 100, counter'length);
  91. --          elsif(counter > 0) then
  92. --              counter := counter - 1;
  93. --          else
  94. --              if(button = '1') then
  95. --                  leds_counter <= leds_counter + 1;
  96. --              end if;
  97. --          end if;
  98. --      end if;
  99. --  end process;
  100.  
  101. --  process(clk, rst)
  102. --      constant max : unsigned(31 downto 0) := to_unsigned(100_000_000/10, 32);
  103. --      variable counter : unsigned(31 downto 0) := max;
  104. --      variable last_button_state : std_logic := '0';
  105. --  begin
  106. --      if(rst = '1') then
  107. --          counter := max;
  108. --          last_button_state := '0';
  109. --      elsif(rising_edge(clk)) then
  110. --          if(counter > 0) then
  111. --              counter := counter - 1;
  112. --          elsif(button = '1' and last_button_state = '0') then
  113. --              counter := max;
  114. --              leds_counter <= leds_counter + 1;
  115. --          end if;
  116. --         
  117. --          last_button_state := button;
  118. --      end if;
  119. --  end process;
  120.  
  121.  
  122.     process(clk, rst)
  123.        
  124.         constant start : unsigned(31 downto 0) := to_unsigned(100_000_000 / 1000, 32);
  125.         variable counter : unsigned(start'range) := (others => '0');
  126.     begin
  127.         if(rst = '1') then
  128.             counter := (others => '0');
  129.             ffs <= (others => '0');
  130.         elsif(rising_edge(clk)) then
  131.             ffs(1) <= ffs(0);
  132.             ffs(0) <= button;
  133.            
  134.             if(ffs = "10" or ffs = "10") then
  135.                 counter := start;
  136.             elsif(counter > 0) then
  137.                 counter := counter - 1;
  138.             else
  139.                 result <= button;
  140.             end if;
  141.         end if;
  142.     end process;
  143.  
  144. --  PROCESS(clk)
  145. --  variable flipflops : std_logic_vector(1 downto 0) := (others => '0');
  146. --  variable counter_out : unsigned(27 downto 0) := (others => '0');
  147. --  --variable result : std_logic;
  148. --  variable counter_set : std_logic;
  149. --  BEGIN
  150. --  
  151. --    IF(clk'EVENT and clk = '1') THEN
  152. --      flipflops(0) := button;
  153. --      flipflops(1) :=  flipflops(0);
  154. --      counter_set := flipflops(0) xor flipflops(1);
  155. --      If(counter_set = '1') THEN                  --reset counter because input is changing
  156. --        counter_out :=  (OTHERS => '0');
  157. --      ELSIF(counter_out(counter_out'high) = '0') THEN --stable input time is not yet met
  158. --        counter_out :=  counter_out + 1;
  159. --      ELSE                                        --stable input time is met
  160. --        result <= flipflops(1);
  161. --      END IF;    
  162. --    END IF;
  163. --  END PROCESS;
  164.  
  165. --  process(clk, rst)
  166. --      variable ffs : std_logic_vector(1 downto 0) := (others => '0');
  167. --      variable counter_out : unsigned(19 downto 0) := (others => '0');
  168. --  begin
  169. --      if(rst = '1') then
  170. --          counter_out := (others => '0');
  171. --          ffs := (others => '0');
  172. --      elsif(rising_edge(clk)) then
  173. --          ffs(1) := ffs(0);
  174. --          ffs(0) := button;
  175. --          if(ffs(0) xor ffs(1) = '1') then
  176. --              counter_out := (others => '0');
  177. --          elsif(counter_out(counter_out'high) = '0') then
  178. --              counter_out := counter_out + 1;
  179. --          else
  180. --              leds_counter <= leds_counter + 1;
  181. --          end if;
  182. --      end if;
  183. --  end process;
  184.                
  185.  
  186. --  process(clk, rst)
  187. --      --variable countdown : unsigned(31 downto 0) := (others => '0');
  188. --  begin
  189. --      if(rst = '1') then
  190. --          countdown <= (others => '0');
  191. --          leds_counter <= (others => '0');
  192. --      elsif(rising_edge(clk)) then
  193. --          if(countdown /= 0) then
  194. --              countdown <= countdown - 1;
  195. --              leds(7) <= '0';
  196. --          else
  197. --              leds(7) <= '1';
  198. --              if(button = '1' and last_button_value = '0') then
  199. --                  countdown <= to_unsigned(100_000_000 / 100, countdown'length);
  200. --                  leds_counter <= leds_counter + 1;
  201. --              elsif(button = '0' and last_button_value = '1') then
  202. --                  countdown <= to_unsigned(100_000_000 / 100, countdown'length);
  203. --              end if;
  204. --             
  205. --             
  206. --          end if;
  207. --         
  208. --         
  209. --      end if;
  210. --  end process;
  211.  
  212. --  process(clk, rst)
  213. --  begin
  214. --      -- clear things
  215. --      if(rst = '1') then
  216. --          leds_counter <= (others => '0');
  217. --          csp <= (others => '0');
  218. --          -- Assume the button has not been pressed
  219. --          last_button_value <= '0';
  220. --      elsif(rising_edge(clk)) then
  221. --          -- Check to make sure some time has passed since the button was pressed
  222. --          if(csp > 100_000_000 / 5) then
  223. --              -- Check to see if the button is on the rising edge
  224. --              if(button = '1' and last_button_value = '0') then
  225. --                  -- Increment the counter and reset the counts since last pressed
  226. --                  leds_counter <= leds_counter + 1;
  227. --                  csp <= (others => '0');
  228. --              end if;
  229. --          else
  230. --              -- The max count has not been reached.  Increment.
  231. --              csp <= csp + 1;
  232. --          end if;
  233. --         
  234. --          -- Always update the last value signal with the current value
  235. --          last_button_value <= button;
  236. --      end if;
  237. --  end process;
  238.  
  239.  
  240.  
  241. --  process(clk, rst)
  242. --      variable c : unsigned(31 downto 0) := (others => '0');
  243. --  begin
  244. --      if(rst = '1') then
  245. --          c := (others => '0');
  246. --      elsif(rising_edge(clk)) then
  247. --          if(c > 100_000_000 / 10 and button = '1' and last_button_value = '0') then
  248. --              leds_counter <= leds_counter + 1;
  249. --              c := (others => '0');
  250. --          else
  251. --              if(c <= 100_000_000 / 4) then
  252. --                  c := c + 1;
  253. --              end if;
  254. --          end if;
  255. --      end if;
  256. --     
  257. --      last_button_value <= '0';
  258. --  end process;
  259.  
  260. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement