What is the function of the microcontroller's dead loop?
[Copy link]
The microcontroller is a programmable device. When using it, you need to write a program that meets your needs. After the initialization of each port and configuration is completed, its C language program will enter an infinite loop, generally in the form of while (1) {;}. After the initialization is completed, the microcontroller executes the program logic over and over again in the infinite loop. After reset, it starts from the beginning, and after the initialization is completed, it enters the infinite loop again.
The microcontroller executes the program one statement at a time from top to bottom. The configuration of the timer and the initialization of the port only need to be executed once, so the initialization content is placed outside the while (1) loop. The logic that requires real-time detection such as tasks needs to be placed in an infinite loop to allow the microcontroller to execute continuously.
MCU programs are divided into query type and interrupt type. The so-called query requires continuous scanning, which consumes more resources; while the interrupt has an interrupt flag bit, and the interrupt content is processed only after the interrupt flag is set, which saves more resources. Therefore, the query is guaranteed by the while (1) loop.
What is the function of the microcontroller's dead loop?
When the microcontroller is executing the main program, if an interrupt occurs, it will give priority to processing the interrupt content. After processing, it will continue to execute from the previous breakpoint. If multiple interrupts occur, the interrupt with a higher interrupt priority will be executed first.
|