Detailed explanation of 51 single chip microcomputer program execution process

Publisher:江上HZLatest update time:2017-12-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    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:

     


Reference address:Detailed explanation of 51 single chip microcomputer program execution process

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号