Advertisement
Guest User

Untitled

a guest
May 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_ARITH.ALL;
  4. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  5.  
  6. entity Traffic is
  7. Port ( Reset : in STD_LOGIC;
  8. Clock : in STD_LOGIC;
  9.  
  10. -- for debug
  11. debugLED : out std_logic;
  12. LEDs : out std_logic_vector(2 downto 0);
  13.  
  14. -- Car and pedestrian buttons
  15. CarEW : in STD_LOGIC; -- Car on EW road
  16. CarNS : in STD_LOGIC; -- Car on NS road
  17. PedEW : in STD_LOGIC; -- Pedestrian moving EW (crossing NS road)
  18. PedNS : in STD_LOGIC; -- Pedestrian moving NS (crossing EW road)
  19.  
  20. -- Light control
  21. LightsEW : out STD_LOGIC_VECTOR (1 downto 0); -- controls EW lights
  22. LightsNS : out STD_LOGIC_VECTOR (1 downto 0) -- controls NS lights
  23. );
  24. end Traffic;
  25.  
  26. architecture Behavioral of Traffic is
  27.  
  28. -- Encoding for lights
  29. constant RED : std_logic_vector(1 downto 0) := "00";
  30. constant AMBER : std_logic_vector(1 downto 0) := "01";
  31. constant GREEN : std_logic_vector(1 downto 0) := "10";
  32. constant WALK : std_logic_vector(1 downto 0) := "11";
  33. type StateType is (GreenEW, YellowEW, GreenNS, YellowNS);
  34. signal state, nextState : StateType;
  35. signal t : integer range 0 to 60;
  36.  
  37. begin
  38.  
  39. -- Show reset status on FPGA LED
  40. debugLed <= Reset;
  41.  
  42. -- Threee LEDs for debug
  43. LEDs <= "000";
  44.  
  45. -- Luck North-south traffic
  46. LightsEW <= RED;
  47. LightsNS <= WALK;
  48.  
  49.  
  50. sync: -- CLOCK
  51. process(Reset, clock)
  52. begin
  53. if (Reset = '1') then
  54. state <= GreenEW;
  55. elsif rising_edge(clock) then
  56. state <= nextState;
  57. end if;
  58. end process;
  59.  
  60. comb:
  61. process(state, CarNS, CarEW, PedEW, PedNS, reset, t)
  62. begin
  63. case state is
  64. when GreenEW =>
  65. LightsEW <= GREEN;
  66. LightsNS <= RED;
  67. t <= '0';
  68. if rising_edge(clock) then
  69. t <= t+1;
  70. if ((t = 60) and (CarEW = '0') and (CarNS = '1') and (t = 59) and (PedEW = 0)) then
  71. nextState <= YellowEW;
  72. end if;
  73. when YellowEW =>
  74. LightsEW <= AMBER;
  75. LightsNS <= AMBER;
  76. nextState <= GreenNS;
  77. when GreenNS =>
  78. LightsEW <= RED;
  79. LightsNS <= GREEN;
  80. t <= '0';
  81. if rising_edge(clock) then
  82. t <= t+1;
  83. if ((t = 60) and (CarEW = '0') and (CarNS = '1') and (t = 59) and (PedEW = 0))then
  84. nextState <= YellowEW;
  85. end if;
  86. when YellowNS =>
  87. LightsEW <= AMBER;
  88. LightsNS <= AMBER;
  89. nextState <= GreenEW;
  90. end case;
  91. end process comb;
  92. end architecture Behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement