System design with state machine[SOLVED]

Thread Starter

Pushkar1

Joined Apr 5, 2021
212
I know that in practice the state transition diagram is created before the code is written for embedded system. i don't understand state machine logic for program or system

In simple terms an FSM consists of:

  1. States
  2. event
  3. Transition

So there can be many states in the diagram, we can go from one state to another, we can come from another state to the previous state through edges. We only enter or leave state when conditions change

How do we decide how many states a system can have? What does a state represent in a state transition diagram?
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
212
hi,
There is a lot of information and tutorials on the web, look thru them and ask if you have any specific queries.
How many states can we have if the push button is read by the microcontroller?

Button has two options
1 Button not pressed
2 Button pressed

What does a state represent in a state transition diagram?

If Possible please move to embedded programming forum
 

KeithWalker

Joined Jul 10, 2017
1,916
I know that in practice the state transition diagram is created before the code is written for embedded system. i don't understand state machine logic for program or system

In simple terms an FSM consists of:

  1. States
  2. event
  3. Transition

So there can be many states in the diagram, we can go from one state to another, we can come from another state to the previous state through edges. We only enter or leave state when conditions change

How do we decide how many states a system can have? What does a state represent in a state transition diagram?
The number of states in a system will be determined purely by the functionality of the system.
A state represents a situation in a system that will exist until all predetermined conditions are reached. The state will then go through the required transition to the next state.
NOTE: State machines do not require an embedded controller. They existed using standard logic components long before microcontrollers were available.
 

KeithWalker

Joined Jul 10, 2017
1,916
How many states can we have if the push button is read by the microcontroller?

Button has two options
1 Button not pressed
2 Button pressed

What does a state represent in a state transition diagram?

If Possible please move to embedded programming forum
Judging by your questions, I believe that you have not done any research on state machines.
You have a lot of reading to do if you really want to understand state machines and state diagrams. Read and understand the tutorial that ericgibbs suggested before you post any more questions on the subject. We are here to try to help solve your problems, not to educate uou one question at a time.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
212
@KeithWalker

I want to learn how to design state transition diagrams for embedded systems. I have researched on the subject before asking the question.

my question is what is state in embedded system? if i want to design state diagram i should know what are state for embedded system.

In embedded system there is input , output and process but what are the states I didn't understand that's why I asked the question
 

Papabravo

Joined Feb 24, 2006
16,787
You are familiar with combinatorial logic. At all times, the outputs are a function of the inputs. There is no memory in the system.
States represent 'memory' of what the inputs and outputs have done previously. State machines come in two flavors:
  1. Flavor #1 (the Moore Machine) has outputs which are only a function of the present state (aka memory).
  2. Flavor #2 (the Mealy Machine) has outputs which are a function of the present state (memory) and the inputs.
To implement a state machine in hardware we used registered devices (aka flip-flops) to provide memory. In a processor we use memory locations to hold a state variable which can be read and written.

In the abstract sense at discrete intervals in time:
We use the present inputs and the current state to compute two new values
  1. The next state
  2. The new outputs
You mentioned pressing a button, or a "key" on a keyboard. A mechanical switch will "bounce", both when the contact is made and also when the contact is released. A state machine can be used to debounce the "key" so that the higher level program sees only a single event when the "key" is pressed and a single event when the "key" is released. It does this by "sampling" the key at successive fixed time intervals and recording the value of the "key" as a zero or a one. You can now apply a condition that in order to declare that the key contact has been made you need to see 3 successive 0's, and correspondingly to declare that the key contact has been released you need to see 3 successive 1's.
 
Last edited:

Beau Schwabe

Joined Nov 7, 2019
100
A flow chart is in a sense a state machine where each node in the flow chart is a state. With a complex design or just to get my head around something, first I create a flow chart. each node on the flowchart is designed to "fall through" meaning it doesn't hang in a loop waiting for a condition to be met. Each Node is assigned an index value. Each Node is capable of changing an Index value based on a decision within the node or completion of the node, but a node will never call another node. a node always returns to a dispatcher where the dispatcher calls another node based on the index value. In PIC assembly language (which is what I mainly write code in) the dispatcher is simply a lookup table rather than a series of If/Then statements. In higher level languages you may have a series of If/Then statements in which case I would implement a binary tree to minimize the code execution time. The reason for the dispatcher is to allow for additional flow charts to be added to the system. Each flow chart gets it's own Node Index variable as well as it's own dispatcher. Say for instance you have two dispatchers indicating you have two separate flow charts. When the first flow chart completes a node and returns to dispatcher #1 then instead of dispatcher #1 returning to the first flow chart, the second dispatcher returns to flow chart #2. ... And when flow chart #2 completes the node THEN dispatcher #1 returns to the first flow chart. This allows for "atomic" functions to seemingly take place in 'real time' throughout the system. Essentially what I have described is "CPU polling" but heavily reliant on state machine structure.
 

KeithWalker

Joined Jul 10, 2017
1,916
@KeithWalker

I want to learn how to design state transition diagrams for embedded systems. I have researched on the subject before asking the question.

my question is what is state in embedded system? if i want to design state diagram i should know what are state for embedded system.

In embedded system there is input , output and process but what are the states I didn't understand that's why I asked the question
I already explained in post #4. Did you not understand what I wrote?
This is a simple tutorial. Read it and then let us know what you didn't understand.
https://sparxsystems.com/resources/tutorials/uml2/state-diagram.html
 

andrewmm

Joined Feb 25, 2011
1,751
I know that in practice the state transition diagram is created before the code is written for embedded system. i don't understand state machine logic for program or system

In simple terms an FSM consists of:

  1. States
  2. event
  3. Transition

So there can be many states in the diagram, we can go from one state to another, we can come from another state to the previous state through edges. We only enter or leave state when conditions change

How do we decide how many states a system can have? What does a state represent in a state transition diagram?
Home work , I hope not ...

States are the steps you stop in till you have a transition to a state triggered by an event. Note the state to go to can be the same as your in,

Number of states is "enough to do the job you want to do"

A traffic light controller for a three way junction is unlikely to have the same number of states as a controller for a 4 way junction,
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
212
You mentioned pressing a button, or a "key" on a keyboard. A mechanical switch will "bounce", both when the contact is made and also when the contact is released. A state machine can be used to debounce the "key" so that the higher level program sees only a single event when the "key" is pressed and a single event when the "key" is released. It does this by "sampling" the key at successive fixed time intervals and recording the value of the "key" as a zero or a one. You can now apply a condition that in order to declare that the key contact has been made you need to see 3 successive 0's, and correspondingly to declare that the key contact has been released you need to see 3 successive 1's.
@Papabravo I understand logic for reading button look like this.

C:
If ( Button == HIGH)  // check button
     Debounce = 100 ms // wait for debounc period
    If ( Button == HIGH)  // check again
        Do_something () ;
else
       Do_something other () ;
I am trying to draw state diagram for debounce point of view.

Button has there points
1. Button Not Pressed
2. Button Pressed
3. Button released

I gave my best but I don't have any idea for debounce.

1626529019434.png
I am trying to figure out which kind of connection are between these three points to make state machine .

Is this logically correct state diagram? Please help me
 
Last edited:

KeithWalker

Joined Jul 10, 2017
1,916
@Papabravo I understand logic for reading button look like this.

C:
If ( Button == HIGH)  // check button
     Debounce = 100 ms // wait for debounc period
    If ( Button == HIGH)  // check again
        Do_something () ;
else
       Do_something other () ;
I am trying to draw state diagram for debounce point of view.

Button has there points
1. Button Not Pressed
2. Button Pressed
3. Button released

I gave my best but I don't have any idea for debounce.

View attachment 243806
I am trying to figure out which kind of connection are between these three points to make state machine .

Is this logically correct state diagram? Please help me
A state is a set of conditions. If the conditions change, a new state will exist.

State.jpg
 
Last edited:

Thread Starter

Pushkar1

Joined Apr 5, 2021
212
A state is a set of conditions. If the conditions change, a new state will exist.

View attachment 243808
Thank you @KeithWalker for help

Sorry I need a clarification in your state diagram because I think you are missing circular arrow for each state in your diagram. As far as I searched on the internet, there shoud be a circular arrow for each state, you can also see my example. Any specific reason why don't have circular arrow in your diagram

+ I don't see logic for debounc in state diagram
 

andrewmm

Joined Feb 25, 2011
1,751
Thank you @KeithWalker for help

Sorry I need a clarification in your state diagram because I think you are missing circular arrow for each state in your diagram. As far as I searched on the internet, there shoud be a circular arrow for each state, you can also see my example. Any specific reason why don't have circular arrow in your diagram

+ I don't see logic for debounc in state diagram
hows the homework coming on ?
 

KeithWalker

Joined Jul 10, 2017
1,916
Thank you @KeithWalker for help

Sorry I need a clarification in your state diagram because I think you are missing circular arrow for each state in your diagram. As far as I searched on the internet, there shoud be a circular arrow for each state, you can also see my example. Any specific reason why don't have circular arrow in your diagram

+ I don't see logic for debounc in state diagram
There are two recognized ways of representing a state machine diagram. You used the traditional method and I used the newer and simpler "Harel" convention which produces less nodes and is much more readable for a complex machine.
The debounce is contained in both nodes as a condition to entering the transition. It could be represented with its own state diagram if required. That is entirely up to you.
https://en.wikipedia.org/wiki/State_diagram
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
212
There are two recognized ways of representing a state machine diagram. You used the traditional method and I used the newer and simpler "Harel" convention which produces less nodes and is much more readable for a complex machine.
@KeithWalker Thank you very much for the clarification. I don't see the difference in the diagrams of both of us logically because both have not mentioned debounce in the diagrams.

The debounce is contained in both nodes as a condition to entering the transition. It could be represented with its own state diagram if required. That is entirely up to you.
I am still not clear from debounce point of view. My question in #12 and #14 was regarding debounc. To which I have not yet received a satisfactory answer.

How do you trace a diagram for a debounc logic?
 

Dave Lowther

Joined Sep 8, 2016
101
@Pushkar1
For debounce you could have four states:
  1. Switch input stable low
  2. Switch input changed recently from stable low
  3. Switch input stable high
  4. Switch input changed recently from stable high

In the stable low state all you need to do is:
- check if it is still low, if so loop back into the same state.
- otherwise it is not still low so change to the 2nd state listed above

Etc. I don't have time to draw a diagram or explain in more detail right now as I'm in the middle of cooking tea.
 

KeithWalker

Joined Jul 10, 2017
1,916
@KeithWalker Thank you very much for the clarification. I don't see the difference in the diagrams of both of us logically because both have not mentioned debounce in the diagrams.


I am still not clear from debounce point of view. My question in #12 and #14 was regarding debounc. To which I have not yet received a satisfactory answer.

How do you trace a diagram for a debounc logic?
Debounce logic is a sub-process of the process of closing and opening a switch. There are a number of different ways in which it can be implemented, depending on the application. You would treat it like any other process. How would you implement it and draw the state diagram for it?
 

ErnieM

Joined Apr 24, 2011
8,177
State diagrams are something I write to insure I understand how a task needs to be enacted. I just wrote a state machine with well over 20 states, 3 or 4 major states (not running, running, finished) but the running state had 20+ minor states that mainly progressed 1 to 2o to 3 and so on once accomplished. (Aside: this was a python GUI app and you need to exit all subs to get the display to update.)

I would say if the debounce state diagram is confusing it is because an important state is being left out, mainly that of the debounce delay. So this may help (and don't look too hard at the notation I use):

STATE.jpg
 
Top