Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

state machine example codes for embedded systems

Status
Not open for further replies.

userx51

Junior Member level 2
Joined
Apr 21, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,432
Needed some embedded code examples in C that covers the State Machine concept. Need some examples to better grasp this topic.
 

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.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
thanks. awesome replies. If there are more sites you know of, please share.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top