Mbed Technologies
  Learn, Develop, Compete


    Introduction: Welcome to Mbed Blogs!. The periodically submitted blogs in this webpage will discuss more interesting topics on Embedded C programming. Each blog is written with specific focus, interest, to experiment and dip into different important features of embedded C programming. We encourage students and professionals to follow the steps exactly written in the blogs, write, modify and compile the programs to gain maximum knowledge out of it. The blogs will be equally useful to both fresh engineering graduates as well as experienced software/firmware professionals.

    Blog Title:In this blog we will study and experiment about the different types of state machine implementation in C Programming and its usage in embedded applications.

                                Missed/Want to Read Previous Blog: Click Here

by Admin, Posted on February 1, 2018 at 1:00 PM


     Blog Background     
 Compilation Requirement: Linux Environment. Any Linux distribution will be suitable for executing the steps as explained.
 Learning Requirement: Basic and Mandatory Requirement of C Programming.
 Audience Eligibility: Undergraduate engineering students or experienced software/firmware professionals.
 Difficulty Level: Medium to High.

  Step 1: To begin with, let's write a simple c program to change the state of an led. Led has two states LED_ON state and LED_OFF state. When LED is in off state we want to turn it ON and when the LED is in ON state we want to turn it OFF using an if else statement.


#include "stdio.h"

enum states
{
  LED_ON,
  LED_OFF
};

int main()
{
	enum states state = LED_OFF;
	while(1)
	{
	  if(state == LED_OFF)
	  {
	    printf("Led on function to be called\n");
	    state = LED_ON;
	  } else {
		  printf("Led off function to be called\n");
	    state = LED_OFF;
	  }
	  sleep(1); // sleep for a second
	}
    return 0;
}
    

  Step 2: Let's compile the program sm1.c we wrote in the previous step. So we find that led state changes between on and off state in 1 second.


# ./a.out
Led on function to be called
Led off function to be called
Led on function to be called
Led off function to be called
Led on function to be called
Led off function to be called
Led on function to be called
Led off function to be called
    

  Step 3: Let's write another state machine program that will have 4 states and each state changes the led state to immediate next state. Each state has a different sleeping time as shown in program sm2.c. This time to switch the led state to the immediate next state we will use a switch statement.


#include "stdio.h"

enum states
{
  LED_ON1S,
  LED_OFF1S,
  LED_ON3S,
  LED_OFF3S
};

int main()
{
	enum states state = LED_OFF1S;
	while(1)
	{
		switch(state)
		{
			case LED_ON1S:
				state = LED_OFF1S;
				printf("Setting Led state OFF for 1S\n");
				break;
			case LED_OFF1S:
				state = LED_ON3S;
				printf("Setting Led state ON for 3S\n");
				sleep(1);
				sleep(1);
				break;
			case LED_ON3S:
				state = LED_OFF3S;
				printf("Setting Led state OFF for 3S\n");
				sleep(1);
				sleep(1);
				break;
			case LED_OFF3S:
				state = LED_ON1S;
				printf("Setting Led state ON for 1S\n");
				break;
			default:
				state = LED_OFF1S;
				printf("Setting Led default state OFF for 1S\n");
		}
		sleep(1); // sleep for a second
	}
    return 0;
}

  Step 4:Let's compile the program sm2.c we wrote in the previous step. So we find that led state changes between the four led states as defined in the enum with different sleeping time.


# ./a.out
Setting Led state ON for 3S
Setting Led state OFF for 3S
Setting Led state ON for 1S
Setting Led state OFF for 1S
Setting Led state ON for 3S
Setting Led state OFF for 3S
Setting Led state ON for 1S
Setting Led state OFF for 1S
Setting Led state ON for 3S
Setting Led state OFF for 3S
    

  Step 5: Let's write another state machine program that will have 3 states such as Red, Green and blue. Each state changes the led colour to next colour.Let's name this program as sm3.c. This time to switch the led colour to next we will use a function pointer. We have declared a global variable as task_id. We ask user to enter input on which state to switch next. The function pointer f_ptr is declared and initialized to function LED_BLUE. To get started the function pointer is called in the main function.


#include "stdio.h"

unsigned int task_id;

void LED_BLUE();
void LED_RED();
void LED_GREEN();

void (*f_ptr)() = LED_BLUE;

void LED_BLUE()
{
	sleep(1);
	printf("sleeping for 1blue second\n");
	switch(task_id)
	{
	case 0:
		f_ptr = LED_BLUE;
		break;
	case 1:
		f_ptr = LED_RED;
		break;
	case 2:
		f_ptr = LED_GREEN;
		break;
	}
}

void LED_GREEN()
{
	sleep(1);
	printf("sleeping for 1green second\n");
	switch(task_id)
	{
	case 0:
		f_ptr = LED_GREEN;
		break;
	case 1:
		f_ptr = LED_BLUE;
		break;
	case 2:
		f_ptr = LED_RED;
		break;
	}
}

void LED_RED()
{
	sleep(1);
	printf("sleeping for 1red second\n");
	switch(task_id)
	{
	case 0:
		f_ptr = LED_RED;
		break;
	case 1:
		f_ptr = LED_GREEN;
		break;
	case 2:
		f_ptr = LED_BLUE;
		break;
	}
}

int main()
{
	while(1)
	{
		printf("Enter the choice: 0,1,2 or > 2 for exit\n");
		scanf("%d",&task_id);
		if(task_id > 2)
		{
			return 0;
		}
		(*f_ptr)();
	}
	return 0;
}
    

  Step 6 Let's compile the program sm3.c we wrote in the previous step. So we find that led state changes between the four led states as defined in the enum with different sleeping time.


# ./a.out
Enter the choice: 0,1,2 or > 2 for exit
1
sleeping for 1blue second
Enter the choice: 0,1,2 or > 2 for exit
0
sleeping for 1red second
Enter the choice: 0,1,2 or > 2 for exit
2
sleeping for 1red second
Enter the choice: 0,1,2 or > 2 for exit
0
sleeping for 1blue second
Enter the choice: 0,1,2 or > 2 for exit
2
sleeping for 1blue second
Enter the choice: 0,1,2 or > 2 for exit
3
#