Moore Finite state machines in programming

Thread Starter

Vindhyachal Takniki

Joined Nov 3, 2014
594
1. I had been viewing this on youtube/edx course
Explains use of Moore FSM in embedded. Who else use Moore FSM or any other FSM like Mealey?
2. I am used to superloop concept in which I design my own state, step by step. I had tried to put this Morre FSM in my ealier written code for ECG code. I found it extremely difficult by Moore FSM. Since there are so many tasks running, interconnections with ISR, multiple inputs , multiple outputs & other things, I got stuck. What is better method in these kind of situation.
 

JohnInTX

Joined Jun 26, 2012
4,787
FWIW I use state machines in virtually everything I write unless the chip supports a proper RTOS. After you get the hang of it, it makes coordinating many tasks much easier.
 

Thread Starter

Vindhyachal Takniki

Joined Nov 3, 2014
594
Made a code from his course for street light.

Code:
enum
{
    goN = 0U, waitN, goE, waitE 
};   

typedef const struct
{
    uint32_t output;
    uint32_t wait;
    uint32_t next[4];
}street_light;

street_light sl_fsm[4] = {
                            {
                                0x21U,
                                100U,
                                {goN, waitN, goN, waitN},
                            },
                            {
                                0x31U,
                                100U,
                                {goE, goE, goE, goE},
                            },
                            {
                                0x41U,
                                100U,
                                {goE, goE, waitE, waitE},
                            },
                            {
                                0x51U,
                                100U,
                                {goN, goN, goN, goN},
                            },                           
                        };
 

void all_tasks_manager(void)
{
    uint32_t state = goN;
    uint32_t input;
    uint32_t output;   
   
    while(1)
    {
        output = sl_fsm[state].output;       /* set tthe output */
        wait_delay_us(sl_fsm[state].wait);   /* delay for spcified time */
        input = goN;                         /* input is goN for testing, otherwise input is from pins */
        state = sl_fsm[state].next[input];   /* determine next state */
       
    /* to remove compilre warming */
        if(output)
        {
            __nop();
        }
    }   
   
} /* function ends here */
 

WBahn

Joined Mar 31, 2012
30,051
1. I had been viewing this on youtube/edx course
Explains use of Moore FSM in embedded. Who else use Moore FSM or any other FSM like Mealey?
2. I am used to superloop concept in which I design my own state, step by step. I had tried to put this Morre FSM in my ealier written code for ECG code. I found it extremely difficult by Moore FSM. Since there are so many tasks running, interconnections with ISR, multiple inputs , multiple outputs & other things, I got stuck. What is better method in these kind of situation.
I prefer Moore machines, but sometimes a Mealy machine is simply a much better match to the application.

You can use an informal, ad-hoc design approach, which is often just find for small machines, or you can adopt a more formal, standardized approach, which can be needlessly cumbersome on small machines but can make the design of larger machines much more robust.

If doing it in software, FSM implementations lend themselves to switch() statements (if your language supports that). I find that I can implement the machine with more confidence if I set all of the relevant actions, including the next-state assignment, within every case. In and HDL this helps eliminate inferred latches, but in an MCU or other software it tends to slow down the machine because it has to do a lot of assignments that serve no purpose. But you can always go in and comment out the ones that you determine are truly redundant (I don't delete them because they serve as good documentation for what I expect those signals to have in that state.
 
Top