51 microcontroller program in-depth written bare metal code helps to understand the characteristics of the hardware

The microcontroller is not on the operating system, the code written in keil is bare-metal code, and in-depth writing of bare-metal code helps to understand the characteristics of the hardware.

If the hardware is not fixed, the other processes are code. Suddenly thought of exploring the execution process of 51 single-chip microcomputer. This idea originated from the fact that the main function in each of the 51 programs was originally found to hang a while(1); statement. Why add a while loop to let the program stay in the main function. What is the effect of removing the while(1); statement?

Write a very simple program to try.

Execute the above procedure, the water lamp controlled by the P1 port flashes. The program finally enters while(1); it is entangled, this is a good explanation.

Now mask the while(1); statement. I thought that the program could not be executed correctly, because the main function was exited, just as Render needed a loop to implement (although the program that just flashed was not in the loop, I couldn't help but create this illusion). The result of the program execution is: the water light flashes constantly!

Guess and action after seeing this phenomenon ^-^:

(1) This board is broken! (After running a C language file without an infinite loop with an operating system such as linux character interface, it will return to the linux shell program). Hurry and change the board and test it again, obviously the same result.

(2) The last (some) statement in the main function will always be executed in the MCU? (Based on the experience of running standard C language files with the OS platform, it has never been thought that the main function is called multiple times or multiple times)

(3) The C language instruction is taken out from the MCU and loaded into the MCU. The MCU automatically generates a main program to execute the main function of the C language cyclically (although it is ridiculous, still think)

(4) Hurry up Google Baidu's execution process of the MCU (although in the Google Baidu when the "51 MCU program execution process" search, did not find the relevant content). Change the simple search term: "51 single-chip main". Then there is a question with the same question as me: Why doesn't the main function contain while(1); after the statement, the program will be executed repeatedly? The key words for the answer include "program runaway, watchdog, reset".

(5) The opportunity of embedded embedded will move out the “51 MCU program execution flow” and tell the teacher about the phenomenon of the program I wrote, including how to verify it.

Teacher's answer: The Keil C51 program automatically loads a file named "STARTUP.A51". After a series of initialization operations in this file, it enters the main function of the C language program entry written by the user. After the main function is executed, After the STARTUP.A51 file, there is a statement that jumps to the main function of the program entry, so it will enter the main function of the C main program again to execute the relevant content.

Then I used the keil software to simulate running the above code:

The program starts running at the first statement of the main function of the program entry. The Disassembly window is the window corresponding to the C language code and the assembly code. The front is the address, followed by the assembly statement corresponding to the C language. The window below is the location of the running code for the corresponding file, with a yellow arrow pointing to the code currently being executed. Then click the single-step run toolbar to guide the jump out of the main function, the program jumps to the following code position in STARTUP.A51:

Continue to click on the single step until you enter a loop:

Here is a loop, according to the function of the DJNZ instruction: every time DJNZ RO is executed, IDATALOOP decrements the value of R0 by one. If the value of R0 is not 0, it jumps to the IDATALOOP address. Obviously this is a loop, then what is the value of RO, which is displayed in the following window:

It can be seen that the initial value of r0 is 0x7f, and 0x7f(128) times will be looped here. The meaning of r0 value can be seen here. So where will the program go after this loop? The place where the program runs after skipping this loop is as follows:

Run it once more:

According to the content of Disassembly, this statement will be returned to the main function after execution. Try it out:

Yes!

Therefore, in the 51 single-chip microcomputer, the execution flow of the program is continuous (using the value of r0 as the delay condition, the specific meaning can continue to explore) into the main function to execute the code in the main function.

Why do we terminate the program itself after running C code without dead loop on Linux, etc. This is a different operation process:

(1) C51 microcontroller does not have OS (operating system), the execution of the code seems to be arranged by STARTUP.A51, there is no bigger program to manage how to call the main function.

(2) A platform like Linux comes with an OS. Running a C program is a task for Linux. There are other tasks besides running the C program. When running a C language program, the task is complete. For example, if you run a C program named "hello.c" in the linux shell interface to output "hello world!", the process is as follows:

Compile: gcc hello.c –o hello

Run: ./hello

When running the hello executable, you can call the hello executable as a shell. After the hello is finished running, return the return value and so on to the shell interface. The entire C language file life and death have linux shell program management.

The reason for this is that the mechanism prescribed by the code is different.

Right Angle DIP Centronic Connector

Right Angle DIP Centronic Connector.

Current Rating:5A
Dielectric Withstanding Voltage:1000V for one minute
Insulation Resistance:1000MΩ Min.(at 500V DC)
Contact Resistance:35mΩ Max.
Temperature:-55°C to +105°C

Right Angle DIP Centronic Connector

ShenZhen Antenk Electronics Co,Ltd , http://www.coincellholder.com