Advertisement
oussama_ait_ahmad_23

VHDL code for traffic light controller

Sep 14th, 2023 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 5.55 KB | Source Code | 0 0
  1. -- VHDL project: VHDL code for traffic light controller
  2. library IEEE;
  3. use IEEE.STD_LOGIC_1164.ALL;
  4. use IEEE.STD_LOGIC_UNSIGNED.ALL;  
  5. -- Traffic ligh system for a intersection between highway and farm way
  6. -- There is a sensor on the farm way side, when there are vehicles,
  7. -- Traffic light turns to YELLOW, then GREEN to let the vehicles cross the highway
  8. -- Otherwise, always green light on Highway and Red light on farm way
  9. entity traffic_light_controller is
  10.  port ( sensor  : in STD_LOGIC; -- Sensor
  11.         clk  : in STD_LOGIC; -- clock
  12.         rst_n: in STD_LOGIC; -- reset active low
  13.         light_highway  : out STD_LOGIC_VECTOR(2 downto 0); -- light outputs of high way
  14.      light_farm:    out STD_LOGIC_VECTOR(2 downto 0)-- light outputs of farm way
  15.      --RED_YELLOW_GREEN
  16.    );
  17. end traffic_light_controller;
  18. architecture traffic_light of traffic_light_controller is
  19. signal counter_1s: std_logic_vector(27 downto 0):= x"0000000";
  20. signal delay_count:std_logic_vector(3 downto 0):= x"0";
  21. signal delay_10s, delay_3s_F,delay_3s_H, RED_LIGHT_ENABLE, YELLOW_LIGHT1_ENABLE,YELLOW_LIGHT2_ENABLE: std_logic:='0';
  22. signal clk_1s_enable: std_logic; -- 1s clock enable
  23. type FSM_States is (HGRE_FRED, HYEL_FRED, HRED_FGRE, HRED_FYEL);
  24. -- HGRE_FRED : Highway green and farm red
  25. -- HYEL_FRED : Highway yellow and farm red
  26. -- HRED_FGRE : Highway red and farm green
  27. -- HRED_FYEL : Highway red and farm yellow
  28. signal current_state, next_state: FSM_States;
  29. begin
  30. -- next state FSM sequential logic
  31. process(clk,rst_n)
  32. begin
  33. if(rst_n='0') then
  34.  current_state <= HGRE_FRED;
  35. elsif(rising_edge(clk)) then
  36.  current_state <= next_state;
  37. end if;
  38. end process;
  39. -- FSM combinational logic
  40. process(current_state,sensor,delay_3s_F,delay_3s_H,delay_10s)
  41. begin
  42. case current_state is
  43. when HGRE_FRED => -- When Green light on Highway and Red light on Farm way
  44.  RED_LIGHT_ENABLE <= '0';-- disable RED light delay counting
  45.  YELLOW_LIGHT1_ENABLE <= '0';-- disable YELLOW light Highway delay counting
  46.  YELLOW_LIGHT2_ENABLE <= '0';-- disable YELLOW light Farmway delay counting
  47.  light_highway <= "001"; -- Green light on Highway
  48.  light_farm <= "100"; -- Red light on Farm way
  49.  if(sensor = '1') then -- if vehicle is detected on farm way by sensors
  50.   next_state <= HYEL_FRED;
  51.   -- High way turns to Yellow light
  52.  else
  53.   next_state <= HGRE_FRED;
  54.   -- Otherwise, remains GREEN ON highway and RED on Farm way
  55.  end if;
  56. when HYEL_FRED => -- When Yellow light on Highway and Red light on Farm way
  57.  light_highway <= "010";-- Yellow light on Highway
  58.  light_farm <= "100";-- Red light on Farm way
  59.  RED_LIGHT_ENABLE <= '0';-- disable RED light delay counting
  60.  YELLOW_LIGHT1_ENABLE <= '1';-- enable YELLOW light Highway delay counting
  61.  YELLOW_LIGHT2_ENABLE <= '0';-- disable YELLOW light Farmway delay counting
  62.  if(delay_3s_H='1') then
  63.  -- if Yellow light delay counts to 3s,
  64.  -- turn Highway to RED,
  65.  -- Farm way to green light
  66.   next_state <= HRED_FGRE;
  67.  else
  68.   next_state <= HYEL_FRED;
  69.   -- Remains Yellow on highway and Red on Farm way
  70.   -- if Yellow light not yet in 3s
  71.  end if;
  72. when HRED_FGRE =>
  73.  light_highway <= "100";-- RED light on Highway
  74.  light_farm <= "001";-- GREEN light on Farm way
  75.  RED_LIGHT_ENABLE <= '1';-- enable RED light delay counting
  76.  YELLOW_LIGHT1_ENABLE <= '0';-- disable YELLOW light Highway delay counting
  77.  YELLOW_LIGHT2_ENABLE <= '0';-- disable YELLOW light Farmway delay counting
  78.  if(delay_10s='1') then
  79.  -- if RED light on highway is 10s, Farm way turns to Yellow
  80.   next_state <= HRED_FYEL;
  81.  else
  82.   next_state <= HRED_FGRE;
  83.   -- Remains if delay counts for RED light on highway not enough 10s
  84.  end if;
  85. when HRED_FYEL =>
  86.  light_highway <= "100";-- RED light on Highway
  87.  light_farm <= "010";-- Yellow light on Farm way
  88.  RED_LIGHT_ENABLE <= '0'; -- disable RED light delay counting
  89.  YELLOW_LIGHT1_ENABLE <= '0';-- disable YELLOW light Highway delay counting
  90.  YELLOW_LIGHT2_ENABLE <= '1';-- enable YELLOW light Farmway delay counting
  91.  if(delay_3s_F='1') then
  92.  -- if delay for Yellow light is 3s,
  93.  -- turn highway to GREEN light
  94.  -- Farm way to RED Light
  95.  next_state <= HGRE_FRED;
  96.  else
  97.  next_state <= HRED_FYEL;
  98.  -- if not enough 3s, remain the same state
  99.  end if;
  100. when others => next_state <= HGRE_FRED; -- Green on highway, red on farm way
  101. end case;
  102. end process;
  103. -- Delay counts for Yellow and RED light  
  104. process(clk)
  105. begin
  106. if(rising_edge(clk)) then
  107. if(clk_1s_enable='1') then
  108.  if(RED_LIGHT_ENABLE='1' or YELLOW_LIGHT1_ENABLE='1' or YELLOW_LIGHT2_ENABLE='1') then
  109.   delay_count <= delay_count + x"1";
  110.   if((delay_count = x"9") and RED_LIGHT_ENABLE ='1') then
  111.    delay_10s <= '1';
  112.    delay_3s_H <= '0';
  113.    delay_3s_F <= '0';
  114.    delay_count <= x"0";
  115.   elsif((delay_count = x"2") and YELLOW_LIGHT1_ENABLE= '1') then
  116.    delay_10s <= '0';
  117.    delay_3s_H <= '1';
  118.    delay_3s_F <= '0';
  119.    delay_count <= x"0";
  120.   elsif((delay_count = x"2") and YELLOW_LIGHT2_ENABLE= '1') then
  121.    delay_10s <= '0';
  122.    delay_3s_H <= '0';
  123.    delay_3s_F <= '1';
  124.    delay_count <= x"0";
  125.   else
  126.    delay_10s <= '0';
  127.    delay_3s_H <= '0';
  128.    delay_3s_F <= '0';
  129.   end if;
  130.  end if;
  131.  end if;
  132. end if;
  133. end process;
  134. -- create delay 1s
  135. process(clk)
  136. begin
  137. if(rising_edge(clk)) then
  138.  counter_1s <= counter_1s + x"0000001";
  139.  if(counter_1s >= x"0000003") then -- x"0004" is for simulation
  140.  -- change to x"2FAF080" for 50 MHz clock running real FPGA
  141.   counter_1s <= x"0000000";
  142.  end if;
  143. end if;
  144. end process;
  145. clk_1s_enable <= '1' when counter_1s = x"0003" else '0'; -- x"0002" is for simulation
  146. -- x"2FAF080" for 50Mhz clock on FPGA
  147. end traffic_light;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement