What does the microcontroller do after the program ends?

Publisher:堕落的猫Latest update time:2022-12-08 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

For embedded systems, if RTOS is not running, the main function main() in program development needs to use some mechanism to make it run happily forever. It has no end. If you want to exit from the main function, what you do depends on the C language compiler used.


1. Question raising

Today I saw an interesting problem in the definition function of the microcontroller LED module. The questioner was conducting basic C51 programming experiments and wrote a simple C51 program as follows:

After the program is executed, you can see that two LEDs on the experimental board are lit, and the other six are actually slightly bright.

If you add an infinite loop: while(1); in the main program, the "slightly lit" phenomenon on the circuit board will no longer appear.

The difference between the above two situations is that in the second program, the main loop main() function never exits, while in the first program, the main() function exits. It seems that the slight lighting of the front LED should be related to what the microcontroller did after the main function exited.

Then there is only one question left: For ordinary embedded systems, after the main() function exits in C language programming, where does the program go?

2. Where did the program go?

Judging from the code written by the questioner above, he should be a C51 enthusiast, using the C51 compiler, and happily conducting experiments on a C51 development board. At the beginning, he did not install the conventions of embedded program development and used an infinite loop in the main program void main(void) to control the program in the main program function. This led to the confusing situation in the previous experimental results.

2.1 Pangu created the world

For C language programming, all user program world starts from the main program main(). The task of creating a new world for the user program is a small piece of Pangu code STARTUP.A51.

51 microcontroller program execution flow (STARTUP.A51 manages the execution of the Main function)

Below is a section of the STARTUP.A51 code. You can see that after Pangu made some preparations (initializing global variables and stack pointers) after the microcontroller RESET, he jumped directly to: ?C_START

The above code has also been verified by step-by-step debugging and tracking in the 51 microcontroller program execution process (STARTUP.A51):

2.2 The end of the world

Since entering the main() function is a long jump, the main function will not return to the startup program STARTUP.A51 normally, so where does the program go?

In the blog post on the problem of while(1) in the C language of microcontroller, the author disassembled and viewed the last moments of the main function by the KEIL compiler and the PIC's MAPLAB compiler.

Keil compiler

At the end of the main function, the program adds a few lines of code:

Of these statements, the first 4 are to clear the first 128 addresses of our microcontroller's memory, the 5th is to define the stack, and the 6th is to jump the program back to the first line of the main function for execution.

MAPLAB compiler

The PIC microcontroller language program was traced and found that the last statement of the main() function is reset, which means the microcontroller is directly reset. This is a reset statement added by the MAPLAB compiler based on the characteristics of the PIC microcontroller.

Summarize

For embedded systems, if RTOS is not running, the main function (main()) in program development needs to use some mechanism to make it run happily forever. It has no end. If you want to exit from the main function, what you do depends on the C language compiler used.


Reference address:What does the microcontroller do after the program ends?

Previous article:How to "build a conversation" between different types of microcontrollers? From these examples
Next article:"Driver separation" design idea of ​​microcontroller firmware

Latest Microcontroller Articles
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号