How is the main program of the microcontroller executed?
[Copy link]
From the working principle of the MCU, we can see that the MCU executes the program to complete the tasks we require. There are many subroutines in the MCU. The MCU calls different subroutines according to different external execution conditions, thus completing different tasks. Below I will use a specific example to illustrate how the main program of the MCU is executed.
The working process of the microcontroller
Since the working process of the MCU is relatively complicated, in order to explain the problem, let's first briefly understand the working process of the MCU. When the MCU executes the command we give it, it follows a three-step method to execute it. It first takes out a command to be executed from its memory storage "container" ROM, and after receiving the command, it analyzes the specific content of the command, and then completes the corresponding task according to the specific content of the command. This is a bit the same as us humans receiving tasks.
Execution process of the microcontroller main program
Below we take a very familiar and simple traffic light program to illustrate how the main program is executed. We know that traffic lights have alternating red, green and yellow lights, and a digital countdown. Then the main task of the main program is to call each subroutine to work according to whether the conditions are met. The main program is a bit like the conductor of an orchestra. When a certain instrument is needed to make a sound, the conductor will guide the musicians to perform the performance. The conductor can repeat the execution over and over again. The main program of the microcontroller is similar to this. Once the power is turned on, the microcontroller will execute the main program first after the reset is completed. When the conditions are met, the subroutines are called continuously. In the traffic light program, the main program must continuously call the light-on subroutine and the digital tube countdown subroutine. Only in this way can the traffic light run repeatedly and repeatedly. This is the function of the main program. If there is no dead loop in the main program, the traffic light will only run once to flash the light and count down. Once it is executed once, there will be no more. So we have to put these subroutines in the dead loop statement of the main program so that the entire program runs over and over again.
Not only does the traffic light program work like this, but other programs do the same. Regardless of whether it is a branch structure or a sequential structure program, these subroutines must be placed in an infinite loop of the main program, and then the subroutines are called and executed according to the execution conditions.
The relationship between the microcontroller subroutine and the main program
Subroutines can be called in the main program, and subroutines can also call each other. However, there is a kind of program in the subroutine that we do not need to call from the main program, which is the interrupt program or interrupt timing program. This interrupt subroutine is generally a condition for the execution of the main program. Once the interrupt is triggered, the main program will immediately execute the interrupt subroutine. When the interrupt program is completed, the main program must return to the program just executed. Therefore, the subroutine is in the main program, and it must be called and executed by the main program continuously.
|