资源描述
Microchip單晶片上的C語言應用說明(一)
Microchip的PIC單晶片雖然只有37個指令,指令少雖然容易了解,但要做複雜的運算,則非常難以撰寫程式,為了減少開發時間,唯有使用C語言來開發。
筆者這次要跟大家談的是用C語言來開發八位元單晶片,在過去或許聽來覺得不可思議,因單晶片它通常只有幾K的記憶體(數K的ROM合幾十bytes的RAM),那麼少的記憶體還用C語言來開發,真是非常的浪費及不實用,在我參加了Microchip的研討會及使用後,我改變了看法;通常會使用組合語言無非是要求快及精簡,但事實上真的快及精簡了嗎?對於高手而言,或許真的精簡及速度變快,但需花費多少時間呢?一搬來說需比用C語言來開發多三倍以上的時間,有些人甚至用組合語言寫出的程式碼比用C語言轉譯出的組合語言還長,且C語言易懂易維護,他有跨平台的特點,就算換了微處理器也只需些許修改就可以RUN了,組合語言則不然,它必須重新改寫程式碼;至於用C語言寫出來的程式它執行的(程式碼較長)速度較慢,在以前或許存在,現今各微處理器的速度都相當快,速度慢已不是問題,且開發時間可以縮短,是許多工程師及老闆的不二選擇。
因中心在八位元的單晶片上皆使用Microchip的PICmicro Devices,所以就以PICmicro Devices來做說明如何使用C語言來開發,Microchip所生產的單晶片有PIC16、PIC17及PIC18系列,這些都是八位元的單晶片,這些晶片都有A/D轉換、Timer、PWM、IIC、RS232等功能,一般控制會用到的功能這裡都有,以一顆單晶片可以取代像早期8051需搭配一些週邊才能達到目前PIC系列的功能,不僅成本減少、相對板子面積也變小且IC數量較少產品也較穩定,且目前Microchip所開發的PIC系列單皆片皆為FLASH的版本,對於個人或小公司而言,晶片可以重複燒寫測試,非常的經濟實惠,相對開發費用較少,對於產品量少及小公司實在是不二選擇。
Microchip本身也有C語言版本的開發工具,但它只支援PIC17及PIC18系列,PIC16它就沒有支援,可能是PIC16系列目前程式記憶體最大為8K bytes,可能認為只有8K bytes,只要用組合語言來撰寫即可,順便一提Microchip的PIC單晶片其組合語言的開發工具是免費的,但C語言要另外購買,因目前的控制系統使用PIC16系列就夠了,Microchip又沒有PIC16的C語言開發工具,所以轉向購買B Knudsen Data所出的CC5X C Compiler,它支援PIC12/14/16 devices,它的售價也不貴約一萬五千塊台幣,目前中心只要有用到PIC皆使用CC5X來開發,所以針對CC5X C Compiler的用法作一些說明,讓使用PIC的使用者能快速進入C的世界。
CC5X C Compiler支援的Devices有:
A. 12 bit core (PIC16C5X, PIC12C50X, etc.):
‧ up to 2048 words of code on 1 - 4 code pages
‧ up to 73 byte RAM in 1 - 4 banks
B. 14 bit core (PIC12C67X, PIC14000, PIC16CXX, etc.):
‧ up to 8192 words of code on 1 - 4 code pages
‧ up to 512 byte RAM in 1 - 4 banks
CC5X安裝
它可被安裝在MSDOS、Windows95/98/NT/2000,基本上它不需安裝,當你購買時,只需將整個目錄拷貝到電腦的硬碟即可,CC5X C Compiler它是一個Command-line的程式,例如cc5x -a sample1.c <enter>;它也可以與Microchip的MPLAB相結合,提供compilers、assemblers、simulators、emulators、device programmers等功能,其安裝步驟如下或參考cc5x目錄下的install.txt:
1. 將CC5X的檔案拷貝至你的電腦其中的一個目錄,EX:c:\cc5x。
2. 然後從c:\cc5x的目錄裡copy CC5X.MTC and TLCC5X.INI兩個檔案到MPLAB的目錄。
3. 啟動MPLAB,選擇MENU的Project->Install Language,在Language Suite中選擇CC5X,然後再點選工具名稱C-Compiler,然後在右邊執行欄keyin c:\cc5x\cc5x.exe,或者用瀏覽的按鍵去選擇你的CC5X的位置,再去點選Command-line box,然後按OK即可,此時CC5X已成為MPLAB的一部分了。
以下針對CC5X做一檔案說明:
CC5X.EXE : compiler
INTRO.TXT : introduction
INSTALL.TXT : installation guide and MPLAB setup
CC5X.TXT : basic documentation on CC5X
PRAGMA.TXT : the pragma statement
INLINE.TXT : information on inline assembly syntax
DEBUG.TXT : debugging details, MPLAB support
CHIP.TXT : how to make new chip definitions
CONFIG.TXT : the PICmicro configuration bits
CDATA.TXT : info on the #pragma cdata statement
CONST.TXT : standard C strings and constant data
GLOBDEF.TXT : PICmicro registers
STARTUP.TXT : special startup sequences
LINKER.TXT : how to link several modules (C or asm)
INT16CXX.H : interrupt header file
INLINE.H : emulating inline instructions
CC5X.MTC : MPLAB tool configuration file
TLCC5X.INI : MPLAB tool configuration file
OP.INC : command line options on a file
RELOC.INC : options for object modules (MPLINK)
SAMPLE1.C : minimal program example
IICBUS.C : IIC-bus interface
IIC-COM.C : IIC-bus communication
SERIAL.C : serial communication (RS232, RS485)
STATE.C : state machines
DELAY.C : implementing delays
INT16XX.C : simple interrupt example
MATH.TXT : Math library support
MATH16.H : 8-16 bit math library
MATH16M.H : 8-16 bit multiply, speed
MATH24.H : 8-24 bit math library
MATH24M.H : 8-24 bit multiply, speed
MATH32.H : 8-32 bit math library
MATH32M.H : 8-32 bit multiply, speed
MATH16X.H : 16 bit fixed point library
MATH24X.H : 24 bit fixed point library
MATH32X.H : 32 bit fixed point library
MATH16F.H : 16 bit floating point library
MATH24F.H : 24 bit floating point library
MATH32F.H : 32 bit floating point library
MATH24LB.H : 24 bit floating point functions(log,sqrt,cos,..)
MATH32LB.H : 32 bit floating point functions(log,sqrt,cos,..)
12C508.H .. 16C924.H : PICmicro header files
NEWS.TXT : Recent added features
README.TXT
EXAMPLE
因CC5X支援PIC12、PIC16等系列的單晶片,那我們所撰寫的程式要如何指定特定的PICmicro Devices,它有三種方法可以去指定:
1. 在command-line上直接加上,其方法為-p[Device name],EX:-p16C73、-pPIC16F877。
2. 在主程式上加入pragma statement,EX:#pragma chip PIC16F84。
3. 使用include file,EX:在主程式加入#include "16c73.h"。
以下為使用CC5X的執行範例,其執行方式為:
cc5x -a delay.c<enter>
delay.c:
Chip = 16C74
RAM: 00h : -------- -------- -------- --------
RAM: 20h : ==.***** ******** ******** ********
RAM: 40h : ******** ******** ******** ********
RAM: 60h : ******** ******** ******** ********
RAM: 80h : -------- -------- -------- --------
RAM: A0h : ******** ******** ******** ********
RAM: C0h : ******** ******** ******** ********
RAM: E0h : ******** ******** ******** ********
Optimizing - removed 11 instructions (-14 %)
File 'delay.asm'
Codepage 0 has 68 word(s) : 3 %
Codepage 1 has 0 word(s) : 0 %
File 'delay.hex'
Total of 68 instructions (1 %)
Symbols:
* : free location
- : predefined or pragma variable
= : local variable(s)
. : global variable
7 : 7 free bits in this location
上面的-a意思是產生.asm(組合語言)檔案,上面的範例產生三個檔案,delay.asm、delay.occ及delay.hex,其中delay.asm為使用C語言所轉譯出的組合語言檔;delay.occ為compiler後所產生的相關訊息,包括使用記憶體(RAM)狀況及使用多少程式記憶體(ROM)等;delay.hex為產生INHX8M格式,此檔案就可利用燒錄器直接燒到PIC裡面。
COMMAND LINE OPTIONS
以下針對CC5X Command-line再做一詳細說明:
The syntax is:
CC5X [options] <src>.c [options]
-a[<asmfile>]: produce assembly file.
The default file name is <src>.asm
-A[scHDpftmiJbeokgN+N+N] : assembly file options
s: symbolic arguments are replaced by numbers
c: no C source code is printed
H: hexadecimal numbers only
D: decimal numbers only
p: no '.' in front of decimal constants
f: no object format directive is printed
t: no tabulators, normal spaces only
m: single source line only
i: no source indentation, straight left margin
J: put source after instructions to achieve a compact assembly file.
b: do not add rambank info to variables in the assembly file
e: do not add ',1' to instructions when result is written back to the register
o: do not replace OPTION with OPTION_REG
k: do not convert all hexadecimal numbers (11h -> 0x11)
g: do not use PROCESSOR instead of the list directive
N+N+N: label, mnemonic and argument spacing. Default is 8+6+10.
-b: do not update bank selection bits
12 bit core: FSR.5 and 6
14 bit core: STATUS.RP0 and RP1
-bu: non-optimized updating of the bank selection bits
-CC[<file>] : produce COD file, C mode
-CA[<file>] : produce COD file, ASM mode
-dc: do not write compiler output file <src>.occ
-D<name>[=xxx] : define macro. Equivalent to #define name xxx
-e: single line error messages (no source lines are printed).
-ed : do not print error details
-ew : do not print warning details
-eL : list error and warning details at the end
-E<N> : stop after <N> errors (default is 4).
-f<hex-file-format> : i.e. INHX8M, INHX8S, INHX16, INHX32. Default is INHX8M. Note that
INHX8S use output files: <file>.HXH and <file>.HXL
-F: produce error file <src>.err
-g: do not replace call by goto
-GW: dynamic selected skip() format, warning on long format (default)
CC5X C Compiler B Knudsen Data
49
-GD: dynamic selected skip() format
-GS: always short skip() format (error if boundary is crossed)
-GL: always long skip() format
-I<directory> : include files directory. Up to 5 library directories can be supplied by using separate -
I<dir> options. When using #include "test.h" the current directory is first searched. If the file is not found
there, then the library directories are searched, in the same order as supplied in the command line option
list (-I<dir>). The current directory is skipped when using #include <test.h>.
-j: do not update page selection bits
12 bit core: STATUS.PA0 and PA1
14 bit core: PCLATH.3 and 4
-L[<col>,<lin>] : produce list file <src>.lst
The maximun number of columns per line <col> and lines per page <lin> can be changed. The default
setting is -L80,60
-mc1: default 'const' pointer size is 1 byte (8 bits)
-mc2: default 'const' pointer size is 2 bytes (16 bits)
-mr1: default RAM pointer size is 1 byte
-mr2: default RAM pointer size is 2 bytes
-mm1: default pointer size is 1 byte (all pointer types)
-mm2: default pointer size is 2 bytes (all pointer types)
-o<name> : write hex file to name
-O<directory> : output files directory. Files generated by the compiler are put on this directory, except
when a full path name is supplied.
-p<device> : defines the chip type. The device have to be known internally: 16C54,55,56,57,58,
61,64,65, 71,73,74, 84 or supported by a header file (i.e. 16F877.H). Default device is 16C54.
-q<N> : assume disabled interrupt at the <N> deepest call levels. I.e. –q1 allows the main program to use
all stack levels for function calls. If the interrupt routine makes function calls, then –q2 etc. can be used.
Disabling interrupt at the deepest call level MUST be properly ensured in the user application program.
-r : generate relocatable assembly (no hex file)
-r2[<filename.lkr>] : use separate logical section for interrupt routine
-rp<N> : name on codepage 0 is PROG<N>, default PROG1
-rb<N> : name on RAM bank 0 is BANK<N>, default BANK0
-ro<N> : add offset <N> when generating local variable block name
-Q: write the call tree to <src>.fcs.
-S: silent operation of the compiler
-u : no optimizing
-V[rnuD] : generate variable file, <src>.var, sorted by address as default.
r: only variables which are referenced in the code
n: sort by name
u: unsorted
D: decimal numbers
-we: no warning when fixed point constants are rounded
-wB: warning when function is called from another code page
CC5X C Compiler B Knudsen Data
-wI: warning on long inline code for multiplication and division
-wL: (12 bit core only) print warning on all GOTO links to functions residing on hidden half of a
codepage.
-wO: warning on operator library calls
-wP: warning when code page bits are not correct
-wr: no warning on recursive calls
-wS: warning (no error) when constant expression loses significant bits
-wU: warning on uncalled functions
-cu: use 32 bit evaluation of constant expressions
-x<file> : assembler executable: -xC:\progra~1\mplab\mpasm.exe
-X<option> : assembler option: -X/q (all options must be separate)
-W: wait until key pressed after compilation
A path name can be written using '/' if this is supported by the file system, example:
c:/compiler/lib/file.h
Default compiler settings:
‧ hex file output on file <name>.hex
‧ processor = 16C54
‧ optimizing on
‧ extended call level is allowed
‧ update bank and page selection bits
Permanent assigned settings:
‧ nested comments is allowed
‧ char is unsigned
展开阅读全文