Why State Machine Architecture is Frequently Used in Embedded Development?

Why State Machine Architecture is Frequently Used in Embedded Development?

1. Improving CPU UtilizationWhenever I see a program filled with delay_ms(), I get a headache. Software delays of several tens of milliseconds are a huge waste of CPU resources, with precious CPU time wasted on NOP instructions.It also troubles me when the entire program halts just to wait for a pin level change or serial data. If the event never occurs, do you wait until the end of the world?By applying state machine programming concepts, the program only needs to use global variables to record the working state, allowing it to perform other tasks. Of course, after completing those tasks, it should check if the working state has changed.As long as the target event (timer not reached, level not changed, serial data not fully received) has not occurred, the working state remains unchanged, and the program continuously loops through “query – do something else – query – do something else,” keeping the CPU busy.This approach essentially inserts meaningful work during the program’s wait for events, preventing the CPU from idly waiting.2. Logical CompletenessLogical completeness is the greatest advantage of state machine programming.I wonder if anyone has ever written a small calculator program in C. I did a long time ago, and the results were disastrous!When I input a proper expression, the program produces the correct result, but if I deliberately input random combinations of numbers and operators, the program always yields nonsensical results.Later, I tried to mentally simulate the program’s operation. The correct expressions were clear and smooth, but when faced with irregular expressions, I became confused with so many flags and variables, and I couldn’t analyze it anymore.It wasn’t until much later that I encountered state machines and realized that my previous program had logical flaws.If we treat this calculator program as a reactive system, then a number or operator can be seen as an event, and an expression is a combination of events.For a logically complete reactive system, no matter what combination of events occurs, the system can correctly handle the events, and the system’s working state remains known and controllable.Conversely, if a system’s logical functionality is incomplete, it may enter an unknown and uncontrollable state under certain specific event combinations, contrary to the designer’s intentions.State machines can solve the problem of logical completeness.A state machine is a design method centered on system states and variable events, focusing on the characteristics of each state and the relationships between state transitions.The transitions between states are precisely triggered by events, so when studying a specific state, we naturally consider how any event affects that state.Thus, every event occurring in each state is taken into account, leaving no logical gaps.This may sound abstract, but practice reveals the truth. One day, if you really need to design a logically complex program, you will find state machines to be incredibly useful!3. Clear Program StructurePrograms written using state machines have a very clear structure.One of the most painful things for programmers is reading code written by others. If the code is not well-structured and there is no flowchart, reading it can be dizzying. You have to read through the program repeatedly to vaguely understand its overall operation.Having a flowchart helps a bit, but if the program is large, the flowchart won’t be very detailed, and many details still need to be understood from the code.In contrast, programs written with state machines are much better. With a standard UML state transition diagram and some concise textual explanations, all elements of the program are clear at a glance.What states exist in the program, what events can occur, how the state machine responds, and which state it transitions to are all very clear, and many action details can even be found in the state transition diagram.It is not an exaggeration to say that with a UML state transition diagram, there is no need to write a program flowchart.

Why State Machine Architecture is Frequently Used in Embedded Development?

Why State Machine Architecture is Frequently Used in Embedded Development?

Screenshots from some electronic books

Why State Machine Architecture is Frequently Used in Embedded Development?

Screenshots from some courseware PPTs

Why State Machine Architecture is Frequently Used in Embedded Development?

【Complete Set of Hardware Learning Materials】

Why State Machine Architecture is Frequently Used in Embedded Development?

Leave a Comment