;------------------------------------------------------------------------------ ; 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 ; L51 invocation: ;
; L51
;------------------------------------------------------------------------------ ;
; User-defined Power-On Initialization of Memory ;
; With the following EQU statements the initialization of memory ; at processor reset can be defined: ;
; ; the absolute start-address of IDATA memory is always 0 IDATALEN EQU 80H ; the length of IDATA memory in bytes. ;
XDATASTART EQU 0H ; the absolute start-address of XDATA memory XDATALEN EQU 0H ; the length of XDATA memory in bytes. ;
PDATASTART EQU 0H ; the absolute start-address of PDATA memory PDATALEN EQU 0H ; the length of PDATA memory in bytes. ;
; Notes: The IDATA space overlaps physically the DATA and BIT areas of the ; 8051 CPU. At minimum the memory space occupied from the C51 ; run-time routines must be set to zero.
;------------------------------------------------------------------------------ ;
; Reentrant Stack Initilization ;
; The following EQU statements define the stack pointer for reentrant ; functions and initialized it: ;
; Stack Space for reentrant functions in the SMALL model.
17
IBPSTACK EQU 0 ; set to 1 if small reentrant is used.
IBPSTACKTOP EQU 0FFH+1 ; set top of stack to highest location+1. ;
; Stack Space for reentrant functions in the LARGE model. XBPSTACK EQU 0 ; set to 1 if large reentrant is used.
XBPSTACKTOP EQU 0FFFFH+1; set top of stack to highest location+1. ;
; Stack Space for reentrant functions in the COMPACT model. PBPSTACK EQU 0 ; set to 1 if compact reentrant is used.
PBPSTACKTOP EQU 0FFFFH+1; set top of stack to highest location+1. ;
;------------------------------------------------------------------------------ ;
; Page Definition for Using the Compact Model with 64 KByte 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. ;
PPAGEENABLE EQU 0 ; set to 1 if pdata object are used. PPAGE EQU 0 ; define PPAGE number. ;
;------------------------------------------------------------------------------ 3. 标准输入输出文件 putchar.c
putchar.c是一个低级字符输出子程,开发人员可修改后应用到自己的硬件系统上,例如向CLD或LEN输出字符。 缺省:putchar.c是向串口输出一个字符XON|XOFF是流控标志,换行符“\\*n”自动转化为回车/换行“\\r\\n”。 getkey.c
getkey函数是一个低级字符输入子程,该程序可用到自己硬件系统,如矩阵键盘输入中,缺省时通过串口输入字符。 4. 其它文件
还包括对Watch-Dog有独特功能的INIT.A51函数以及对8×C751适用的函数,可参考源代码。 第四节 段名协定与程序优化
1. 段名协定(Segment Naming Conventions)
C51编译器生成的目标文件存放于许多段中,这些段是代码空间或数据空间的一些单元,一个段可以是可重定位的,也可以是绝对段,每一个可重定位的段都有一个类型和名字,C51段名有以下规定:
每个段名包括前缀与模块名两部分,前缀表示存储类型,模块名则是被编译的模块的名字,例如: ?CO?main1 :表示main1模块中的代码段中的常数部分
?PR?function1?module 表module模块中函数function1的可执行段,具体规定参阅手册。 2. 程序优化
18
C51编译器是一个具有优化功能的编译器,它共提供六级优化功能。确保生成目标代码的最高效率(代码最少,运行速度最快)。具体六级优化的内容可参考帮助。
在C51中提供以下编译控制指令控制代码优化: OPTIMIZE(SJXE):尽量采用子程序,使程序代码减少。
NOAREGS:不使用绝对寄存器访问,程序代码与寄存器段独立。
NOREGPARMS:参数传递总是在局部数据段实现,程序代码与低版本C51兼容。
OPTIMIZE(SIZE)AK OPTIMIZE(speed)提供6级优化功能,缺省为: OPTIMIZE(6,SPEED)。
第五章 Keil C51库函数参考
C51强大功能及其高效率的重要体现之一在于其丰富的可直接调用的库函数,多使用库函数使程序代码简单,结构清晰,易于调试和维护,下面介绍C51的库函数系统。 第一节 本征库函数(intrinsic routines)和非本征证库函数
C51提供的本征函数是指编译时直接将固定的代码插入当前行,而不是用ACALL和LCALL语句来实现,这样就大大提供了函数访问的效率,而非本征函数则必须由ACALL及LCALL调用。 C51的本征库函数只有9个,数目虽少,但都非常有用,列如下: _crol_,_cror_:将char型变量循环向左(右)移动指定位数后返回 _iror_,_irol_:将int型变量循环向左(右)移动指定位数后返回 _lrol_,_lror_:将long型变量循环向左(右)移动指定位数后返回 _nop_: 相当于插入NOP
_testbit_: 相当于JBC bitvar测试该位变量并跳转同时清除。 _chkfloat_: 测试并返回源点数状态。 使用时,必须包含#inclucle
例如8031、8051均为REG51.h其中包括了所有8051的SFR及其位定义,一般系统都必须包括本文件。 2. 绝对地址include文件absacc.h
该文件中实际只定义了几个宏,以确定各存储空间的绝对地址。 3. 动态内存分配函数,位于stdlib.h中 4. 缓冲区处理函数位于“string.h”中 其中包括拷贝比较移动等函数如:
memccpy memchr memcmp memcpy memmove memset 这样很方便地对缓冲区进行处理。 5. 输入输出流函数,位于“stdio.h”中
流函数通8051的串口或用户定义的I/O口读写数据,缺省为8051串口,如要修改,比如改为LCD显示,可修改lib目录中的getkey.c及putchar.c源文件,然后在库中替换它们即可。 第三节 Keil C51库函数原型列表 1. CTYPE.H
bit isalnum(char c); bit isalpha(char c);
19
bit iscntrl(char c); bit isdigit(char c); bit isgraph(char c); bit islower(char c); bit isprint(char c); bit ispunct(char c); bit isspace(char c); bit isupper(char c); bit isxdigit(char c); bit toascii(char c); bit toint(char c); char tolower(char c); char __tolower(char c); char toupper(char c); char __toupper(char c); 2. INTRINS.H
unsigned char _crol_(unsigned char c,unsigned char b); unsigned char _cror_(unsigned char c,unsigned char b); unsigned char _chkfloat_(float ual);
unsigned int _irol_(unsigned int i,unsigned char b); unsigned int _iror_(unsigned int i,unsigned char b); unsigned long _irol_(unsigned long l,unsigned char b); unsigned long _iror_(unsigned long L,unsigned char b); void _nop_(void); bit _testbit_(bit b); 3. STDIO.H
char getchar(void); char _getkey(void);
char *gets(char * string,int len);
int printf(const char * fmtstr[,argument]…); char putchar(char c); int puts (const char * string);
int scanf(const char * fmtstr.[,argument]…);
int sprintf(char * buffer,const char *fmtstr[;argument]); int sscanf(char *buffer,const char * fmtstr[,argument]); char ungetchar(char c);
void vprintf (const char *fmtstr,char * argptr);
void vsprintf(char *buffer,const char * fmtstr,char * argptr);4. STDLIB.H
20

