Altera_Forum
Honored Contributor
18 years agoProblems with FSM's and Counters
I'm having some major problems with this VHDL code. It's supposed to simulate an intersection traffic lights but the behavior that the waveform exhibits doesn't make any sense for the simplest cases. All it should do is alternate between the states for the amount of time given. 16 clock cycles for all of NS split among two states and 8 clock cycles for all of EW split among two states. The waveform behavior is not correct at all, certain states trigger when they shouldn't ie I should never have a Red light for NS and a green light for NS at the same time.
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.numeric_std.all ;
ENTITY TrafficLight2 IS
PORT(NS,EW,NSL,EWL: IN STD_LOGIC_VECTOR(1 DOWNTO 0);--cars that want to go in those directions
Clk,Reset: IN STD_LOGIC;
GNS,GEW,RNS,REW,GNSL,GEWL: OUT STD_LOGIC);--GNS = greens for North and South, GEW = greens for East and West, GNSL = greens for North and South left lanes, GEWL = greens for East and Weset left lanes
END TrafficLight2;
ARCHITECTURE Behavior OF TrafficLight2 IS
TYPE State_type IS(NSStart,EWStart,GoNS,GoEW,GoNSL,GoEWL);--GoNS=North and South green,GoEW=East and West green,GoNSL=North Left and South Left green,GoEWL=East Left and South Left green
SIGNAL state: State_type;
SIGNAL count: INTEGER RANGE 0 to 16;
BEGIN
PROCESS(Clk)
BEGIN
IF Reset = '1' THEN
state<=NSStart;
ELSIF(Clk'EVENT AND Clk='1') THEN
CASE state IS
WHEN NSStart =>
count <= 16;
state <= GoNS;
WHEN EWStart =>
count <= 8;
state <= GoEW;
WHEN GoNS =>
IF count=8 THEN
state <=GoNSL;
ELSE
count <= count - 1;
END IF;
WHEN GoEW =>
IF count=4 THEN
state <=GoEWL;
ELSE
count <= count - 1;
END IF;
WHEN GoNSL =>
IF count=0 THEN
state<=EWStart;
ELSE
count <= count -1;
END IF;
WHEN GoEWL =>
IF count=0 THEN
state<=NSStart;
ELSE
count <=count-1;
END IF;
END CASE;
END IF;
END PROCESS;
GNS<='1' WHEN state = GoNS ELSE '0';
GEW<='1' WHEN state = GoEW ELSE '0';
RNS<='1' WHEN state = GoEW OR state = GoEWL ELSE '0';
REW<='1' WHEN state = GoNS OR state = GoNSL ELSE '0';
GNSL<='1' WHEN state = GoNSL ELSE '0';
GEWL<='1' WHEN state = GoEWL ELSE '0';
END Behavior;