Hello!
For a state machine, you have to list up the possible states (and combinations of states), and
list up the inputs / events that cause changes from a state to another. Here is an example (not
in C, in pseudo code with a C syntax).
Let's say you want to toggle a LED with a button press. You have 2 states, LED_ON and LED_OFF.
The button generates an event which is caught by an interrupt routine. Basically the code would
look like this:
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| enum {LED_OFF, LED_ON};
bool led_state;
main_program() {
ConfigButton();
}
void ButtonInterrupt(void) {
if(led_state) {
led_state = LED_OFF;
LEDPowerOff();
}
else {
led_state = LED_ON;
LEDPowerOn();
}
} |
That's about it, you have a two state system, and a state switching event.
NB: The main program does only configuration. This is not a mistake. In the embedded
world, everything can be event-based. If there is an event, the processor works, if not,
it sleeps. But everything can happen outside of the main program.
Dora.