Advertisement
Guest User

Untitled

a guest
Nov 19th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.54 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. -- Title      : Counter
  3. -- Project    :
  4. -------------------------------------------------------------------------------
  5. -- File       : counter.vhd
  6. -- Author     : Steffen Malkowsky  <Steffen@Steffens-MacBook-Air.local>
  7. -- Company    :
  8. -- Created    : 2014-06-14
  9. -- Last update: 2014-07-12
  10. -- Platform   :
  11. -- Standard   : VHDL'93/02
  12. -------------------------------------------------------------------------------
  13. -- Description: Counter for the stop watch
  14. -------------------------------------------------------------------------------
  15. -- Copyright (c) 2014 Lund University
  16. -------------------------------------------------------------------------------
  17. -- Revisions  :
  18. -- Date        Version  Author  Description
  19. -- 2014-06-14  1.0      Steffen Created
  20. -------------------------------------------------------------------------------
  21. library ieee;
  22. use ieee.std_logic_1164.all;
  23. use ieee.numeric_std.all;
  24.  
  25. entity Counter is
  26.  
  27.   port (
  28.     clk      : in  std_logic;
  29.     n_rst    : in  std_logic;
  30.     run      : in  std_logic;
  31.     set0     : in  std_logic;
  32.     sec_int  : out std_logic_vector(6 downto 0);
  33.     sec_frac : out std_logic_vector(6 downto 0));
  34.  
  35. end entity Counter;
  36.  
  37. architecture Counter_arch of counter is
  38.  
  39.   signal cnt_q, cnt_in : std_logic_vector(19 downto 0);
  40.   signal cnt_int_in, cnt_int_q : std_logic_vector(6 downto 0);
  41.   signal cnt_frac_in, cnt_frac_q : std_logic_vector(6 downto 0);
  42.  
  43. begin  -- architecture Counter_arch
  44.  
  45.   -- purpose: This process implements registers for saving the counter values
  46.   -- type   : sequential
  47.   -- inputs : clk, n_rst, cnt_in, cnt_int_in, cnt_frac_in
  48.   -- outputs: cnt_q, cnt_int_q, cnt_frac_q
  49.   reg : process (clk, n_rst) is
  50.   begin  -- process reg
  51.     if n_rst = '0' then                 -- asynchronous reset (active low)
  52.       cnt_q      <= (others => '0');
  53.       cnt_int_q  <= (others => '0');            
  54.       cnt_frac_q <= (others => '0');
  55.     elsif clk'event and clk = '1' then  -- rising clock edge
  56.       cnt_q      <= cnt_in;
  57.       cnt_int_q  <= cnt_int_in;
  58.       cnt_frac_q <= cnt_frac_in;
  59.     end if;
  60.   end process reg;
  61.  
  62. -- purpose: Implement the combinational part of the circuit.
  63. -- type   : combinational
  64. -- inputs : cnt_q, cnt_int_q, cnt_frac_q, run, set0
  65. -- outputs: cnt_in, cnt_int_in, cnt_frac_in
  66.   comb : process (cnt_q, cnt_int_q, cnt_frac_q, run, set0) is
  67.   begin  -- process comb
  68.     -- Default values
  69.     cnt_in      <= cnt_q;
  70.     cnt_int_in  <= cnt_int_q;
  71.     cnt_frac_in <= cnt_frac_q;
  72.  
  73.     -- If set0 is set, clear all counter values
  74.     if set0 = '1' then
  75.       cnt_in      <= (others => '0');
  76.       cnt_int_in  <= (others => '0');
  77.       cnt_frac_in <= (others => '0');
  78.     -- If run is set, count values
  79.     elsif run = '1' then
  80.       cnt_in <= std_logic_vector(unsigned(cnt_q)+1);
  81.       -- Counted 1/100s
  82.       if to_integer(unsigned(cnt_q)) = 999999 then
  83.         cnt_frac_in <= std_logic_vector(unsigned(cnt_frac_q)+1);
  84.         cnt_in      <= (others => '0');
  85.         -- Counted 1s
  86.         if to_integer(unsigned(cnt_frac_q)) = 99 then
  87.           if to_integer(unsigned(cnt_int_q)) < 99 then
  88.             cnt_int_in <= std_logic_vector(unsigned(cnt_int_q)+1);
  89.           else
  90.             cnt_int_in <= (others => '0');
  91.           end if;
  92.           cnt_frac_in <= (others => '0');
  93.         end if;
  94.       end if;
  95.     end if;
  96.   end process comb;
  97.  
  98.   -- Assign counter registers to output
  99.   sec_int <= cnt_int_q;
  100.   sec_frac <= cnt_frac_q;
  101.  
  102. end architecture Counter_arch;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement