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
; 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.
Previous article:keil error ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT
Next article:Detailed explanation of Keil usage
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Guess you like
- 【CH579M-R1】+Color OLED screen display
- Wireless remote control circuit diagram production
- TI DaVinci Technology
- A list of classic books on communication principles and an analysis of the characteristics of each book
- FPGA Design Tips
- MSP430 LaunchPad Timer Interrupt (Continuous Mode)
- Recruiting BMS hardware design engineers Annual salary: 150,000-300,000 | Experience: 3 years or more | Work location: Wuxi City, Hebei Province
- I used STM32MP1 to build an epidemic monitoring platform 2—Qt environment construction
- AD18 fatal flaw BUG
- How to understand the power indication circuit composed of triodes and MOS tubes?