Engineering 15 Digital Systems

Lab 5: Sequential Logic

Alex Benn, Brian Park, Aron Dobos

21 November 2005

 

Task 1

 

Use one set of three red LED's that are arranged vertically to represent a red, yellow and green light of a traffic signal (the North-South road).  Use another set to represent another signal (the East-West road).  Let's assume the North-South road has heavier traffic.  Write VHDL code so:

 

We implemented a state machine to control two traffic lights at an intersection.  It was assumed that the North-South road generally had heavier traffic, so longer delays were used for that direction.  The state diagram is shown below.

 

Figure 1. State Transition Diagram for Task 1

 

The state machine was implemented in VHDL, and the commented code is listed below.  The clock frequency is 25.125 MHz, so to obtain the transition delays, the appropriate number of counts was calculated for the delay timer.

 

LIBRARY ieee;

USE ieee.std_logic_1164.all;

USE ieee.std_logic_unsigned.all;

USE ieee.std_logic_arith.all;

 

ENTITY task1 IS

      PORT (      Clk, Clr    :     IN    STD_LOGIC;

            red,yellow,green : out std_logic_vector(1 downto 0));

END task1;

 

ARCHITECTURE a OF task1 IS

      SIGNAL num : STD_LOGIC_VECTOR(27 DownTo 0);

      type state_type is(A,B,C,D);

      signal y : state_type;

BEGIN

      PROCESS (Clk, Clr)

      BEGIN

            IF Clr='0' THEN

                  num <= CONV_STD_LOGIC_VECTOR(0,28);

                  y<=A;

            ELSIF Clk'EVENT AND Clk='1' THEN

                  num<=num+1;

                  case y is

                        when A=>

                              red<="01";        --EW light red (0 is on)

                              green<="10";            --NS light green (0 is on)

                              yellow<="11";

                              if num=CONV_STD_LOGIC_VECTOR(201400000,28) then  --after 8s change state to B

                                    y<=B;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28); --reset count

                              end if;

                        when B=>

                              red<="01";        --EW light still red

                              green<="11";           

                              yellow<="10";           --NS light now yellow

                              if num=CONV_STD_LOGIC_VECTOR(50350000,28) then  --after 2s change state to C

                                    y<=C;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28);

                              end if;

                        when C=>

                              red<="10";        --NS light now red

                              green<="01";            --EW light now green

                              yellow<="11";

                              if num=CONV_STD_LOGIC_VECTOR(100700000,28) then --after 4s change state to D

                                    y<=D;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28);

                              end if;

                        when D=>

                              red<="10";        --NS light still red

                              green<="11";

                              yellow<="01";           --EW light now yellow

                              if num=CONV_STD_LOGIC_VECTOR(25175000,28) then  --after 1s go back to A

                                    y<=A;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28);

                              end if;

                  end case;

            end if;    

      end process;

end a;

 

Task 2

 

Since the North-South route was significantly busier, the state machine was modified so that the N-S light would be green unless a button was pressed, at which point the state machine would progress to the sequence where the East-West light turns green.   However, the state machine ensures that the North-South light is on for at least 8 seconds, so that a new car approaching the E-W intersection will not immediately cause the state machine to advance, which could potentially result in the N-S light never staying green for an adequate amount of time. The new state transition diagram is given below.

 

Figure 2. State Transition Diagram for Task 2.

LIBRARY ieee;

USE ieee.std_logic_1164.all;

USE ieee.std_logic_unsigned.all;

USE ieee.std_logic_arith.all;

 

ENTITY task2 IS

      PORT (      Clk, Clr,Sensor   :     IN    STD_LOGIC;

            red,yellow,green : out std_logic_vector(1 downto 0));

END task2;

 

ARCHITECTURE a OF task2 IS

      SIGNAL num : STD_LOGIC_VECTOR(27 DownTo 0);

      type state_type is(A,B,C,D);

      signal y : state_type;

BEGIN

      PROCESS (Clk, Clr)

      BEGIN

            IF Clr='0' THEN

                  num <= CONV_STD_LOGIC_VECTOR(0,28);

                  y<=A;

            ELSIF Clk'EVENT AND Clk='1' THEN

                  num<=num+1;

                  case y is

                        when A=>

                              red<="01";        --EW light red (0 is on)

                              green<="10";            --NS light green (0 is on)

                              yellow<="11";

                              if num>=CONV_STD_LOGIC_VECTOR(201400000,28) and Sensor='0' then --once car is sensed,  and the count has reached 8 seconds, start the sequence

                                    y<=B;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28); --reset count

                              end if;

                        when B=>

                              red<="01";        --EW light still red

                              green<="11";           

                              yellow<="10";           --NS light now yellow

                              if num=CONV_STD_LOGIC_VECTOR(50350000,28) then  --after 2s change state to C

                                    y<=C;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28);

                              end if;

                        when C=>

                              red<="10";        --NS light now red

                              green<="01";            --EW light now green

                              yellow<="11";

                              if num=CONV_STD_LOGIC_VECTOR(100700000,28) then --after 4s change state to D

                                    y<=D;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28);

                              end if;

                        when D=>

                              red<="10";        --NS light still red

                              green<="11";

                              yellow<="01";           --EW light now yellow

                              if num=CONV_STD_LOGIC_VECTOR(25175000,28) then  --after 1s go back to A

                                    y<=A;

                                    num <= CONV_STD_LOGIC_VECTOR(0,28);

                              end if;

                  end case;

            end if;    

      end process;

end a;