The microcontroller does not have an operating system. The codes written in Keil are all bare-metal codes. In-depth writing of bare-metal codes helps to understand the characteristics of the hardware.
If the hardware characteristics are not fixed, the other processes are all caused by the code. I suddenly thought of exploring the execution process of the 51 microcontroller. This idea originated from the initial observation that the main function of each 51 program has a while(1); statement at the end. Why add a while infinite loop to make the program stay in the main function? What is the impact of removing the while(1); statement?
Write a very simple program to try it out.
When the above program is executed, the running light controlled by the P1 port flashes. The program finally enters while(1); and gets stuck. This is easy to explain.
Now block the while(1); statement. I thought the program could not be executed correctly because the main function was exited, just like Render needs a loop to be implemented (although the program that just flashed the lights was not in the loop, I still had this illusion). The result of the program execution is: the running lights keep flashing!
My guesses and actions after seeing this phenomenon^-^:
(1) The board must be broken! (When running a C language file without an infinite loop in an operating system such as Linux, the program will return to the Linux shell program after completion.) I quickly changed the board and tested it again. Obviously, the result was the same.
(2) Will the last statement(s) in the main function always be executed in the MCU? (Based on my experience of running standard C language files under an OS platform, I have never thought about the main function being called or entered multiple times.)
(3) The C language instructions are taken out and loaded into the MCU, and a main program loop is automatically generated in the MCU to execute the contents of the main function in the C language? (Although it is ridiculous, I still thought about it)
(4) Quickly search for the execution flow of the microcontroller on Google (although no relevant content was found when searching for "51 microcontroller program execution flow" on Google). Change the search term to "51 microcontroller main". Then the same question as mine will appear: Why does the program execute repeatedly without adding a while(1); statement in the main function? The key words to answer the question include "program runaway, watchdog, reset".
(5) I took advantage of the opportunity of embedded system to bring out the “51 single-chip microcomputer program execution process” and told the teacher about the phenomena I got from the program I wrote, including how I verified it, etc.
Teacher's answer: The Keil C51 program automatically loads a file named "STARTUP.A51". After a series of initialization operations are performed in this file, it enters the main function of the C language program written by the user. After the main function is executed, there is a statement after the STARTUP.A51 file that jumps to the program entry main function, so it will enter the C language main program main function again to execute related content.
Then I used Keil software to simulate and run the above code:
The program starts running at the first statement of the main function at the program entry. The DISA ssembly window is the window corresponding to the C language code and the assembly code. The front is the address, and the back is the assembly statement corresponding to the C language. The window below is the location of the running code of the corresponding file, and the yellow arrow points to the code currently being executed. Then click the single-step run toolbar until the main function is jumped out, and the program jumps to the following code location in STARTUP.A51:
Continue clicking single-step debugging until you enter a loop:
Previous article:Summary of issues concerning 51 single-chip microcomputer crystal oscillator
Next article:Water supply system control automation based on 51 single chip microcomputer
- Popular Resources
- Popular amplifiers
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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board evaluation] Transplant RT-Thread Nano
- Since it is so similar to inductors, why can you only use ferrite beads in your circuit?
- Device tree interrupt node
- Can CC2541 support 10 external interrupts?
- PCB failure analysis and some cases
- Evaluation report summary: Nengdian Technology capacitive and photoelectric liquid level sensors
- Xunwei i.MX6ULL Terminator Mfgtools Modify Only Burn Uboot, Kernel, File System
- How to choose the flow capacity
- ADRV9002 Dual Narrow Bandwidth RF Transceiver
- Help, STM32 address data acquisition HardFault_Handler