12.07.2015 Views

MPLAB C30

MPLAB C30

MPLAB C30

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Section 8<strong>C30</strong> 入 門 及 應 用<strong>MPLAB</strong> ® <strong>C30</strong>的 其 他 好 用 的 功 能© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 1


Agenda• 介 紹 <strong>C30</strong>_Template.c• <strong>MPLAB</strong> <strong>C30</strong> Peripheral Library 的 使 用• <strong>C30</strong> 練 習 3 : 使 用 <strong>MPLAB</strong> <strong>C30</strong> PeripheralLibrary 來 操 作 Timer1• PSV 的 定 義 與 使 用• Table Read/Write 的 使 用• dsPI<strong>C30</strong>F 的 EEPROM Data• <strong>MPLAB</strong> <strong>C30</strong> Calling Conversion• C 呼 叫 組 合 語 言 的 範 例© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 2


<strong>MPLAB</strong> <strong>C30</strong> 的 入 門 範 例• <strong>C30</strong>_Template.c− 提 供 一 個 簡 單 的 範 例 , 讓 使 用 者 知 道如 何 完 成 下 列 事 項• 宣 告 變 數 在 不 同 的 節 區• 宣 告 陣 列 在 指 定 位 址• 如 何 讀 寫 SFR 以 及 特 定 的 位 元• 安 排 中 斷 服 務 程 式 ( ISR )• 您 可 以 利 用 此 程 式 做 為 C 程 式 開 發的 基 礎© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 3


使 用 <strong>C30</strong> 提 供 之 PeripheralLibraries• <strong>MPLAB</strong> <strong>C30</strong> 提 供 完 整 的 Peripheral Library 來 供 使 用 者方 便 設 定 及 操 作 各 種 周 邊• Peripheral Library 的 使 用 需 要 注 意−−−在 V3.12 後 , PIC24F 的 Peripheral Library 自 成 一 獨 立 的 檔 案放 置 於 C:\Program Files\Microchip\<strong>MPLAB</strong> \<strong>MPLAB</strong> <strong>C30</strong>\docs\periph_lib\ \ Microchip PIC24F Peripheral Library 為 一 已 編 譯 的 HTMLHELP 檔−−參 考 此 檔 案 來 找 出 使 用 特 定 Library Function 時 參 數 的 有 效 定 義, 以 及 須 要 被 include 的 Header file在 專 案 中 要 將 Peripheral library 加 入• libpPIC24Fxxx-coff.a for PIC24F 系 列© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 4


在 HELP 檔 找 尋 特 定 Function• 執 行 此 HTML 檔 並 點 選 Peripheral LibraryModule© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 5


在 HELP 檔 找 尋 特 定 Function• 在 周 邊 Library 列 表 中 選 擇 Timer 並 點 選 TimerFunctions© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 6


在 HELP 檔 找 尋 特 定 Function• 列 出 的 是 所 有 操 作 Timer 時 可 用 的 Functions• 在 顯 示 的 Function 列 表 選 取 OpenTimer1© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 7


在 HELP 檔 找 尋 特 定 Function• HELP 檔 會 列 出 使 用 OpenTimer1 時 的 可 用 參 數 與 格 式© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 8


<strong>C30</strong> 練 習 3使 用 <strong>MPLAB</strong> <strong>C30</strong>Peripheral Library© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 9


<strong>C30</strong> 練 習 3• 使 用 <strong>MPLAB</strong> <strong>C30</strong> 提 供 的 Peripheral Library 來 對 Timer1做 Initial 的 工 作−−−−使 用 <strong>C30</strong>_Template.c 為 基 礎加 入 程 式 Initial Timer1 – 不 可 直 接 寫 入 T1CON , PR1 等 暫 存 器使 用 OpenTimer1() & ConfigIntTimer1() 來 Initial TIMER1讓 Timer1 每 250 ms 進 入 中 斷 服 務 程 式 一 次• Toggle 自 行 指 定 的 LED• PIC24FJ128GB106 的 執 行 速 度 : 請 設 定 Fosc = 32 Mhz• 提 示 : 可 以 使 用 <strong>MPLAB</strong> SIM !!© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 10


Constant Data Access from PMUsing Program Space Visibilityibilit• 32 KB segment of PM may be mapped into thedata memory address space−8-bits fromPSVPAGRegisterUserSpaceO nlyIf PSV bit (CORCON) is set and if SrcPointer (DMEA) is ‘1’ then data is accessed from PMPSVPAG RegisterPSVPAGOP CODE24-bit InstructionSource & Dest Regs16-bit Source Pointer1 w w w w w w w w w w w w w w wSelect PSV14-bits from Source Pointer0 User Space only 0Effective 24-bit PM AddressByte SelectOnly even words can be accessed© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 11


PSV Addressing ExampleData Space230 PC 0001SFRsNear Data MemoryFar Data Memory15 0Data Space EAUser SpaceCORCON = 1PSVPAG = 0x20ProgEA = 0x101000DataEA = 0x9000Reset VectorInterrupt Vector TableAlternate Vector TableOn Chip User FlashMemoryOn Chip User FlashMemoryFlash Config Words0 PSVPAGSource Source reg032KB© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 12


Data Access from PMUsing PSV23Actual 24-bit Program Memory00 0 0 0 0 0 0 08-bits0x0000 or 0xFF16-bitsWORD access instructionByte access instructionwith odd addressByte access instructionwith even address• Only lower 16 bits of PM location canbe accessed via mapping−Upper 8 bits should be programmed for a NOP© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 13


Using the PSV in <strong>C30</strong>• Three usage options− Manual PSV• char data[256] __attribute__ ((space(psv)))• Must manually point to the correct PSV whenaccessing the variable− Auto PSV• const char data[256];• Maximum of 32 KB of constant data allowed• Linker does not allow PSV space to cross a PSVPAGboundary© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 14


Using the PSV Window• Program Space Visibility (PSV)• Read only access to Flash memory• Compiler can manage PSV window• User may manage PSV window© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 15


Compiler Managed PSV• const variables in PSV• Limited to 1 PSV page (32K)• PSVPAG set automatically• PSV window enabled© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 16


User Managed PSV• Allocate variables into PSV• Use space attributeint foo __ attribute __((space(psv))),(p bar __attribute__((space(psv)));• With section attributeint FOO __attribute__((space(psv), section(“EEK”)));int BAR __attribute__((space(psv), section(“EEK”)));© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 17


Accessing PSV Variables• Compiler managed:int foo __attribute__((space(auto_psv)));psv)));…if (foo == 1) …• User managed:int bar __attribute__((space(psv)));…CORCONbits.PSV = 1;PSVPAG = __builtin_psvpage(&bar);if (bar == 1) ...© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 18


Accessing PSV Variables• Mixing? i − Only 1 PSV page at a time− Be careful to save and restore PSVPAGsaved_psv = PSVPAG;PSVPAG = __builtin_psvpage(&bar);/* access bar */PSVPAG = saved_psv;© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 19


Constant Data Access fromPM Using Table Instructionsti• 24-bit Effective Address(EA) formation− Configuration space is accessed by settingTBLPAG (i.e. EA)• Only means of accessing configuration space8-bits fromTBLPAGRegisterTBLPAG RegisterTBLPAG0>OP CODE p24-bit InstructionSource & Dest Regs16-bit Source/Dest. Pointerw w w w w w w w w w w w w w w w15-bits from Source/Dest. PointerByte SelectEffective 24-bit PM AddressWord SelectFrom Op Code© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 20


Constant Data Access fromPM Using Table Instructions23Actual 24-bit Program Memory0Phantom Byte0 0 0 0 0 0 0 08-bits16-bitsTBLRDH/TBLWTHTBLRDL/TBLWTLTBLRDH.B/TBLWTH.BB/TBLWTH BTBLRDL.B/TBLWTL.BB/TBLWTL BTBLRDL.B/TBLWTL.BB/TBLWTL BTable Instruction View of Program Memory© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 21


Using Table Read/Write in <strong>C30</strong>• Must use assembly for specialinstructions and exact timing− Add assembly file to project, or,− Use <strong>C30</strong> Built-ins: ins:const unsigned int myVar = 0xFFFF;unsigned int varAddr;TBLPAG = __builtin_tblpage(&myVar);varAddr = __builtin_tbloffset(&myVar);tbl t(& __builtin_tblwtl(varAddr, 0xAAAA); //load data to write://0xAAAANVMCON = 0x4003;//NVM word write opcode__ builtin_ write_ NVM();//unlock & set WR bit© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 22


內 建 的 EEDATA• EEDATA: non-volatile data space• Can be accessed using table reads• Also as user managed PSV− PSV window is read-only• 只 有 dsPI<strong>C30</strong>F 系 列 有 內 建 的EEPROM© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 23


建 立 EEDATA• <strong>C30</strong> 提 供 現 成 巨 集 讓 宣 告 EEPROM 的資 料 更 為 簡 單 , 格 式 為 :_EEDATA(alignment)• 使 用 範 例 :int _ EEDATA(2) config_data[12] ={0,1,2,3,4,5,6,7,8,9,10,1110 11};• <strong>C30</strong> 的 相 關 程 式 會 自 動 的 將 資 料 安 排 於EEPROM 的 位 址© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 24


讀 取 EEDATA• 透 過 PSV 的 機 制 :void main(void) {// 將 Compiler Managed 的 PSVPAG 先 保 存 起 來}PSVPAG = 0xFF; /* set PSV page */CORCONbits.PSV = 1; /* enable PSV window */if (config_data[0])/* .... */// 將 Compiler Managed 的 PSVPAG 還 原• 使 用 者 需 自 行 管 理 PSV window 的設 定© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 25


<strong>MPLAB</strong> <strong>C30</strong> Calling Conversion• <strong>MPLAB</strong> <strong>C30</strong> 呼 叫 副 程 式 時 傳 遞 參 數 的 方 法− 使 用 W0 .. W7 來 做 參 數 傳 遞• 由 左 向 右 的 次 序 – 由 W0 開 始 放− 如 果 超 過 W0..W7 W7 可 容 納 範 圍 就 使 用 堆 疊• 由 右 向 左 的 次 序• <strong>MPLAB</strong> <strong>C30</strong> 處 理 被 呼 叫 程 式 傳 回 值 的 方 法− 使 用 W0 .. W3• 8 & 16 bit 資 料 使 用 W0• 32 bit 資 料 使 用 W1:W0• 64 bit 資 料 使 用 W3:W2:W1:W0© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 26


<strong>MPLAB</strong> <strong>C30</strong> Calling Conversion• <strong>MPLAB</strong> <strong>C30</strong> 在 傳 遞 不 同 型 別 參 數 所 須 的Working Register 數 量© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 27


<strong>MPLAB</strong> <strong>C30</strong> Calling Conversion• <strong>MPLAB</strong> <strong>C30</strong> 參 數 傳 遞 範 例© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 28


<strong>MPLAB</strong> <strong>C30</strong> Calling Conversion• 無 法 使 用 Working Register 傳 遞 的 參 數 就 用堆 疊 的 範 例© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 29


由 C 來 呼 叫 asm 副 程 式• 假 設 有 一 個 使 用 組 合 語 言 寫 成 的 副 程 式 如下 :.global _ EE_write_EE_write:mov w0, TBLPAGtblwtl tl w2, [w1]mov #0x4004, w0mov w0, NVMCONpush SR; truncated to fit© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 30


由 C 來 呼 叫 asm 副 程 式• 在 C 程 式 中 就 要 用 以 下 的 方 式 來 呼 叫 組 合 語言 副 程 式extern void EE_write(int, int, int);int __attribute__((space(eedata))config_bits[4] = { 0,1,2,3 };void main(void){EE_write(});__builtin_tblpage(&config_bits),__builtin_tbloffset(&config_bits), 119© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 31


Inline Assembly• 有 兩 種 InLine Assembly 的 格 式• Simple form 的 功 能 簡 單−asm(“assembly statement”)• Extended form 的 複 雜 度 高−asm(“template” [: outputs[: inputs[: clobbers]]])• 儘 量 避 免 使 用 InLine Assembly 寫 很 長 的 程式© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 32


Inline Assembly• 建 議 的 使 用 方 式 與 時 機 : 只 有 簡 單 的 工作 要 完 成 時asm (“clrwdt”);asm (“disi #16383”);• 使 用 volatile 關 鍵 字 避 免 被 刪 除 或 移 位asm volatile (“clrwdt”);© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 33


Inline Assembly• 使 用 Extended form 來 取 用 C 的 變 數unsigned intdivf(unsigned int num, unsigned int den){register unsigned int quo asm(“w0”);}asm(“repeat #17\n\t”“divf %1,%2” : /* template */“=r”(quo) : /* outputs */“r”(num), ”e"(den) : /* inputs */“w1”); /* clobber */return(quo);© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 34


TrademarksThe Microchip name and logo, the Microchip logo, Accuron, dsPIC, KeeLoq,microID, <strong>MPLAB</strong>, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart,rfPIC and SmartShunt are registered trademarks of MicrochipTechnology Incorporated in the U.S.A. and other countries.AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB, SEEVAL,SmartSensor and The Embedded Control Solutions Company areregistered trademarks of Microchip Technology Incorporated in theU.S.A.Analog-for-the-Digital Age, Application Maestro, dsPICDEM, dsPICDEM.net,dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB,In-Circuit Serial Programming, g, ICSP, ICEPIC, Linear Active Thermistor,Mindi, MiWi, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM,PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo, PowerMate,PowerTool, REAL ICE, rfLAB, rfPICDEM, Select Mode, Smart Serial,SmartTel, Total Endurance, UNI/O, WiperLock and ZENA are trademarksof Microchip Technology Incorporated in the U.S.A. and other countries.SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.All other trademarks mentioned herein are property of their respectivecompanies.© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!