Altera_Forum
Honored Contributor
14 years agoSimple State Machine not working as expected
Hi, guys!
So, I'm new here. I'm a student whose lab professor gave him the following assignment. Translated from spanish to english, it reads like this:An electrical engine is controlled by a single button. When the button is pushed, the engine is turned on. When the button is pushed again, the engine returns to 'off' status. Syntethize the engine's control circuit in VHDL.And then I have to program a FPGA with it, SignalTap it, and show the professor that it works as intended. The FPGA I'm using is a Cyclone III EP3C16F484C6. Since the pushable button I use continuously sends a '1' signal when it's not pushed, and a '0' signal while it's pushed, I wrote the following VHDL code for the assignment. ENTITY enginecontrol IS
PORT (clk,button: IN BIT; engine: OUT BIT);
END enginecontrol;
ARCHITECTURE behavior OF enginecontrol IS
TYPE statesec IS (A,B,C,D);
SIGNAL state: statesec:=A;
BEGIN
PROCESS (clk)
BEGIN
IF (clk'EVENT AND clk ='1') THEN
CASE state IS
WHEN A =>
IF button ='0' THEN
state<=B;
ELSE
state<=A;
END IF;
WHEN B =>
IF button ='1' THEN
state<=C;
ELSE
state<=B;
END IF;
WHEN C =>
IF button ='0' THEN
state<=D;
ELSE
state<=C;
END IF;
WHEN D =>
IF button='1' THEN
state<=A;
ELSE
state<=D;
END IF;
END CASE;
END IF;
END PROCESS;
engine<= '0' WHEN state=A ELSE
'1' WHEN state=B ELSE
'1' WHEN state=C ELSE
'0';
END behavior;Here, remember, button pushed is 0 and button released is 1. Engine 'on' is 1 and engine 'off' is 0. This code, I believe, clearly describes a state machine with four possible states. It starts at state A, with the engine off. If the button is pushed, it goes to state B, and the engine turns on. It will remain on state B as long as the button is pushed, and when the button is released, it goes to state C, engine still on. Push the button again, and you get state D, engine off. Let it go and the cycle begins again, at state A. This is the desired behavior! Always the same, reliable... But the real behavior is... random! Around 70% of the time, when you release a button, it goes from state B to A, instead of C, or from state D to C, instead of A. This, of course, has undesired results on the output. Nowhere in the code I can see anything that could produce this behavior! And around 30% of the time, releasing a pushed button works as intended, and the machine goes to the correct next state. Since I won't be seeing my lab professor again until January 10th, can you guys tell me why this is happening? Many thanks for your time and help!