Keil C51編譯器的使用
吳宏偉 2012-2-5
對於學習51系列單片機的人來說,Keil 可以說是最熟悉不過的了,但一直在Keil UI界面的掩蓋之下,我們並不了解一個51的HEX文件是怎樣從源代碼文件一步一步生成的,這其中又有哪些跟我們的目標MCU有關。我們都知道,程序是從源代碼—預編譯—編譯—連接—生成可以執行文件,今天,我將談談C51編譯器的使用。
-
安裝KEIL4軟件
在網上找到KEIL4的安裝文件,假設安裝路徑設在 D:\Keil4 ,則找到這個文件夾,你會發現里面有兩個子文件夾,一個是"C51",另外一個是"UV4";
"UV4"其實就是Keil軟件,利用它可以很方便地建立和編譯單片機工程,但是它的編譯其實也是調用了"C51"中的編譯器來完成的,所以我們將着重講解一下"C51"文件夾;
-
C51文件夾下有以下文件夾
(1).ASM – 使用匯編語言時所包含的頭文件
(2).BIN – 編譯器執行文件
(3).INC – 使用C語言時所包含的頭文件(例如reg52.h等文件)
(4).LIB – 各種類型51系列的庫文件(正因為有了這些庫文件的存在,我們才能使用C語言來對單片機進行編程)
(5).HLP – 幫助,里面有對各個編譯器使用方法的介紹,有什么不懂的地方都可以在這里面找到解答
(6).Examples \ FlashMon \ ISD51 \ MON51 \ MON 390 \ RtxTiny2這幾個文件夾跟我們今天所講的內容無關,不作介紹
-
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)
-
完整的DEMO
我們的工程目錄設在D:\51project;
-
新建 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);
}
-
新建fun.h文件,內容如下:
#ifndef __FUN__
void fun();
#endif
-
新建fun.c文件,內容如下:
/** @file fun.c
* @brief fun()函數定義,設置各IO引腳的輸出為0XF0;
* @author 吳宏偉
* @date 2012-2-5
*/
#include<reg52.h>
void fun()
{
P0=P1=P2=P3=0xf0;
}
-
新建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
-
雙擊makefile.cmd文件,系統為自動編譯並生成51.hex
后記:最近才剛剛接觸對各種單片機代碼的手工編譯,因此今天只是簡單介紹一下C51編譯器的簡單用法,更高級的應用本人將繼續學習,接下來將有一篇在SlickEdit環境下如何配置C51編譯命令的文章,敬請期待。