Moore Finite state machines in programming

Discussion in 'Embedded Systems and Microcontrollers' started by Vindhyachal Takniki, Mar 1, 2016.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    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.
  2. Papabravo


    Feb 24, 2006
    I have used both types on numerous occasions in hardware and in pure software. It is a very powerful technique.
  3. JohnInTX


    Jun 26, 2012
    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.
  4. Vindhyachal Takniki

    Thread Starter Member

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

    Code (Text):
    1. enum
    2. {
    3.     goN = 0U, waitN, goE, waitE
    4. };  
    6. typedef const struct
    7. {
    8.     uint32_t output;
    9.     uint32_t wait;
    10.     uint32_t next[4];
    11. }street_light;
    13. street_light sl_fsm[4] = {
    14.                             {
    15.                                 0x21U,
    16.                                 100U,
    17.                                 {goN, waitN, goN, waitN},
    18.                             },
    19.                             {
    20.                                 0x31U,
    21.                                 100U,
    22.                                 {goE, goE, goE, goE},
    23.                             },
    24.                             {
    25.                                 0x41U,
    26.                                 100U,
    27.                                 {goE, goE, waitE, waitE},
    28.                             },
    29.                             {
    30.                                 0x51U,
    31.                                 100U,
    32.                                 {goN, goN, goN, goN},
    33.                             },                          
    34.                         };
    37. void all_tasks_manager(void)
    38. {
    39.     uint32_t state = goN;
    40.     uint32_t input;
    41.     uint32_t output;  
    43.     while(1)
    44.     {
    45.         output = sl_fsm[state].output;       /* set tthe output */
    46.         wait_delay_us(sl_fsm[state].wait);   /* delay for spcified time */
    47.         input = goN;                         /* input is goN for testing, otherwise input is from pins */
    48.         state = sl_fsm[state].next[input];   /* determine next state */
    50.     /* to remove compilre warming */
    51.         if(output)
    52.         {
    53.             __nop();
    54.         }
    55.     }  
    57. } /* function ends here */    
  5. WBahn


    Mar 31, 2012
    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.
  6. xennar

    New Member

    Jul 10, 2013
    I'd been working on a table based state machine framework for Arduino when I read your discussion. The idea is that each state machine is its own object which can be shared like any Arduino library. It comes with a number of reusable state machines.

    I'd be interested to hear your thoughts.

    The code and doumentation are on Github: