單片機(jī)STARTUP.A51詳解
Startup code:?jiǎn)?dòng)代碼。在Keil中,啟動(dòng)代碼在復(fù)位目標(biāo)系統(tǒng)后立即被執(zhí)行。啟動(dòng)代碼主要實(shí)現(xiàn)以下功能:
(1) 清除內(nèi)部數(shù)據(jù)存儲(chǔ)器
(2) 清除外部數(shù)據(jù)存儲(chǔ)器
(3) 清除外部頁(yè)存儲(chǔ)器
(4) 初始化small模式下的可重入棧和指針
(5) 初始化large模式下的可重入棧和指針
(6) 初始化compact模式下的可重入棧和指針
(7) 初始化8051硬件棧指針
(8) 傳遞初始化全局變量的控制命令或者在沒(méi)有初始化全局變量時(shí)給main函數(shù)傳遞命令。
在每一個(gè)啟動(dòng)文件中,提供了可供用戶自己修改有來(lái)控制程序執(zhí)行的匯編常量。見(jiàn)表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. |
上面這些只是標(biāo)號(hào),如果愿意,自己可以換成其他的名字。這樣寫(xiě)意義更直觀。
$NOMOD51
;不使用預(yù)先定義的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.
;代碼在處理器復(fù)位后執(zhí)行
; To translate this file use A51 with the following invocation:
;通過(guò)A51用下面的命令傳遞該文件
; A51 STARTUP.A51
;
; To link the modified STARTUP.OBJ file to your application use the following
; Lx51 invocation:
;利用下面Lx51命令來(lái)鏈接修改的STARTUP.OBJ文件到實(shí)際的應(yīng)用
; Lx51 your object file list, STARTUP.OBJ controls
;
;------------------------------------------------------------------------------
;
; User-defined
;用戶自定義上電初始化內(nèi)存
; With the following EQU statements the initialization of memory
; at processor reset can be defined:
;在處理器復(fù)位時(shí)通過(guò)EQU來(lái)初始化內(nèi)存
;
;IDATALEN:IDATA存儲(chǔ)區(qū)的大小<0-256>,可以根據(jù)自己的選擇修改
; Note: The absolute start-address of IDATA memory is always 0
; The IDATA space overlaps physically the DATA and BIT areas.
;IDATA絕對(duì)的起始地址總是0
;IDATA空間涵蓋物理上的DATA和BIT區(qū)
;需用0來(lái)初始化idata區(qū)的字節(jié)數(shù)
IDATALEN EQU 80H
;
;
; The absolute start address of XDATA memory
;XDATA存儲(chǔ)區(qū)的起始地址,XDATA內(nèi)存的絕對(duì)起始地址。
XDATASTART EQU 0
;指定初始的XDATA地址清0
;
; The length of XDATA memory in bytes.
;XDATA空間的長(zhǎng)度,以字節(jié)為單位
XDATALEN EQU 0
;說(shuō)明xdata的字節(jié)數(shù)清0,該值默認(rèn)為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語(yǔ)句定義了重入函數(shù)的棧指針并初始化
;
;SMALL模式下的重入函數(shù)棧
; 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:棧的結(jié)束地址<0x0-0xFF>
; Set the top of the stack to the highest location.
;設(shè)置為最高地址(向下生長(zhǎng)的)
;默認(rèn)FFH+1(其實(shí)就是0,使用時(shí)進(jìn)行操作(+0xFF)使其變?yōu)?xFF)
IBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
;
;下面是LARGE模式下的模擬棧,和SMALL相同不做特別說(shuō)明
;
; 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的頁(yè)存儲(chǔ)器
; Memory Page for Using the Compact Model with 64 KByte xdata RAM
;
;通過(guò)PDATA變量定義XDATA頁(yè)
; Define the XDATA page used for PDATA variables.
; PPAGE must conform with the PPAGE set in the linker invocation.
;PPAGE必須與鏈接器引用設(shè)置一致
; Enable pdata memory page initalization
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used.
;設(shè)置1使能
;
; uppermost 256-byte address of the page used for PDATA variables.
;頁(yè)號(hào),PDATA變量高256字節(jié)地址(高8位地址)
PPAGE EQU 0
;
;
; most 8051 variants use P2 as uppermost address byte
;提供高字節(jié)地址的SFR的地址(高8位地址的存放位置),一般的8051單片機(jī)用P2口
PPAGE_SFR DATA 0A0H
;
;
;------------------------------------------------------------------------------
; Standard SFR Symbols
;使用偽指令DATA為寄存器分配地址或者為響應(yīng)的地址起一個(gè)別名
ACC DATA 0E0H
B DATA 0F0H
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
NAME ?C_STARTUP ;定義當(dāng)前模塊的目標(biāo)模塊名
;NAME:
; NAME (modulename)
;NAME為輸出文件定義一個(gè)模塊名稱(modulename),如果在文件中沒(méi)有特定的
; (modulename),默認(rèn)的應(yīng)用第一個(gè)輸入文件的名稱。
?C_C51STARTUP SEGMENT CODE
;定義一個(gè)代碼段,名稱?C_C51STARTUP
?STACK SEGMENT IDATA ;堆棧
RSEG ?STACK ;RSEG選擇一個(gè)先前聲明的可重定位的段
DS 1 ;為堆棧預(yù)留一個(gè)低階的存儲(chǔ)空間
EXTRN CODE (?C_START) ;當(dāng)前源文件中用的代碼段存儲(chǔ)區(qū)的符號(hào)?C_START,在其他的目標(biāo)模塊中定義
PUBLIC ?C_STARTUP ;聲明可以用于其他目標(biāo)模塊的全局符號(hào)?C_STARTUP,用于和C相連接在.src文件中可以看到這個(gè)符號(hào)
CSEG AT 0 ;選擇代碼存儲(chǔ)區(qū)內(nèi)的一個(gè)絕對(duì)段,匯編從上面命令中的地址0開(kāi)始執(zhí)行這個(gè)段。
?C_STARTUP: LJMP STARTUP1 ;芯片上電復(fù)位后,執(zhí)行的第一句就是該句,該句往下是開(kāi)始執(zhí)行啟動(dòng)代碼
RSEG ?C_C51STARTUP ;選擇代碼段?C_C51STARTUP
STARTUP1: IF IDATALEN <> 0 ;如果IDATALEN不為0,則將長(zhǎng)度-1送R0
MOV R0,#IDATALEN - 1
CLR A
IDATALOOP: MOV @R0,A ;將IDATA區(qū)清0
DJNZ R0,IDATALOOP
ENDIF
IF XDATALEN <> 0 ;如果有外部數(shù)據(jù)存儲(chǔ)區(qū)
MOV DPTR,#XDATASTART ;起始地址送DPTR
MOV R7,#LOW (XDATALEN) ;長(zhǎng)度低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 ;使能外部頁(yè)編址
MOV PPAGE_SFR,#PPAGE ;頁(yè)號(hào)送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) ;使用其他目標(biāo)模塊中定義的?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賦值
;硬件棧與模擬棧相區(qū)別,硬件棧是向上的,模擬棧是向下的
;在M51中可以看到具體的?STACK的值
;模擬棧的指針指向所在模式下,存儲(chǔ)區(qū)最大地址+1(0xFF+1或0xFFFF+1)
;硬件棧初始化指向棧底-1;
;因?yàn)镃51中棧操作是先操作棧指針(加或減),然后在寫(xiě)數(shù)
; 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 ;執(zhí)行main函數(shù)
END
;該文件中的一些符號(hào)的值的大小,可以在M51中看到,此外,可以通過(guò)系統(tǒng)上電復(fù)位后反
;匯編程序看到詳細(xì)的執(zhí)行過(guò)程
;文件中的一些命令可以查閱KeilC幫助文檔
編輯:admin 最后修改時(shí)間:2019-09-03