对于初学者来说,Keil中的起始代码是不需要更改的,在新建工程的时候也是不需要进行添加的。但是随着接触51的时间与经历越来越久的时候,我们还是需要了解一些这个文件为好.况且一项简单的事情做到极致就不简单,51看起来简单,但是要真正的达到极致却没有几个人能达到.其实不难,主要是国内的初学者在初学51时首先是从C语言入手的,而C语言是很简单的(至少对于简单的应用来说),所以一些初学者与其说是会单片机,不如说仅仅是会C语言的几个简单的语句而已,而对于几个简单的寄存器的控制,其实是很容易上手的,也可以随时翻阅工具书进行查看.但是这种状况却导致了对单片机的学习不能深入,特别对于程序出现了问题或系统稍微复杂的时候就不知道怎么解决了。
下面的代码中的用<>表示的含义说明都包含在上面的表格之中。该表格在Keil的help文档中输入Configuration Wizard即可找到。
进入主题:
startup.A51的内容粘贴如下,可以在你新建工程的时候可以看到这段代码
$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 <h> Power-On Initialization of Memory
; With the following EQU statements the initialization of memory
; at processor reset can be defined:
; <o> IDATALEN: IDATA memory size <0x0-0x100>
; <i> Note: The absolute start-address of IDATA memory is always 0
; <i> The IDATA space overlaps physically the DATA and BIT areas.
IDATALEN EQU 80H
; <o> XDATASTART: XDATA memory start address <0x0-0xFFFF>
; <i> The absolute start address of XDATA memory
XDATASTART EQU 0
; <o> XDATALEN: XDATA memory size <0x0-0xFFFF>
; <i> The length of XDATA memory in bytes.
XDATALEN EQU 0
; <o> PDATASTART: PDATA memory start address <0x0-0xFFFF>
; <i> The absolute start address of PDATA memory
PDATASTART EQU 0H
;
; <o> PDATALEN: PDATA memory size <0x0-0xFF>
; <i> The length of PDATA memory in bytes.
PDATALEN EQU 0H
;
;</h>
;------------------------------------------------------------------------------
;<h> Reentrant Stack Initialization
;
; The following EQU statements define the stack pointer for reentrant
; functions and initialized it:
;
; <h> Stack Space for reentrant functions in the SMALL model.
; <q> IBPSTACK: Enable SMALL model reentrant stack
; <i> Stack space for reentrant functions in the SMALL model.
IBPSTACK EQU 0 ; set to 1 if small reentrant is used.
; <o> IBPSTACKTOP: End address of SMALL model stack <0x0-0xFF>
; <i> Set the top of the stack to the highest location.
IBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
; </h>
;
; <h> Stack Space for reentrant functions in the LARGE model.
; <q> XBPSTACK: Enable LARGE model reentrant stack
; <i> Stack space for reentrant functions in the LARGE model.
XBPSTACK EQU 0 ; set to 1 if large reentrant is used.
; <o> XBPSTACKTOP: End address of LARGE model stack <0x0-0xFFFF>
; <i> Set the top of the stack to the highest location.
XBPSTACKTOP EQU 0xFFFF +1 ; default 0FFFFH+1
; </h>
;
; <h> Stack Space for reentrant functions in the COMPACT model.
; <q> PBPSTACK: Enable COMPACT model reentrant stack
; <i> Stack space for reentrant functions in the COMPACT model.
PBPSTACK EQU 0 ; set to 1 if compact reentrant is used.
;
; <o> PBPSTACKTOP: End address of COMPACT model stack <0x0-0xFFFF>
; <i> Set the top of the stack to the highest location.
PBPSTACKTOP EQU 0xFF +1 ; default 0FFH+1
; </h>
;</h>
;------------------------------------------------------------------------------
; Memory Page for Using the Compact Model with 64 KByte xdata RAM
; <e>Compact Model Page Definition
; <i>Define the XDATA page used for PDATA variables.
; <i>PPAGE must conform with the PPAGE set in the linker invocation.
; Enable pdata memory page initalization
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used.
; <o> PPAGE number <0x0-0xFF>
; <i> uppermost 256-byte address of the page used for PDATA variables.
PPAGE EQU 0
; <o> SFR address which supplies uppermost address byte <0x0-0xFF>
; <i> most 8051 variants use P2 as uppermost address byte
PPAGE_SFR DATA 0A0H
; </e>
;------------------------------------------------------------------------------
; Standard SFR Symbols
ACC DATA 0E0H
B DATA 0F0H
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
NAME ?C_STARTUP
?C_C51STARTUP SEGMENT CODE
?STACK SEGMENT IDATA
RSEG ?STACK
DS 1
EXTRN CODE (?C_START)
PUBLIC ?C_STARTUP
CSEG AT 0
?C_STARTUP: LJMP STARTUP1
RSEG ?C_C51STARTUP
STARTUP1:
IF IDATALEN <> 0
MOV R0,#IDATALEN - 1
CLR A
IDATALOOP: MOV @R0,A
DJNZ R0,IDATALOOP
ENDIF
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
ENDIF
IF PPAGEENABLE <> 0
MOV PPAGE_SFR,#PPAGE
ENDIF
IF PDATALEN <> 0
MOV R0,#LOW (PDATASTART)
MOV R7,#LOW (PDATALEN)
CLR A
PDATALOOP: MOVX @R0,A
INC R0
DJNZ R7,PDATALOOP
ENDIF
IF IBPSTACK <> 0
EXTRN DATA (?C_IBP)
MOV ?C_IBP,#LOW IBPSTACKTOP
ENDIF
IF XBPSTACK <> 0
EXTRN DATA (?C_XBP)
MOV ?C_XBP,#HIGH XBPSTACKTOP
MOV ?C_XBP+1,#LOW XBPSTACKTOP
ENDIF
IF PBPSTACK <> 0
EXTRN DATA (?C_PBP)
MOV ?C_PBP,#LOW PBPSTACKTOP
ENDIF
MOV SP,#?STACK-1
; This code is required if you use L51_BANK.A51 with Banking Mode 4
;<h> Code Banking
; <q> Select Bank 0 for L51_BANK.A51 Mode 4
#if 0
; <i> 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
;</h>
LJMP ?C_START
END
好的。上面的代码大家稍微了解一下之后可以看到Keil已经给他做了详细的解释了。
STARTUP.A51: This code is executed after processor reset. 即该code 是在处理器复位之后执行的。其主要完成的作用就是清理RAM.设置堆栈等.而在执行完start.a51后跳转到用户的主函数部分.这一部分的代码可以在你的程序中的主函数的前面会看到。
NOMOD51 (代码中的第一行)(在help文档中输入NOMOD51即可看到相关的说明解释)
Description |
The NOMOD51 directive suppresses pre-definition of 8051 SFR names. This directive must be used when a customer-specific SFR definition file is included. |
作用就是禁止禁止预定义的8051的SFR(特殊功能寄存器).
; This file is part of the C51 Compiler package
; Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
;------------------------------------------------------------------------------
; STARTUP.A51: This code is executed after processor reset.
; STARTUP.A51: STARTUP.A51文件的代码在处理器复位后执行
; 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
; BL51 invocation:
; 用下面的命令行语句调用BL51连接器把STARTUP.OBJ目标文件连接到程序代码中
; BL51 <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:
; 使用下列EQU伪指令,初始化后的存储区域在单片机复位后生效
; <o> IDATALEN: IDATA memory size <0x0-0x100>
; <i> Note: The absolute start-address of IDATA memory is always 0
; <i> The IDATA space overlaps physically the DATA and BIT areas.
; IDATA 空间范围 0-0x100;
; 翻译:IDATA内存的绝对启始地址总为0
; IDATA 区在物理空间区中已经包括了DATA区(直接寻址区)以及 BIT区 (位寻址区)。
IDATALEN EQU 80H ; the length of IDATA memory in bytes.
; IDATA(间接寻址区)其起始地址固定为0;
IDATALEN用于指定需要初始化的IDATA区长度(以字节为单位)
;XDATA和PDATA的相关解释类似,节约篇幅,不在说明
;注释:C51(库)占用了最小化内存空间,运行时程序需要把IDATA设为0
; Reentrant Stack Initilization
; 重入堆栈初始化
; The following EQU statements define the stack pointer for reentrant
; functions and initialized it:
; 下面的EQU语句定义重入函数的堆栈指针并初始化它
; Stack Space for reentrant functions in the SMALL model.
; SMALL模式下的重入函数的堆栈空间
IBPSTACK EQU 0 ; set to 1 if small reentrant is used.
;如果再SMALL模式下使用重入则设为1
IBPSTACKTOP EQU 0FFH+1 ; set top of stack to highest location+1.
;设置堆栈顶为最大位置+1
;Compact和Large模式下类似,不再说明
;但是我们可以看到在compact和small模式下时用于重入函数的堆栈大小为FF,而在large
;模式下时为FFFF。
; Page Definition for Using the Compact Model with 64 KByte xdata RAM
; 使用COMPACT模式时为64KB的XDATA RAM定义页
; The following EQU statements define the xdata page used for pdata
; variables. The EQU PPAGE must conform with the PPAGE control used
; in the linker invocation.
; 下面的EQU语句定义PDATA变量的使用了XDATA页
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used.
;如果使用PDATA页则设为1
;
PPAGE EQU 0 ; define PPAGE number.
;定义页号
PPAGE_SFR DATA 0A0H ; SFR that supplies uppermost address byte
;SFR的最高地址字节
; (most 8051 variants use P2 as uppermost address byte)
; (大多数8051变量要用P2的最高地址字节)
限于篇幅,分上下分开写笔记。上面的翻译部分借鉴与网上,但是都能很好的理解。还有下面一部分见下篇。