Finite State Machine: This is a note to video 2 of 10.4 edx embedded systems - shape the world These definitions help define a finite state machine for a system that counts whether the number of high inputs reached are even or odd: So if input string is 011001001 (left is first input. the first input is 0 - the number of ones is even so the output is 0, next the input is one, making the number of high inputs odd, the output goes high, next the output is one making the number of highs input even, the output goes low. 010 (output so far) So to define the finite state machine we make a data structure struct State{ unsigned char out; //this will be the output state unsigned long wait; //this is the delay between inputs read unsigned char next[2]; //this will hold the transition info } typedef struct State Stype; #define Even 0 #define Odd 1 Stype fsm[2] = { // one array element for each output state {0,100,{Even, Odd}} // so here we are saying that if the output is in an even state and the transition or input // is 0 (next(0)=Even), we stay in the Even State, else switch to odd {1,100,{Odd,Even}} // so here we are saying that if the output is in an odd state and the transition is // a one (Next(1)) switch to the even state, } //And here is the engine to drive the machine: unsigned char cState; //current state Even/Odd int main (void){ unsigned char input; PortF_Init(); SysTick_Init(); cState = Even; //Initial State while (1){ //output based on current state GPIO_PORTF_DATA_R = fsm[cState].out<<2; //current state 0000 000x bit shifted twice to PF2(led) //wait for time relevent to state SysTick_Wait10ms(fsm[cState].wait]); //get input input = (~GPIO_PORTF_DATA_R & 0x10)>>4; check switch one (PF4) bit shift to 0 cState = fsm[cState].next[input]; } }