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.
Previous article:How to "build a conversation" between different types of microcontrollers? From these examples
Next article:"Driver separation" design idea of microcontroller firmware
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- 【GD32L233C-START Review】Display driver for color OLED screen
- Would you choose the popular outdoor power supply?
- Solution to severe static heating of wireless network card
- China's latest classification catalogue of medical devices (No. 104, 2017)
- What is the function of the cross connection of the five tubes in the middle of this comparator?
- What is the difference between pyb.delay and time.sleep?
- Stepper motor current test
- [RVB2601 creative application development] + light intensity detection
- 【DIY Creative LED】Effect Demonstration
- [GD32450I-EVAL] USART and DMA variable length reception