Advertisement
Guest User

Four_Bit_Couter.vhd

a guest
Aug 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.46 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(3 downto 0);
  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. signal count : natural;
  49.  
  50. begin
  51.    
  52.     -- Counter Process
  53.     process(clk, reset) begin
  54.        
  55.         if(LOAD = '1') then
  56.         --LOAD asynchronously into Port_counter
  57.             temp <= value;
  58.         elsif rising_edge(clk) then
  59.  
  60. --                if reset = '1' then
  61. --                    -- Do reset
  62. --                    temp <= "0000";
  63. --                    sync_r <= '0';
  64. --                    old_state_r <= '1';
  65. --                else
  66.                     if clk_enable = '1' then  
  67.                         --enabled at 1Hz rate without using another downscaled clock
  68.                         if (AUTO_MANUAL = '1') then
  69.                             --Auto-counting                              
  70.                             if UP_DOWN = '1' then
  71.                                 if temp < "1111" then
  72.                                     temp <= temp + 1;
  73.                                 else
  74.                                     temp <= "1111";
  75.                                 end if;          
  76.                             else
  77.                                 if temp > "0000" then
  78.                                     temp <= temp - 1;
  79.                                 else
  80.                                     temp <= "0000";
  81.                                 end if;  
  82.                             end if;
  83.                            
  84.                         else
  85.                             --Manual Counting
  86.                             if counter_enable = '1' then
  87.                                 if UP_DOWN = '1' then
  88.                                     if temp < "1111" then
  89.                                         temp <= temp + 1;
  90.                                     else
  91.                                         temp <= "1111";
  92.                                     end if;          
  93.                                 else
  94.                                     if temp > "0000" then
  95.                                         temp <= temp - 1;
  96.                                     else
  97.                                         temp <= "0000";
  98.                                     end if;  
  99.                                 end if;                                
  100.                             end if;
  101.                             --here "sync_r" is the name i gave to the first register (it does metastability synchronization)
  102.                             --and old_state_r is the name of the second register (for storing the previous state of the signal
  103.                             --to detect edges)
  104.                             sync_r <= TICK;
  105.                             old_state_r <= sync_r;
  106.                             counter_enable <= sync_r and (not old_state_r);
  107.                         end if;    
  108.                     end if;  
  109.                 end if;      
  110.  --        end if;
  111.     end process;
  112.        
  113.     Port_Counter <= temp;
  114.    
  115.     --clock_enable process
  116.     process begin
  117.    
  118.         wait until rising_edge(clk);
  119.            clk_enable <= '0';
  120.            count <= count + 1;
  121.            if count = 100000000/1 then --clock_freq/desired_freq
  122.               clk_enable <= '1';
  123.               count <= 0;
  124.            end if;
  125.        
  126.     end process;
  127.  
  128. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement