11.07.2015 Views

MPLAB C30

MPLAB C30

MPLAB C30

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

More About <strong>MPLAB</strong> ® <strong>C30</strong>● 設 定 目 標 – 相 容 於 ANSI: 1989 規 範●●●架 構 於 GCC Compiler− 提 供 標 準 的 C functions提 供 Run time libraries− Maths library , Helper routines …One-time cost of $895US− P/N : SW006012− 可 以 使 用 於 所 有 的 16-bit Family− www.microchip.com/c30© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 5


<strong>MPLAB</strong> ® <strong>C30</strong> 相 關 的 重 要 檔 案● C compiler: pic30-gcc.exe● Assembler: pic30-as.exe● Linker: pic30-ld.exe● Simulator: sim30.exe● Binary utilities – Next Page !● Support files – Next Page !© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 7


<strong>MPLAB</strong> ® <strong>C30</strong> 重 要 內 容●●Binary utilities− Archive librarian : pic30-ar.exe− Intel(tm) Hexfile generatorSupport files− Libraries: libc.a, libm.a, libdsp.a, libp24Fxxxcoff.a( 24FJ 的 Peripheral library 整 合 為 1 , 其 他的 16-bit MCU 每 個 都 有 自 己 的 Peripherallibrary)− Header files – 各 個 不 同 MCU 的 Header file (.h檔 )− Linker scripts – 各 個 不 同 MCU 的 連 結 器 描 述 檔(.gld 檔 )© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 8


<strong>C30</strong> 對 Debugging g 的 Support●<strong>C30</strong> 整 合 於 <strong>MPLAB</strong> IDE 中 , 對 於 有 被 最 佳 化調 整 後 的 程 式 仍 然 可 進 行 Source Level 的debuggingg© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 9


Compilation ProcessC Source Assembly Source LibrariesCompiler PA Assembler LinkerAssembly File Object File Execution File© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 10


<strong>MPLAB</strong> <strong>C30</strong> 的 安 裝●●直 接 由 www.microchip.com/c30 com/c30 下 載適 用 的 版 本− Evaluation Version 或 Upgrade Version建 議 安 裝 之 目 錄− 不 論 PIC24 or dsPIC DSC 的 軟 體 , 都 建議 安 裝 於 預 設 的 目 錄− C:\Program Files\Microchip\<strong>MPLAB</strong> <strong>C30</strong>− 若 有 舊 版 本 安 裝 於 C:\pic30_tools\ 則 建 議將 其 移 除 後 一 併 將 整 個 目 錄 刪 除© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 11


<strong>MPLAB</strong> <strong>C30</strong> 的 安 裝●安 裝 成 功 後 可 由 功 能 表 Project – Set Language ToolLocation 來 檢 查 Microchip <strong>C30</strong> Toolsuite 的 安 裝 情 形© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 12


如 何 找 尋 有 關 <strong>MPLAB</strong> <strong>C30</strong> 的 文 件 (1)● 先 選 則 功 能 表 上 的 Help ->Topics●●再 由 Topics 的 列 表 中 找尋 <strong>C30</strong> Compiler 的 help現 有 的 為 <strong>MPLAB</strong> <strong>C30</strong>Libraries 以 及 <strong>MPLAB</strong><strong>C30</strong> Users Guide© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 13


如 何 找 尋 有 關 <strong>MPLAB</strong> <strong>C30</strong> 的 文 件 (2)●也 可 以 在 C:\Program Files\Microchip\<strong>MPLAB</strong> <strong>C30</strong>\docs裡 找 到 許 多 相 關 的 文 件© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 14


經 常 需 要 參 考 的 <strong>C30</strong> 目 錄●●●●●support\PIC24F\gld−內 含 各 不 同 MCU 的 Linker Script 檔support\PIC24F\h−bin−Lib−Include−內 含 各 不 同 CPU 的 標 準 Header file 以及 周 邊 Libraries 的 宣 告 檔<strong>MPLAB</strong> <strong>C30</strong> 的 各 種 執 行 檔存 放 <strong>MPLAB</strong> <strong>C30</strong> 提 供 之 各 種Libraries內 含 <strong>MPLAB</strong> <strong>C30</strong> 標 準 Libraries 的Header file , 例 如 stdio.h© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 15


經 常 需 要 參 考 的 <strong>C30</strong> 目 錄●support\Peripheral_24F\p −−−內 含 使 用 各 周 邊 Library 時 需 要 的header file例 如 uart.h or spi.hPIC24F_periph_features.h 是 個 重 要 的檔 案 , 其 定 義 了 每 一 不 同 的 MCU 型 號 的周 邊 為 那 一 個 Version. 以 便 各 周 邊 的header file 能 正 確 定 義 參 數●參 數 連 接 方 式 的 改 變 也 由 此 header file 控制 , 只 要 將 檔 案 中 #define USE_AND_OR的 註 解 去 掉 , 就 能 將 預 設 用 & 連 接 參 數 的 方式 改 為 用 | 來 連 接− 每 個 周 邊 的 header file 都 會 include :●●PIC24F_periph_features.hGenericTypeDefs.h© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 16


Review16-bit MCU 記 憶 體 結 構© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 17


Data Memory OrganizationMS ByteAddressMSB16-bitsLSBLS ByteAddress2 KBSFR SpaceSRAM Space0x00010x07FF0x0801SFR SpaceX Data Ram0x00000x07FE0x0800Near DataMemory8 KBY Data Ram0x1FFE2 KBDual-PortDMA RAM0x8001Dual Port RAM0x8000PIC24H or dsPIC33FOptionally MappedintoProgram MemoryUsing PSVUnimplemented0xFFFF0xFFFE© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 19


PSV - Program Space VisibilityFrom Data Space可 以 一 次 map 16KW (32KB) 的 program memory segments 到 Data Memory15SFR Space00x00002315 00x12340x0000000x001000PAGE 1Data Memory0x0080000x56780x0090000x8000PAGE 20x90000x01000032 KBProgram SpaceVisibility (PSV)Data Memory0xFFFE0xABCDPAGE 3Program Memory0x0110000x018000018000© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 20


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 21


Data Type – 資 料 型 別●●Data Type 是 用 來 定 義 資 料 儲 存 時 所 佔 記 憶 體 的 大 小 及 其 被處 理 的 方 式以 下 為 <strong>MPLAB</strong> <strong>C30</strong> 支 援 的 Data Type 及 其 所 佔 記 憶 體 和 可表 示 範 圍© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 22


Data Type – 資 料 型 別●●●●●變 數 - Variable一 般 變 數 存 放 於 Data Memory 中 , 可 以 被 任 意 的 讀 取 或 寫入變 數 一 定 要 先 宣 告 才 可 以 被 使 用宣 告 範 例− int X,Y ;− unsigned char ASCII_Char ;− long MyID = 0x1234 ; // 宣 告 並 指 定 初 值如 果 變 數 要 用 於 中 斷 服 務 程 式 或 不 希 望 被 最 佳 化 處 理 則 需 加上 volatile 修 飾 語− Volatile int VarForISR ;© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 23


Data Type – 資 料 型 別●常 數 可 存 放 於 Data Memory 或 Program Memory中 , 常 數 的 值 是 不 可 被 改 變 的● const – 用 來 指 定 要 將 常 數 資 料 存 放 於 program●●memory space 的 修 飾 語 , 而 且 資 料 將 透 過 “Auto PSV” 的方 式 存 取 (i.e. compiler-managed PSV)− ex. const char data[256];也 可 以 在 <strong>C30</strong> 的 Project – Build Options – Project 中 的<strong>MPLAB</strong> <strong>C30</strong> 選 項 中 選 擇 constants-in-memorymemory model 來 指 定 常 數 資 料 要 被 存 放 在 Data Memory之 中const data 的 大 小 限 制 於 32kB)© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 24


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 25


Special Function Registers●●●●MCU 使 用 Special Function Registers(SFRs) 來 控 制 HardwareC extern definitions 存 放 於 MCU 的 headerfiles 中 ( 包 括 SFR 內 控 制 位 元 的 宣 告 )要 使 用 SFR 就 必 需 在 程 式 開 頭 加 入#include or:#include SFR 的 命 名 與 data sheets 的 名 稱 幾 近 相 同− 原 始 定 義 於 各 MCU 的 .gld 檔 中© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 26


SFR 的 存 取●●Special Function Registers 可 以 被 C 程 式 碼 直 接 存取 :−−−Word access: PORTA = 0xFFEC;SFRNAMEbits.BITNAME 的 結 構 位 元 存 取 方 式 可 以 直 接讀 / 寫 暫 存 器 內 特 定 位 元●e.g. PORTAbits.RA1 = 1; orIPC0bits.INT0IP = 0;_SFRNAME and _SFRNAMEbits 也 可 以 被 用 來 參 考 到 同 一SFR : 因 為 .gld 檔 定 義 兩 種 名 稱 參 考 同 一 位 址●eg e.g. PORTA & PORTAbits 都 指 向 同 一 位 址如 此 使 用 的 前 提 必 須 使 用 標 準 的 Header file (.h) 以及 Linker Script (.gld)© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 27


.gld 檔 對 SFR 的 原 始 宣 告●P24FJ128GA006.gld 的 部 份 內 容− 起 始 於 Line 954TMR1 = 0x100;_TMR1 = 0x100;PR1 = 0x102;_PR1 = 0x102;T1CON = 0x104;_T1CON = 0x104;_T1CONbits = 0x104;TMR2 = 0x106;_TMR2 = 0x106;TMR3HLD = 0x108;_TMR3HLD = 0x108;TMR3= 0x10A;_TMR3 = 0x10A;© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 28


<strong>C30</strong> Header file 的 內 容●P24FJ128GB106.h 的 宣 告 內 容 – 以 T1CON 為 例extern volatile unsigned int T1CON __attribute__((__sfr__));__extension__ typedef struct tagT1CONBITS {union {struct {unsigned :1;unsigned TCS:1;unsigned TSYNC:1;unsigned :1;unsigned TCKPS0:1;unsigned TCKPS1:1;unsigned TGATE:1;unsigned :6;unsigned TSIDL:1;unsigned :1;unsigned TON:1;};struct {unsigned :4;unsigned TCKPS:2;};};} T1CONBITS;extern volatile T1CONBITS T1CONbits __attribute__((__sfr__));© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 29


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 30


建 立 第 一 個 <strong>C30</strong> Project●●確 定 <strong>MPLAB</strong> IDE 以 及 <strong>MPLAB</strong> <strong>C30</strong> 皆 已 安 裝 於 PC使 用 Project 功 能 表 中 的 Project Wizard 來 建 立 專 案●●必 要 加 入 Project 的 檔 案 有−Source file -.c− Linker Script - .gld // <strong>MPLAB</strong> V8.20 版 搭 配 <strong>C30</strong> V3.12 已 不 需要 , 但 若 要 用 修 改 過 的 非 標 準 .gld 則 還 是 要 加 入 project 中Optional 於 Project 的 檔 案 有−−Header file - .h 檔 以 便 於 在 <strong>MPLAB</strong> IDE 中 調 用 檔 案 參 考Object & Library file - .obj & .a 檔 包 含 相 關 的 Library & Subroutines© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 31


簡 易 的 <strong>C30</strong> 範 例 程 式●C:\Elite_Exercise\Day2\<strong>C30</strong>A.c#include const int Offset = 3;int Result , Gain ;int{}main(void)int Loop ;Result = 0 ;Gain = 1;for ( Loop = 0 ; Loop < 10 ; Loop ++ ){Result = Gain + Offset * Loop ;Gain ++}while (1) ;© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 32


建 立 <strong>C30</strong> Project 步 驟 1●使 用 Project 功 能 表 上 的 Project Wizard© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 33


建 立 <strong>C30</strong> Project 步 驟 2●“Next” 後 由 列 表 中 選 出 PIC24FJ128GB106 為 Device© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 34


建 立 <strong>C30</strong> Project 步 驟 3●Next 後 由 Active Toolsuite 中 選 取 Microchip <strong>C30</strong>Toolsuite 為 語 言 工 具© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 35


建 立 <strong>C30</strong> Project 步 驟 4●Next 後 使 用 Browse 按 鈕 或 直 接 輸 入 Project 的 路 徑 及 檔 名−Project 檔 案 最 好 與 程 式 的 Source code 放 於 同 一 目 錄© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 36


建 立 <strong>C30</strong> Project 步 驟 5●使 用 Browse 按 鈕 指 向 目 錄 C:\Elite_Exercise\ Day1 並 以My1st_Proj 為 專 案 檔 名 並 按 下 “ 儲 存 ”© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 37


建 立 <strong>C30</strong> Project 步 驟 6● 確 定 了 Project 的 路 徑 及 名 稱 後 就 選 取 “ 下 一 步 ”© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 38


建 立 <strong>C30</strong> Project 步 驟 7● 使 用 Add>> 將 <strong>C30</strong>A.c 加 入 Project 中 然 後 選 擇 ” 下 一 步 ”−−C:\Elite_Exercise\Day1\<strong>C30</strong>A.cE i \D 也 可 加 入●C:\Program Files\Microchip\<strong>MPLAB</strong> <strong>C30</strong>\Support\PIC24F\gld\p24fJ128GB106.gld© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 39


Linker Scripts 的 主 要 功 用●Linker scripts:− 指 定 各 可 用 Memory 的 區 間− 為 標 準 節 區 間 (Section) 定 位 指− 設 置 重 置 向 量●在 位 址 0x0000 安 插 goto reset 指 令− 初 始 中 斷 向 量 表●定 義 預 設 的 ISR 名 稱− 定 義 SFR 的 名 稱 與 對 應 的 位 址© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 40


建 立 <strong>C30</strong> Project 步 驟 8●檢 查 Summary , 若 一 切 無 誤 則 選 取 ” 完 成 ” 來 結 束Project Wizard© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 41


建 立 <strong>C30</strong> Project 步 驟 9● 若 Project 的 關 連 劃 面 未 呈 現 , 使 用 View – Project來 致 能− View 功 能 表 可 以 用 來 Enable/Disable 各 式 的 視 窗© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 42


建 立 <strong>C30</strong> Project 步 驟 10●專 案 建 立 完 成 !! 當 Project 視 窗 出 現 後 , 只 要 雙 擊 列 表 中 的 檔案 即 可 使 用 Editor 編 輯 – <strong>C30</strong>A.c 的 內 容 要 自 行 完 成 !!© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 43


Compile/Link <strong>C30</strong> 程 式●Compiler/Link 的 程 序 稱 為 Build All 或 Make− Make : 只 重 新 Compile 修 改 過 的 檔 案 後 進 行 Link− Build All : 重 新 Compile 所 有 檔 案 後 進 行 LinkMakeBuild All●Project 的 Link 程 序 分 為 Debug & Release 兩 種 Mode−−Debug : 要 使 用 硬 體 的 Debugger ( 如 ICD2 , Real ICE 等 ) 時 選 此 項目 , Linker 會 自 動 避 開 Debugger 要 用 到 的 資 源 以 避 免 資 源 衝 突Release : 當 不 做 Debug 而 要 在 程 式 燒 錄 完 成 後 讓 MCU 不 靠 PC就 能 運 作© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 44


<strong>C30</strong> Project Build 的 結 果●若 Project Build 的 結 果 無 誤 , 在 Output 視 窗 應 該 可 以 看 到以 下 SUCCESSED 的 結 果−Output 視 窗 可 用 功 能 表 項 目 View – Output 來 打 開© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 45


Build 錯 誤 的 處 理●若 Build 的 結 果 有 誤 , 仔 細 閱 讀 <strong>C30</strong> 所 給 的 error 訊 息 並 以滑 鼠 雙 擊 第 一 個 error 的 位 置−游 標 將 指 向 程 式 碼 中 被 發 現 錯 誤 的 地 方 , 冷 靜 地 判 斷 及 修 改© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 46


初 學 者 常 見 的 錯 誤● 初 學 者 使 用 的 程 式 都 很 小 , 常 見 的 錯 誤 如 下 :−−−−−大 小 寫 不 一 致●C 語 言 中 大 小 寫 不 同 就 代 表 不 一 樣 的 名 稱變 數 未 定 義●變 數 一 定 要 被 定 義 過 才 能 使 用 , 若 要 使 用 在 其 他 地 方 定 義 的 變數 需 使 用 extern 修 飾 詞重 複 宣 告●變 數 只 能 被 宣 告 一 次 , 若 要 使 用 在 其 他 地 方 定 義 的 變 數 需 使 用extern 修 飾 詞敘 述 (Statement) 區 塊 不 完 整●少 了 敘 述 結 尾 的 分 號 或 Statement Block 的 括 弧引 數 或 參 數 列 不 對 稱●呼 叫 函 式 時 的 參 數 列 未 有 正 確 數 目 的 參 數 或 型 別 有 錯− void MuFunc(int , int , int ) ; 在 呼 叫 時 要 給 3 個 整 數 型 態 的 參 數© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 47


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 48


選 擇 Debug 工 具●使 用 功 能 表 上 的 Debugger – Select Tool 選 項 來 選 擇 所 需要 的 除 錯 工 具 – Debugger 也 可 以 當 成 燒 錄 器 !!● PIC24FJ128GB106 可 用 的 除 錯 工 具 有 :−<strong>MPLAB</strong> ICD 2 : 硬 體 除 錯 工 具− <strong>MPLAB</strong> SIM : 軟 體 模 擬− REAL ICE : 硬 體 除 錯 工 具− PICket 2 : 硬 體 除 錯 工 具© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 49


<strong>MPLAB</strong> SIM 的 設 定●選 取 了 <strong>MPLAB</strong> SIM 後 可採 取 的 步 驟−−−−使 用 Settings 功 能 來 對 要模 擬 的 環 境 做 設 定 , 如MCU 的 工 作 頻 率使 用 StopWatch 來 觀 察特 定 執 行 程 序 所 用 指 令 週期 數 以 及 時 間使 用 Mouse 在 欲 設 定 斷點 的 程 式 碼 上 Double-Click 來 設 定 斷 點使 用 Run , HALT , StepInto 等 按 鈕 進 行 Debug© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 50


<strong>MPLAB</strong> SIM 的 設 定●●Simulator Setting 可 用 來 設 定 CPU 操 作 頻 率StopWatch 可 看 出 特 定 程 序 的 執 行 時 間 並 可 以 歸 零© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 51


●開 始 使 用 <strong>MPLAB</strong> SIM 除 錯1. 按 下 “RESET” 按 鈕 來 重 置 MCU● 2. 按 下 “RUN” 按 鈕 讓 程 式 執 行●3. 程 式 會 停 在 斷 點 上© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 52


使 用 <strong>MPLAB</strong> SIM 除 錯●●可 以 由 StopWatch 視 窗 看 到 執 行 的 Cycle 數 以 及所 花 費 時 間可 以 使 用 Step Into 做 單 步 執 行© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 53


更 多 Debugger 的 資 源●使 用 功 能 表 中 的 View – Watch 選 項 可 以 打 開Watch 視 窗 對 暫 存 器 或 程 式 的 變 數 進 行 觀 察−−選 取 欲 觀 察 物 件 後 用 Add SFR 或 Add Symbol 加 入也 可 直 接 用 滑 鼠 將 變 數 拉 進 Watch 視 窗© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 54


Watch 視 窗 與 程 式 的 執 行●不 論 使 用 軟 體 或 硬 體 模 擬 , Watch 都 可 以 用 來 觀 察變 數 的 變 化 – 也 可 以 用 不 同 格 式 來 顯 示 !© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 55


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 56


Mixing C and Assembly●●Assembly files (.s s ) can be used in<strong>C30</strong> projects and called from C− 請 參 考 <strong>C30</strong> 入 門 及 應 用 Part-2Inline assembly can be inserted intoC source code− Simple method:asm(“reset”) asm(“clrwdt”)− Complex method: a = b + casm volatile(“add %1, %2, %0” : “=r”(a): “r”(b), “r”(c))© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 57


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 58


Built-in Functions●讓 MCU 內 建 的 特 殊 功 能 可 以 被 <strong>C30</strong> 使 用− Multiply – 使 用 H/W 乘 法 器__builtin_mulss(const signed int p0, const signed int p1)− Divide – 使 用 H/W 除 法 器__builtin_divsd(const long num, const int den)− Table pointers– 使 用 H/W 查 表 操 作__builtin_tblpage(const void *p)__builtin_tbloffset(const void *p)__ builtin_ tblwtl(unsigned int offset, unsigned int data)__builtin_tblrdl(unsigned int offset)− PSV – retrieve and access PSV information__builtin_psvpage(const void *p)__builtin_psvoffset(const void *p)© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 59


Built-in Functions●讓 程 式 執 行 更 有 效 率 – Faster & Smaller− Bit toggle – 一 般 在 C 與 言 使 用 _bit = !_bit 的 方 式−−__builtin_btg(unsigned int *, unsigned int 0xn)Locked register writes – 硬 體 解 鎖 的 巨 集__builtin_write_NVM(void); _ _//sets WR bit__builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)All DSP Operations – 使 用 DSP 的 指 令 執 行 運 算__builtin_mac(…)●其 他 還 有 更 多 的 “built-ins”− Nop(), Sleep() & Idle(), ClrWdt()© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 60


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 61


Attributes of Variables● __attribute__ 這 個 關 鍵 字 讓 您可 以 為 需 要 的 variables orstructures 指 定 特 有 的 屬 性 , 包 括 :− 安 排 於 特 定 的 節 區 以 及 位 址●只 有 特 殊 的 情 況 下 須 要 如 此− 設 定 記 憶 體 安 排 時 的 邊 界 值 及 對 齊© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 62


Target Memory Attributes● __ attribute __((space(area)));( ●where area is:− auto_psv - compiler managed PSV− psv - user managed PSV− xmemory - data memory (X)− ymemoryy - data memory y( (Y)− eedata - EEDATA memory− dma - DMA memory有 關 attribute 的 說 明 請 參 考 <strong>MPLAB</strong><strong>C30</strong> Users Guide 第 2 章© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 63


Miscellaneous Attributes● aligned() - start align boundary● reverse() - end align boundary● near - near data (1st 8K)− int num __attribute__ ((near)) ;● far - far data (anywhere)● address() - start address● persistent - uninitialized on warmreset© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 64


Data Attributes: Examples●將 變 數 安 排 在 指 定 位 址 specific addressint MyVar __attribute__((address(0x860)))int __attribute__((address(0x862))) MyVar2− ** Be careful about overlap and auto variables確 定 變 數 被 安 排 在 near memory y( (below●8kB)char __ attribute __((near)) MyVar3, MyVar4© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 65


Data Attributes: Examples●將 變 數 安 排 於 特 定 section 或 spaceint foo __attribute__((section(“ThePlace”)))int bar __attribute__((space(psv)))− New section will be created, if needed− Spaces: data, prog, psv, eedata開 始 或 結 束 multi-byte boundary●Alignmantchar __ attribute __((aligned(32))) ( MyArray[18]y[ int __attribute__((reverse(64))) EndArray[25]© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 66


Attributes of Functions●針 對 Function 的 __attribute__ 讓 使 用者 可 以 設 定 特 定 Function 的 屬 性●●讓 Compiler 可 以 進 一 步 的 對 最 佳 化 做 調 整 並且 檢 驗 程 式 碼 的 正 確 性Examples include− Absolute function placement in memory− Specification of ISR functions© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 67


Function Attributes: Examples● Placement of a function at aparticular addressvoid foo() __attribute__ (address(0x1000))){...}● Declaration of interrupt t serviceroutines…void __attribute__((interrupt)) _ADC11Interrupt(void)© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 68


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 69


Interrupt Support● Declaring interrupts with thecompiler has two components:− 使 用 interrupt 屬 性 來 將 一 個 特 定 的Function 宣 告 為 中 斷 服 務 程 式 (ISR)− 也 可 以 額 外 的 宣 告 其 他 可 用 屬 性●shadow , save … 等© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 70


Interrupt Support以 ADC 的 中 斷 服 務 程 式 為 例 , 可 以 寫 成 以 下 的 型 式© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 71


Interrupt Support – 幾 種 可 用 的attribute 宣 告 (1)●Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)●Make function use shadow registersvoid __attribute__((interrupt,shadow))_ADC1Interrupt(void)−Saves W0-W3 and SR bits with push.s and pop.s●Optionally, save other variables on context savevoid__attribute__((interrupt(save(var1,var2))))_ADC1Interrupt(void)© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 72


Interrupt Support – 幾 種 可 用 的attribute 宣 告 (2)●●宣 告 ISR 使 用 compiler managed PSV (auto PSV)−若 ISR 使 用 到 auto PSV 所 用 到 的 PSVPAG 將 增 加Context Saving 的 時 間void __attribute__((interrupt,auto_psv)) _T1Interrupt(void) ;−若 ISR 不 會 使 用 到 auto PSV 所 用 到 的 PSVPAG 則 可 宣 告如 下void __attribute__((interrupt,no_auto_psv)) _T1Interrupt(void) ;<strong>MPLAB</strong> <strong>C30</strong> 的 預 設 值 是 假 設 ISR 會 用 到 auto PSV 所 使 用 的PSVPAG !!© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 73


<strong>MPLAB</strong> <strong>C30</strong> help 對 ISR 與 PSV使 用 上 的 說 明© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 74


Interrupt Support – 幾 種 可 用的 attribute 宣 告 (3)●●簡 易 的 ISR 宣 告void_ISR _ADC1Interrupt(void);_ISR 的 巨 集 宣 告 放 在 .h 檔 中−−−P24FJ128GB106.h for PIC24FJ128GB106#define _ISR __attribute__((interrupt))#define _ISRFAST __attribute__((interrupt,__(( shadow))© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 75


ISR 使 用 auto_psv 宣 告 的 中 斷 程 式© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 76


ISR 使 用 no_auto_psv 宣 告 的中 斷 程 式© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 77


中 斷 服 務 程 式 (ISR) 的 注 意 事 項●●●●void parameter 以 及 return types<strong>C30</strong> 會 自 動 完 成 Context save也 可 以 使 用 以 Shadow Registers 來 完成 的 Fast Interrupt SupportCompiler 使 用 特 殊 的 返 回 指 令 – 使 用RETFIE 指 令© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 78


中 斷 服 務 程 式 (ISR) 的 命 名 原 則●●●●●ISR 的 名 稱 以 底 線 開 頭_T2Interrupt_ AltINT1InterruptISR 的 名 稱 要 與 .gld 檔 的 定 義 相 同See <strong>MPLAB</strong> ® <strong>C30</strong> User’s Guide−請 參 考 7-4 節如 果 在 程 式 中 使 用 的 ISR 名 稱 無 法 在 .gld 檔找 到 時 , Linker 會 給 WarningLinker 會 自 動 將 相 關 ISR 的 起 始 位 寫 入 該 中斷 的 Interrupt Vector 位 址© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 79


P24FJ128GB106.gld 的 ISR 名 稱●定 義Function 名 稱 一 定 要 實 際 存 在 , 否 則 Linker 會 將中 斷 向 量 指 向 _DefaultInterrupt© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 80


Linker 將 ISR 位 址 寫 入 Vector© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 81


ISR 可 以 在 程 式 的 任 意 位 址MCU 會 在 Vector 位 址 找 到 進 入 點© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 82


當 沒 有 安 排 ISR 的 中 斷 發 生 !?●●Linker 會 將 向 量 指 到 _DefaultInterrupt – CPU 會 RESET最 好 改 寫 自 己 的 處 理 程 式 ! 例 如 以 UART 將 訊 息 送 出© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 83


增 加 自 己 的 _DefaultInterrupt處 理 程 式 (1)● 您 可 以 在 程 式 中 加 入 以 下 片 斷voidod _ DefaultInterrupt(void)te upt( {Nop();// printf ("Help Me !") ;while(1) ;}© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 84


增 加 自 己 的 _DefaultInterrupt處 理 程 式 (2)●由 Program Memory Window 可 看 到 預 設 的 程 式 動 作 已 不再 是 RESET 了© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 85


為 每 一 個 Trap 安 排 ISR●Trap 的 預 設 ISR 也 是 _DefaultInterrupt , 最 好 每一 個 Trap 都 有 自 己 的 ISR !●Trap 的 ISR 命 名 , 請 參 考 相 關 的 ,gld 檔● 以 Oscillator Trap 為 例 :void od __attributebute__(( ((interrupt)) _Osc OscillatorFail(void)a o {Nop() ; // 自 定 的 Trap 處 理 程 式while(1) ;}© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 86


其 他 針 對 中 斷 的 Support● Disabling Traps 以 外 的 中 斷#include unsigned int ipl;SET_AND_SAVE_CPU_IPL(ipl, SAVE 7);/*** Protected code here*/RESTORE_CPU_IPL(ipl);© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 87


其 他 針 對 中 斷 的 Support● Disabling level 7 以 下 的 中 斷− DISI 指 令 會 暫 時 將 CPU 優 先 等 級 提 升 為6 ( Cycle 數 由 disi 的 參 數 決 定 )__builtin_disi(16384);/*** Protected code here*/__builtin_disi(0);© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 88


ISR 使 用 變 數 時 的 注 意 事 項●使 用 volatile 關 鍵 字 於 共 用 的 變 數int volatile ticks = 0;void _ISR _T2Interrupt(void)t( {ticks++;IFS0bits.T2IF = 0;}© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 89


<strong>MPLAB</strong> <strong>C30</strong> 的 基 礎 使 用●●●●●●●●●VariablesSFR Access在 <strong>MPLAB</strong> IDE 建 立 Project使 用 <strong>MPLAB</strong> IDE & <strong>MPLAB</strong> SIM 來 觀 察 程 式 運 作Mixing C and AssemblyBuilt-in FunctionsSpecifying Attributes of Variables & FunctionsInterrupt SupportConfiguration Settings Support© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 90


Configuration Settings●●Device 的 configuration bits 可 以 直 接 在<strong>MPLAB</strong> ® IDE 的 “Configuration bits” 視 窗中 直 接 選 擇 及 設 定比 較 好 的 方 式 為 在 程 式 中 使 用 _CONFIGx 巨集 來 設 定 – 保 持 完 整 正 確 的 設 定 且 好 維 護− _CONFIG1( & ) ;_CONFIG2();e.g. _CONFIG1(GCP_OFF & GWRP_OFF & FWDT_ON & ICS_PGx2)− MCU 對 應 的 header files 包 含 了 使 用 範 例 以 及有 效 的 參 數 名 稱© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 91


_CONFIGx 的 範 例 – 取 自 PIC24FJ128GB106.h© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 92


<strong>C30</strong> 練 習 1在 程 式 中 直 接 指 定 CPU 的Configuration Bits© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 93


<strong>MPLAB</strong> <strong>C30</strong> 練 習 1●使 用 _CONFIGx 巨 集 來 將 MCU 的Configuration Bits 由 程 式 內 指 定●− _CONFIG1( ) & _CONFIG2( ) 要 安 插 在 程式 的 起 始 處− 適 當 的 位 置 為 #include … 敘 述 之 後使 用 在 <strong>C30</strong>A.c 中 有 兩 個 用 // 註 解 符 號Disable 的 敘 述− 將 註 解 符 號 除 去 後 加 上 正 確 的 參 數 列 組 合© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 94


設 定 Configuration bits●每 當 重 新 選 擇 Device 後 , Configuration Words 的 內 容 會 被RESET 成 0x7FFF – 所 有 bits 都 是 1© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 95


設 定 Configuration bits●請 修 改 _CONFIG1( ) & _CONFIG2( )的 內 容 讓 每 次 build 成功 後 的 Configuration 都 如 下 列 的 組 合© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 96


<strong>C30</strong> 練 習 2在 程 式 中 加 入 Traps 的處 理 程 式© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 97


在 程 式 中 加 入 Traps 的 處 理 程 式●●在 預 設 的 情 況 下 , 所 有 Traps 的 向 量 都 指 向_DefaultInterrupt請 為 Oscillator Fail , Address Erroe & StackerError 加 入 各 別 的 處 理 程 式© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 98


在 程 式 中 加 入 Traps 的 處 理 程 式●請 參 考 P24FJ128GB106.gld 內 的 定 義 名 稱 將 處 理程 式 分 別 加 入●© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 99


在 程 式 中 加 入 Traps 的 處 理 程 式● 處 理 程 式 只 是 測 試 用 , 所 以 統 一 的 內容 為 :Nop() ;while(1) ;© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 100


在 程 式 中 加 入 Traps 的 處 理 程 式●如 果 一 切 正 確 , Project 在 build 成 功 後 的Program Memory 視 窗 應 該 改 為 以 下 的 內 容© 2008 Microchip Technology Incorporated. All Rights Reserved. <strong>C30</strong> 入 門 及 應 用 Slide 101

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

Saved successfully!

Ooh no, something went wrong!