The basics of programming embedded processors: Part 1
Design Patterns for Embedded Systems
In this section, we consider design patterns for two very different styles of programs: the state machine and the circular buffer. State machines are well suited to reactive systems such as user interfaces, and circular buffers are useful in digital signal processing.
style. When inputs appear intermittently rather than as
periodic samples, it is often convenient to think of the system as
reacting to those inputs. The reaction of most systems can be
characterized in terms of the input received and the current state of
the system. This leads naturally to a finite-state
style of describing the reactive system's behavior.
Moreover, if the behavior is specified in that way,
it is natural to write the program implementing that behavior in a
state machine style. The state machine style of programming is also an
efficient implementation of such computations.
Finite-state machines are usually first encountered in the context of hardware design.The programming example describe below shows how to write a finite-state machine in a high-level programming language.
Programming Example: A state machine in C
The behavior we want to implement is a simple seat belt controller [Chi94]. The controller's job is to turn on a buzzer if a person sits in a seat and does not fasten the seat belt within a fixed amount of time. This system has three inputs and one output.
The inputs are a sensor for the seat to know when a person has sat down, a seat belt sensor that tells when the belt is fastened, and a timer that goes off when the required time interval has elapsed. The output is the buzzer. Shown below is a state diagram that describes the seat belt controller's behavior.
The idle state is in force when there is no person in the seat. When the person sits down, the machine goes into the seated state and turns on the timer. If the timer goes off before the seat belt is fastened, the machine goes into the buzzer state. If the seat belt goes on first, it enters the belted state. When the person leaves the seat, the machine goes back to idle.
To write this behavior in C, we will assume that we have loaded the current values of all three inputs ( seat , belt , timer ) into variables and will similarly hold the outputs in variables temporarily ( timer_on , buzzer_on ). We will use a variable named state to hold the current state of the machine and a switch statement to determine what action to take in each state. The code follows:
#define IDLE 0
#define SEATED 1
#define BELTED 2
#define BUZZER 3
This code takes advantage of the fact that the state
will remain the same unless explicitly changed; this makes self-loops
back to the same state easy to implement.
This state machine may be
executed forever in a while(TRUE) loop or periodically called by some
other code. In either case, the code must be executed regularly so that
it can check on the current value of the inputs and, if necessary, go
into a new state.
Currently no items