单片机STARTUP.A51详解
扫描二维码
随时随地手机看文章
Startup code:启动代码。在Keil中,启动代码在复位目标系统后立即被执行。启动代码主要实现以下功能:
(1) 清除内部数据存储器
(2) 清除外部数据存储器
(3) 清除外部页存储器
(4) 初始化small模式下的可重入栈和指针
(5) 初始化large模式下的可重入栈和指针
(6) 初始化compact模式下的可重入栈和指针
(7) 初始化8051硬件栈指针
(8) 传递初始化全局变量的控制命令或者在没有初始化全局变量时给main函数传递命令。
在每一个启动文件中,提供了可供用户自己修改有来控制程序执行的汇编常量。见表1
表1
Name |
Description |
IDATALEN |
Specifies the number of bytes of idata to clear to 0. The default is 80h because most 8051 derivatives contain at least 128 bytes of internal data memory. Use a value of 100h for the 8052 and other derivatives that have 256 bytes of internal data memory. |
XDATASTART |
Specifies the initial xdata address to clear to 0. |
XDATALEN |
Indicates the number of bytes of xdata to clear to 0. The default is 0. |
PDATASTART |
Specifies the initial pdata address to clear to 0. |
PDATALEN |
Specifies the number of bytes of pdata to clear to 0. The default is 0. |
IBPSTACK |
Specifies whether or not the small model reentrant stack pointer (?C_IBP) should be initialized. A value of 1 causes this pointer to be initialized. A value of 0 prevents initialization of this pointer. The default is 0. |
IBPSTACKTOP |
Specifies the top of the small model reentrant stack. The default is 0xFF in idata memory. |
XBPSTACK |
Specifies whether or not the large model reentrant stack pointer (?C_XBP) should be initialized. A value of 1 causes this pointer to be initialized. A value of 0 prevents initialization of this pointer. The default is 0. |
XBPSTACKTOP |
Specifies the top of the large model reentrant stack. The default is 0xFFFF in xdata memory. |
PBPSTACK |
Specifies whether the compact model reentrant stack pointer (?C_PBP) should be initialized. A value of 1 causes this pointer to be initialized. A value of 0 prevents initialization of this pointer. The default is 0. |
PBPSTACKTOP |
Specifies the top of the compact model reentrant stack. The default is 0xFF in pdata memory. |
PPAGEENABLE |
Enables (a value of 1) or disables (a value of 0) Port 2 initialization for pdata memory access. The default is 0. pdata addressing uses Port 2 for the upper address (or page) byte. |
PPAGE |
Specifies the value to write to Port 2 of the 8051 for pdata memory access. This value represents the xdata memory page to use for pdata. This is the upper 8 bits of the absolute address range to use for pdata. For example, if the pdata area begins at address 1000h (page 10h) in xdata memory, PPAGEENABLE should be set to 1, and PPAGE should be set to 10h. You must specify the starting pdata address to use to the BL51 Linker using the PDATA directive. For example: BL51 input modules PDATA (1050H) Neither the BL51 Linker nor the Cx51 Compiler checks to see if the PDATA directive and the PPAGE startup constant are correctly specified. You must ensure that these parameters contain suitable values. |
上面这些只是标号,如果愿意,自己可以换成其他的名字。这样写意义更直观。
$NOMOD51
;不使用预先定义的SFR,The NOMOD51 directive suppresses pre-definition of 8051 SFR
;names. This directive must be used when a customer-specific SFR definition file is included.
;------------------------------------------------------------------------------
; 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用下面的命令传递该文件
; A51 STARTUP.A51
;
; To link the modified STARTUP.OBJ file to your application use the following
; Lx51 invocation:
;利用下面Lx51命令来链接修改的STARTUP.OBJ文件到实际的应用
; Lx51 your object file list, STARTUP.OBJ controls
;
;------------------------------------------------------------------------------
;
; User-defined
;用户自定义上电初始化内存
; With the following EQU statements the initialization of memory
; at processor reset can be defined:
;在处理器复位时通过EQU来初始化内存
;
;IDATALEN:IDATA存储区的大小<0-256>,可以根据自己的选择修改
; Note: The absolute start-address of IDATA memory is always 0
; The IDATA space overlaps physically the DATA and BIT areas.
;IDATA绝对的起始地址总是0
;IDATA空间涵盖物理上的DATA和BIT区
;需用0来初始化idata区的字节数
IDATALEN EQU 80H
;
;
; The absolute start address of XDATA memory
;XDATA存储区的起始地址,XDATA内存的绝对起始地址。
XDATASTART EQU 0
;指定初始的XDATA地址清0
;
; The length of XDATA memory in bytes.
;XDATA空间的长度,以字节为单位
XDATALEN EQU 0
;说明xdata的字节数清0,该值默认为0
;
; The absolute start address of PDATA memory
PDATASTART EQU 0H
;
;
; The length of PDATA memory in bytes.
PDATALEN EQU 0H
;
;
;------------------------------------------------------------------------------
;
;
;重入栈的初始化
; The following EQU statements define the stack pointer for reentrant
; functions and initialized it:
;EQU语句定义了重入函数的栈指针并初始化
;
;SMALL模式下的重入函数栈
; IBPSTACK: Enable SMALL model reentrant stack
; IBPSTACK = 1使能模拟栈
; Stack space for reentrant functions in the SMALL model.
IBPSTACK EQU 0 ; set to 1 if small reentrant is used.
;
; IBPSTACKTOP:栈的结束地址<0x0-0xFF>
; Set the top of the stack to the highest location.
;设置为最高地址(向下生长的)
;默认FFH+1(其实就是0,使用时进行操作(+0xFF)使其变为0xFF)
IBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
;
;下面是LARGE模式下的模拟栈,和SMALL相同不做特别说明
;
; 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.
;
; Set the top of the stack to the highest location.
XBPSTACKTOP EQU 0xFFFF +1 ; default 0FFFFH+1
;
;COMPACT模式下
;
; 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.
;
;
; Set the top of the stack to the highest location.
PBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
;
;
;------------------------------------------------------------------------------
;COMPACT模式下64K xdata的页存储器
; Memory Page for Using the Compact Model with 64 KByte xdata RAM
;
;通过PDATA变量定义XDATA页
; Define the XDATA page used for PDATA variables.
; PPAGE must conform with the PPAGE set in the linker invocation.
;PPAGE必须与链接器引用设置一致
; Enable pdata memory page initalization
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used.
;设置1使能
;
; uppermost 256-byte address of the page used for PDATA variables.
;页号,PDATA变量高256字节地址(高8位地址)
PPAGE EQU 0
;
;
; most 8051 variants use P2 as uppermost address byte
;提供高字节地址的SFR的地址(高8位地址的存放位置),一般的8051单片机用P2口
PPAGE_SFR DATA 0A0H
;
;
;------------------------------------------------------------------------------
; Standard SFR Symbols
;使用伪指令DATA为寄存器分配地址或者为响应的地址起一个别名
ACC DATA 0E0H
B DATA 0F0H
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
NAME ?C_STARTUP ;定义当前模块的目标模块名
;NAME:
; NAME (modulename)
;NAME为输出文件定义一个模块名称(modulename),如果在文件中没有特定的
; (modulename),默认的应用第一个输入文件的名称。
?C_C51STARTUP SEGMENT CODE
;定义一个代码段,名称?C_C51STARTUP
?STACK SEGMENT IDATA ;堆栈
RSEG ?STACK ;RSEG选择一个先前声明的可重定位的段
DS 1 ;为堆栈预留一个低阶的存储空间
EXTRN CODE (?C_START) ;当前源文件中用的代码段存储区的符号?C_START,在其他的目标模块中定义
PUBLIC ?C_STARTUP ;声明可以用于其他目标模块的全局符号?C_STARTUP,用于和C相连接在.src文件中可以看到这个符号
CSEG AT 0 ;选择代码存储区内的一个绝对段,汇编从上面命令中的地址0开始执行这个段。
?C_STARTUP: LJMP STARTUP1 ;芯片上电复位后,执行的第一句就是该句,该句往下是开始执行启动代码
RSEG ?C_C51STARTUP ;选择代码段?C_C51STARTUP
STARTUP1: IF IDATALEN <> 0 ;如果IDATALEN不为0,则将长度-1送R0
MOV R0,#IDATALEN - 1
CLR A
IDATALOOP: MOV @R0,A ;将IDATA区清0
DJNZ R0,IDATALOOP
ENDIF
IF XDATALEN <> 0 ;如果有外部数据存储区
MOV DPTR,#XDATASTART ;起始地址送DPTR
MOV R7,#LOW (XDATALEN) ;长度低8为送R7
IF (LOW (XDATALEN)) <> 0 ;低8位不为0
MOV R6,#(HIGH (XDATALEN)) +1 ;高8位+1送R6,下面清0用
ELSE
MOV R6,#HIGH (XDATALEN)
ENDIF
CLR A ;清0
XDATALOOP: MOVX @DPTR,A
INC DPTR
DJNZ R7,XDATALOOP
DJNZ R6,XDATALOOP
ENDIF
IF PPAGEENABLE <> 0 ;使能外部页编址
MOV PPAGE_SFR,#PPAGE ;页号送SFR
ENDIF
IF PDATALEN <> 0 ;0-FF之间
MOV R0,#LOW (PDATASTART)
MOV R7,#LOW (PDATALEN)
CLR A
PDATALOOP: MOVX @R0,A ;清0
INC R0
DJNZ R7,PDATALOOP
ENDIF
IF IBPSTACK <> 0 ;使能SMALL模式下的模拟栈
EXTRN DATA (?C_IBP) ;使用其他目标模块中定义的?C_IBP(模拟栈指针)
;(.M51文件中)
MOV ?C_IBP,#LOW IBPSTACKTOP ;模拟栈指针指向栈顶
ENDIF
IF XBPSTACK <> 0 ;Large模式下使能模拟栈
EXTRN DATA (?C_XBP)
;栈指针指向栈顶
MOV ?C_XBP,#HIGH XBPSTACKTOP
MOV ?C_XBP+1,#LOW XBPSTACKTOP
ENDIF
IF PBPSTACK <> 0 ;COMPACT模式下的模拟栈
EXTRN DATA (?C_PBP)
MOV ?C_PBP,#LOW PBPSTACKTOP ;指向栈顶
ENDIF
MOV SP,#?STACK-1 ;硬件栈SP赋值
;硬件栈与模拟栈相区别,硬件栈是向上的,模拟栈是向下的
;在M51中可以看到具体的?STACK的值
;模拟栈的指针指向所在模式下,存储区最大地址+1(0xFF+1或0xFFFF+1)
;硬件栈初始化指向栈底-1;
;因为C51中栈操作是先操作栈指针(加或减),然后在写数
; This code is required if you use L51_BANK.A51 with Banking Mode 4
;
; Select Bank 0 for L51_BANK.A51 Mode 4
#if 0
; Initialize bank mechanism to code bank 0 when using L51_BANK.A51 with Banking Mode 4.
EXTRN CODE (?B_SWITCH0)
CALL ?B_SWITCH0 ; init bank mechanism to code bank 0
#endif
;
LJMP ?C_START ;执行main函数
END
;该文件中的一些符号的值的大小,可以在M51中看到,此外,可以通过系统上电复位后反
;汇编程序看到详细的执行过程
;文件中的一些命令可以查阅KeilC帮助文档