Don't like ads? PRO users don't see any ads ;-)

counter

By: ctqctq on May 8th, 2012  |  syntax: None  |  size: 0.97 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use ieee.std_logic_unsigned.all;
  4.  
  5. entity counter is
  6. generic(n: integer := 4);
  7.  
  8. port( clock:      in std_logic;
  9.       reset:      in std_logic;
  10.       UpDownCount: in std_logic;
  11.       Q:    out std_logic_vector(n-1 downto 0));
  12. end counter;
  13.  
  14. architecture Behavioral of counter is
  15.   --signal declaration
  16.   signal temp : std_logic_vector(3 downto 0);
  17.  
  18. Begin
  19.   Process(clock, reset, UpDownCount, temp) is
  20.     Begin
  21.       if reset = '1' then
  22.         temp <= "0000";
  23.       elsif (clock'event and clock = '1') then
  24.         if(UpDownCount = '1') then
  25.           if(temp = "1111") then
  26.             temp <= "0000";
  27.           else
  28.             temp <= temp + "0001";
  29.           end if;
  30.         else
  31.           if(temp = "0000") then
  32.             temp <= "1111";
  33.           else
  34.             temp <= temp - "0001";
  35.           end if;
  36.         end if;
  37.       end if;
  38.     end process;
  39.    
  40.     Q <= temp;
  41.   end Behavioral;