Importance of StartUp.A51 in keil

Publisher:数字狂想Latest update time:2016-06-23 Source: eefocusKeywords:keil  StartUp.A51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I recently wanted to write a program using an STC microcontroller, but STC was not in the KEIL microcontroller library, so I just found a replacement. However, the Keil I found did not add the StartUp.A51 file. At first, I wrote and debugged the program normally. But later, it became more and more strange. There were frequent errors when powering on. After searching for a long time, I found that a variable was not initialized to 0. Suddenly I realized that Keil did not help me clear the memory space when powering on! I found that there was no StartUp.A51 file. After adding it, I set the internal and external space addresses, and everything was normal. It wasted a whole day of my time. Due to the importance of the CPU and program startup code file STARTUP.a51, some 8051-derived CPU products require the CPU to be initialized to meet the corresponding hardware in the design. Therefore, sometimes users need to modify STARTUP.a51, so here is a comment: ;--------------------------------------------------- ; startup.A51: User power-on initialization program; ---------------------------------------------------- ; ; Use the following EQU command to define the memory space that needs to be initialized with 0 when the CPU is reset; ; The absolute starting address of the IDATA memory space is always zero IDATALEN EQU 80H ; The number of bytes of the IDATA memory space that need to be initialized with 0; XDATASTART EQU 0H ; The absolute starting address of the XDATA memory space XDATALEN EQU 0H ; The number of bytes of the XDATA memory space that need to be initialized with 0; PDATASTART EQU 0H ; The absolute starting address of the PDATA memory space PDATALEN EQU 0H ;The number of bytes of PDATA memory space that need to be initialized with 0;Note: The IDATA memory space physically includes the DATA and BIT storage spaces of the 8051 microcontroller;At least the memory space related to the C51 compiler runtime library must be initialized with 0; ;Reentrant function simulation initialization;----------------------------------------------------------- ;The following uses the EQU instruction to define the initialization of the reentrant function simulation stack pointer; ;When using SMALL memory mode, the stack space of the reentrant function IBPSACK EQU 0 ;When using SMALL memory mode, set it to 1 when reentering the function IBPSTACKTOP EQU 0FFH+1 ;Set the top of the stack to the highest address plus 1; ;When using LARGE memory mode, the stack space of the reentrant function XBPSTACK EQU 0 ;When using LARGE memory mode, set it to 1 when reentering the function XBPSTACKTOP WQU 0FFFFH+1 ;Set the top of the stack to the highest address plus 1; ;When using COMPACT memory mode, the stack space of the reentrant function PBPSTACK EQU 0 ; Set it to 1 when reentering the function using COMPACT memory mode PBPSTACKTOP WQU 0FFFFH+1 ; Set the stack top to the highest address plus 1 ;;----------------------------------------------------; When using COMPACT memory mode, the paging definition of the 64KB XDATA memory space; ; The following uses the EQU instruction to define the page address of the PDATA type variable in the XDATA memory space; When using the EQU instruction to define PFAGE, it must be consistent with the control parameters of the L51 link locator PDATA instruction 
; PPAGEENABLE EQU 0 ; Set it to 1 when using PDATA type variables PPAGE EQU 0 ; Define the page number; ;------------------------------------------------ NAME ? C_STARTUP ; Module name? C_STARTUP ? C_51STARTUP SEGMENT CODE ; Code segment? STACK SEGMENT IDATA ; Stack segment RSEG ? STACK ; Stack DS 1 EXTRN COE(? C_START) ; Program start address PUBLIC ? C_STARTUP CSEG AT 0x8000 ;Define the starting address of the user program, which may be useful when using the MON51 emulator? C_STARTUP: LFMP STARTUP1 RSEG ? C_51STARTUP STARTUP1: ; ;Initialize the serial port MOV SCOM, #40H MOV TMOD, #20H MOV TH1, #0FDH SETB TR1 CLR T1 ;Clear the IDATA memory when the microcontroller is powered on. If you do not need to clear IDATA on power-on, you can cancel the ;statement between IF and IFEDN, or modify the length of IDTALEN. In order to make the CPU have power-off protection function, you need to determine the length of IDTALEN IF IDATALEN <> 0 MOV R0, # IDATALEN-1 CLR A IDATALOOP: MOV @R0,A DJNZ R0,IDATALOOP ENDIF ; ;Clear the XDATA memory when the microcontroller is powered on. If you do not need to clear XDATA on power-on, you can cancel the ;statement between IF and IFEDN, or modify the length of XDTALEN IF XDATALEN <> 0 MOV DPTR, #XDATASTART MOV R7,#LOW (XDATALEN) IF (LOW(XDATALEN)) <> 0 MOV R6, #(HIGH(XDATALEN))+1 ELSE MOV R6, #HIGH (XDATALEN) ENDIF CLR A XDATALOOP: MOVX @DPTR, A 
INC DPTR DJNZ R7, XDATALOOP DJNZ R6, XDATALOOP END IF ; ;Send the high address of the PDATA memory page IF PPAGEENABLE <> 0 MOV P2, #PPAGE ENDIF ; ;The PDATA memory is cleared when the MCU is powered on. If you do not need to clear XDATA on power-on, you can cancel the statements between IF and IFEDN or modify the length of PDATALEN IF PDATALEN <> 0 MOV R0, #PDATASTART MOV R7, #LOW (PDATALEN) CLR A PDATALOOP: MOV @R0, A INC R0 DJNZ R7,PDATALOOP ENDIF ; ;Set the stack space for reentry function when using SMALL memory mode IF IBPSTACK <> 0 EXTRN DATA(? C_IBP) MOV ? C_IBP, #LOW IBPSTACKTOP ENDIF ; ;Set the stack space for reentry function when using LARGE memory mode IF XBPSTACK <> 0 EXTRN DATA (? C_XBP) MOV ? C_XBP, #HIGH XBPSTACKTOP MOV ? C_XBP +1, #LOW XBPSTACKTOP ENDIF ; ;Set the stack space for reentry function when using COMPACT memory mode IF PBPSTACK <> 0 EXTRN DATA(? C_PBP) MOV ? C_PBP, #LOW PBPSTACKTOP END IF ; ;Set the starting address of the stack MOV SP, #? STACK-1 ;For example, MOV SP, #4FH ; ;If the program exceeds 64K, use program grouping technology to start the following program 
; EXTRN CODE(? B_SWITCH0) ;CALL ? B_SWITCH0 ;The program starts executing from the first group of bank 0 blocks;Jump to the user program MAIN function LJMP ? C_START END The role of startup.a51 is the same as that of assembly. The variables and arrays defined in C are initialized in startup.a51. If you define a global variable with a value, such as unsigned char data xxx="100";, then there will be a related assignment in startup.a51. If there is no =100, startup.a51 will clear it to 0. (startup.a51 == variable initialization). After these initializations are completed, the SP pointer will also be set. There will be no assignment or clearing action for non-variable areas, such as the stack area. Some people like to modify startup.a51 to satisfy their own self-righteous hobbies. This is unnecessary and may be wrong. For example, if you want to save some variables during power-off protection, it is a stupid method to modify startup.a51 to achieve this. In fact, you only need to use the characteristics of the non-variable area and define a pointer variable to point to the lower part of the stack: 0xff. Why do you still need to modify it? It can be said that you don't need to modify startup.a51 at any time if you understand its characteristics.
Keywords:keil  StartUp.A51 Reference address:Importance of StartUp.A51 in keil

Previous article:keil error ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT
Next article:Detailed explanation of Keil usage

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号