I am Lao Wen, an embedded engineer who loves learning.Follow me to become even better together!The state machine is a commonly used architecture in embedded software programming. In embedded software without an RTOS, using a state machine allows for convenient handling of multiple tasks within the main program loop. The benefits of using a state machine framework are as follows.1. Improved CPU UtilizationWhenever I see a program filled with delay_ms(), I get a headache. Software delays of 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 have to wait until the end of the world?If we apply the state machine programming concept, 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 “check – do something else – check – do something else,” keeping the CPU busy.This method 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 learned about state machines and realized that my previous program had logical flaws.If we consider 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, regardless of the event combinations, the system can correctly handle the events, and its 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 events as variables, 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 the code can be dizzying. You have to follow the program repeatedly, and only after many iterations can you vaguely understand the overall working process.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 descriptions, 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. 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 at all.
-END-
Previous Recommendations: Click the image to read more
Where does the program go after the main() function ends?

What should you pay attention to when using embedded open-source code?

Embedded development has low pay and many troubles; I advise you not to enter this field?
I am Lao Wen, an embedded engineer who loves learning.Follow me to become even better together!