Oftentimes when we are creating electronics projects, we are trying to emulate or create a series of events. Most of the time the order of events we want to happen, and when they happen depends on what is currently happening and a series of external factors. For example, James Colvin wrote a Simon Says game. In Simon Says, you either go on to the next challenge or lose depending on if you do the action correctly and which action was previous.
This is sequential logic, and an example of that is called a state machine. If you don’t understand sequential logic, read this post. State machines are an important building block in FPGA design. In a state machine, you have your state register, which stores what the current state is, that would be the current event in my previous example. If you don’t know what a register is, read this post about registers. Feeding into the state register is the next state logic, and this logic dictates the next state based on the present state and inputs. Out of the state register comes the present state which also leads to the output logic. This decides what signals have what value depending on the present state. You can see the block diagram for a state machine below. A block diagram is a visual tool to divide code and ideas into chunks, much like a flow chart.
The next state logic can be represented with something called a state diagram. For an example, I have included my state diagram for the stepper motor controller for the claw machine I’m making. The claw machine is a classic game in arcades where you control a claw to attempt to get a prize out of the box.
Below you can see the state diagram for he stepper motor controller. The blue circles are the states named for which of the 4 signals on the stepper motor they set high. The two-color blue circle is the start state. This is the state that the reset signal goes to. The system will automatically start in this position. The purple text by each state are the output signals. The text on the arrows show which signal causes the machine to go in that direction.
To demonstrate how the state diagram works, I will trace a path. The path I am going to trace is in green dotted lines in the state diagram below.
Starting at the state sig0, the motor will be off, as enable is 0 to get to that state. Then if we turn enable on we will follow the green dotted arrow to sig1. In sig1 the motor will take a full step as one of the pins is now high. Then if we leave enable on and make sure direction is 1 we will move to sig2 where the next pin will be high. This will take another step in the same direction. Then if enable and direction are kept the same we move to sig3, and then sig4. The direction it steps depends on the orientation of the pins. Just know that going this direction in the state digram is one direction of steps, and going the other direction is another direction in steps. Then if we turn enable off we will move back to sig0 where the motor will turn off.
Now that you understand the concept behind state machines, stay tuned to hear about how to code a state machine.
For more information on state diagrams, visit the Learn site page.
To learn how to use a stepper motor with FPGA using this state diagram, check out my instructable.
5 Comments on “What Is a State Machine?”
State machines are a great concept because they clarify the flow of the program. There is also less coding bugs thanks to automatic code generators where you draw the state diagram
I had no idea there were automatic code generators, that could certainly come in handy. I find even without that though, having a standard structure to code helps minimize errors.
I shared a link to the page on twitter https://twitter.com/JKowal7/status/693549741876051968
On which software you draw state diagram.