What exactly is written in the startup code of the 51 microcontroller?

Publisher:SereneSerenityLatest update time:2023-06-26 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When we use kei c51 to create a 51 microcontroller project, there will be a prompt as shown below:

picture

When keil creates a new project, it prompts whether to add a startup file


Normally, you need to select "Yes". Of course, you can also choose not to add it. So, what is the role of this startup file? Under what circumstances does it need to be added, and under what circumstances can it not be added?


Today we will take a closer look at the content of this startup file. After understanding this content, we will have a sudden realization: "Oh, so this is it!"

Start the first paragraph of code

▼The following is the first paragraph of the original startup code:

strongerHuang • Source: Lao Ma Shi Tu MCU • Author: Lao Ma Shi Tu MCU • 2021-09-22 10:15 • 1508 times read

 0

When we use kei c51 to create a 51 microcontroller project, there will be a prompt as shown below:



picture


When keil creates a new project, it prompts whether to add a startup file


Normally, you need to select "Yes". Of course, you can also choose not to add it. So, what is the role of this startup file? Under what circumstances does it need to be added, and under what circumstances can it not be added?


Today we will take a closer look at the content of this startup file. After understanding this content, we will have a sudden realization: "Oh, so this is it!"


Start the first paragraph of code


▼The following is the first paragraph of the original 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  Power-On Initialization of Memory

;

;  With the following EQU statements the initialization of memory

;  at processor reset can be defined:

;

;  IDATALEN: IDATA memory size <0x0-0x100>

;      Note: The absolute start-address of IDATA memory is always 0

;            The IDATA space overlaps physically the DATA and BIT areas.

IDATALEN        EQU     80H

;

;  XDATASTART: XDATA memory start address <0x0-0xFFFF> 

;      The absolute start address of XDATA memory

XDATASTART      EQU     0     

;

;  XDATALEN: XDATA memory size <0x0-0xFFFF> 

;      The length of XDATA memory in bytes.

XDATALEN EQU 0      

;

;  PDATASTART: PDATA memory start address <0x0-0xFFFF> 

;      The absolute start address of PDATA memory

PDATASTART      EQU     0H

;

;  PDATALEN: PDATA memory size <0x0-0xFF> 

;      The length of PDATA memory in bytes.

PDATALEN        EQU     0H

;

;

▼The following is the translation of the first paragraph of the startup code:


Predefined SFRs are not used. It is to tell the assembler not to use the predefined register name, because the assembler internally defines the register name of 51, but in actual use, it will use the 51 expansion chip such as 52. If the 52 header file is included, it will appear. Repeated definition, so you must first declare the register name that is not applicable to the internal definition of the assembler.


This file is part of the C51 compiler package


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


Version 8.01


*** <>>> ***


----------------------------------------------------


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


Use the following command line statement to call A51 to compile and generate the target file:


A51 STARTUP.A51


Use the following command line statement to call the BL51 connector to connect the STARTUP.OBJ target file to the program code,


Lx51 invocation:


Lx51 call


---------------------------------------------------


Lx51 calls the target file list, controlled by the STARTUP.OBJ target file


User-defined storage area that needs to be initialized after power-on (initialize data in RAM area)


Use the following EQU directive to initialize the memory (RAM unit) when the processor is reset.


IDATALEN: The size of the IDATA storage area <0-256>, which can be modified according to your own choice


The absolute starting address of IDATA is always 0


The IDATA area covers the DATA and BIT areas (DATA area (direct addressing area) and BIT area (bit addressing area)); at least the memory space related to the C51 compiler runtime library must be guaranteed to be 0-initialized


Starting address of XDATA storage area <0x0-0xFFFF>


The absolute starting address of XDATA memory.


The absolute starting address of the XDATA memory space is 0,


Size of XDATA space


The length of the XDATA space in bytes


Note that the number of bytes in xdata is cleared to 0, and the value defaults to 0.


PDATA space size


The absolute starting address of the PDATA memory space


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


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


data: fixedly refers to the previous 128 RAMs from 0x00-0x7f.


idata: fixedly refers to the first 256 RAMs from 0x00-0xff, of which the first 128 are exactly the same as the 128 of data, just because of different access methods.


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


pdata: Lower 256 bytes of external expansion RAM.


The number of bytes of IDATA memory space that needs to be initialized with 0. IDATALEN is just a label (different from IDATA). EQU is just a macro replacement, similar to #define uint (unsigned int) in C language. The above The code causes the program to replace it with 80H when encountering IDATALEN in the future. IDATALEN can be defined as your own favorite name such as MyDataLen, etc. The reason why IDATALEN is used is that it is easy to remember, and the other is to show that it is related to IDATA.


Start the second section of code


Let's continue to take a look at what is in the startup code of the 51 microcontroller.


▼The original text of the second part of the 51 microcontroller startup code is listed below:


;------------------------------------------------------------------------------

;

; Reentrant Stack Initialization

;

;  The following EQU statements define the stack pointer for reentrant

;  functions and initialized it:

;

;  Stack Space for reentrant functions in the SMALL model.

;   IBPSTACK: Enable SMALL model reentrant stack

;      Stack space for reentrant functions in the SMALL model.

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

;   IBPSTACKTOP: End address of SMALL model stack <0x0-0xFF>

;      Set the top of the stack to the highest location.

IBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  

;

;  Stack Space for reentrant functions in the LARGE model.      

;   XBPSTACK: Enable LARGE model reentrant stack

;      Stack space for reentrant functions in the LARGE model.

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

;   XBPSTACKTOP: End address of LARGE model stack <0x0-0xFFFF>

;      Set the top of the stack to the highest location.

XBPSTACKTOP     EQU     0xFFFF +1   ; default 0FFFFH+1 

;

;  Stack Space for reentrant functions in the COMPACT model.    

;   PBPSTACK: Enable COMPACT model reentrant stack

;      Stack space for reentrant functions in the COMPACT model.

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

;

;    PBPSTACKTOP: End address of COMPACT model stack <0x0-0xFFFF>

;      Set the top of the stack to the highest location.

PBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  

;

The original text is full of pseudo-instructions and macro definitions, which really seems to be a headache. Let’s briefly translate it.


▼The following is the translation of the second startup code:


Reentry function simulation initialization;


The following uses the EQU instruction to define the initialization of the reentry function simulation stack pointer;


The stack space of the reentrant function when using SMALL memory mode;


IBPSTACK EQU 0 ; Set it to 1 when using the SMALL memory mode reentry function;


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


The stack space of the reentrant function when using LARGE memory mode;


XBPSTACK EQU 0 ; Set it to 1 when using the LARGE memory mode reentry function;


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


The stack space of the reentrant function when using COMPACT memory mode;


PBPSTACK EQU 0 ; Set it to 1 when using the COMPACT memory mode reentry function;


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


▼Analysis of three modes


Three modes, SMALL, LARGE and COMPACT, are mentioned here. What do these three modes mean? Let’s find out below.


Stack in different memory modes. There are three mode settings in the Keil compiler. This is caused by the various addressing modes of the 51 processor. Different addressing modes have different efficiencies.


Small mode: In small mode, all default variables are loaded into the RAM inside the microcontroller. The default internal RAM of the 51 microcontroller is only 128B; the default internal RAM of the 52 microcontroller is 256B. The advantage of this mode is fast access speed, but the disadvantage is limited space.


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


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

[1] [2]
Reference address:What exactly is written in the startup code of the 51 microcontroller?

Previous article:The composition and format analysis of 89 series microcontroller model codes
Next article:Design of I2C bus interface circuit based on MCS-51 microcontroller

Latest Microcontroller Articles
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号