1、Microchip單晶片上的C語言應用說明(一) Microchip的PIC單晶片雖然只有37個指令,指令少雖然容易了解,但要做複雜的運算,則非常難以撰寫程式,為了減少開發時間,唯有使用C語言來開發。 筆者這次要跟大家談的是用C語言來開發八位元單晶片,在過去或許聽來覺得不可思議,因單晶片它通常只有幾K的記憶體(數K的ROM合幾十bytes的RAM),那麼少的記憶體還用C語言來開發,真是非常的浪費及不實用,在我參加了Microchip的研討會及使用後,我改變了看法;通常會使用組合語言無非是要求快及精簡,但事實上真的快及精簡了嗎?對於高手而言,或許真的精簡及速度變快,但需花費多少時間呢?一搬來
2、說需比用C語言來開發多三倍以上的時間,有些人甚至用組合語言寫出的程式碼比用C語言轉譯出的組合語言還長,且C語言易懂易維護,他有跨平台的特點,就算換了微處理器也只需些許修改就可以RUN了,組合語言則不然,它必須重新改寫程式碼;至於用C語言寫出來的程式它執行的(程式碼較長)速度較慢,在以前或許存在,現今各微處理器的速度都相當快,速度慢已不是問題,且開發時間可以縮短,是許多工程師及老闆的不二選擇。 因中心在八位元的單晶片上皆使用Microchip的PICmicro Devices,所以就以PICmicro Devices來做說明如何使用C語言來開發,Microchip所生產的單晶片有PIC16、P
3、IC17及PIC18系列,這些都是八位元的單晶片,這些晶片都有A/D轉換、Timer、PWM、IIC、RS232等功能,一般控制會用到的功能這裡都有,以一顆單晶片可以取代像早期8051需搭配一些週邊才能達到目前PIC系列的功能,不僅成本減少、相對板子面積也變小且IC數量較少產品也較穩定,且目前Microchip所開發的PIC系列單皆片皆為FLASH的版本,對於個人或小公司而言,晶片可以重複燒寫測試,非常的經濟實惠,相對開發費用較少,對於產品量少及小公司實在是不二選擇。 Microchip本身也有C語言版本的開發工具,但它只支援PIC17及PIC18系列,PIC16它就沒有支援,可能是PIC1
4、6系列目前程式記憶體最大為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
5、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安裝 它
6、可被安裝在MSDOS、Windows95/98/NT/2000,基本上它不需安裝,當你購買時,只需將整個目錄拷貝到電腦的硬碟即可,CC5X C Compiler它是一個Command-line的程式,例如cc5x -a sample1.c
7、5x的目錄裡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 : introducti
8、on 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 co
9、nfiguration 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 : emulatin
10、g 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
11、 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
12、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 M
13、ATH32F.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等系列的單晶片,那我們所撰
14、寫的程式要如何指定特定的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
15、 -------- -------- -------- RAM: 20h : ==.***** ******** ******** ******** RAM: 40h : ******** ******** ******** ******** RAM: 60h : ******** ******** ******** ******** RAM: 80h : -------- -------- -------- -------- RAM: A0h : ******** ******** ******** ******** RAM: C0h : ******** ********
16、 ******** ******** 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
17、 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 OPT
18、IONS
以下針對CC5X Command-line再做一詳細說明:
The syntax is:
CC5X [options]
19、cimal 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 asse
20、mbly 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 dir
21、ective
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[
22、SM mode
-dc: do not write compiler output file
23、nd
-E
24、ing 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
25、e - I
26、h>. -j: do not update page selection bits 12 bit core: STATUS.PA0 and PA1 14 bit core: PCLATH.3 and 4 -L[
27、r 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
28、te hex file to name
-O
29、 header file (i.e. 16F877.H). Default device is 16C54.
-q
30、e deepest call level MUST be properly ensured in the user application program.
-r : generate relocatable assembly (no hex file)
-r2[
31、efault BANK0
-ro
32、 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) prin
33、t 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 unc
34、alled functions
-cu: use 32 bit evaluation of constant expressions
-x
35、rted by the file system, example:
c:/compiler/lib/file.h
Default compiler settings:
‧ hex file output on file
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818