Guest User

Four_Bit_Counter.vhd

a guest
Aug 25th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.41 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date:    09:25:59 23/08/2016
  6. -- Design Name:
  7. -- Module Name:    Four_Bit_Counter - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use IEEE.STD_LOGIC_ARITH.ALL;
  23. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  24.  
  25. entity Four_Bit_Counter is
  26. Port ( clk          : in STD_LOGIC;
  27.        reset        : in STD_LOGIC;
  28.        UP_DOWN      : in STD_LOGIC;
  29.        LOAD         : in STD_LOGIC;
  30.        AUTO_MANUAL  : in STD_LOGIC;
  31.        TICK         : in STD_LOGIC;
  32.        Value        : in STD_LOGIC_VECTOR(3 downto 0);
  33.        Port_Counter : out STD_LOGIC_VECTOR(3 downto 0));
  34.    
  35. end Four_Bit_Counter;
  36.  
  37. architecture Behavioral of Four_Bit_Counter is
  38. -- to store counter before assigning to Port_counter
  39. signal temp: STD_LOGIC_VECTOR(0 to 3);
  40.  
  41. signal clk_enable: STD_LOGIC;
  42. signal counter_enable: STD_LOGIC;
  43.  
  44. --for TICK pushbutton
  45. signal sync_r: STD_LOGIC;
  46. signal old_state_r: STD_LOGIC;
  47.  
  48. begin
  49.  
  50.     -- asynchronous process for LOAD pushbutton
  51.     process(LOAD) begin    
  52.         if (counter_enable = '0' and LOAD = '1') then
  53.             temp <= Value;
  54.         else
  55.             temp <= "0000";
  56.         end if;        
  57.     end process;
  58.  
  59. --    here "sync_r" is the name i gave to the first register (it does metastability synchronization)
  60. --    and old_state_r is the name of the second register (for storing the previous state of the signal
  61. --    to detect edges)
  62.     process (clk) begin  
  63.         if rising_edge(clk) then  
  64.             sync_r <= TICK;
  65.             old_state_r <= sync_r;
  66.             counter_enable <= sync_r and (not old_state_r);
  67.         end if;
  68.     end process;
  69.    
  70.     -- Counter Process
  71.     process(clk, reset) begin
  72.         if (rising_edge(clk)) then
  73.             if(reset = '1') then
  74.             --Do reset
  75.                 temp <= "0000";
  76.             else  
  77.                 if (AUTO_MANUAL = '1') then
  78.                 --Auto-counting                              
  79.                     if UP_DOWN = '1' then
  80.                         if temp < "1111" then
  81.                             temp <= temp + 1;
  82.                         else
  83.                             temp <= "1111";
  84.                         end if;          
  85.                     else
  86.                         if temp > "0000" then
  87.                             temp <= temp - 1;
  88.                         else
  89.                             temp <= "0000";
  90.                         end if;  
  91.                     end if;
  92.                    
  93.                 else
  94.                     --Manual Counting
  95.                    
  96.                 end if;              
  97.             end if;
  98.         end if;
  99.     end process;
  100.        
  101.     Port_Counter <= temp;
  102.    
  103.     --clock_enable process
  104. --    process
  105. --        variable count : natural; begin
  106. --        if rising_edge(clk) then
  107. --           clk_enable <= '0';
  108. --           count := count + 1;
  109. --           if count = 100000000/1 then --clock_freq/desired_freq
  110. --              clk_enable <= '1';
  111. --              count := 0;
  112. --           end if;
  113. --        end if;
  114. --    end process;
  115.  
  116. end Behavioral;
Add Comment
Please, Sign In to add comment