当前位置:首页 > 单片机 > 单片机
[导读] Startup code:启动代码。在Keil中,启动代码在复位目标系统后立即被执行。启动代码主要实现以下功能:(1) 清除内部数据存储器(2) 清除外部数据存储器(3) 清除外部页存储器(4) 初始化small模式下的可重入栈和指针(5

 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.
The Cx51 Compiler does not check to see if the stack area available satisfies the requirements of the application. It is your responsibility to perform such a test.

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.
The Cx51 Compiler does not check to see if the stack area available satisfies the requirements of the application. It is your responsibility to perform such a test.

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.
The Cx51 Compiler does not check to see if the stack area available satisfies the requirements of the application. It is your responsibility to perform such a test.

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 Power-On Initialization of Memory

;用户自定义上电初始化内存

; With the following EQU statements the initialization of memory

; at processor reset can be defined:

;在处理器复位时通过EQU来初始化内存

; IDATALEN: IDATA memory size <0x0-0x100>

;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

;

; XDATASTART: XDATA memory start address <0x0-0xFFFF>

; The absolute start address of XDATA memory

;XDATA存储区的起始地址,XDATA内存的绝对起始地址。

XDATASTART EQU 0

;指定初始的XDATA地址清0

; XDATALEN: XDATA memory size <0x0-0xFFFF>

; The length of XDATA memory in bytes.

;XDATA空间的长度,以字节为单位

XDATALEN EQU 0

;说明xdata的字节数清0,该值默认为0

; PDATASTART: PDATA memory start address <0x0-0xFFFF>

; The absolute start address of PDATA memory

PDATASTART EQU 0H

;

; PDATALEN: PDATA memory size <0x0-0xFF>

; The length of PDATA memory in bytes.

PDATALEN EQU 0H

;

;

;------------------------------------------------------------------------------

;

; Reentrant Stack Initialization

;重入栈的初始化

; 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: 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: End address of SMALL model stack <0x0-0xFF>

; 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相同不做特别说明

; Stack Space for reentrant functions in the LARGE model.

; 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.

; XBPSTACKTOP: End address of LARGE model stack <0x0-0xFFFF>

; Set the top of the stack to the highest location.

XBPSTACKTOP EQU 0xFFFF +1 ; default 0FFFFH+1

;

;COMPACT模式下

; Stack Space for reentrant functions in the COMPACT model.

; 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.

;

; PBPSTACKTOP: End address of COMPACT model stack <0x0-0xFFFF>

; 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

; Compact Model Page Definition

;通过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使能

; PPAGE number <0x0-0xFF>

; uppermost 256-byte address of the page used for PDATA variables.

;页号,PDATA变量高256字节地址(高8位地址)

PPAGE EQU 0

;

; SFR address which supplies uppermost address byte <0x0-0xFF>

; 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

; Code Banking

; 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帮助文档

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

该系列产品有助于嵌入式设计人员在更广泛的系统中轻松实现USB功能

关键字: 单片机 嵌入式设计 USB

单片机编程语言是程序员与微控制器进行交流的桥梁,它们构成了单片机系统的软件开发基石,决定着如何有效、高效地控制和管理单片机的各项资源。随着微控制器技术的不断发展,针对不同应用场景的需求,形成了丰富多样的编程语言体系。本文...

关键字: 单片机 微控制器

单片机,全称为“单片微型计算机”或“微控制器”(Microcontroller Unit,简称MCU),是一种高度集成化的电子器件,它是现代科技领域的关键组件,尤其在自动化控制、物联网、消费电子、汽车电子、工业控制等领域...

关键字: 单片机 MCU

STM32是由意法半导体公司(STMicroelectronics)推出的基于ARM Cortex-M内核的32位微控制器系列,以其高性能、低功耗、丰富的外设接口和强大的生态系统深受广大嵌入式开发者喜爱。本文将详细介绍S...

关键字: STM32 单片机

在当前的科技浪潮中,单片机作为嵌入式系统的重要组成部分,正以其强大的功能和广泛的应用领域受到越来越多行业的青睐。在众多单片机中,W79E2051以其卓越的性能和稳定的工作特性,成为市场上的明星产品。本文将深入探讨W79E...

关键字: 单片机 w79e2051单片机

单片机,又称为微控制器或微处理器,是现代电子设备中的核心部件之一。它集成了中央处理器、存储器、输入输出接口等电路,通过外部信号引脚与外部设备进行通信,实现对设备的控制和管理。本文将详细介绍单片机的外部信号引脚名称及其功能...

关键字: 单片机 微控制器 中央处理器

随着科技的飞速发展,单片机和嵌入式系统在现代电子设备中的应用越来越广泛。它们不仅提高了设备的智能化水平,还推动了各行各业的创新与发展。在单片机和嵌入式系统的开发中,编程语言的选择至关重要。本文将深入探讨单片机和嵌入式系统...

关键字: 单片机 嵌入式系统 电子设备

PLC(可编程逻辑控制器)和单片机是两种不同的控制设备,它们之间存在明显的区别:

关键字: 单片机 plc 控制器

Holtek隆重推出全新一代32-bit Arm® Cortex®-M0+ 5V CAN MCU - HT32F53231/HT32F53241/HT32F53242/HT32F53252。这一系列单片机带有来自Bosc...

关键字: MCU 工业自动化 单片机

Holtek精益求精,宣布推出全新5V宽电压Arm® Cortex®-M0+ 32-bit MCU系列HT32F50431/HT32F50441/HT32F50442/HT32F50452。此系列MCU经多方位升级能满...

关键字: 单片机 智能家居 工业控制
关闭
关闭