/* Moore FSM filename MOORE11.C Simulator program of a Moore Finite State Machine This example accompanies the book "Embedded Microcomputer Systems: Real Time Interfacing", Brooks-Cole, copyright (c) 2000, Jonathan W. Valvano 7/6/99 Interface between the TExaS simulator running a MC68HC11 EVB with ICC11 freeware compiler TExaS Copyright 1999 by Jonathan W. Valvano for more information about ICC11 see http://www.imagecraft.com You may use this file without restrictions */ /* PortC bits 1-0 are inputs, PortB bits 7-0 are outputs Each state has a function to execute a time to wait and 4 next state arrows */ #include "HC11.h" const struct State{ void (*CmdPt)(void); /* function to execute */ unsigned int Time; /* Time (cycles) to wait in this state */ const struct State *Next[4];}; /* Next state if input=0,1,2,3 */ typedef const struct State StateType; typedef StateType * StatePtr; void DoStop(void){ PORTB=0x34;} void DoTurn(void){ PORTB=0xB3;} void DoBend(void){ PORTB=0x75;} #define stop &fsm[0] #define turn &fsm[1] #define bend &fsm[2] StateType fsm[3]={ {&DoStop, 200, {turn, stop, turn, bend}}, /* Stop state */ {&DoTurn, 500, {bend, stop, turn, turn}}, /* Turn state */ {&DoBend, 400, {stop, stop, turn, stop}}}; /* Bend state */ StatePtr Pt; unsigned char Input; void Wait(unsigned int delay){ int Endt; Endt=TCNT+delay; /* Time (500ns cycles) to wait */ while((Endt-(int)TCNT)>0); /* wait */ }; void main(void){ /* PortB bits 7-0 are outputs */ DDRC=0x00; /* PortC bits 1-0 are inputs */ Pt=stop; /* Initial State */ while(1){ (*Pt->CmdPt)(); /* 1) execute function */ Wait(Pt->Time); /* 2) Time (cycles) to wait in this state */ Input=(PORTC)&0x03; /* 3) input */ Pt=Pt->Next[Input]; /* 4) next depends on input */ } }; void resetVectors(void){ /* not really a function, but this must be last */ asm(" org $FFFE"); asm(" FDB __start"); asm(" org $FC00"); /* puts rts in a harmless place */ /* string constants also placed here */ /* the value FC00 means you can have up to 1000 characters of strings */ }