State Machine tutorial part 1 is the first post in a series of 3.
A state machine or as it is also known a finite state machine (FSM), is a model used to design computer programs and sequential logic circuits. The machine can only be in one state at a time, this is known as the current state. Changes from one state to another are brought about by an event or certain condition, this brings about a transition into a new state.
State machines are found in many devices, some we come across in our every day lives, for example a microwave oven, dishwasher and washing machine. All three of these devices have a standby state where they are waiting for input from a user, the devices will then switch to an active state performing some task, when the task is completed an event is triggered (usually determined by a timer), the device will switch to a new state or possibly back to standby and the user may be alerted by the transition to the new state.
State machines can be very simply with just a few states, or have many different states with many transitions between them.
So to sum up, a state machine is a device that stores the status of something at a particular time, can operate on an input to change the status and/or cause an action or output to take place for any given change.
Further reading on state machines can be found here
Basic State Machine Example
Over the next few tutorials a basic state machine will be explained, using C code that is portable and gives a good basic structure which can be built upon. This can then be used used to form simple microcontroller embedded systems. A further example will also be shown based on the Texas Instruments MSP430G223x device, which allows human interaction with a basic interface.
The image above shows a basic state machine diagram, with the following specification:
- There are 3 states S_OFF, S_ON and S_PROCESS. The states are prefixed with an ‘S’ as I find this makes the code easier to understand later.
- There are 3 events E_OFF, E_ON and E_START. Again these are prefixed but with an ‘E’.
- There are 4 transitions shown by the green arrows, these also indicate the direction which determines how the state machine structure functions.
S_OFF has 3 transitions attached to it’s state. It can only move to the S_ON state via the E_ON event, the other 2 transitions are triggered by the E_OFF event, which causes a transition from either S_ON or S_PROCESS into the S_OFF state. So it is not possible for the S_OFF state to move to the S_PROCESS state.
S_ON has 3 transitions attached to it’s state. The E_ON event causes a transition from the S_OFF state into the S_ON state. The E_OFF event causes a transition from the S_ON state into the S_OFF state, and the E_START event brings about a transition to the S_PROCESS state.
S_PROCESS has 2 transitions attached to it’s state. It can only move to the S_OFF state via the E_OFF event. The other transition is from the S_ON state via the E_START event. So it is not possible for the S_PROCESS state to move to the S_ON state.
This example should illustrate the basic mechanism of a state machines logical process, it also aims to show the power of a state machine diagram which can be very useful when constructing the code around the system.
State machines inherently promote good design techniques, so when designing an application think about the states and events you will use.
In the next part of this tutorial we will dive into the C programming code using this basic example for the system.
I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers. All this takes time and a great deal of work, so please support the site by using the Adfly links etc. If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.