What Is the Function of the Microcontroller Startup File?

What Is the Function of the Microcontroller Startup File?

Author: Lao Ma Shi Tu Microcontroller

When we create a 51 microcontroller project using Keil C51, we will see a prompt as shown in the following image:

What Is the Function of the Microcontroller Startup File?

Keil prompts whether to add a startup file when creating a new project

Generally, you need to choose “Yes”. Of course, you can also choose not to add it. So, what is the function of this startup file? When is it necessary to add it, and when can it be omitted?

Today, we will take a detailed look at the content of this startup file. After understanding this content, we will have an epiphany: “Oh, so that’s how it is!”

First Part of the Startup Code

▼ Below is the original text of the first part of the startup code:

$NOMOD51;------------------------------------------------------------------------------;  This file is part of the C51 Compiler package;  Copyright (c) 1988-2005 Keil Elektronik GmbH and Keil Software, Inc.;  Version 8.01;;  *** <<< Use Configuration Wizard in Context Menu >>> ***;------------------------------------------------------------------------------;  STARTUP.A51:  This code is executed after processor reset.;;  To translate this file use A51 with the following invocation:;;     A51 STARTUP.A51;;  To link the modified STARTUP.OBJ file to your application use the following;  Lx51 invocation:;;     Lx51 your object file list, STARTUP.OBJ  controls;;------------------------------------------------------------------------------;;  User-defined <h> Power-On Initialization of Memory;;  With the following EQU statements the initialization of memory;  at processor reset can be defined:;; <o> IDATALEN: IDATA memory size <0x0-0x100>;     <i> Note: The absolute start-address of IDATA memory is always 0;     <i>       The IDATA space overlaps physically the DATA and BIT areas.IDATALEN        EQU     80H;; <o> XDATASTART: XDATA memory start address <0x0-0xFFFF> ;     <i> The absolute start address of XDATA memoryXDATASTART      EQU     0     ;; <o> XDATALEN: XDATA memory size <0x0-0xFFFF> ;     <i> The length of XDATA memory in bytes.XDATALEN        EQU     0      ;; <o> PDATASTART: PDATA memory start address <0x0-0xFFFF> ;     <i> The absolute start address of PDATA memoryPDATASTART      EQU     0H;; <o> PDATALEN: PDATA memory size <0x0-0xFF> ;     <i> The length of PDATA memory in bytes.PDATALEN        EQU     0H;;</h>

Below is the translation of the first part of the startup code:

Do not use predefined SFR. This tells the assembler not to use predefined register names, as the assembler internally defines the register names for the 51. However, in practical use, extended chips like 52 are used. If the header file for 52 is included, it will cause duplicate definitions, so it is necessary to declare that predefined register names will not be used.

This file is part of the C51 Compiler package

Copyright (c) 1988-2005 Keil Elektronik GmbH and Keil Software, Inc.

Version 8.01

*** <<Use Configuration Wizard in Context Menu>>> ***

—————————————————-

The code in STARTUP.A51 is executed after processor reset.

Use the following command line to invoke A51 to compile and generate the target file,

A51 STARTUP.A51

Use the following command line to invoke the BL51 linker to link the STARTUP.OBJ target file into the program code,

Lx51 invocation:

Lx51 invocation of the object file list, controlled by STARTUP.OBJ

User-defined power-on initialization of memory (initializing RAM data)

Initialize memory (RAM units) at processor reset using the following EQU pseudo-instructions

IDATALEN: Size of IDATA memory <0-256>, can be modified as desired

The absolute starting address of IDATA is always 0

IDATA area overlaps with DATA and BIT areas (DATA area (direct addressing area) and BIT area (bit addressing area)); at least ensure zero initialization of memory related to the C51 compiler runtime library

XDATA memory starting address <0x0-0xFFFF>

The absolute starting address of XDATA memory.

The absolute starting address of XDATA memory is 0,

The size of XDATA memory

The length of XDATA memory in bytes. It is specified that the number of bytes for XDATA is cleared to 0, and this value defaults to 0

The size of PDATA memory

The absolute starting address of PDATA memory

The number of bytes of PDATA memory that need to be initialized to 0

Differences between data, idata, xdata, and pdata in the 51 series:

  • data: Refers to the fixed 128 RAM from 0x00-0x7F.

  • idata: Refers to the fixed 256 RAM from 0x00-0xFF, where the first 128 is identical to data’s 128, just accessed differently.

  • xdata: External extended RAM, generally refers to the external space from 0x0000-0xFFFF.

  • pdata: The lower 256 bytes of external extended RAM.

What Is the Function of the Microcontroller Startup File?

The number of bytes of IDATA memory space that need to be initialized to 0, IDATALEN is just a label (different from IDATA), EQU serves as a macro-like replacement, similar to #define in C language, the above code makes the program replace IDATALEN with 80H in the future.

IDATALEN can be defined with any name you prefer, such as MyDataLen, etc. The reason for using IDATALEN is that it is easy to remember and indicates its relation to IDATA.

What Is the Function of the Microcontroller Startup File?

Various constant names and their meanings

Second Part of the Startup Code

Let’s continue to see what is included in the startup code of the 51 microcontroller.

▼ Below is the original text of the second part of the startup code:

;------------------------------------------------------------------------------;;&lt;h&gt; Reentrant Stack Initialization;;  The following EQU statements define the stack pointer for reentrant;  functions and initialized it:;; &lt;h&gt; Stack Space for reentrant functions in the SMALL model.;  &lt;q&gt; IBPSTACK: Enable SMALL model reentrant stack;     &lt;i&gt; Stack space for reentrant functions in the SMALL model.IBPSTACK        EQU     0       ; set to 1 if small reentrant is used.;  &lt;o&gt; IBPSTACKTOP: End address of SMALL model stack &lt;0x0-0xFF&gt;;     &lt;i&gt; Set the top of the stack to the highest location.IBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  ; &lt;/h&gt;;; &lt;h&gt; Stack Space for reentrant functions in the LARGE model.      ;  &lt;q&gt; XBPSTACK: Enable LARGE model reentrant stack;     &lt;i&gt; Stack space for reentrant functions in the LARGE model.XBPSTACK        EQU     0       ; set to 1 if large reentrant is used.;  &lt;o&gt; XBPSTACKTOP: End address of LARGE model stack &lt;0x0-0xFFFF&gt;;     &lt;i&gt; Set the top of the stack to the highest location.XBPSTACKTOP     EQU     0xFFFF +1   ; default 0FFFFH+1 ; &lt;/h&gt;;; &lt;h&gt; Stack Space for reentrant functions in the COMPACT model.    ;  &lt;q&gt; PBPSTACK: Enable COMPACT model reentrant stack;     &lt;i&gt; Stack space for reentrant functions in the COMPACT model.PBPSTACK        EQU     0       ; set to 1 if compact reentrant is used.;;   &lt;o&gt; PBPSTACKTOP: End address of COMPACT model stack &lt;0x0-0xFFFF&gt;;     &lt;i&gt; Set the top of the stack to the highest location.PBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  ; &lt;/h&gt;;&lt;/h&gt;

The original text is full of pseudo-instructions and macro definitions, which indeed looks quite daunting. Let’s simplify the translation.

▼ Below is the translation of the second part of the startup code:

Reentrant function stack initialization;

The following EQU statements define the initialization of the stack pointer for reentrant functions;

Stack space for reentrant functions in the SMALL model;

IBPSTACK EQU 0; set to 1 if small reentrant is used;

IBPSTACKTOP EQU 0FFH+1; set the top of the stack to the highest address +1;

Stack space for reentrant functions in the LARGE model;

XBPSTACK EQU 0; set to 1 if large reentrant is used;

XBPSTACKTOP EQU 0FFFFH+1; set the top of the stack to the highest address +1;

Stack space for reentrant functions in the COMPACT model;

PBPSTACK EQU 0; set to 1 if compact reentrant is used;

PBPSTACKTOP EQU 0FFFFH+1; set the top of the stack to the highest address +1.

▼ Analysis of the Three Models

Here we mention the SMALL, LARGE, and COMPACT modes. What do these three modes mean? Let’s learn more about them below.

Stack under different memory modes. The Keil compiler has three mode settings. This is caused by the many addressing modes of the 51 processor, and different addressing modes have different efficiencies.

Small Mode: In small mode, all default variables are loaded into the internal RAM of the microcontroller, which has a default internal RAM of only 128B for the 51 microcontroller and 256B for the 52 microcontroller. The advantage of this mode is fast access speed, while the disadvantage is limited space.

Compact Mode: In compact mode, all default variables are located in the 256B RAM of the microcontroller, which is similar to using the keyword pdata to define data variables in small mode. In this mode, the total variable space cannot exceed 256B.

Large Mode: In large mode, all default variables can be placed in up to 64KB of RAM, including internal and external RAM, which is the same effect as using the keyword xdata to define variables.

  • Small: Variables are stored in internal RAM.

  • Compact: Variables are stored in external RAM, using page 8-bit indirect addressing.

  • Large: Variables are stored in external RAM, using 16-bit indirect addressing.

We generally use small to store variables, meaning the microcontroller prioritizes storing variables in internal RAM, and only stores them externally if internal RAM is insufficient.

The compact method requires you to specify the high address of the page through the program, which is more complex. If there is very little external RAM, only 256 bytes, then reading that 256 bytes is relatively fast. However, if it exceeds 256 bytes, switching constantly can be troublesome. Compact mode is suitable for situations with less external RAM.

Large mode means that variables will be allocated to external RAM first.

All three storage methods support internal 256-byte and external 64K-byte RAM. The difference lies in where the variables are prioritized (or default) stored. Unless you do not want to store variables in internal RAM, you would use the latter compact and large modes. Because storing variables in internal RAM is much faster than storing them in external RAM, most applications choose the Small mode.

What Is the Function of the Microcontroller Startup File?

1. None

2. Why is the schematic always poorly drawn? These tips need to be known

3. How do embedded devices display IP location?

4. RISC-V targets MCU/MPU, RTOS, but faces challenges…

5. A method for self-updating firmware in microcontrollers!

6. The Xuantie Cup RISC-V Application Innovation Competition is officially launched, and registration is now open!

What Is the Function of the Microcontroller Startup File?

Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are copyright issues, please contact us. We will confirm the copyright and pay the manuscript fee or delete the content based on the copyright certificate you provide.

Leave a Comment