Design a simple traffic light controller ( 3 states ) in VHDL. The timing is configurable...
Assistant
User
Add a 'turn' phase , to the lamp cycle.. It's a green with a fliter to allow a left turn... It's active before the main green...
Assistant
A simple traffic light controller in VHDL with 3 states (red, yellow, green) and configurable timing can be achieved by creating a finite state machine (FSM). The following is a VHDL example of such a controller. The timer values for each state are provided as generic parameters, which allows you to change the timing without modifying the internal logic.
vhdl
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity traffic_light_controller is
generic (
RED_DURATION : natural := 10; -- Time for red light (in clock cycles)
YELLOW_DURATION : natural := 2; -- Time for yellow light (in clock cycles)
GREEN_DURATION : natural := 10 -- Time for green light (in clock cycles)
);
port (
clk : in std_logic; -- Clock input
reset : in std_logic; -- Asynchronous reset input
red : out std_logic; -- Red light output
yellow : out std_logic; -- Yellow light output
green : out std_logic -- Green light output
);
end traffic_light_controller;
architecture fsm of traffic_light_controller is
type state_type is (RED, YELLOW, GREEN);
signal state, next_state : state_type := RED;
signal timer : natural := 0; -- Timer for state duration
begin
-- State transition process
state_transition: process(clk, reset)
begin
if reset = '1' then
state <= RED;
timer <= 0;
elsif rising_edge(clk) then
state <= next_state;
-- Timer handling for state durations
if timer > 0 then
timer <= timer - 1;
else
case state is
when RED =>
if next_state = GREEN then
timer <= GREEN_DURATION - 1;
end if;
when YELLOW =>
if next_state = RED then
timer <= RED_DURATION - 1;
end if;
when GREEN =>
if next_state = YELLOW then
timer <= YELLOW_DURATION - 1;
end if;
when others =>
timer <= 0;
end case;
end if;
end if;
end process state_transition;
-- Next state logic process
next_state_logic: process(state, timer)
begin
case state is
when RED =>
if timer = 0 then
next_state <= GREEN;
else
next_state <= RED;
end if;
when YELLOW =>
if timer = 0 then
next_state <= RED;
else
next_state <= YELLOW;
end if;
when GREEN =>
if timer = 0 then
next_state <= YELLOW;
else
next_state <= GREEN;
end if;
when others =>
next_state <= RED;
end case;
end process next_state_logic;
-- Output logic process
output_logic: process(state)
begin
-- Turn off all lights initially
red <= '0';
yellow <= '0';
green <= '0';
-- Turn on the appropriate light
case state is
when RED =>
red <= '1';
when YELLOW =>
yellow <= '1';
when GREEN =>
green <= '1';
when others =>
red <= '1'; -- Fail-safe state
end case;
end process output_logic;
end fsm;
In this example:
You have an entity traffic_light_controller with three generics: RED_DURATION, YELLOW_DURATION, and GREEN_DURATION. These are the duration for each light in terms of clock cycles.
The state_type declares the possible states. The state signal keeps track of the current state, while next_state is used to keep track of what the next state should be.
Two processes are running simultaneously: state_transition to manage state changes and the timer, and next_state_logic is to determine the next state based on the current state and timer value.
output_logic is a process to turn on the corresponding light based on the current state.
The reset signal is used to asynchronously reset the state machine to the initial RED state.
Keep in mind that for this code to work properly, it needs to be part of a project that includes a clock signal source component, and it should be synthesized onto an FPGA or simulated in a VHDL simulator with the appropriate clock frequency. The clock frequency and cycle counts determine the actual duration of the traffic light states in real-world time.
Adding a "turn" phase to the traffic light controller in VHDL can be done by introducing a new state for the "turn" light (let's call it TURN_GREEN). This "turn" phase might allow vehicles to make a protected left turn before the main green phase. We also need to define an additional generic parameter for the duration of the turn green phase.
vhdl
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity traffic_light_controller is
generic (
RED_DURATION : natural := 10; -- Time for red light (in clock cycles)
YELLOW_DURATION : natural := 2; -- Time for yellow light (in clock cycles)
GREEN_DURATION : natural := 10; -- Time for green light (in clock cycles)
TURN_GREEN_DURATION : natural := 5 -- Time for turn green light (in clock cycles)
);
port (
clk : in std_logic; -- Clock input
reset : in std_logic; -- Asynchronous reset input
red : out std_logic; -- Red light output
yellow : out std_logic; -- Yellow light output
green : out std_logic; -- Green light output
turn : out std_logic -- Turn green light output
);
end traffic_light_controller;
architecture fsm of traffic_light_controller is
type state_type is (RED, TURN_GREEN, GREEN, YELLOW);
signal state, next_state : state_type := RED;
signal timer : natural := 0; -- Timer for state duration
begin
-- State transition process
state_transition: process(clk, reset)
begin
if reset = '1' then
state <= RED;
timer <= 0;
elsif rising_edge(clk) then
state <= next_state;
-- Timer handling for state durations
if timer > 0 then
timer <= timer - 1;
else
case next_state is
when RED =>
timer <= RED_DURATION - 1;
when TURN_GREEN =>
timer <= TURN_GREEN_DURATION - 1;
when GREEN =>
timer <= GREEN_DURATION - 1;
when YELLOW =>
timer <= YELLOW_DURATION - 1;
when others =>
timer <= 0;
end case;
end if;
end if;
end process state_transition;
-- Next state logic process
next_state_logic: process(state, timer)
begin
case state is
when RED =>
if timer = 0 then
next_state <= TURN_GREEN;
else
next_state <= RED;
end if;
when TURN_GREEN =>
if timer = 0 then
next_state <= GREEN;
else
next_state <= TURN_GREEN;
end if;
when GREEN =>
if timer = 0 then
next_state <= YELLOW;
else
next_state <= GREEN;
end if;
when YELLOW =>
if timer = 0 then
next_state <= RED;
else
next_state <= YELLOW;
end if;
when others =>
next_state <= RED;
end case;
end process next_state_logic;
-- Output logic process
output_logic: process(state)
begin
-- Turn off all lights initially
red <= '0';
yellow <= '0';
green <= '0';
turn <= '0'; -- Ensure the turn signal is also defaulted to '0'
-- Turn on the appropriate light
case state is
when RED =>
red <= '1';
when TURN_GREEN =>
turn <= '1'; -- Turn phase for left turn
when GREEN =>
green <= '1';
when YELLOW =>
yellow <= '1';
when others =>
red <= '1'; -- Fail-safe state
end case;
end process output_logic;
end fsm;
In this updated version:
An additional TURN_GREEN state has been introduced, represented by the turn output signal.
A new generic parameter TURN_GREEN_DURATION has been added to specify the duration of the protected turn phase.
The state transition and next state logic processes have been updated to include the TURN_GREEN state before transitioning to the GREEN state.
The output_logic process now also controls the turn output signal accordingly when the TURN_GREEN state is active.
This controller should be connected to traffic lights where there's a separate bulb or set of bulbs that maintain the "turn" state for protected left turns. Be sure to hook up the turn output to the correct hardware in your system to control the "turn" signal light.