Advertisement
Guest User

Untitled

a guest
May 6th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
4CS 3.51 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date:    22:19:55 02/26/2016
  6. -- Design Name:
  7. -- Module Name:    UpDownCounter - 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_UNSIGNED.ALL;
  23. use IEEE.NUMERIC_STD.ALL;
  24.  
  25. -- Uncomment the following library declaration if using
  26. -- arithmetic functions with Signed or Unsigned values
  27. --use IEEE.NUMERIC_STD.ALL;
  28.  
  29. -- Uncomment the following library declaration if instantiating
  30. -- any Xilinx primitives in this code.
  31. --library UNISIM;
  32. --use UNISIM.VComponents.all;
  33.  
  34. entity UpDownCounter is
  35.     Generic(   N : integer := 4;        -- Use of generics to make port declarations more flexible.
  36.                 M : integer := 10);     -- M is not used in this project, but will be used in further examples.
  37.     Port ( CLK, CLR, DIRECTION, ENABLE : in  STD_LOGIC;
  38.                 LED : out  STD_LOGIC_VECTOR(N-1 downto 0));
  39. end UpDownCounter;
  40.  
  41. architecture Behavioral of UpDownCounter is
  42. --  signal count: std_logic_vector(N-1 downto 0);   -- NOTE: if you uncomment this line, you should comment line 45, and
  43.                                                             -- uncomment line 70 and comment line 71. In this case,
  44.                                                             --  you don't need the package "IEEE.NUMERIC_STD.ALL" declared on line 23.
  45.     signal count: unsigned(N-1 downto 0) := (others => '0');                    -- Initialization is very important!
  46.     signal prescaler : std_logic_vector(26 downto 0) := (others => '0');    -- The prescaler is used to implement a clock divider.
  47. begin
  48.     countProc: process (CLK, CLR)
  49.     begin
  50.         if CLR ='1' then
  51.             count <= (others => '0');
  52.         else
  53. --          if (CLK'event and CLK = '1') then -- Tracks the transition from low to high in the clock signal. Similar to
  54. --          if rising_edge(CLK) then
  55. --          if (CLK'event and CLK = '0') then -- Tracks the transition from high to low in the clock signal. Similar to
  56.             if falling_edge(CLK) then          
  57. --              if prescaler = "101111101011110000100000000" then   -- checking if the prescaler has reached 100 000 000, 100Mhz.
  58.                 if prescaler = x"5F5E100" then  -- The same using Hexadecimal notation.
  59. --              if prescaler = "000101000000000000000000000" then   -- checking if the prescaler has reached 50 000 000, 50Mhz.
  60.                     prescaler <= (others => '0');                       -- If so, then we set it to zero, and
  61.                     if ENABLE = '0' and DIRECTION = '0' then        --  if ENABLE and DIRECTION are both '0',
  62.                         count <= count + 1;                             -- we increment the couter, else
  63.                     elsif ENABLE = '0' and DIRECTION = '1' then     -- if ENABLE is '0' and DIRECTION '1',
  64.                         count <= count - 1;                                 -- we decrement the counter.
  65.                     end if;
  66.                 else
  67.                     prescaler <= prescaler + 1;     -- Increment the prescaler case it is less then 100 000 000.
  68.                 end if;
  69.             end if;
  70.         end if;
  71.     end process;
  72. --  LED <= count;   -- Typecasting is not necessary if count is declared as "std_logic_vector", same type as LED. See line 42.
  73.     LED <= std_logic_vector(count);     -- Typecasting: converts from "unsigned" to "std_logic_vector".
  74.                                                 -- This is always necessary when connecting signals and ports of different types.
  75.  
  76. -- Note: In order to run the simulation succesfully, you must comment lines 57, 58, and 63.
  77. -- But it is important to leave those lines uncommented when implementing the desing.
  78. end Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement