Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library IEEE;
- use IEEE.STD_LOGIC_1164.ALL;
- use IEEE.STD_LOGIC_ARITH.ALL;
- use IEEE.STD_LOGIC_UNSIGNED.ALL;
- entity Traffic is
- Port ( Reset : in STD_LOGIC;
- Clock : in STD_LOGIC;
- -- for debug
- debugLED : out std_logic;
- LEDs : out std_logic_vector(2 downto 0);
- -- Car and pedestrian buttons
- CarEW : in STD_LOGIC; -- Car on EW road
- CarNS : in STD_LOGIC; -- Car on NS road
- PedEW : in STD_LOGIC; -- Pedestrian moving EW (crossing NS road)
- PedNS : in STD_LOGIC; -- Pedestrian moving NS (crossing EW road)
- -- Light control
- LightsEW : out STD_LOGIC_VECTOR (1 downto 0); -- controls EW lights
- LightsNS : out STD_LOGIC_VECTOR (1 downto 0) -- controls NS lights
- );
- end Traffic;
- architecture Behavioral of Traffic is
- -- Encoding for lights
- constant RED : std_logic_vector(1 downto 0) := "00";
- constant AMBER : std_logic_vector(1 downto 0) := "01";
- constant GREEN : std_logic_vector(1 downto 0) := "10";
- constant WALK : std_logic_vector(1 downto 0) := "11";
- type StateType is (GreenEW, YellowEW, GreenNS, YellowNS);
- signal state, nextState : StateType;
- signal t : integer range 0 to 60;
- begin
- -- Show reset status on FPGA LED
- debugLed <= Reset;
- -- Threee LEDs for debug
- LEDs <= "000";
- -- Luck North-south traffic
- LightsEW <= RED;
- LightsNS <= WALK;
- sync: -- CLOCK
- process(Reset, clock)
- begin
- if (Reset = '1') then
- state <= GreenEW;
- elsif rising_edge(clock) then
- state <= nextState;
- end if;
- end process;
- comb:
- process(state, CarNS, CarEW, PedEW, PedNS, reset, t)
- begin
- case state is
- when GreenEW =>
- LightsEW <= GREEN;
- LightsNS <= RED;
- t <= '0';
- if rising_edge(clock) then
- t <= t+1;
- if ((t = 60) and (CarEW = '0') and (CarNS = '1') and (t = 59) and (PedEW = 0)) then
- nextState <= YellowEW;
- end if;
- when YellowEW =>
- LightsEW <= AMBER;
- LightsNS <= AMBER;
- nextState <= GreenNS;
- when GreenNS =>
- LightsEW <= RED;
- LightsNS <= GREEN;
- t <= '0';
- if rising_edge(clock) then
- t <= t+1;
- if ((t = 60) and (CarEW = '0') and (CarNS = '1') and (t = 59) and (PedEW = 0))then
- nextState <= YellowEW;
- end if;
- when YellowNS =>
- LightsEW <= AMBER;
- LightsNS <= AMBER;
- nextState <= GreenEW;
- end case;
- end process comb;
- end architecture Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement