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
//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