embedded system state machine

Status
Not open for further replies.

Blaze

Seniorius Lurkius
20
I am working an embedded system project. I'm a ME student at CalPoly, SLO so my coding skill are a little lacking. So the way I see it there are two ways to implement at State Machine in C ; A switch statement with a case for each state or an array of pointers (state_table) that point to functions that represent each state. I would like to implement the state machine using the array of pointers. I'm working of some sample code givent to me by my adviser:

//State Machine
//state_machine.c
//Test state machine framework

#include<stdio.h>

typedef enum {ST_1=0 , ST_2=1, ST_3=3, ST_4=4} State_Type;

void (*state_table[])() = {st_1, st_2, st_3, st_4};

State_Type curr_state;

//## Prototypes ##//

void Init_StateTBL(void);


void main(void){
printf("Start /n");
Init_StateTBL();

while(1){

state_table[curr_state]();

}

return;
}

//#### Functions ####//

void Init_StateTBL(void){

curr_state = ST_1;
printf("Init_StateTBL /n");

return;
}

void st_1(){

curr_state = ST_2;
printf("ST_1 /n");

return;
}

void st_2(){

curr_state = ST_3;
printf("ST_2 /n");

return;
}

void st_3(){

curr_state = ST_4;
printf("ST_3 /n");

return;
}

void st_4(){

curr_state = ST_1;
printf("ST_4 /n");

return;
}

The errors I'm getting relate to this line void (*state_table[])() = {st_1, st_2, st_3, st_4};

My target is a 68HC12 micro controller

Any help is greatly appreciated

geoff
 

Blaze

Seniorius Lurkius
20
Have you ever make changes to code, but end up testing old versions? I have. L I'm writing the code in UltraEdit and then using UltraEdit's FTP save to push it to my Linux box for compile and testing. Well FTP save was not putting the file where I thought it was and I have been compiling the same old bad version of my code for the last 4 hours.

The code now works

code:<HR>
Code:
  //State Machine
//state_machine.c
//Test state machine framework

#include<stdio.h>

typedef enum {ST_1=0 , ST_2=1, ST_3=3, ST_4=4} State_Type;

//## Prototypes ##//
void Init_StateTBL(void);
void st_1(void);
void st_2(void);
void st_3(void);
void st_4(void);


void (*state_table[])(void) = {st_1, st_2, st_3, st_4};

State_Type curr_state;


void main(void){
	printf("Start /n");
	Init_StateTBL();	

	while(1){

		state_table[curr_state]();

	}

return;
}

//#### Functions  ####//

void Init_StateTBL(void){

	curr_state = ST_1;
	printf("Init_StateTBL /n");

return;
}

void st_1(){
	
	curr_state = ST_2;
        printf("ST_1 /n");

return;
}

void st_2(){

        curr_state = ST_3;
        printf("ST_2 /n");

return;
}

void st_3(){

        curr_state = ST_4;
        printf("ST_3 /n");

return;
}

void st_4(){

        curr_state = ST_1;
        printf("ST_4 /n");

return;
}

Thanks for the help

geoff
 

HitScan

Ars Tribunus Militum
2,523
Subscriptor++
For readabilites sake, I would do (void) instead of just (). Makes it clearer that it is a declaration rather than a call. Don't some compilers (maybe old ones) imply int?
Well, in C yes. And it's all valid C code, but if you'll notice his comments, he's using a c++ compiler, so it should still work. (void) and () are both perfectly fine in C++, but if his source file is a .c then he should be slapped. :D

HitScan
 
Status
Not open for further replies.