101
Section 7 Section 7 C30 入門及應用 C30 入門及應用 MPLAB ® C30 A Discussion on C il F t Compiler Features © 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 1

Microchip Taiwan - Section 7Section 7 C30C30 入門及應用 · 2012-04-18 · − 內含使用各周邊Library 時需要的 header file − 例如uarthorspihuart.h or spi.h − PIC24F_periph_features.h

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Section 7Section 7 C30 入門及應用C30 入門及應用

MPLABreg C30 A Discussion on

C il F tCompiler Features

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 1

Agendag MPLAB C30 語言工具

MPLAB C30 的基礎及應用minus Variables

SFR Accessminus SFR Accessminus 在 MPLAB IDE 建立 Projectminus 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

minus Mixing C and Assemblyminus Built-in Functionsminus Specifying Attributes of Variables amp FunctionsSpec y g bu es o a ab es amp u c o sminus Interrupt Support minus Configuration Settings Support

練習 1 在程式中直接指定 CPU 的 C fi i Bi 練習 1 在程式中直接指定 CPU 的 Configuration Bits 練習 2 在程式中加入 Traps 的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 2

典型的 C 程式架構

include ltxxxxhgtinclude ldquoxxxx hrdquoinclude ldquoxxxxhrdquoint X Y Int main(void)

The Statementshellip The Statements hellip

include 將置於各別 Header File 的宣告及定義引入

int XY 則宣告特定型別的變數供程式參考與使用 函數或副程式的主體有特定的結構 如範例中的 main 函

數包含了函數型別 (int) 及引數的型別 (void ) 一個完整的 C 程式一定由 main() 函數開始

minus C 的啟動模組一定將控制權交由 main

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 3

Statement ndash 敘述 C 的指令與運算式的組合 X = Y + 2

Whatrsquos MPLABreg C30

一個可被整合於 MPLAB IDE 中執一個可被整合於 MPLAB IDE 中執行並支援 Microchiprsquos 16-bit 系列的 C 語言工具套件的 C 語言工具套件

Microchip 16-bit 系列包括 c oc p 6 b t 系列包括minus PIC24Fminus PIC24Hminus dsPIC30Fminus dsPIC30Fminus dsPIC33F

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 4

More About MPLABreg C30 設定目標 ndash 相容於 ANSI 1989 規範 設定目標 相容於 ANSI 1989 規範

架構於 GCC Compilerminus 提供標準的 C functions

提供 Run time libraries 提供 Run time librariesminus Maths library Helper routines hellip

$ One-time cost of $895USminus PN SW006012PN SW006012minus 可以使用於所有的 16-bit Family

i hi 30copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 5

minus wwwmicrochipcomc30

MPLAB C30 的其他版本

PN SW006013 ndash US$ 495 PN SW006013 US$ 495minus 使用於 dsPIC DSC 系列

PN SW006014 US$ 495 PN SW006014 ndash US$ 495minus 使用於 PIC24 MCU 系列使用於 PIC24 MCU 系列

Microchip 提供 Evaluation VersionS Sminus SW006013 or SW006014

minus 安裝後的 60 天內功能與正式版相同

minus 60 天後只有最佳化的功能受影響只能使用 Level 1 的最佳化

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 6

只能使用 Level 1 的最佳化

MPLABreg C30 相關的重要檔案

C compiler pic30-gcc exe C compiler pic30-gccexe Assembler pic30-asexe Assembler pic30 asexe Linker pic30-ldexe Simulator sim30exe Binary utilities ndash Next Page

S fil N P Support files ndash Next Page

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 7

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Agendag MPLAB C30 語言工具

MPLAB C30 的基礎及應用minus Variables

SFR Accessminus SFR Accessminus 在 MPLAB IDE 建立 Projectminus 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

minus Mixing C and Assemblyminus Built-in Functionsminus Specifying Attributes of Variables amp FunctionsSpec y g bu es o a ab es amp u c o sminus Interrupt Support minus Configuration Settings Support

練習 1 在程式中直接指定 CPU 的 C fi i Bi 練習 1 在程式中直接指定 CPU 的 Configuration Bits 練習 2 在程式中加入 Traps 的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 2

典型的 C 程式架構

include ltxxxxhgtinclude ldquoxxxx hrdquoinclude ldquoxxxxhrdquoint X Y Int main(void)

The Statementshellip The Statements hellip

include 將置於各別 Header File 的宣告及定義引入

int XY 則宣告特定型別的變數供程式參考與使用 函數或副程式的主體有特定的結構 如範例中的 main 函

數包含了函數型別 (int) 及引數的型別 (void ) 一個完整的 C 程式一定由 main() 函數開始

minus C 的啟動模組一定將控制權交由 main

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 3

Statement ndash 敘述 C 的指令與運算式的組合 X = Y + 2

Whatrsquos MPLABreg C30

一個可被整合於 MPLAB IDE 中執一個可被整合於 MPLAB IDE 中執行並支援 Microchiprsquos 16-bit 系列的 C 語言工具套件的 C 語言工具套件

Microchip 16-bit 系列包括 c oc p 6 b t 系列包括minus PIC24Fminus PIC24Hminus dsPIC30Fminus dsPIC30Fminus dsPIC33F

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 4

More About MPLABreg C30 設定目標 ndash 相容於 ANSI 1989 規範 設定目標 相容於 ANSI 1989 規範

架構於 GCC Compilerminus 提供標準的 C functions

提供 Run time libraries 提供 Run time librariesminus Maths library Helper routines hellip

$ One-time cost of $895USminus PN SW006012PN SW006012minus 可以使用於所有的 16-bit Family

i hi 30copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 5

minus wwwmicrochipcomc30

MPLAB C30 的其他版本

PN SW006013 ndash US$ 495 PN SW006013 US$ 495minus 使用於 dsPIC DSC 系列

PN SW006014 US$ 495 PN SW006014 ndash US$ 495minus 使用於 PIC24 MCU 系列使用於 PIC24 MCU 系列

Microchip 提供 Evaluation VersionS Sminus SW006013 or SW006014

minus 安裝後的 60 天內功能與正式版相同

minus 60 天後只有最佳化的功能受影響只能使用 Level 1 的最佳化

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 6

只能使用 Level 1 的最佳化

MPLABreg C30 相關的重要檔案

C compiler pic30-gcc exe C compiler pic30-gccexe Assembler pic30-asexe Assembler pic30 asexe Linker pic30-ldexe Simulator sim30exe Binary utilities ndash Next Page

S fil N P Support files ndash Next Page

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 7

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

典型的 C 程式架構

include ltxxxxhgtinclude ldquoxxxx hrdquoinclude ldquoxxxxhrdquoint X Y Int main(void)

The Statementshellip The Statements hellip

include 將置於各別 Header File 的宣告及定義引入

int XY 則宣告特定型別的變數供程式參考與使用 函數或副程式的主體有特定的結構 如範例中的 main 函

數包含了函數型別 (int) 及引數的型別 (void ) 一個完整的 C 程式一定由 main() 函數開始

minus C 的啟動模組一定將控制權交由 main

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 3

Statement ndash 敘述 C 的指令與運算式的組合 X = Y + 2

Whatrsquos MPLABreg C30

一個可被整合於 MPLAB IDE 中執一個可被整合於 MPLAB IDE 中執行並支援 Microchiprsquos 16-bit 系列的 C 語言工具套件的 C 語言工具套件

Microchip 16-bit 系列包括 c oc p 6 b t 系列包括minus PIC24Fminus PIC24Hminus dsPIC30Fminus dsPIC30Fminus dsPIC33F

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 4

More About MPLABreg C30 設定目標 ndash 相容於 ANSI 1989 規範 設定目標 相容於 ANSI 1989 規範

架構於 GCC Compilerminus 提供標準的 C functions

提供 Run time libraries 提供 Run time librariesminus Maths library Helper routines hellip

$ One-time cost of $895USminus PN SW006012PN SW006012minus 可以使用於所有的 16-bit Family

i hi 30copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 5

minus wwwmicrochipcomc30

MPLAB C30 的其他版本

PN SW006013 ndash US$ 495 PN SW006013 US$ 495minus 使用於 dsPIC DSC 系列

PN SW006014 US$ 495 PN SW006014 ndash US$ 495minus 使用於 PIC24 MCU 系列使用於 PIC24 MCU 系列

Microchip 提供 Evaluation VersionS Sminus SW006013 or SW006014

minus 安裝後的 60 天內功能與正式版相同

minus 60 天後只有最佳化的功能受影響只能使用 Level 1 的最佳化

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 6

只能使用 Level 1 的最佳化

MPLABreg C30 相關的重要檔案

C compiler pic30-gcc exe C compiler pic30-gccexe Assembler pic30-asexe Assembler pic30 asexe Linker pic30-ldexe Simulator sim30exe Binary utilities ndash Next Page

S fil N P Support files ndash Next Page

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 7

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Whatrsquos MPLABreg C30

一個可被整合於 MPLAB IDE 中執一個可被整合於 MPLAB IDE 中執行並支援 Microchiprsquos 16-bit 系列的 C 語言工具套件的 C 語言工具套件

Microchip 16-bit 系列包括 c oc p 6 b t 系列包括minus PIC24Fminus PIC24Hminus dsPIC30Fminus dsPIC30Fminus dsPIC33F

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 4

More About MPLABreg C30 設定目標 ndash 相容於 ANSI 1989 規範 設定目標 相容於 ANSI 1989 規範

架構於 GCC Compilerminus 提供標準的 C functions

提供 Run time libraries 提供 Run time librariesminus Maths library Helper routines hellip

$ One-time cost of $895USminus PN SW006012PN SW006012minus 可以使用於所有的 16-bit Family

i hi 30copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 5

minus wwwmicrochipcomc30

MPLAB C30 的其他版本

PN SW006013 ndash US$ 495 PN SW006013 US$ 495minus 使用於 dsPIC DSC 系列

PN SW006014 US$ 495 PN SW006014 ndash US$ 495minus 使用於 PIC24 MCU 系列使用於 PIC24 MCU 系列

Microchip 提供 Evaluation VersionS Sminus SW006013 or SW006014

minus 安裝後的 60 天內功能與正式版相同

minus 60 天後只有最佳化的功能受影響只能使用 Level 1 的最佳化

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 6

只能使用 Level 1 的最佳化

MPLABreg C30 相關的重要檔案

C compiler pic30-gcc exe C compiler pic30-gccexe Assembler pic30-asexe Assembler pic30 asexe Linker pic30-ldexe Simulator sim30exe Binary utilities ndash Next Page

S fil N P Support files ndash Next Page

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 7

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

More About MPLABreg C30 設定目標 ndash 相容於 ANSI 1989 規範 設定目標 相容於 ANSI 1989 規範

架構於 GCC Compilerminus 提供標準的 C functions

提供 Run time libraries 提供 Run time librariesminus Maths library Helper routines hellip

$ One-time cost of $895USminus PN SW006012PN SW006012minus 可以使用於所有的 16-bit Family

i hi 30copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 5

minus wwwmicrochipcomc30

MPLAB C30 的其他版本

PN SW006013 ndash US$ 495 PN SW006013 US$ 495minus 使用於 dsPIC DSC 系列

PN SW006014 US$ 495 PN SW006014 ndash US$ 495minus 使用於 PIC24 MCU 系列使用於 PIC24 MCU 系列

Microchip 提供 Evaluation VersionS Sminus SW006013 or SW006014

minus 安裝後的 60 天內功能與正式版相同

minus 60 天後只有最佳化的功能受影響只能使用 Level 1 的最佳化

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 6

只能使用 Level 1 的最佳化

MPLABreg C30 相關的重要檔案

C compiler pic30-gcc exe C compiler pic30-gccexe Assembler pic30-asexe Assembler pic30 asexe Linker pic30-ldexe Simulator sim30exe Binary utilities ndash Next Page

S fil N P Support files ndash Next Page

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 7

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的其他版本

PN SW006013 ndash US$ 495 PN SW006013 US$ 495minus 使用於 dsPIC DSC 系列

PN SW006014 US$ 495 PN SW006014 ndash US$ 495minus 使用於 PIC24 MCU 系列使用於 PIC24 MCU 系列

Microchip 提供 Evaluation VersionS Sminus SW006013 or SW006014

minus 安裝後的 60 天內功能與正式版相同

minus 60 天後只有最佳化的功能受影響只能使用 Level 1 的最佳化

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 6

只能使用 Level 1 的最佳化

MPLABreg C30 相關的重要檔案

C compiler pic30-gcc exe C compiler pic30-gccexe Assembler pic30-asexe Assembler pic30 asexe Linker pic30-ldexe Simulator sim30exe Binary utilities ndash Next Page

S fil N P Support files ndash Next Page

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 7

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLABreg C30 相關的重要檔案

C compiler pic30-gcc exe C compiler pic30-gccexe Assembler pic30-asexe Assembler pic30 asexe Linker pic30-ldexe Simulator sim30exe Binary utilities ndash Next Page

S fil N P Support files ndash Next Page

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 7

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLABreg C30 重要內容

Binary utilitiesyminus Archive librarian pic30-arexeminus Intel(tm) Hexfile generatorIntel(tm) Hexfile generator

Support filesLibraries libc a libm a libdsp a libp24Fxxxminus Libraries libca libma libdspa libp24Fxxx-coffa ( 24FJ 的 Peripheral library 整合為 1 其他的 16-bit MCU 每個都有自己的 Peripheral 的 每個都有自己的 plibrary)

minus Header files ndash 各個不同 MCU 的 Header file (h 檔

(檔 )

minus Linker scripts ndash 各個不同 MCU 的連結器描述檔( ld 檔 )

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 8

(gld 檔 )

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

C30 對 Debugging 的 Supportgg g pp

C30 整合於 MPLAB IDE 中 對於有被最佳化C30 整合於 中 對於有被最佳化調整後的程式仍然可進行 Source Level 的debugginggg g

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 9

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Compilation Processp

C Source Assembly Source Libraries

Compiler PA Assembler Linker

A bl Fil Obj t Fil E ti FilAssembly File Object File Execution File

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 10

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的安裝

直接由 www microchip comc30 下載 直接由 wwwmicrochipcomc30 下載適用的版本

或minus Evaluation Version 或 Upgrade Version 建議安裝之目錄 建議安裝之目錄

minus 不論 PIC24 or dsPIC DSC 的軟體都建議安裝於預設的目錄議安裝於預設的目錄

minus CProgram FilesMicrochipMPLAB C30CProgram FilesMicrochipMPLAB C30minus 若有舊版本安裝於 Cpic30_tools 則建議

將其移除後一併將整個目錄刪除

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 11

將其移除後一併將整個目錄刪除

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的安裝 安裝成功後可由功能表 Project ndash Set Language Tool

Location 來檢查 Microchip C30 Toolsuite 的安裝情形Location 來檢查 Microchip C30 Toolsuite 的安裝情形

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 12

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

如何找尋有關 MPLAB C30 的文件 (1)

先選則功能表上的 Help -gt pTopics

再由 Topics 的列表中找尋 C30 C il 的 h l尋 C30 Compiler 的 help

現有的為 MPLAB C30 Libraries 以及 MPLABLibraries 以及 MPLAB C30 Users Guide

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 13

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

如何找尋有關 MPLAB C30 的文件 (2)

也可以在 CProgram FilesMicrochipMPLAB C30docs g p裡找到許多相關的文件

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 14

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

經常需要參考的 C30 目錄

supportPIC24Fgldpp gminus 內含各不同 MCU 的 Linker Script 檔

supportPIC24Fhminus 內含各不同 CPU 的標準 Header file 以

及周邊 Libraries 的宣告檔

bin binminus MPLAB C30 的各種執行檔

Lib Libminus 存放 MPLAB C30 提供之各種

Libraries

Includeminus 內含 MPLAB C30 標準 Libraries 的

Header file 例如 stdio h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 15

Header file 例如 stdioh

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

經常需要參考的 C30 目錄

supportPeripheral_24Fpp p _minus 內含使用各周邊 Library 時需要的

header file例如 uart h or spi hminus 例如 uarth or spih

minus PIC24F_periph_featuresh 是個重要的檔案 其定義了每一不同的 MCU 型號的周邊為那一個 V i 以便各周邊的周邊為那一個 Version 以便各周邊的header file 能正確定義參數 參數連接方式的改變也由此 header file 控

制 只要將檔案中 d fi USE AND OR制 只要將檔案中 define USE_AND_OR 的註解去掉 就能將預設用 amp 連接參數的方式改為用 | 來連接

minus 每個周邊的 header file 都會 include 每個周邊的 header file 都會 include PIC24F_periph_featuresh GenericTypeDefsh

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 16

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

ReviewReview

16 bit MCU 記憶體結構16-bit MCU 記憶體結構

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 17

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Program Memory Organization 最大可定址 4M 指令

g y g

000000h000000hPClt221gt0 0

023

minus 4MB x 24-bit minus 23-bit PC (PCH amp PCL)

PC i i d

000000h000000h

ce

Reset Vector

Interrupt Vector Table 000004h000004h

000104h000104h PC increments in words (LSB always lsquo0rsquo)Reset Vector at 0 er

Spa

c

On Chip User Flash

Alternate Vector Table 000104h000104h

000200h000200h Reset Vector at 0 Interrupt Vector Table

from 4h to 1FEh

Us On Chip User Flash

Memory

Flash Config Words from 4h to 1FEh User Code space from

200h to 7FFFFEh (what 7FFFFEh7FFFFEh

ce

Unimplemented

g

(ever is implemented)

TABPAG Source reg fig S

pac

Config Registers

Table Ops023

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 18

FFFFFEhFFFFFEh

TABPAG Source reg

Con

f Device IDSource reg PSVPAG 00

Table OpsPSV

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Data Memory OrganizationMS ByteAddress

LS ByteAddress

16-bits

MSB LSB

y g

Near Data 0x0801 0x0800

SFR Space0x0001 0x0000

0x07FF 0x07FE

2 KBSFR Space

MSB LSB

Memory8 KB

SRAM Space

X Data Ram

Y Data RamY Data Ram 0x1FFE

Dual Port RAM2 KBDual-Port DMA RAM

PIC24H or dsPIC33FPIC24H or dsPIC33F

Unimplemented Optionally Mapped

0x8001 0x8000DMA RAM

p y ppinto

Program Memory Using PSV

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 19

0xFFFF 0xFFFE

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

PSV - Program Space Visibility From Data SpaceFrom Data Space

可以一次可以一次 map 16KW (32KB) map 16KW (32KB) 的的 program memory segments program memory segments 到到 Data Memory Data Memory

15SFR Space

0x0000 0x00000015 0230x0000

0x12340x1234 0x0010000x12340x1234

015

Data Memory 0x008000

PAGE 1PAGE 1

0 8000

0x008000

PAGE 2PAGE 2

0x0090000x56780x56780x56780x5678

0x8000

0x0100000x9000

PAGE 2PAGE 2

0x0110000xABCD0xABCD0xABCD0xABCD

32 KB32 KBProgram SpaceProgram SpaceVisibility (PSV)Visibility (PSV)

0 018000

PAGE 3PAGE 3

0x011000

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 20

Program Memory0x018000

Data Memory0xFFFE

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 21

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Data Type ndash 資料型別yp Data Type 是用來定義資料儲存時所佔記憶體的大小及其被

處理的方式處理的方式

以下為 MPLAB C30 支援的 Data Type 及其所佔記憶體和可表示範圍表示範圍

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 22

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Data Type ndash 資料型別yp 變數 - Variable

一般變數存放於 D t M 中 可以被任意的讀取或寫 一般變數存放於 Data Memory 中 可以被任意的讀取或寫入

變數一定要先宣告才可以被使用 變數一定要先宣告才可以被使用

宣告範例minus int XY minus unsigned char ASCII_Char minus long MyID = 0x1234 宣告並指定初值

如果變數要用於中斷服務程式或不希望被最佳化處理則需加 如果變數要用於中斷服務程式或不希望被最佳化處理則需加上 volatile 修飾語minus Volatile int VarForISR

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 23

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Data Type ndash 資料型別yp

常數可存放於 Data Memory 或 Program Memory 常數可存放於 Data Memory 或 Program Memory 中 常數的值是不可被改變的

const ndash 用來指定要將常數資料存放於 program const 用來指定要將常數資料存放於 program memory space 的修飾語 而且資料將透過 ldquoAuto PSVrdquo 的方式存取 (ie compiler-managed PSV)minus ex const char data[256]

也可以在 C30 的 Project ndash Build Options ndash Project 中的MPLAB C30 選項中選擇 iMPLAB C30 選項中選擇 constants-in-memorymemory model 來指定常數資料要被存放在 Data Memory 之中之中

const data 的大小限制於 lt32kBminus 使用 Auto PSV 時

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 24

使用 Auto PSV 時minus See PSV slides for more options (gt32kB)

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 25

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Special Function Registersp g

MCU 使用 Special Function Registers MCU 使用 Special Function Registers (SFRs) 來控制 HardwareC e tern definitions 存放於 MCU 的 header C extern definitions 存放於 MCU 的 header files 中 ( 包括 SFR 內控制位元的宣告 )

要使用 SFR 就必需在程式開頭加入include ltp24fj128ga006hgtp j g

orinclude ltp24fxxxx hgtinclude ltp24fxxxxhgt

SFR 的命名與 data sheets 的名稱幾近相同

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 26

minus 原始定義於各 MCU 的 gld 檔中

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

SFR 的存取

Special Function Registers 可以被 C 程式碼直接存 Special Function Registers 可以被 C 程式碼直接存取minus Word access PORTA = 0xFFECminus SFRNAMEbitsBITNAME 的結構位元存取方式可以直接

讀寫暫存器內特定位元 eg PORTAbitsRA1 = 1 or

IPC0bitsINT0IP = 0

d bi 也可以被用來參考到同一minus _SFRNAME and _SFRNAMEbits也可以被用來參考到同一SFR 因為 gld 檔定義兩種名稱參考同一位址 e g PORTA amp PORTAbits 都指向同一位址 eg PORTA amp PORTAbits 都指向同一位址

如此使用的前提必須使用標準的 Header file (h) 以及 Linker Script ( gld)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 27

及 Linker Script (gld)

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

gld 檔對 SFR 的原始宣告g P24FJ128GA006gld 的部份內容

起始於minus 起始於 Line 954

TMR1 TMR1 = 0x100= 0x100_TMR1 _TMR1 = 0x100= 0x100PR1 PR1 = 0x102= 0x102PR1PR1 0 1020 102_PR1 _PR1 = 0x102= 0x102

T1CON T1CON = 0x104= 0x104_T1CON _T1CON = 0x104= 0x104

T1CONbitsT1CONbits = 0x104= 0x104_T1CONbits _T1CONbits = 0x104= 0x104TMR2 TMR2 = 0x106= 0x106_TMR2 _TMR2 = 0x106= 0x106TMR3HLDTMR3HLD = 0x108= 0x108TMR3HLD TMR3HLD = 0x108= 0x108_TMR3HLD _TMR3HLD = 0x108= 0x108TMR3 TMR3 = 0x10A= 0x10ATMR3TMR3 = 0x10A= 0x10A

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 28

_TMR3 _TMR3 0x10A 0x10A

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

C30 Header file 的內容 P24FJ128GB106h 的宣告內容 ndash 以 T1CON 為例

extern volatile unsigned int T1CON __attribute__((__sfr__))__extension__ typedef struct tagT1CONBITS

union struct

i d 1unsigned 1unsigned TCS1unsigned TSYNC1unsigned 1unsigned TCKPS01unsigned TCKPS01unsigned TCKPS11unsigned TGATE1unsigned 6unsigned TSIDL1unsigned TSIDL1unsigned 1unsigned TON1

struct

unsigned 4unsigned TCKPS2

T1CONBITS

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 29

T1CONBITSextern volatile T1CONBITS T1CONbits __attribute__((__sfr__))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 30

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立第一個 C30 Projectj 確定 MPLAB IDE 以及 MPLAB C30 皆已安裝於 PC 使用 Project 功能表中的 Project Wizard 來建立專案

必要加入 Project 的檔案有Source file cminus Source file - c

minus Linker Script - gld MPLAB V820 版搭配 C30 V312 已不需要 但若要用修改過的非標準 gld 則還是要加入 project 中

於 的檔案有 Optional 於 Project 的檔案有minus Header file - h 檔以便於在 MPLAB IDE 中調用檔案參考

minus Object amp Library file - obj amp a 檔包含相關的 Library amp Subroutines

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 31

Object amp Library file obj amp a 檔包含相關的 Library amp Subroutines

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

簡易的 C30 範例程式

CElite_ExerciseDay2C30Acinclude include ltp24fxxxxhgtltp24fxxxxhgt

constconst intint Offset = 3 Offset = 3 const const intint Offset 3 Offset 3 intint Result Gain Result Gain

intint main(void)main(void)

intint Loop Loop Result = 0 Result = 0 Gain = 1 Gain = 1 Gain 1 Gain 1 for ( Loop = 0 Loop lt 10 Loop ++ )for ( Loop = 0 Loop lt 10 Loop ++ )

Result = Gain + Offset Loop Result = Gain + Offset Loop G iG iGain ++Gain ++

while (1) while (1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 32

while (1) while (1)

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 1j

使用 Project 功能表上的 Project Wizard 使用 Project 功能表上的 Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 33

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 2j ldquoNextrdquo 後由列表中選出 PIC24FJ128GB106 為 Device

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 34

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 3j Next 後由 Active Toolsuite 中選取 Microchip C30 p

Toolsuite 為語言工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 35

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 4j Next 後使用 Browse 按鈕或直接輸入 Project 的路徑及檔名

Project 檔案最好與程式的 Source code 放於同一目錄minus Project 檔案最好與程式的 Source code 放於同一目錄

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 36

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 5j 使用 Browse 按鈕指向目錄 CElite_Exercise Day1並以_ y

My1st_Proj 為專案檔名並按下 ldquo儲存rdquo

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 37

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 6j 確定了 Project 的路徑及名稱後就選取 ldquo下一步rdquo 確定了 Project 的路徑及名稱後就選取 下一步

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 38

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 7j 使用 Addgtgt 將C30Ac 加入Project 中然後選擇rdquo下一步rdquo

C Elit E i D 1C30Aminus CElite_ExerciseDay1C30Acminus 也可加入

CProgram FilesMicrochipMPLAB C30SupportPIC24Fgldp24fJ128GB106gld

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 39

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Linker Scripts 的主要功用p

Linker scripts Linker scriptsminus 指定各可用 Memory 的區間

為標準節區間(S ti ) 定位指minus 為標準節區間(Section) 定位指

minus 設置重置向量 在位址 0x0000 安插 goto reset 指令

minus 初始中斷向量表初始中斷向量表 定義預設的 ISR 名稱

定義 SFR 的名稱與對應的位址minus 定義 SFR 的名稱與對應的位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 40

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 8j 檢查 Summary 若一切無誤則選取rdquo完成rdquo 來結束 檢查 Summary 若一切無誤則選取 完成 來結束

Project Wizard

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 41

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 9j 若 Project 的關連劃面未呈現 使用 View ndash Project 若 Project 的關連劃面未呈現 使用 View Project

來致能

minus View 功能表可以用來 EnableDisable 各式的視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 42

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

建立 C30 Project 步驟 10j 專案建立完成 當Project 視窗出現後 只要雙擊列表中的檔j

案即可使用 Editor 編輯 ndash C30Ac 的內容要自行完成

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 43

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

CompileLink C30 程式

CompilerLink 的程序稱為 Build All 或 Make

p

minus Make 只重新 Compile 修改過的檔案後進行 Linkminus Build All 重新 Compile 所有檔案後進行 Link

Build AllBuild AllMakeMake

Project 的 Link 程序分為 Debug amp Release 兩種 Modeminus Debug 要使用硬體的 Debugger ( 如 ICD2 Real ICE 等)時選此項

目 Li k 會自動避開 D b 要用到的資源以避免資源衝突目 Linker 會自動避開 Debugger 要用到的資源以避免資源衝突

minus Release 當不做 Debug 而要在程式燒錄完成後讓 MCU 不靠 PC 就能運作

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 44

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

C30 Project Build 的結果j 若 Project Build 的結果無誤 在 Output 視窗應該可以看到

以下 SUCCESSED 的結果以下 SUCCESSED 的結果minus Output 視窗可用功能表項目 View ndash Output 來打開

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 45

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Build 錯誤的處理

若 Build 的結果有誤 仔細閱讀 C30 所給的 error 訊息並以滑鼠雙擊第一個 error 的位置滑鼠雙擊第一個 error 的位置minus 游標將指向程式碼中被發現錯誤的地方 冷靜地判斷及修改

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 46

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

初學者常見的錯誤

初學者使用的程式都很小 常見的錯誤如下minus 大小寫不一致

C 語言中大小寫不同就代表不一樣的名稱

minus 變數未定義 變數一定要被定義過才能使用 若要使用在其他地方定義的變

數需使用 extern 修飾詞飾

minus 重複宣告 變數只能被宣告一次 若要使用在其他地方定義的變數需使用

extern 修飾詞extern 修飾詞

minus 敘述(Statement)區塊不完整 少了敘述結尾的分號或 Statement Block 的括弧少了敘述結尾的分號或 的括弧

minus 引數或參數列不對稱 呼叫函式時的參數列未有正確數目的參數或型別有錯

在呼叫時要給 個整數型態的參數

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 47

minus void MuFunc(int int int ) 在呼叫時要給 3 個整數型態的參數

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 48

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

選擇 Debug 工具g

使用功能表上的 Debugger ndash Select Tool 選項來選擇所需要的除錯工具 ndash Debugger 也可以當成燒錄器 要的除錯工具 ndash Debugger 也可以當成燒錄器

PIC24FJ128GB106 可用的除錯工具有 minus MPLAB ICD 2 硬體除錯工具MPLAB ICD 2 硬體除錯工具

minus MPLAB SIM 軟體模擬

minus REAL ICE 硬體除錯工具

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 49

minus PICket 2 硬體除錯工具

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB SIM 的設定

選取了 MPLAB SIM 後可 選取了 MPLAB SIM 後可採取的步驟

minus 使用 Settings 功能來對要g模擬的環境做設定 如MCU 的工作頻率

使用 S 來觀察minus 使用 StopWatch 來觀察特定執行程序所用指令週期數以及時間期數以及時間

minus 使用 Mouse 在欲設定斷點的程式碼上 Double-Click 來設定斷點

minus 使用 Run HALT Step I t 等按鈕進行 D b

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 50

Into 等按鈕進行 Debug

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB SIM 的設定

Simulator Setting 可用來設定 CPU 操作頻率 Simulator Setting 可用來設定 CPU 操作頻率

StopWatch 可看出特定程序的執行時間並可以歸零

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 51

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

開始使用 MPLAB SIM 除錯 1 按下 ldquoRESETrdquo 按鈕來重置 MCU 2 按下 ldquoRUNrdquo 按鈕讓程式執行 2 按下 RUN 按鈕讓程式執行

3 程式會停在斷點上

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 52

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

使用 MPLAB SIM 除錯 可以由 StopWatch 視窗看到執行的 Cycle 數以及

所花費時間所花費時間

可以使用 Step Into 做單步執行

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 53

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

更多 Debugger 的資源gg 使用功能表中的 View ndash Watch 選項可以打開

Watch 視窗對暫存器或程式的變數進行觀察Watch 視窗對暫存器或程式的變數進行觀察

minus 選取欲觀察物件後用 Add SFR 或 Add Symbol 加入

也可直接用滑鼠將變數拉進 Watch 視窗minus 也可直接用滑鼠將變數拉進 Watch 視窗

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 54

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Watch 視窗與程式的執行

不論使用軟體或硬體模擬 Watch 都可以用來觀察變數的變化 也可以用不同格式來顯示 變數的變化 ndash 也可以用不同格式來顯示

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 55

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 56

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Mixing C and Assemblyg y

Assembly files ( s ) can be used in Assembly files (s ) can be used in C30 projects and called from C

請參考 入門及應用minus 請參考 C30 入門及應用 Part-2 Inline assembly can be inserted into Inline assembly can be inserted into

C source codeminus Simple method asm(ldquoresetrdquo) asm(ldquoclrwdtrdquo)

minus Complex method a = b + casm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 57

asm volatile(ldquoadd 1 2 0rdquo ldquo=rrdquo(a) ldquorrdquo(b) ldquorrdquo(c))

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 58

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Built-in Functions

讓 MCU 內建的特殊功能可以被 C30 使用 讓 MCU 內建的特殊功能可以被 C30 使用

minus Multiply ndash 使用 HW 乘法器builtin mulss(const signed int p0 const signed int p1)

minus Divide ndash 使用 HW 除法器

__builtin_mulss(const signed int p0 const signed int p1)

minus Table pointers ndash 使用 HW 查表操作

__builtin_divsd(const long num const int den)

Table pointers 使用 HW 查表操作__builtin_tblpage(const void p)__builtin_tbloffset(const void p)builtin tblwtl(unsigned int offset unsigned int data)

minus PSV ndash retrieve and access PSV information

__ _ ( g g )__builtin_tblrdl(unsigned int offset)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 59

__builtin_psvpage(const void p)__builtin_psvoffset(const void p)

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Built-in Functions 讓程式執行更有效率 ndash Faster amp Smaller

minus Bit toggle ndash 一般在 C 與言使用 _bit = _bit 的方式

__builtin_btg(unsigned int unsigned int 0xn)

minus Locked register writes ndash 硬體解鎖的巨集

__builtin_write_NVM(void) sets WR bit

All DSP Operations 使用 DSP 的指令執行運算

__ _ ___builtin_write_OSCCONL(unsigned char value)__builtin_write_OSCCONH(unsigned char value)

minus All DSP Operations ndash 使用 DSP 的指令執行運算

__builtin_mac(hellip)

其他還有更多的 ldquobuilt-insrdquominus Nop() Sleep() amp Idle() ClrWdt()

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 60

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 61

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Attributes of Variables

attribute 這個關鍵字讓您 __attribute__ 這個關鍵字讓您可以為需要的 variables or

指定特有的屬性 包括structures 指定特有的屬性 包括 安排於特定的節區以及位址minus 安排於特定的節區以及位址

只有特殊的情況下須要如此

minus 設定記憶體安排時的邊界值及對齊

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 62

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Target Memory Attributesg y attribute ((space(area)))__ __(( p ( )))

where area isauto psv compiler managed PSVminus auto_psv - compiler managed PSV

minus psv - user managed PSVminus xmemory - data memory (X)minus ymemory - data memory (Y)y y y ( )minus eedata - EEDATA memoryminus dma - DMA memorydma DMA memory

有關 attribute 的說明請參考 MPLAB C30 Users Guide 第 2 章

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 63

C30 Users Guide 第 2 章

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Miscellaneous Attributes aligned() - start align boundary aligned() start align boundary reverse() - end align boundary near - near data (1st 8K)

minus int num attribute ((near)) int num __attribute__ ((near)) far - far data (anywhere) address() - start address persistent - uninitialized on warm persistent uninitialized on warm

reset

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 64

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Data Attributes Examplesp

將變數安排在指定位址 specific address 將變數安排在指定位址 specific addressint MyVar __attribute__((address(0x860)))int attribute ((address(0x862))) MyVar2int __attribute__((address(0x862))) MyVar2

minus Be careful about overlap and auto variables

確定變數被安排在 near memory (below y (8kB)

char attribute ((near)) MyVar3 MyVar4__ __(( )) y y

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 65

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Data Attributes Examplesp

將變數安排於特定 section 或 space 將變數安排於特定 section 或 spaceint foo __attribute__((section(ldquoThePlacerdquo)))int bar attribute ((space(psv)))int bar __attribute__((space(psv)))

minus New section will be created if neededSminus Spaces data prog psv eedata

開始或結束 multi-byte boundary y yAlignmant

char attribute ((aligned(32))) MyArray[18]__ __(( g ( ))) y y[ ]int __attribute__((reverse(64))) EndArray[25]

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 66

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Attributes of Functions 針對 Function 的 attribute 讓使用 針對 Function 的 __attribute__ 讓使用

者可以設定特定 Function 的屬性

讓 Compiler 可以進一步的對最佳化做調整並 讓 Compiler 可以進一步的對最佳化做調整並且檢驗程式碼的正確性

Examples includeminus Absolute function placement in memoryAbsolute function placement in memoryminus Specification of ISR functions

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 67

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Function Attributes Examplesp

Placement of a function at a Placement of a function at a particular addressvoid foo() __attribute__ (address(0x1000)))

D l ti f i t t i

Declaration of interrupt service routineshellipout esvoid __attribute__((interrupt)) _ADC11Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 68

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 69

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Interrupt Supportp pp

Declaring interrupts with the Declaring interrupts with the compiler has two componentsminus 使用 interrupt 屬性來將一個特定的

Function 宣告為中斷服務程式 (ISR)Function 宣告為中斷服務程式 (ISR)minus 也可以額外的宣告其他可用屬性

shadow save hellip 等

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 70

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Interrupt Supportp pp以以 ADC ADC 的中斷服務程式為例的中斷服務程式為例 可以寫成以下的型式可以寫成以下的型式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 71

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Interrupt Support ndash 幾種可用的attribute 宣告 (1)attribute 宣告 (1)

Declare an interrupt functionvoid __attribute__((interrupt))_ADC1Interrupt(void)

Make function use shadow registersvoid __attribute__((interruptshadow))_ADC1Interrupt(void)

minus Saves W0-W3 and SR bits with pushs and pops

Optionally save other variables on context savevoid__attribute__((interrupt(save(var1var2))))_ADC1Interrupt(void)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 72

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Interrupt Support ndash 幾種可用的attribute 宣告 (2)attribute 宣告 (2)

宣告 ISR 使用 compiler managed PSV (auto PSV) 宣告 ISR 使用 compiler managed PSV (auto PSV)minus 若 ISR 使用到 auto PSV所用到的 PSVPAG 將增加

Context Saving 的時間void __attribute__((interruptauto_psv)) _T1Interrupt(void)

minus 若 ISR 不會使用到 auto PSV 所用到的 PSVPAG 則可宣告如下如下

void __attribute__((interruptno_auto_psv)) _T1Interrupt(void)

MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的 MPLAB C30 的預設值是假設 ISR 會用到 auto PSV 所使用的PSVPAG

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 73

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 help 對 ISR 與 PSV 使用上的說明使用上的說明

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 74

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Interrupt Support ndash 幾種可用的 attribute 宣告 (3)的 attribute 宣告 (3)

簡易的 ISR 宣告 簡易的 ISR 宣告void _ISR _ADC1Interrupt(void)

的巨集宣告放在 檔中 _ISR 的巨集宣告放在 h 檔中minus P24FJ128GB106h for PIC24FJ128GB106minus define _ISR __attribute__((interrupt))minus define _ISRFAST __attribute__((interrupt shadow))_ __ __(( p ))

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 75

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

ISR 使用 auto_psv 宣告的中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 76

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

ISR 使用 no_auto_psv 宣告的中斷程式中斷程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 77

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

中斷服務程式(ISR) 的注意事項( )

void parameter 以及 return types C30 會自動完成 Context save 也可以使用以 Shadow Registers 來完 也可以使用以 Shadow Registers 來完成的 Fast Interrupt SupportC il 使用特殊的返回指令 使用 Compiler 使用特殊的返回指令 ndash 使用RETFIE 指令

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 78

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

中斷服務程式(ISR) 的命名原則( ) ISR 的名稱以底線開頭S 的名稱以底線開頭

_T2InterruptAltINT1Interrupt_ p

ISR 的名稱要與 gld 檔的定義相同

S MPLABreg C30 U rsquo G id See MPLABreg C30 Userrsquos Guideminus 請參考 7-4 節

如果在程式中使用的 ISR 名稱無法在 gld 檔找到時 Linker 會給 Warning找到時 Linker 會給 Warning

Linker 會自動將相關 ISR 的起始位寫入該中斷的Interrupt Vector 位址

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 79

斷的Interrupt Vector 位址

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

P24FJ128GB106gld 的 ISR 名稱定義定義

Function 名稱一定要實際存在 否則 Linker 會將中斷向量指向 _DefaultInterrupt

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 80

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Linker 將 ISR 位址寫入 Vector

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 81

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

ISR 可以在程式的任意位址MCU 會在 Vector 位址找到進入點MCU 會在 Vector 位址找到進入點

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 82

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

當沒有安排 ISR 的中斷發生 Linker 會將向量指到 _DefaultInterrupt ndash CPU 會 RESET_ p 最好改寫自己的處理程式 例如以 UART 將訊息送出

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 83

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

增加自己的 _DefaultInterrupt 處理程式 (1)處理程式 (1)

您可以在程式中加入以下片斷 您可以在程式中加入以下片斷

voidvoid DefaultInterrupt(void)DefaultInterrupt(void)o do d _ e au t te upt( o d)_ e au t te upt( o d)

Nop()Nop() printf (Help Me ) printf (Help Me )

while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 84

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

增加自己的 _DefaultInterrupt 處理程式 (2)處理程式 (2)

由 Program Memory Window 可看到預設的程式動作已不g y再是 RESET 了

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 85

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

為每一個 Trap 安排 ISR p Trap 的預設 ISR 也是 DefaultInterrupt 最好每 Trap 的預設 ISR 也是 _DefaultInterrupt 最好每

一個 Trap 都有自己的 ISR Trap 的 ISR 命名 請參考相關的 gld 檔 Trap 的 ISR 命名 請參考相關的 gld 檔 以 Oscillator Trap 為例

void void attribute ((interrupt)) OscillatorFail(void)attribute ((interrupt)) OscillatorFail(void)o do d __att bute__(( te upt)) _Osc ato a ( o d)__att bute__(( te upt)) _Osc ato a ( o d)

Nop() Nop() 自定的自定的 Trap Trap 處理程式處理程式while(1) while(1) while(1) while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 86

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

其他針對中斷的 Supportpp

Disabling Traps 以外的中斷 Disabling Traps 以外的中斷include ltp24fxxxxhgtunsigned int iplunsigned int ipl

SET AND SAVE CPU IPL(ipl 7)SET_AND_SAVE_CPU_IPL(ipl 7)

Protected code here

RESTORE_CPU_IPL(ipl)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 87

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

其他針對中斷的 Supportpp

Disabling level 7 以下的中斷 Disabling level 7 以下的中斷minus DISI 指令會暫時將 CPU 優先等級提升為

數由 的參數決定6 ( Cycle 數由 disi 的參數決定)builtin disi(16384)__builtin_disi(16384)

Protected code here__builtin_disi(0)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 88

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

ISR 使用變數時的注意事項

使用 volatile 關鍵字於共用的變數 使用 volatile 關鍵字於共用的變數

int volatile ticks = 0int volatile ticks = 0

id S 2 t t( id)void _ISR _T2Interrupt(void)ticks++IFS0bitsT2IF = 0

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 89

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 的基礎使用

Variables Variables SFR Access

在 MPLAB IDE 建立 Project 在 MPLAB IDE 建立 Project 使用 MPLAB IDE amp MPLAB SIM 來觀察程式運作

Mixing C and Assembly Built-in Functions Specifying Attributes of Variables amp Functions

Interrupt Support Interrupt Support Configuration Settings Support

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 90

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

Configuration Settingsg g Device 的 configuration bits 可以直接在 Device 的 configuration bits 可以直接在

MPLABreg IDE 的 ldquoConfiguration bitsrdquo 視窗中直接選擇及設定中直接選擇及設定

比較好的方式為在程式中使用 _CONFIGx 巨集來設定 ndash 保持完整正確的設定且好維護集來設定 ndash 保持完整正確的設定且好維護minus _CONFIG1(ltVALUE1gt amp ltVALUE2gt)

CONFIG2(ltVALUEgt)_CONFIG2(ltVALUEgt)

eg _CONFIG1(GCP_OFF amp GWRP_OFF amp FWDT_ON amp ICS_PGx2)

minus MCU 對應的 header files 包含了使用範例以及有效的參數名稱

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 91

有效的參數名稱

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

_CONFIGx 的範例 ndash 取自PIC24FJ128GB106h

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 92

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

C30 練習 1 在程式中直接指定 CPU 的在程式中直接指定 C U 的

Configuration Bits

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 93

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

MPLAB C30 練習 1

使用 CONFIGx 巨集來將 MCU 的 使用 _CONFIGx 巨集來將 MCU 的Configuration Bits 由程式內指定

要安插在程minus _CONFIG1( ) amp _CONFIG2( ) 要安插在程式的起始處

minus 適當的位置為 include hellip 敘述之後

使用在 C30A 中有兩個用 註解符號 使用在 C30Ac 中有兩個用 註解符號Disable 的敘述

minus 將註解符號除去後加上正確的參數列組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 94

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

設定 Configuration bitsg 每當重新選擇 Device 後 Configuration Words 的內容會被 g

RESET 成 0x7FFF ndash 所有 bits 都是 1

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 95

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

設定 Configuration bitsg 請修改 _CONFIG1( ) amp _CONFIG2( )的內容讓每次 build 成_ ( ) _ ( )

功後的 Configuration 都如下列的組合

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 96

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

C30 練習 2在程式中加入 Traps 的在程式中加入 aps 的

處理程式

copy 2005 Microchip Technology Incorporated All Rights Reserved Slide 97

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

在程式中加入 Traps 的處理程式

在預設的情況下 所有 Traps 的向量都指向 在預設的情況下 所有 Traps 的向量都指向_DefaultInterrupt

請為 Oscillator Fail Address Erroe amp Stacker 請為 Oscillator Fail Address Erroe amp Stacker Error 加入各別的處理程式

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 98

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

在程式中加入 Traps 的處理程式

請參考 P24FJ128GB106gld 內的定義名稱將處理 請參考 P24FJ128GB106gld 內的定義名稱將處理程式分別加入

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 99

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

在程式中加入 Traps 的處理程式

處理程式只是測試用 所以統一的內 處理程式只是測試用 所以統一的內容為 Nop()

hil (1)while(1)

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 100

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101

在程式中加入 Traps 的處理程式

如果一切正確 Project 在 build 成功後的 如果一切正確 Project 在 build 成功後的Program Memory 視窗應該改為以下的內容

copy 2008 Microchip Technology Incorporated All Rights Reserved C30 入門及應用 Slide 101