zia
Full Member level 5
- Joined
- Sep 24, 2010
- Messages
- 284
- Helped
- 27
- Reputation
- 54
- Reaction score
- 26
- Trophy points
- 1,318
- Location
- Islamabad Pakistan
- Activity points
- 2,746
state machine very beautiful example for microcontrollers
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 //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; }
Last edited: