Keil C51编译器的使用

Keil C51编译器的使用

吴宏伟 2012-2-5

对于学习51系列单片机的人来说,Keil 可以说是最熟悉不过的了,但一直在Keil UI界面的掩盖之下,我们并不了解一个51的HEX文件是怎样从源代码文件一步一步生成的,这其中又有哪些跟我们的目标MCU有关。我们都知道,程序是从源代码—预编译—编译—连接—生成可以执行文件,今天,我将谈谈C51编译器的使用。

  1. 安装KEIL4软件

    在网上找到KEIL4的安装文件,假设安装路径设在 D:\Keil4 ,则找到这个文件夹,你会发现里面有两个子文件夹,一个是"C51",另外一个是"UV4";

    "UV4"其实就是Keil软件,利用它可以很方便地建立和编译单片机工程,但是它的编译其实也是调用了"C51"中的编译器来完成的,所以我们将着重讲解一下"C51"文件夹;

  2. C51文件夹下有以下文件夹

    (1).ASM – 使用汇编语言时所包含的头文件

    (2).BIN – 编译器执行文件

    (3).INC – 使用C语言时所包含的头文件(例如reg52.h等文件)

    (4).LIB – 各种类型51系列的库文件(正因为有了这些库文件的存在,我们才能使用C语言来对单片机进行编程)

    (5).HLP – 帮助,里面有对各个编译器使用方法的介绍,有什么不懂的地方都可以在这里面找到解答

    (6).Examples \ FlashMon \ ISD51 \ MON51 \ MON 390 \ RtxTiny2这几个文件夹跟我们今天所讲的内容无关,不作介绍

  3. C51编译器

对51系列的单片机程序源文件的编译,主要是通过BIN文件夹下面的C51.EXE(编译) 、BL51.EXE(链接)、OH51.EXE(生成HEX文件)来实现,下面将逐个介绍。

    (1)C51

    打开HLP\C51.CHM文件,在左边的目录中找到Compiling Programs,这里有介绍它的用法。

首先要设置环境变量(Environment Settings):

If you run the Cx51 Compiler within µVision IDE, you do not need any additional settings on your computer. If you want to run the Cx51 Compiler and utilities from the command prompt, you must manually create the following environment variables.

Variable

Path

Description

PATH

\KEIL\C51\BIN

Path to the C51 and CX51 executable programs.

TMP

 

Path to use for temporary files generated by the compiler. If the specified path does not exist, the compiler generates an error and aborts compilation.

C51INC

\KEIL\C51\INC

Path to the folder for Cx51 include files.

C51LIB

\KEIL\C51\LIB

Path to the folder for Cx51 library files.

在批处理中的设置方法为:

PATH= D:\Keil4\C51\BIN;%PATH%

SET TMP=D:\TMP

SET C51INC=D:\Keil4\C51\INC

SET C51LIB=D:\Keil4\C51\LIB

接下来是错误级别:

0为无错误;1为有警告;2为有错误,并且可能有警告;3为有致命错误;

ERRORLEVEL

After linking, the number of errors and warnings detected is output to the screen. The BL51 Linker/Locator then sets the ERRORLEVEL to indicate the status of the link process. Values are listed in the following table:

ERRORLEVEL

Description

0

No Errors or Warnings

1

Warnings Only

2

Errors and Possibly Warnings

3

Fatal Errors

最后是C51的用法(Command Prompt):

C51 sourcefile <[>directives...<]> 
C51 @commandfile 

比如要编译main.c文件的语法就是

C51 main.c 

经过编译之后会生成main.obj文件。

Directives为附加指令,可有可无,具体有什么附件指令及用法见帮助文件中的

Compiling Programs\Directives

(2)BL51

BL51的用法跟C51大同小异,用法(Command Prompt)如下:

BL51 <[>inputlist<]> <[>TO outputfile<]> <[>directives<]> 
BL51 @commandfile 

比如要链接之前的main.obj和51库,用法如下:

BL51 main.obj,D:\Keil4\C51\LIB\c51s.lib TO main.out RAMSIZE(256) 

这里我们用到了RAMSIZE这个附加指令,后面的数字代表我们的目标MCU的RAM大小

C51S.LIB为传统51系列单片机的库文件。

链接之后会产生main.out文件。

(3)OH51

转换输出文件成HEX文件,这个的用法最简单了,如下:

OH51 abs_file <[>HEXFILE (file)<]> 

比如转换上面的main.out文件到main.hex文件,则用法如下:

OH51 main.out HEXFILE(main.hex)

  1. 完整的DEMO

    我们的工程目录设在D:\51project;

    1. 新建 main.c 文件,内容如下

/** @file main.c

* @brief 调用fun.c文件中的fun()函数

* @author 吴宏伟

* @date 2012-2-5

*/

#include<reg52.h>

#include "fun.h"

void main()

{    

    fun();

    while(1);

}

  1. 新建fun.h文件,内容如下:

#ifndef __FUN__

void fun();

#endif

  1. 新建fun.c文件,内容如下:

/** @file fun.c

* @brief fun()函数定义,设置各IO引脚的输出为0XF0

* @author 吴宏伟

* @date 2012-2-5

*/

#include<reg52.h>

void fun()

{

    P0=P1=P2=P3=0xf0;

}

  1. 新建Makefile.cmd批处理文件(什么是批处理,网上搜下吧),内容如下

@echo off

@rem this makefile is executed to build a 51 project

@rem written by homeway from Chaofeng Studio, 2012/2/5

 

@rem first set the environment settings

PATH=D:\KEIL4\C51\BIN;%PATH%

SET C51INC=D:\KEIL4\C51\INC

SET C51LIB=D:\KEIL4\C51\LIB

SET PROJECT=D:\51PROJECT

SET OUTPUT=D:\51PROJECT\51.hex

 

%rem compile the project files

C51 %PROJECT%\main.c

C51 %PROJECT%\fun.c

 

IF ERRORLEVEL 2 GOTO :FAILED

IF ERRORLEVEL 3 GOTO :FAILED

 

%rem link the object files

BL51 main.obj,fun.obj,%C51LIB%\C51S.lib TO 51 RAMSIZE(256)

 

IF ERRORLEVEL 2 GOTO :FAILED

IF ERRORLEVEL 3 GOTO :FAILED

 

%rem convert 51 to 51.hex

OH51 51 HEXFILE(%OUTPUT%)

goto :SUCCESS

 

:FAILED

echo 编译失败!

pause

exit 1

 

:SUCCESS

echo 编译成功!

pause

exit 0

  1. 双击makefile.cmd文件,系统为自动编译并生成51.hex

     

后记:最近才刚刚接触对各种单片机代码的手工编译,因此今天只是简单介绍一下C51编译器的简单用法,更高级的应用本人将继续学习,接下来将有一篇在SlickEdit环境下如何配置C51编译命令的文章,敬请期待。

以上文件下载:https://files.cnblogs.com/chaofeng/51project.zip

原文地址:https://www.cnblogs.com/chaofeng/p/2339087.html