State machine programming ideas for single-chip microcomputer

Publisher:玉树琼花Latest update time:2023-04-06 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


The essence of this processing method is to insert some meaningful work at intervals while the program is waiting for events, so that the CPU does not wait needlessly all the time.


(2) Logical completeness

I think logical completeness is the biggest advantage of state machine programming.

I don’t know if you have ever written a small calculator program in C language. I wrote it a long time ago, and when I tested it, it was terrible! When I input calculations in a disciplined manner, the program can get correct calculation results, but if I deliberately input random combinations of numbers and operation symbols, the program always gets inexplicable results.


Later, I tried to simulate the working process of the program. The correct calculation formula was clear and the process was smooth. But when I encountered an irregular formula, I felt dizzy while walking. There were so many flags, so many The variables kept changing, and finally the analysis could no longer be carried out.


After a long time, I got to know the state machine, and then I suddenly realized that the program at that time had logical loopholes. If this calculator program is regarded as a reactive system, then a number or operator can be regarded as an event, and a calculation is a set of event combinations.


For a reactive system with complete logic, the system can handle events correctly no matter what combination of events, and the working status of the system itself is always in a knowable and controllable state. On the other hand, if the logical function of a system is incomplete, driven by certain combinations of specific events, the system will enter an unknowable and uncontrollable state, contrary to the designer's intention.


State machines can solve the problem of logical completeness.

State machine is a design method that centers on the system state and uses events as variables. It focuses on the characteristics of each state and the mutual conversion relationship between states. State transitions are precisely caused by events, so when studying a specific state, we will naturally consider the impact of any event on this state. In this way, every event that occurs in every state will be considered by us, and there will be no logical loopholes left.


Maybe everyone will think that this is empty, but practice will bring true knowledge. One day, if you really want to design a program with complex logic, I guarantee you will say: wow! State machines are really useful!


[1] [2]
Reference address:State machine programming ideas for single-chip microcomputer

Previous article:Selection of microcontroller, a few steps that have to be mentioned
Next article:Doesn’t the microcontroller deserve to be called embedded? What is the relationship between embedded and microcontroller?

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号