Guest User

Untitled

a guest
Sep 12th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 14.29 KB | None | 0 0
  1. LIBRARY ieee;
  2. USE ieee.std_logic_1164.ALL;
  3. USE ieee.numeric_std.ALL;
  4.  
  5. --
  6. -- 7-segment display driver. It displays a 4-bit number on 7-segments
  7. -- This is created as an entity so that it can be reused many times easily
  8. --
  9.  
  10. ENTITY SevenSegment IS PORT (
  11.    
  12.    dataIn      :  IN  std_logic_vector(3 DOWNTO 0);   -- The 4 bit data to be displayed
  13.    blanking    :  IN  std_logic;                      -- This bit turns off all segments
  14.    
  15.    segmentsOut :  OUT std_logic_vector(6 DOWNTO 0)    -- 7-bit outputs to a 7-segment
  16. );
  17. END SevenSegment;
  18.  
  19. ARCHITECTURE Behavioral OF SevenSegment IS
  20.  
  21. --
  22. -- The following statements convert a 4-bit input, called dataIn to a pattern of 7 bits                                                                                                                                                                                                                                                                                                                                                                                                                            
  23. -- The segment turns on when it is '0' otherwise '1'
  24. -- The blanking input is added to turns off the all segments
  25. --
  26.  
  27. BEGIN
  28.  
  29.    with blanking & dataIn SELECT --  gfedcba        b3210      -- D7S
  30.       segmentsOut(6 DOWNTO 0) <=    "1000000" WHEN "00000",    -- [0]
  31.                                     "1111001" WHEN "00001",    -- [1]
  32.                                     "0100100" WHEN "00010",    -- [2]      +---- a ----+
  33.                                     "0110000" WHEN "00011",    -- [3]      |           |
  34.                                     "0011001" WHEN "00100",    -- [4]      |           |
  35.                                     "0010010" WHEN "00101",    -- [5]      f           b
  36.                                     "0000010" WHEN "00110",    -- [6]      |           |
  37.                                     "1111000" WHEN "00111",    -- [7]      |           |
  38.                                     "0000000" WHEN "01000",    -- [8]      +---- g ----+
  39.                                     "0010000" WHEN "01001",    -- [9]      |           |
  40.                                     "0001000" WHEN "01010",    -- [A]      |           |
  41.                                     "0000011" WHEN "01011",    -- [b]      e           c
  42.                                     "0100111" WHEN "01100",    -- [c]      |           |
  43.                                     "0100001" WHEN "01101",    -- [d]      |           |
  44.                                     "0000110" WHEN "01110",    -- [E]      +---- d ----+
  45.                                     "0001110" WHEN "01111",    -- [F]
  46.                                     "1111111" WHEN OTHERS;     -- [ ]
  47.  
  48. END Behavioral;
  49.  
  50. --------------------------------------------------------------------------------
  51. -- Main entity
  52. --------------------------------------------------------------------------------
  53.  
  54. LIBRARY ieee;
  55. USE ieee.std_logic_1164.ALL;
  56. USE ieee.numeric_std.ALL;
  57.  
  58. ENTITY Lab4B IS
  59.    PORT(
  60.      
  61.       clock_50                  : IN  STD_LOGIC;
  62.       sw                        : IN  STD_LOGIC_VECTOR(17 DOWNTO 0); -- 18 dip switches on the board
  63.  
  64.       ledr                      : OUT STD_LOGIC_VECTOR(17 DOWNTO 0); -- LEDs, many Red ones are available
  65.       ledg                      : OUT STD_LOGIC_VECTOR( 8 DOWNTO 0); -- LEDs, many Green ones are available
  66.       hex0, hex2, hex4, hex6 : OUT STD_LOGIC_VECTOR( 6 DOWNTO 0)  -- seven segments to display numbers
  67. );
  68. END Lab4B;
  69.  
  70. ARCHITECTURE SimpleCircuit OF Lab4B IS
  71.  
  72. --------------------------------------------------------------------------------
  73. -- In order to use the "SevenSegment" entity, we should declare it with first
  74. --------------------------------------------------------------------------------
  75.  
  76.    COMPONENT SevenSegment PORT(        -- Declare the 7 segment component to be used
  77.       dataIn      : IN  STD_LOGIC_VECTOR(3 DOWNTO 0);
  78.       blanking    : IN  STD_LOGIC;
  79.       segmentsOut : OUT STD_LOGIC_VECTOR(6 DOWNTO 0)
  80.    );
  81.    END COMPONENT;
  82.  
  83. ----------------------------------------------------------------------------------------------------
  84.    CONSTANT CLK_DIV_SIZE: INTEGER := 25;     -- size of vectors for the counters
  85.  
  86.    SIGNAL Main1HzCLK:   STD_LOGIC;      -- main 1Hz clock to drive FSM
  87. -- SIGNAL OneHzBinCLK:  STD_LOGIC;      -- binary 1 Hz clock
  88.    SIGNAL OneHzModCLK , TenHzModCLK:  STD_LOGIC;
  89.  
  90. -- SIGNAL bin_counter:  UNSIGNED(CLK_DIV_SIZE-1 DOWNTO 0) := to_unsigned(0,CLK_DIV_SIZE); -- reset binary counter to zero
  91.    SIGNAL mod_counter:  UNSIGNED(CLK_DIV_SIZE-1 DOWNTO 0) := to_unsigned(0,CLK_DIV_SIZE); -- reset modulus counter to zero
  92.    SIGNAL mod_terminal: UNSIGNED(CLK_DIV_SIZE-1 DOWNTO 0) := to_unsigned(0,CLK_DIV_SIZE); -- reset terminal count of modulus counter to zero
  93.    
  94.    TYPE STATES IS (STATE0, STATE1, STATE2, STATE3, STATE4, STATE5);   -- list all the STATES
  95.    SIGNAL state, next_state:  STATES;                 -- current and next state signals od type STATES
  96.    
  97.    SIGNAL state_number: STD_LOGIC_VECTOR(3 DOWNTO 0); -- binary state number to display on seven-segment
  98.  
  99.    SIGNAL state_counter: UNSIGNED(3 DOWNTO 0);        -- binary state counter to display on seven-segment
  100.      
  101.    SIGNAL nightMode:                STD_LOGIC;
  102.    SIGNAL defaultSide:          STD_LOGIC;
  103.    SIGNAL sensorNS:                 STD_LOGIC;
  104.    SIGNAL sensorEW:                 STD_LOGIC;
  105.    SIGNAL isCarDetected:            STD_LOGIC;
  106.    SIGNAL timer:                UNSIGNED(3 downto 0) := "0000";     -- counter for changes in light
  107.    SIGNAL RedNS:                            STD_LOGIC;                  -- red light for NS
  108.    SIGNAL RedEW:                            STD_LOGIC;                  -- red light for EW
  109.    SIGNAL GreenNS:                      STD_LOGIC;                  -- green light for NS
  110.    SIGNAL GreenEW:                      STD_LOGIC;                  -- green light for EW
  111.    SIGNAL waitCounterNS              UNSIGNED(3 downto 0) := "0000";       -- waitCounter for NS
  112.    SIGNAL waitCounterEW              UNSIGNED(3 downto 0) := "0000";       -- waitCounter for EW
  113.  
  114.    
  115.    SIGNAL onemod_counter, tenmod_counter:    UNSIGNED(CLK_DIV_SIZE-1 DOWNTO 0) := to_unsigned(0,CLK_DIV_SIZE);
  116. ----------------------------------------------------------------------------------------------------
  117.  
  118. BEGIN
  119.    WITH sw(2 DOWNTO 0) SELECT -- terminal count for modulus counter for F0= 50 MHz clock input (T0 = 20 ns)
  120.       mod_terminal <= "1011111010111100000111111" WHEN "000",  -- F =   1 Hz, T/2 = 25000000 * T0
  121.                       "0010011000100101100111111" WHEN "001",  -- F =   5 Hz, T/2 =  5000000 * T0
  122.                       "0001001100010010110011111" WHEN "010",  -- F =  10 Hz, T/2 =  2500000 * T0
  123.                       "0000000111101000010001111" WHEN "011",  -- F = 100 Hz, T/2 =   250000 * T0
  124.                       "1011111010111100000111111" WHEN OTHERS; -- *** default ***
  125.    
  126.    ModTenClk: PROCESS(clock_50)
  127.    BEGIN
  128.       IF (rising_edge(clock_50)) THEN                       -- modulus counter increments on rising clock edge
  129.          IF (tenmod_counter = "0001001100010010110011111") THEN             -- 2499999 (Terminal Value for reset))
  130.             tenmod_counter <= to_unsigned(0,CLK_DIV_SIZE);              -- reset counter
  131.             TenHzModCLK <= NOT TenHzModCLK;                                 -- toggle clock
  132.          ELSE
  133.             tenmod_counter <= tenmod_counter + 1;                   -- increment clock
  134.          END IF;
  135.       END IF;
  136.    END PROCESS;
  137. ----------------------------------------------------------------------------------------------------
  138.  
  139.    ModOneClk: PROCESS(TenHzModCLK)
  140.    BEGIN
  141.       IF (rising_edge(TenHzModCLK)) THEN                        -- modulus counter increments on rising clock edge
  142.          IF (onemod_counter = "0000000000000000000000101") THEN             -- half period
  143.             OneHzModCLK <= NOT OneHzModCLK;                                 -- toggle
  144.             onemod_counter <= to_unsigned(0,CLK_DIV_SIZE);              -- reset counter
  145.          ELSE
  146.             onemod_counter <= onemod_counter + 1;
  147.          END IF;
  148.       END IF;
  149.    END PROCESS;
  150.    
  151. ------------------------------------------------------------------------------------------------------         
  152.    sensorNS     <= sw(15);
  153.    sensorEW     <= sw(14);
  154.    nightMode    <= sw(17);
  155.    defaultSide  <= sw(16);
  156.    
  157.    ledr(11)      <= RedNS;      -- RedNS is assigned to ledr11
  158.    ledg(8)   <= GreenNS;        -- GreenNS is assigned to ledg8
  159.    ledr(0)       <= RedEW;      -- RedEW is assigned to ledr0
  160.    ledg(7)       <= GreenEW;        -- GreenEW is assigned to ledg7
  161. ------------------------------------------------------------------------------------------------------
  162.  
  163. --   BinCLK: PROCESS(clock_50)
  164. --   BEGIN
  165. --      IF (rising_edge(clock_50)) THEN -- binary counter increments on rising clock edge
  166. --         bin_counter <= bin_counter + 1;
  167. --      END IF;
  168. --   END PROCESS;
  169. --   OneHzBinCLK <= std_logic(bin_counter(CLK_DIV_SIZE-1)); -- binary counter MSB
  170. --   LEDG(2) <= OneHzBinCLK;
  171. ------------------------------------------------------------------------------------------------------     
  172.  
  173.     FSM: PROCESS(state, sw)
  174.     BEGIN
  175.    
  176.     IF ((sensorNS='1' AND defaultSide='1') OR (sensorEW='1' AND defaultSide='0') ) THEN   -- Condition to check if a car is detected on the Non-Default Side
  177.         isCarDetected <= '1';                                  -- If detected, assign '1' to isCarDetected variable
  178.     ELSE
  179.         isCarDetected <= '0';                                  -- Else, '0'
  180.     END IF;
  181.  
  182.        IF (nightMode ='1' AND isCarDetected = '0') THEN                       -- Check if OperationMode = nightMode and a car is detected on ND side   
  183.         next_state  <=STATE0; -- Day Mode                         -- If yes, then go to State 0 (Day Mode)
  184.        ELSE
  185.         next_state  <=STATE3; --Green-Amber (Red Flashing) on Default Side        -- If no, then go to State 3 (Green Amber flashing) on Default Side
  186.        END IF;
  187.        
  188.     CASE state IS
  189.         WHEN STATE0 =>
  190.         state_number <= "0000";
  191.         next_state <= STATE1;
  192.  
  193.         IF (defaultSide = '1') THEN         -- DefaultSide: EW
  194.             RedNS       <='1';          -- RedNS ON
  195.             GreenNS     <='0';          -- Green NS OFF
  196.             RedEW       <='0';          -- RedEW OFF
  197.             GreenEW     <=TenHzModCLK;      -- GreenNS Flashing
  198.  
  199.         ELSE                        -- DefaultSide:  NS
  200.             RedNS       <='0';          -- RedNS OFF
  201.             GreenNS     <=TenHzModCLK;      -- GreenNS Flashing
  202.             RedEW       <='1';          -- RedEW ON
  203.             GreenEW     <='0';          -- GreenEW
  204.         END IF;
  205.            
  206.         WHEN STATE1 =>
  207.         state_number <= "0001";
  208.         next_state <= STATE2;
  209.  
  210.         IF (DefaultSide = '1') THEN         -- DefaultSide: EW
  211.             RedNS       <='1';          -- RedNS ON
  212.             GreenNS     <='0';          -- GreenNS OFF
  213.             RedEW       <='0';          -- RedEW OFF           
  214.             GreenEW     <='1';          -- GreenEW ON  
  215.            
  216.         ELSE                        -- DefaultSide: NS
  217.             RedNS       <='0';          -- RedNS OFF
  218.             GreenNS     <='1';          -- GreenNS ON
  219.             RedEW       <='1';          -- RedEW ON
  220.             GreenEW     <='0';          -- GreenEW OFF
  221.         END IF;
  222.                
  223.         WHEN STATE2 =>
  224.         state_number <= "0010";    
  225.         IF (DefaultSide = '1') THEN         -- DefaultSide: EW
  226.             RedNS       <='1';          -- RedNS ON
  227.             GreenNS     <='0';          -- GreenNS OFF
  228.             RedEW       <=TenHzModCLK;      -- RedEW Flashing
  229.             GreenEW     <='0';          -- GreenEW OFF
  230.         ELSE
  231.             RedNS       <=TenHzModCLK;      -- RedNS Flashing
  232.             GreenNS     <='0';          -- GreenNS OFF
  233.             RedEW       <='1';          -- RedEW ON
  234.             GreenEW     <='0';          -- GreenEW OFF
  235.         END IF;
  236.  
  237.         WHEN STATE3 =>
  238.         state_number <= "0011";
  239.         next_state <= STATE4;      
  240.  
  241.         IF (DefaultSide = '1') THEN         -- DefaultSide: EW
  242.             RedNS       <='0';          -- RedNS OFF
  243.             GreenNS     <=TenHzModCLK;      -- GreenNS Flashing
  244.             RedEW       <='1';          -- RedEW ON
  245.             GreenEW     <='0';          -- GreenEW Flashing
  246.                    
  247.         ELSE                        -- DefaultSide: NS
  248.             RedNS       <='1';          -- RedNS ON
  249.             GreenNS     <='0';          -- GreenNS OFF
  250.             RedEW       <='0';          -- RedEW OFF
  251.             GreenEW     <=TenHzModCLK;      -- GreenEW Flashing
  252.              
  253.         END IF;
  254.            
  255.         WHEN STATE4 =>
  256.         state_number <= "0100" ;
  257.         next_state  <=STATE5;
  258.                
  259.         IF (DefaultSide = '1') THEN         -- DefaultSide: EW
  260.             RedNS       <='0';          -- RedNS OFF
  261.             GreenNS     <='1';          -- GreenNS ON
  262.             RedEW       <='1';          -- RedEW ON
  263.             GreenEW     <='0';          -- GreenEW OFF
  264.  
  265.         ELSE                        -- DefaultSide: NS
  266.             RedNS       <='1';          -- RedNS ON
  267.             GreenNS     <='0';          -- GreenNS OFF
  268.             RedEW       <='0';          -- RedEW OFF
  269.             GreenEW     <='1';          -- GreenEW ON
  270.         END IF;
  271.            
  272.         WHEN STATE5 =>
  273.         state_number <= "0101";
  274.         next_state <= STATE0;  
  275.  
  276.         IF (DefaultSide = '1') THEN         -- DefaultSide: EW
  277.             RedNS       <=TenHzModCLK;      -- RedNS Flashing
  278.             GreenNS     <='0';          -- GreenNS OFF
  279.             RedEW       <='1';          -- RedEW ON
  280.             GreenEW     <='0';          -- GreenEW OFF
  281.  
  282.         ELSE                        -- DefaultSide: NS
  283.             RedNS       <='1';          -- RedNS ON
  284.             GreenNS     <='0';          -- GreenNS OFF
  285.             RedEW       <=TenHzModCLK;      -- RedEW Flashing
  286.             GreenEW     <='0';          -- GreenEW O
  287.         END IF;
  288.     END CASE;
  289.     END PROCESS;
  290.    
  291. ------------------------------------------------------------------------------------------------------
  292. SeqLogic: PROCESS (OneHzModCLK, state)
  293.     BEGIN
  294.    
  295.         IF(rising_edge(OneHzModCLK)) THEN
  296.  
  297.         timer <= timer +1;              -- on rising edge of clock, increment timer by 1
  298.  
  299.         state_counter <= state_counter + 1;         -- on rising edge of clock, increment state_counter by 1
  300.            
  301.             IF(timer = "0001") THEN     -- at 1.0s, go to next state
  302.                 state <= next_state;
  303.                
  304.             ELSIF(timer = "0101") THEN      -- at 5.0s, go to next state
  305.                 state <= next_state;       
  306.                
  307.             ELSIF(timer = "0111") THEN      -- at 7.0s, go to next state
  308.                 state <= next_state;
  309.                
  310.             ELSIF(timer = "1001") THEN      -- at 9.0s, go to next state
  311.                 state <= next_state;
  312.                
  313.             ELSIF(timer = "1101") THEN      -- at 13.0s, go to next state
  314.                 state <= next_state;
  315.                
  316.             ELSIF(timer = "1111") THEN      -- at 15.0s, go to next state
  317.                 state <= next_state;
  318.                
  319.             timer <= "0000";            -- now, reset the timer, so that at 15.0s, next state is state0
  320.             state_counter <= "0000";        -- reset the counter as well, so another new sequence can be started
  321.  
  322.                IF (waitCounterNS = '1' AND redNS = '1') THEN
  323.                 waitCounterNS <= waitCounterNS + 1;
  324.                ELSE
  325.                     waitCounterNS <= "0000";
  326.                END IF;
  327.  
  328.                IF (waitCounterEW = '1' AND redEW = '1') THEN
  329.                 waitCounterEW <= waitCounterEW + 1;
  330.                ELSE
  331.                     waitCounterEW <= "0000";
  332.                END IF;
  333.  
  334.  
  335.             END IF;
  336.         END IF;
  337.     END PROCESS;
  338. ------------------------------------------------------------------------------------------------------
  339.    D7S0: SevenSegment PORT MAP( state_number, '0', hex0 );              -- Show state number
  340.    D7S2: SevenSegment PORT MAP( std_logic_vector(state_counter), '0', hex2 );   -- Show timer
  341.    D7S4: SevenSegment PORT MAP( std_logic_vector(waitCounterNS), '0', hex4 );      -- Show wait counter for NS
  342.    D7S6: SevenSegment PORT MAP( std_logic_vector(waitCounterEW), '0', hex6 );   -- Show wait counter for EW
  343.    
  344.    
  345. END SimpleCircuit;
Add Comment
Please, Sign In to add comment