Altera_Forum
Honored Contributor
14 years agosimulate simple traffic light example (VHDL, DE2)
Hi,
i wrote a simple VHDL traffic light program using a clock divider to get a 1hz clock which i think is helpfull. clockdivider:library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- clockdivider ... divides x-hz clock into 1hz clock
entity clockdiv is
port
(
-- Input ports
clock50mhz : in STD_LOGIC;
clockReset : in STD_LOGIC;
-- Output ports
clockOut : out STD_LOGIC
);
end clockdiv;
architecture controller of clockdiv is
signal count : integer;
signal clock1hz : STD_LOGIC;
begin
divide: process(clock50mhz,clockReset)
begin
if(clockReset = '1') then
count <= 0;
clock1hz <= '0';
elsif rising_edge(clock50mhz) then
if (count >= 25000000) then
clock1hz <=not clock1hz;
count <= 0;
else
count<=count+1;
end if;
end if;
end process;
clockOut <= clock1hz;
end controller; traffic light control: library IEEE;
use IEEE.STD_LOGIC_1164.all;
use IEEE.STD_LOGIC_unsigned.all;
entity ampel_basic is
port
(
-- Input ports
clock1hz : in STD_LOGIC;
reset : in STD_LOGIC;
-- Output ports
tLightsRed1 : out STD_LOGIC;
tLightsYellow1 : out STD_LOGIC;
tLightsGreen1 : out STD_LOGIC;
tLightsRed2 : out STD_LOGIC;
tLightsYellow2 : out STD_LOGIC;
tLightsGreen2 : out STD_LOGIC
);
end ampel_basic;
architecture controller of ampel_basic is
signal count : integer;
begin
process(clock1hz, reset)
begin
if(reset = '1') then
count <= 0;
elsif rising_edge(clock1hz) then
count <= count +1;
if (count = 1) then -- light 1 green
tLightsGreen1 <= '1';
tLightsYellow1 <= '0';
tLightsRed1 <= '0';
tLightsGreen2 <= '0';
tLightsYellow2 <= '0';
tLightsRed2 <= '1';
elsif (count = 30) then
tLightsGreen1 <= '0';
tLightsYellow1 <= '1';
tLightsRed1 <= '0';
tLightsGreen2 <= '0';
tLightsYellow2 <= '0';
tLightsRed2 <= '1';
elsif (count = 31) then -- light 2 green
tLightsGreen1 <= '0';
tLightsYellow1 <= '0';
tLightsRed1 <= '1';
tLightsGreen2 <= '1';
tLightsYellow2 <= '0';
tLightsRed2 <= '0';
elsif (count = 60) then
tLightsGreen1 <= '0';
tLightsYellow1 <= '0';
tLightsRed1 <= '1';
tLightsGreen2 <= '0';
tLightsYellow2 <= '1';
tLightsRed2 <= '0';
count <= 0; -- reset counter
end if;
end if;
end process;
end controller;
i connected both vhdl parts with a root shematic file and mapped the necessary ins and outs according to the DE2 user manual. First problem -> i'm not sure that my code is working as i expect. An the second one -> i want to create a waveform simulation to visualise the traffic light behavior any help would be great! thx alot