Download presentation
Presentation is loading. Please wait.
Published byCarter O'Leary Modified over 10 years ago
1
State Machines An approach to assembler coding
2
Intro State Machines are an integral part of software programming. State machines make code more efficient, easier to debug and help organize the program flow. State machines are not limited to just firmware, they can be used to streamline any system. We will look at their use in assembler.
3
What is a FSM A Finite State Machine (FSM) is based on the idea of there being finite number of states for a given system. For instance, when an application turns an LED on and off, two states exist; one state is when the LED is on and the other is when it is off. The example will turn on eight LEDs sequentially. Only one LED is on at a time, therefore eight states exist. Each state consists of one LED being turned on while all the rest are off.
4
State variable State machines require a State Variable (SV). The SV is essentially a pointer that keeps track of the state that the microcontroller is in, and directs the program flow to the corresponding software module. The SV can be modified in the software modules (or states) themselves or by an outside function. The example firmware uses an outside function which detects a button press to advance through the states.
5
State machine state variable next state logic inputs from environment outputs to the environment Next state
6
Benefits The first advantage is using state machines inherently promotes good design techniques. When beginning to implement an application, think about what states are necessary to make the application work. List all the pieces, or states, of an application and then explore how they tie to one another. This will help prevent developing bugs in the code. This line of thinking also leads to the development of a very useful engineering tool – the flow chart. The following slide covers state machine development in greater detail.
7
Return to sender State machine programs have one characteristic that cause them to be very beneficial. They always return to one spot (or jump station) in the code at which the program flow is channeled, by the state variable, to the corresponding software module. This characteristic makes calling repetitive tasks on a regular basis quite simple. Clearing the watchdog timer, checking for I/O button presses or communicating with a host that requires periodic communication are examples of repetitive tasks.
8
Better than loops The alternative to using state machines is to use looping code. In order for looping code to handle repetitive tasks, the functions that handle these tasks must be distributed throughout the code in each of the loops. This is not only highly inefficient but also confusing to understand. The next slide shows a block diagram of what the example code would look like if a state machine where not used.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.