C51工具包的总体结构,如图(1)所示,其中uVision与Ishell分别是C51 for Windows和for Dos的集成开发环境(IDE),能够完成编辑、编译、链接、调试、仿真等整个开发流程。开发人员可用IDE自己或其它编辑器编辑C或汇编源文件。而后分别由C51及A51编译器编译生成目标文件(.OBJ)。目标文件可由LIB51建立生成库文件,也能够与库文件一块儿经L51链接定位生成绝对目标文件(.ABS)。ABS文件由OH51转换成标准的Hex文件,以供调试器dScope51或tScope51使用进行源代码级调试,也可由仿真器使用直接对目标板进行调试,也能够直接写入程序存贮器如EPROM中。 git
BL51也是C51软件包的链接/定位器,其具备L51的全部功能,此外它还具备如下3点特别之处: a. 能够链接定位大于64kBytes的程序。 b. 具备代码域及域切换功能(CodeBanking & Bank Switching) c. 可用于RTX51操做系统RTX51是一个实时多任务操做系统,它改变了传统的编程模式,甚至没必要用main( )函数,单片机系统软件向RTOS发展是一种趋势,这种趋势对于186和386及68K系列CPU更为明显和必须,对8051因CPU较为简单,程序结构等都不太复杂,RTX51做用显得不太突出,其专业版软件PK51软件包甚至不包括RTX51Full,而只有一个RTX51TINY版本的RTOS。RTX51 TINY适用于无外部RAM的单片机系统,于是可用面很窄,在本文中不做介绍。Bank switching技术因使用不多也不做介绍。windows
3. DScope51,Tscope51及Monitor51
(1) dScope51
dScope51是一个源级调试器和模拟器,它能够调试由C51编译器、A51汇编器、PL/M-51编译器及ASM-51汇编器产生的程序。它不需目标板(for windows也可经过mon51接目标板),只能进行软件模拟,但其功能强大,可模拟CPU及其外围器件,如内部串口,外部I/O及定时器等,能对嵌入式软件功能进行有效测试。其使用方法为: DS51[debugfile][INIT(initfile)]其中debugfile是一个Hex格式的8051文件,即待调试的文件其为可选的,可在进入dScope51后用load命令装入。Initfile为一个初使化文件,它在启动dScope51后,在debugfile装入前装入,装有一些dScope的初使化参数及经常使用调试函数等。下面是一个dScope.ini文件(for dos)的内容: Load ../../ds51/8051.iof Map 0,0xffffdScope51 for Windows则直接用鼠标进入,而后用load装入待调文件。数组
dScope for windows具备dScope for dos的所有功能,此外,它还具备如下明显的优势: (1) 标准的Windows界面,操做更容易更简单; (2) 经常使用操做多用对话框,而非Dos的行命令方式; (3) 窗口资源更加丰富:存储器窗口、覆盖率分析、运行状态分析窗口,增强了调试功能;由于dScope for Windows功能强大,具体操做在第八章详细介绍。
每个project都有一个Ishell.CFG,其中存放有“Option菜单和Setup菜单下的部分信息;Bell enabled、Monochrome enabled、Editor Selected、CRT Lines、target enviroment、name of user edit、Automatic load for configuration enabled、file window enabled、file specification for file window、translate command line controls、project name等。对每一个project都必须设置以上信息,而后存盘“setup”的的“save”,这样才可正式开始下面工做。
(2) IShell.col文件
对IDE颜色设置,如不改动,能够缺省为主。
(3) CDF文件
该文件位于BIN目录下,每一文件定义一组外部函数工具包,即定义外部环境如8051.CDF,USER.CDF等,开发者可修改CDF文件,供本身使用,至于CDF文件内容可查看一下8051.CDF便可知道。注意.CDF文件是Ishell系统的核心所在,不一样的CDF文件可以使本IDE适用于不一样的编译、链接系统,即本IDE并不只适于C51。下面谈一谈Automake工具:C51的Automake是一个project管理器,在8051工具包中以OBJECT文件形式保留了一个project的信息,AutoMake用这些信息来进行project管理,一旦手工创建一个project,Automake可生成一个新的OBJECT,AutoMake利用此文件来编译那些修改过的文件。Automake支持C5一、A5一、L51/BL5一、C16六、A16六、L166等编译链接器。点中主菜单中的Automake即运行本工具。Ishell for Dos使用比较繁琐,推荐使用uVision for windows。
2. uVision for windows的使用
uVision是一个标准的windows应用程序,其编译功能、文件处理功能、project处理功能、窗口功能以及工具引用功能(如A5一、C5一、PL/M4一、BL51 dScope等)等都较Ishell for Dos要强得多。uVision采用BL51做链接器,由于BL51兼容L51,因此一切能在Dos下工做的project均可以到uVision中进行链接调试。uVision采用dScope for windows做调试器,该调试器支持MON51及系统模拟两种方式,功能较for DOS要强大好用,调试功能强大。注意:(1) Option菜单下的各项要会使用,其中A5一、C5一、PL/M5一、BL51定义各文件所使用的编译、链接控制指令,dScope定义一个dScope初始化文件。Make则是定义一个make文件。(2) 进入调试是在RUN菜单下运行dScope。(3) project中包括新建、打开、修改、更新、编译、链接等poject处理,具体使用可参考后面的例子。
中断声明方法以下:void serial_ISR () interrupt 4 [using 1]{/* ISR */}为提升代码的容错能力,在没用到的中断入口处生成iret语句,定义没用到的中断。/* define not used interrupt, so generate \"IRET\" in their entrance */void extern0_ISR() interrupt 0{} /* not used */void timer0_ISR () interrupt 1{} /* not used */void extern1_ISR() interrupt 2{} /* not used */void timer1_ISR () interrupt 3{} /* not used */void serial_ISR () interrupt 4{} /* not used */
2. 通用存储工做区
3. 选通用存储工做区由using x声明,见上例。
4. 指定存储模式
由small compact 及large说明,例如:void fun1(void) small { }提示:small说明的函数内部变量所有使用内部RAM。关键的常常性的耗时的地方能够这样声明,以提升运行速度。
直接在数据定义后加上_at_ const便可,可是注意:(1)绝对变量不能被初使化;(2)bit型函数及变量不能用_at_指定。例如:idata struct link list _at_ 0x40;指定list结构从40h开始。xdata char text[25b] _at_0xE000;指定text数组从0E000H开始提示:若是外部绝对变量是I/O端口等可自行变化数据,须要使用volatile关键字进行描述,请参考absacc.h。
启动文件STARTUP.A51中包含目标板启动代码,可在每一个project中加入这个文件,只要复位,则该文件当即执行,其功能包括:l 定义内部RAM大小、外部RAM大小、可重入堆栈位置l 清除内部、外部或者以此页为单元的外部存储器l 按存储模式初使化重入堆栈及堆栈指针l 初始化8051硬件堆栈指针l 向main( )函数交权开发人员可修改如下数据从而对系统初始化 常数名 意义IDATALEN 待清内部RAM长度XDATA START 指定待清外部RAM起始地址XDATALEN 待清外部RAM长度IBPSTACK 是否小模式重入堆栈指针需初始化标志,1为须要。缺省为0IBPSTACKTOP 指定小模式重入堆栈顶部地址XBPSTACK 是否大模式重入堆栈指针需初始化标志,缺省为0XBPSTACKTOP 指定大模式重入堆栈顶部地址PBPSTACK 是否Compact重入堆栈指针,需初始化标志,缺省为0PBPSTACKTOP 指定Compact模式重入堆栈顶部地址PPAGEENABLE P2初始化容许开关PPAGE 指定P2值PDATASTART 待清外部RAM页首址PDATALEN 待清外部RAM页长度提示:若是要初始化P2做为紧凑模式高端地址,必须:PPAGEENAGLE=1,PPAGE为P2值,例如指定某页1000H-10FFH,则PPAGE=10H,并且链接时必须以下:L51
PDATA(1080H),其中1080H是1000H-10FFH中的任一个值。如下是STARTUP.A51代码片段,红色是常常可能须要修改的地方:;------------------------------------------------------------------------------; This file is part of the C51 Compiler package; Copyright KEIL ELEKTRONIK GmbH 1990;------------------------------------------------------------------------------; 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 , STARTUP.OBJ ;;------------------------------------------------------------------------------;; 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 0IDATALEN EQU 80H ; the length of IDATA memory in bytes.;XDATASTART EQU 0H ; the absolute start-address of XDATA memoryXDATALEN EQU 0H ; the length of XDATA memory in bytes.;PDATASTART EQU 0H ; the absolute start-address of PDATA memoryPDATALEN 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.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.;;------------------------------------------------------------------------------
bit isalnum(char c); bit isalpha(char c); 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);