Microchip單晶片上的C語言應(yīng)用說明(一)(第八期雙月刊).doc_第1頁
Microchip單晶片上的C語言應(yīng)用說明(一)(第八期雙月刊).doc_第2頁
Microchip單晶片上的C語言應(yīng)用說明(一)(第八期雙月刊).doc_第3頁
Microchip單晶片上的C語言應(yīng)用說明(一)(第八期雙月刊).doc_第4頁
Microchip單晶片上的C語言應(yīng)用說明(一)(第八期雙月刊).doc_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

Microchip單晶片上的C語言應(yīng)用說明(一)Microchip的PIC單晶片雖然只有37個指令,指令少雖然容易了解,但要做複雜的運算,則非常難以撰寫程式,為了減少開發(fā)時間,唯有使用C語言來開發(fā)。筆者這次要跟大家談的是用C語言來開發(fā)八位元單晶片,在過去或許聽來覺得不可思議,因單晶片它通常只有幾K的記憶體(數(shù)K的ROM合幾十bytes的RAM),那麼少的記憶體還用C語言來開發(fā),真是非常的浪費及不實用,在我參加了Microchip的研討會及使用後,我改變了看法;通常會使用組合語言無非是要求快及精簡,但事實上真的快及精簡了嗎?對於高手而言,或許真的精簡及速度變快,但需花費多少時間呢?一搬來說需比用C語言來開發(fā)多三倍以上的時間,有些人甚至用組合語言寫出的程式碼比用C語言轉(zhuǎn)譯出的組合語言還長,且C語言易懂易維護,他有跨平臺的特點,就算換了微處理器也只需些許修改就可以RUN了,組合語言則不然,它必須重新改寫程式碼;至於用C語言寫出來的程式它執(zhí)行的(程式碼較長)速度較慢,在以前或許存在,現(xiàn)今各微處理器的速度都相當(dāng)快,速度慢已不是問題,且開發(fā)時間可以縮短,是許多工程師及老闆的不二選擇。因中心在八位元的單晶片上皆使用Microchip的PICmicro Devices,所以就以PICmicro Devices來做說明如何使用C語言來開發(fā),Microchip所生產(chǎn)的單晶片有PIC16、PIC17及PIC18系列,這些都是八位元的單晶片,這些晶片都有A/D轉(zhuǎn)換、Timer、PWM、IIC、RS232等功能,一般控制會用到的功能這裡都有,以一顆單晶片可以取代像早期8051需搭配一些週邊才能達到目前PIC系列的功能,不僅成本減少、相對板子面積也變小且IC數(shù)量較少產(chǎn)品也較穩(wěn)定,且目前Microchip所開發(fā)的PIC系列單皆片皆為FLASH的版本,對於個人或小公司而言,晶片可以重複燒寫測試,非常的經(jīng)濟實惠,相對開發(fā)費用較少,對於產(chǎn)品量少及小公司實在是不二選擇。Microchip本身也有C語言版本的開發(fā)工具,但它只支援PIC17及PIC18系列,PIC16它就沒有支援,可能是PIC16系列目前程式記憶體最大為8K bytes,可能認(rèn)為只有8K bytes,只要用組合語言來撰寫即可,順便一提Microchip的PIC單晶片其組合語言的開發(fā)工具是免費的,但C語言要另外購買,因目前的控制系統(tǒng)使用PIC16系列就夠了,Microchip又沒有PIC16的C語言開發(fā)工具,所以轉(zhuǎn)向購買B Knudsen Data所出的CC5X C Compiler,它支援PIC12/14/16 devices,它的售價也不貴約一萬五千塊臺幣,目前中心只要有用到PIC皆使用CC5X來開發(fā),所以針對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 banksB. 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 banksCC5X安裝它可被安裝在MSDOS、Windows95/98/NT/2000,基本上它不需安裝,當(dāng)你購買時,只需將整個目錄拷貝到電腦的硬碟即可,CC5X C Compiler它是一個Command-line的程式,例如cc5x -a sample1.c ;它也可以與Microchip的MPLAB相結(jié)合,提供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,然後在右邊執(zhí)行欄keyin c:cc5xcc5x.exe,或者用瀏覽的按鍵去選擇你的CC5X的位置,再去點選Command-line box,然後按OK即可,此時CC5X已成為MPLAB的一部分了。以下針對CC5X做一檔案說明:CC5X.EXE : compilerINTRO.TXT : introductionINSTALL.TXT : installation guide and MPLAB setupCC5X.TXT : basic documentation on CC5XPRAGMA.TXT : the pragma statementINLINE.TXT : information on inline assembly syntaxDEBUG.TXT : debugging details, MPLAB supportCHIP.TXT : how to make new chip definitionsCONFIG.TXT : the PICmicro configuration bitsCDATA.TXT : info on the #pragma cdata statementCONST.TXT : standard C strings and constant dataGLOBDEF.TXT : PICmicro registersSTARTUP.TXT : special startup sequencesLINKER.TXT : how to link several modules (C or asm)INT16CXX.H : interrupt header fileINLINE.H : emulating inline instructionsCC5X.MTC : MPLAB tool configuration fileTLCC5X.INI : MPLAB tool configuration fileOP.INC : command line options on a fileRELOC.INC : options for object modules (MPLINK)SAMPLE1.C : minimal program exampleIICBUS.C : IIC-bus interfaceIIC-COM.C : IIC-bus communicationSERIAL.C : serial communication (RS232, RS485)STATE.C : state machinesDELAY.C : implementing delaysINT16XX.C : simple interrupt exampleMATH.TXT : Math library supportMATH16.H : 8-16 bit math libraryMATH16M.H : 8-16 bit multiply, speedMATH24.H : 8-24 bit math libraryMATH24M.H : 8-24 bit multiply, speedMATH32.H : 8-32 bit math libraryMATH32M.H : 8-32 bit multiply, speedMATH16X.H : 16 bit fixed point libraryMATH24X.H : 24 bit fixed point libraryMATH32X.H : 32 bit fixed point libraryMATH16F.H : 16 bit floating point libraryMATH24F.H : 24 bit floating point libraryMATH32F.H : 32 bit floating point libraryMATH24LB.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 filesNEWS.TXT : Recent added featuresREADME.TXTEXAMPLE因CC5X支援PIC12、PIC16等系列的單晶片,那我們所撰寫的程式要如何指定特定的PICmicro Devices,它有三種方法可以去指定:1. 在command-line上直接加上,其方法為-pDevice name,EX:-p16C73、-pPIC16F877。2. 在主程式上加入pragma statement,EX:#pragma chip PIC16F84。3. 使用include file,EX:在主程式加入#include 16c73.h。以下為使用CC5X的執(zhí)行範(fàn)例,其執(zhí)行方式為:cc5x -a delay.cdelay.c:Chip = 16C74RAM: 00h : - - - -RAM: 20h : =.* * * *RAM: 40h : * * * *RAM: 60h : * * * *RAM: 80h : - - - -RAM: A0h : * * * *RAM: C0h : * * * *RAM: E0h : * * * *Optimizing - removed 11 instructions (-14 %)File delay.asmCodepage 0 has 68 word(s) : 3 %Codepage 1 has 0 word(s) : 0 %File delay.hexTotal of 68 instructions (1 %)Symbols:* : free location- : predefined or pragma variable= : local variable(s). : global variable7 : 7 free bits in this location上面的-a意思是產(chǎn)生.asm(組合語言)檔案,上面的範(fàn)例產(chǎn)生三個檔案,delay.asm、delay.occ及delay.hex,其中delay.asm為使用C語言所轉(zhuǎn)譯出的組合語言檔;delay.occ為compiler後所產(chǎn)生的相關(guān)訊息,包括使用記憶體(RAM)狀況及使用多少程式記憶體(ROM)等;delay.hex為產(chǎn)生INHX8M格式,此檔案就可利用燒錄器直接燒到PIC裡面。COMMAND LINE OPTIONS以下針對CC5X Command-line再做一詳細(xì)說明:The syntax is:CC5X options .c options-a: produce assembly file.The default file name is .asm-AscHDpftmiJbeokgN+N+N : assembly file optionss: symbolic arguments are replaced by numbersc: no C source code is printedH: hexadecimal numbers onlyD: decimal numbers onlyp: no . in front of decimal constantsf: no object format directive is printedt: no tabulators, normal spaces onlym: single source line onlyi: no source indentation, straight left marginJ: put source after instructions to achieve a compact assembly file.b: do not add rambank info to variables in the assembly filee: do not add ,1 to instructions when result is written back to the registero: do not replace OPTION with OPTION_REGk: do not convert all hexadecimal numbers (11h - 0x11)g: do not use PROCESSOR instead of the list directiveN+N+N: label, mnemonic and argument spacing. Default is 8+6+10.-b: do not update bank selection bits12 bit core: FSR.5 and 614 bit core: STATUS.RP0 and RP1-bu: non-optimized updating of the bank selection bits-CC : produce COD file, C mode-CA : produce COD file, ASM mode-dc: do not write compiler output file .occ-D=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 : stop after errors (default is 4).-f : i.e. INHX8M, INHX8S, INHX16, INHX32. Default is INHX8M. Note thatINHX8S use output files: .HXH and .HXL-F: produce error file .err-g: do not replace call by goto-GW: dynamic selected skip() format, warning on long format (default)CC5X C Compiler B Knudsen Data49-GD: dynamic selected skip() format-GS: always short skip() format (error if boundary is crossed)-GL: always long skip() format-I : include files directory. Up to 5 library directories can be supplied by using separate -I options. When using #include test.h the current directory is first searched. If the file is not foundthere, then the library directories are searched, in the same order as supplied in the command line optionlist (-I). The current directory is skipped when using #include .-j: do not update page selection bits12 bit core: STATUS.PA0 and PA114 bit core: PCLATH.3 and 4-L, : produce list file .lstThe maximun number of columns per line and lines per page can be changed. The defaultsetting 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 : write hex file to name-O : output files directory. Files generated by the compiler are put on this directory, exceptwhen a full path name is supplied.-p : 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 : assume disabled interrupt at the deepest call levels. I.e. q1 allows the main program to useall 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 : use separate logical section for interrupt routine-rp : name on codepage 0 is PROG, default PROG1-rb : name on RAM bank 0 is BANK, default BANK0-ro : add offset when generating local variable block name-Q: write the call tree to .fcs.-S: silent operation of the compiler-u : no optimizing-VrnuD : generate variable file, .var, sorted by address as default.r: only variables which are referenced in the coden: sort by nameu: unsortedD: decimal numbers-we: no warning when fixed point constants are rounded-wB: warning when function is called from another code pageCC5X

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論