PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx...

89
單晶片軟體設計講座 單晶片軟體設計講座 單晶片軟體設計講座 單晶片軟體設計講座 PIC18F4520 PIC18F4520 基礎課程 基礎課程 架構,基本週邊及程式開發 Microchip Technology Taiwan Slide 1 102_ASP-TW

Transcript of PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx...

Page 1: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

單晶片軟體設計講座單晶片軟體設計講座單晶片軟體設計講座單晶片軟體設計講座PIC18F4520 PIC18F4520 基礎課程基礎課程

架構基本週邊及程式開發

Microchip Technology Taiwan Slide 1 102_ASP-TW

開發開發 PIC18F4520 PIC18F4520 程式相關的程式相關的軟軟硬體開發工具硬體開發工具

使用軟體工具 ( 包含於 Microchip 產品 DVD 中)使用軟體工具 ( 包含於 Microchip 產品 DVD 中)MPLAB IDE v8xx (或更新版本)

MPASM MPLINK MPLIB

使用硬體工具

MPLAB ICD2

Microchip APP001 Workshop Board (PIC18F4520 inside)

APP026-3X 泛用型實驗板 + PIC18F4520 PIM Module

參考書籍 (可以從網站上下載)參考書籍 (可以從網站上下載)MPASM Userrsquos Guide with MPLINK and MPLIB (DS33014J)

MPLAB IDE Userrsquos Guide

Microchip Technology Taiwan Slide 2 102_ASP-TW

MPLAB IDE User s Guide

PIC18F4520 Data Sheet ( DS39631A )

包含於包含於 MicrochipMicrochip 產品產品 DVD V4 0DVD V4 0包含於包含於 Microchip Microchip 產品產品 DVD V40DVD V40

Microchip Technology Taiwan Slide 3 102_ASP-TW

PIC18F4520PIC18F4520 開發講座內容開發講座內容

PIC18F系列

PIC18F4520 PIC18F4520 開發講座內容開發講座內容

PIC18F MCU 的架構

PIC18F MCU 主要周邊功能

開發工具介紹與使用

撰寫 PIC18 系列組合語言及參考資源

使用 MPLAB C18 於程式開發使用 MPLAB C18 於程式開發

Demo 1 IO IO 的操作的操作

Demo 2 使用使用 Timer1 Timer1 與與中斷中斷

Demo 3 使用使用 AAD D 轉換器轉換器

Microchip Technology Taiwan Slide 4 102_ASP-TW

新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))

PIC18F4520 是廣泛使用的 40 Pi PIC18F 系列 MCUPIC18F4520 是廣泛使用的 40-Pin PIC18F 系列 MCUExtended Instruction Sets for C18 Compiler EfficiencySupport nano-Watt Technology

內建 31KHz ~ 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low-Power Wake-Up省電模式 Idle Sleep

Microchip Technology Taiwan Slide 5 102_ASP-TW

PIC18FxxxxPIC18FxxxxEnhanced PICmicro MCU Enhanced PICmicro MCU

的架構及主要特色的架構及主要特色

Microchip Technology Taiwan Slide 6 102_ASP-TW

PIC18PIC18 系列系列 MCUMCU 的主要特色的主要特色PIC18 PIC18 系列系列 MCU MCU 的主要特色的主要特色

PIC18FxxxxPIC18Fxxxx提供較多的指令 (77個基本指令)

更大的程式與資料記憶體的定址能力

bull Program Memory Up to 2M Bytes

bull Data Memory Up to 4K

程式記憶體使用線性排列方式存取

提供 Access Bank 設計觀念

內建 PLL 4 倍頻線路 用 10Mhz 就得到 10 MIPs 的執行效

能 ( 100 ns 的指令執行時間 )

有 8 8 的硬體乘法器

Microchip Technology Taiwan Slide 7 102_ASP-TW

全系列提供 10 Bits 的 AD 轉換器 及 ICD 除錯功能

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 2: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

開發開發 PIC18F4520 PIC18F4520 程式相關的程式相關的軟軟硬體開發工具硬體開發工具

使用軟體工具 ( 包含於 Microchip 產品 DVD 中)使用軟體工具 ( 包含於 Microchip 產品 DVD 中)MPLAB IDE v8xx (或更新版本)

MPASM MPLINK MPLIB

使用硬體工具

MPLAB ICD2

Microchip APP001 Workshop Board (PIC18F4520 inside)

APP026-3X 泛用型實驗板 + PIC18F4520 PIM Module

參考書籍 (可以從網站上下載)參考書籍 (可以從網站上下載)MPASM Userrsquos Guide with MPLINK and MPLIB (DS33014J)

MPLAB IDE Userrsquos Guide

Microchip Technology Taiwan Slide 2 102_ASP-TW

MPLAB IDE User s Guide

PIC18F4520 Data Sheet ( DS39631A )

包含於包含於 MicrochipMicrochip 產品產品 DVD V4 0DVD V4 0包含於包含於 Microchip Microchip 產品產品 DVD V40DVD V40

Microchip Technology Taiwan Slide 3 102_ASP-TW

PIC18F4520PIC18F4520 開發講座內容開發講座內容

PIC18F系列

PIC18F4520 PIC18F4520 開發講座內容開發講座內容

PIC18F MCU 的架構

PIC18F MCU 主要周邊功能

開發工具介紹與使用

撰寫 PIC18 系列組合語言及參考資源

使用 MPLAB C18 於程式開發使用 MPLAB C18 於程式開發

Demo 1 IO IO 的操作的操作

Demo 2 使用使用 Timer1 Timer1 與與中斷中斷

Demo 3 使用使用 AAD D 轉換器轉換器

Microchip Technology Taiwan Slide 4 102_ASP-TW

新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))

PIC18F4520 是廣泛使用的 40 Pi PIC18F 系列 MCUPIC18F4520 是廣泛使用的 40-Pin PIC18F 系列 MCUExtended Instruction Sets for C18 Compiler EfficiencySupport nano-Watt Technology

內建 31KHz ~ 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low-Power Wake-Up省電模式 Idle Sleep

Microchip Technology Taiwan Slide 5 102_ASP-TW

PIC18FxxxxPIC18FxxxxEnhanced PICmicro MCU Enhanced PICmicro MCU

的架構及主要特色的架構及主要特色

Microchip Technology Taiwan Slide 6 102_ASP-TW

PIC18PIC18 系列系列 MCUMCU 的主要特色的主要特色PIC18 PIC18 系列系列 MCU MCU 的主要特色的主要特色

PIC18FxxxxPIC18Fxxxx提供較多的指令 (77個基本指令)

更大的程式與資料記憶體的定址能力

bull Program Memory Up to 2M Bytes

bull Data Memory Up to 4K

程式記憶體使用線性排列方式存取

提供 Access Bank 設計觀念

內建 PLL 4 倍頻線路 用 10Mhz 就得到 10 MIPs 的執行效

能 ( 100 ns 的指令執行時間 )

有 8 8 的硬體乘法器

Microchip Technology Taiwan Slide 7 102_ASP-TW

全系列提供 10 Bits 的 AD 轉換器 及 ICD 除錯功能

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 3: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

包含於包含於 MicrochipMicrochip 產品產品 DVD V4 0DVD V4 0包含於包含於 Microchip Microchip 產品產品 DVD V40DVD V40

Microchip Technology Taiwan Slide 3 102_ASP-TW

PIC18F4520PIC18F4520 開發講座內容開發講座內容

PIC18F系列

PIC18F4520 PIC18F4520 開發講座內容開發講座內容

PIC18F MCU 的架構

PIC18F MCU 主要周邊功能

開發工具介紹與使用

撰寫 PIC18 系列組合語言及參考資源

使用 MPLAB C18 於程式開發使用 MPLAB C18 於程式開發

Demo 1 IO IO 的操作的操作

Demo 2 使用使用 Timer1 Timer1 與與中斷中斷

Demo 3 使用使用 AAD D 轉換器轉換器

Microchip Technology Taiwan Slide 4 102_ASP-TW

新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))

PIC18F4520 是廣泛使用的 40 Pi PIC18F 系列 MCUPIC18F4520 是廣泛使用的 40-Pin PIC18F 系列 MCUExtended Instruction Sets for C18 Compiler EfficiencySupport nano-Watt Technology

內建 31KHz ~ 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low-Power Wake-Up省電模式 Idle Sleep

Microchip Technology Taiwan Slide 5 102_ASP-TW

PIC18FxxxxPIC18FxxxxEnhanced PICmicro MCU Enhanced PICmicro MCU

的架構及主要特色的架構及主要特色

Microchip Technology Taiwan Slide 6 102_ASP-TW

PIC18PIC18 系列系列 MCUMCU 的主要特色的主要特色PIC18 PIC18 系列系列 MCU MCU 的主要特色的主要特色

PIC18FxxxxPIC18Fxxxx提供較多的指令 (77個基本指令)

更大的程式與資料記憶體的定址能力

bull Program Memory Up to 2M Bytes

bull Data Memory Up to 4K

程式記憶體使用線性排列方式存取

提供 Access Bank 設計觀念

內建 PLL 4 倍頻線路 用 10Mhz 就得到 10 MIPs 的執行效

能 ( 100 ns 的指令執行時間 )

有 8 8 的硬體乘法器

Microchip Technology Taiwan Slide 7 102_ASP-TW

全系列提供 10 Bits 的 AD 轉換器 及 ICD 除錯功能

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 4: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18F4520PIC18F4520 開發講座內容開發講座內容

PIC18F系列

PIC18F4520 PIC18F4520 開發講座內容開發講座內容

PIC18F MCU 的架構

PIC18F MCU 主要周邊功能

開發工具介紹與使用

撰寫 PIC18 系列組合語言及參考資源

使用 MPLAB C18 於程式開發使用 MPLAB C18 於程式開發

Demo 1 IO IO 的操作的操作

Demo 2 使用使用 Timer1 Timer1 與與中斷中斷

Demo 3 使用使用 AAD D 轉換器轉換器

Microchip Technology Taiwan Slide 4 102_ASP-TW

新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))

PIC18F4520 是廣泛使用的 40 Pi PIC18F 系列 MCUPIC18F4520 是廣泛使用的 40-Pin PIC18F 系列 MCUExtended Instruction Sets for C18 Compiler EfficiencySupport nano-Watt Technology

內建 31KHz ~ 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low-Power Wake-Up省電模式 Idle Sleep

Microchip Technology Taiwan Slide 5 102_ASP-TW

PIC18FxxxxPIC18FxxxxEnhanced PICmicro MCU Enhanced PICmicro MCU

的架構及主要特色的架構及主要特色

Microchip Technology Taiwan Slide 6 102_ASP-TW

PIC18PIC18 系列系列 MCUMCU 的主要特色的主要特色PIC18 PIC18 系列系列 MCU MCU 的主要特色的主要特色

PIC18FxxxxPIC18Fxxxx提供較多的指令 (77個基本指令)

更大的程式與資料記憶體的定址能力

bull Program Memory Up to 2M Bytes

bull Data Memory Up to 4K

程式記憶體使用線性排列方式存取

提供 Access Bank 設計觀念

內建 PLL 4 倍頻線路 用 10Mhz 就得到 10 MIPs 的執行效

能 ( 100 ns 的指令執行時間 )

有 8 8 的硬體乘法器

Microchip Technology Taiwan Slide 7 102_ASP-TW

全系列提供 10 Bits 的 AD 轉換器 及 ICD 除錯功能

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 5: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))新改良新改良 PIC18Fxxxx (PIC18Fxxxx (四碼四碼))

PIC18F4520 是廣泛使用的 40 Pi PIC18F 系列 MCUPIC18F4520 是廣泛使用的 40-Pin PIC18F 系列 MCUExtended Instruction Sets for C18 Compiler EfficiencySupport nano-Watt Technology

內建 31KHz ~ 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low-Power Wake-Up省電模式 Idle Sleep

Microchip Technology Taiwan Slide 5 102_ASP-TW

PIC18FxxxxPIC18FxxxxEnhanced PICmicro MCU Enhanced PICmicro MCU

的架構及主要特色的架構及主要特色

Microchip Technology Taiwan Slide 6 102_ASP-TW

PIC18PIC18 系列系列 MCUMCU 的主要特色的主要特色PIC18 PIC18 系列系列 MCU MCU 的主要特色的主要特色

PIC18FxxxxPIC18Fxxxx提供較多的指令 (77個基本指令)

更大的程式與資料記憶體的定址能力

bull Program Memory Up to 2M Bytes

bull Data Memory Up to 4K

程式記憶體使用線性排列方式存取

提供 Access Bank 設計觀念

內建 PLL 4 倍頻線路 用 10Mhz 就得到 10 MIPs 的執行效

能 ( 100 ns 的指令執行時間 )

有 8 8 的硬體乘法器

Microchip Technology Taiwan Slide 7 102_ASP-TW

全系列提供 10 Bits 的 AD 轉換器 及 ICD 除錯功能

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 6: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18FxxxxPIC18FxxxxEnhanced PICmicro MCU Enhanced PICmicro MCU

的架構及主要特色的架構及主要特色

Microchip Technology Taiwan Slide 6 102_ASP-TW

PIC18PIC18 系列系列 MCUMCU 的主要特色的主要特色PIC18 PIC18 系列系列 MCU MCU 的主要特色的主要特色

PIC18FxxxxPIC18Fxxxx提供較多的指令 (77個基本指令)

更大的程式與資料記憶體的定址能力

bull Program Memory Up to 2M Bytes

bull Data Memory Up to 4K

程式記憶體使用線性排列方式存取

提供 Access Bank 設計觀念

內建 PLL 4 倍頻線路 用 10Mhz 就得到 10 MIPs 的執行效

能 ( 100 ns 的指令執行時間 )

有 8 8 的硬體乘法器

Microchip Technology Taiwan Slide 7 102_ASP-TW

全系列提供 10 Bits 的 AD 轉換器 及 ICD 除錯功能

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 7: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18PIC18 系列系列 MCUMCU 的主要特色的主要特色PIC18 PIC18 系列系列 MCU MCU 的主要特色的主要特色

PIC18FxxxxPIC18Fxxxx提供較多的指令 (77個基本指令)

更大的程式與資料記憶體的定址能力

bull Program Memory Up to 2M Bytes

bull Data Memory Up to 4K

程式記憶體使用線性排列方式存取

提供 Access Bank 設計觀念

內建 PLL 4 倍頻線路 用 10Mhz 就得到 10 MIPs 的執行效

能 ( 100 ns 的指令執行時間 )

有 8 8 的硬體乘法器

Microchip Technology Taiwan Slide 7 102_ASP-TW

全系列提供 10 Bits 的 AD 轉換器 及 ICD 除錯功能

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 8: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18F4520PIC18F4520 增加的功能增加的功能PIC18F4520PIC18F4520 增加的功能增加的功能

FLASH Program Memory除了程式記憶體為 FLASH 外 CPU 可自我燒錄程式

10 萬次的寫入壽命

ICD Capability支援 ICD 的介面

每個 CPU 都可視為一個 ICE Chip

高燒錄效率PIC18F4520 燒錄時間約 3 秒 ( 32KB 程式)

EEPROM PIC18F4520 都有內建 256 Bytes 的 EEPROM

100 萬次的寫入壽命

Microchip Technology Taiwan Slide 8 102_ASP-TW

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 9: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18F4520 nanoWattPIC18F4520 nanoWatt 技術技術PIC18F4520 nanoWatt PIC18F4520 nanoWatt 技術技術

什麼是 nano-Watt 技術什麼是 nano-Watt 技術Sleep Mode 下消耗功率低於 1uW 以下

PIC18F4520 Sleep Mode 下低於 01uA p

內建的電源管理內建 31KHz ~ 8MHz 自由切換的 RC 震盪器內建 31KHz 8MHz 自由切換的 RC 震盪器

快速 RC 啟動模式

雙速啟動模式

系統時脈監視 故障切換功能

Low-Power Watch-Dog Timer Ultra Low Power Timer1Ultra Low-Power Timer1硬體設定或軟體控制的 Watch-Dog Timer省電模式 Idle (58uA) Sleep (01uA)

Microchip Technology Taiwan Slide 9 102_ASP-TW

省電模式 ( ) p ( )

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 10: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18F4520PIC18F4520 主要的內建周邊主要的內建周邊PIC18F4520 PIC18F4520 主要的內建周邊主要的內建周邊

最多可提供 36 個 IO 腳位最多可提供 36 個 IO 腳位

3 個可程式規劃的外部中斷

4 個可偵測 Pin Change 的腳位4 個可偵測 Pin-Change 的腳位

增強型 Input CaptureOutput ComparePWM 模組

MSSP module (Master Synchronous Serial Port)MSSP module (Master Synchronous Serial Port)支援硬體 I2C SPI

增強型 Addressable USART module (EUSART)增強型 Addressable USART module (EUSART)支援高達 13 個 Channel 的 AD Converter module兩個 Channel 的類比比較器兩個 Channel 的類比比較器

1 個 8-bit timer 3 個 16-bit Timer

Microchip Technology Taiwan Slide 10 102_ASP-TW

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 11: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction PipeliningInstruction PipeliningInstruction Pipelining

l 0 05預提取指令 執行指令

movlw 0x05 -

指令週期

l 0 05MAIN1

T0程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 11 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 12: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction Pipelining

f REG1 l 0 05

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movwf REG1 movlw 0x05

指令週期

l 0 05MAIN1

T0 T1程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 12 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 13: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction Pipelining

ll SUB1 f REG1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

rcall SUB1 movwf REG1

指令週期

l 0 05MAIN1

T0 T1 T2程式範例此階段執行一般指令

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch

此階段執行一般指令

addwf REG24

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 13 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 14: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction Pipelining

dd f REG2 ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Microchip Technology Taiwan Slide 14 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 15: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction Pipelining

f PORTB ll SUB1

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTBw rcall SUB1

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch 放棄 rcall 指令重新提取下一個指令

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch

Microchip Technology Taiwan Slide 15 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 16: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction Pipelining

t f PORTB

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

return movf PORTBw

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch

Microchip Technology Taiwan Slide 16 102_ASP-TW

movf PORTCwreturn

SUB25354

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 17: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction Pipelining

f PORTC t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

movf PORTCw return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h

Microchip Technology Taiwan Slide 17 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 18: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Instruction PipeliningInstruction Pipelining

dd f REG2 t

Instruction PipeliningInstruction Pipelining預提取指令 執行指令

addwf REG2 return

指令週期

l 0 05MAIN1

T0 T1 T2 T3 T4 T5 T6 T7程式範例

rcall SUB1

movlw 0x05MAINmovwf REG1

123

Fetch Execute

Fetch Execute

Fetch Execute

addwf REG24 FlushFetch Fetch

movf PORTBwreturn

f PORTC

SUB1

SUB2

515253

Fetch Execute

Fetch Execute

F t h Fl h

Microchip Technology Taiwan Slide 18 102_ASP-TW

movf PORTCwreturn

SUB25354

Fetch Flush

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 19: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18F452 PIC18F452 基本架構圖基本架構圖 PORTA

PRODH PRODL

Table Pointer lt2gt

Incdec logic2121

5 8 8Data RAM

PORTBData Bus

8 x 8 Multiply

8

8WREG

Program Memory

g

PCLATU PCLATH

PCU PCH PCL

21

Addresslt12gt

12 (2)

124Addresslt12gt

BIT OP

PORTC

ALUlt8gt

PCU PCH PCLFSR0

FSR1

FSR2

BSR 8

831 Level Stack

Program Counter

TABLELATCH 12

8PORTD

16ALUlt8gt

8Instruction Register

TABLELATCH

812

InstructionPower-upOSC2CLK0

PORTE

MSSP

Program Bus

InstructionDecode

andControl

TimerOscillatorStart-up Timer

Power-onReset

Timing Generation

4X PLL

OSC2CLK0OSC1CLK1

T1OS1T1OSO TIMERUSART

ResetWatchdog

TimerBrown-out

Reset

4X PLL

Precision Voltage

Reference

ADCCP

I i it

Microchip Technology Taiwan Slide 19 102_ASP-TW

MCLRVDDVSS

In-circuitDebugger

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 20: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

引用引用 Register File Register File 的觀念的觀念ggSFRrsquos SFRrsquos 位於位於 Bank 15 Bank 15 所所有指令皆可對任一暫存有指令皆可對任一暫存

Data Memory(Register File)

有指令皆可對任一暫存有指令皆可對任一暫存

器操作無須切換器操作無須切換

BankBank

w fALU

07h

08h

09h

Long word Long word 的指令讓各的指令讓各

暫存器可被直接定址暫存器可被直接定址Dat

a B

us

dw f

0Ah

0Bh

0Ch

暫存器可被直接定址暫存器可被直接定址

MOVFF src destMOVFF src dest

周邊暫存器及狀態暫存周邊暫存器及狀態暫存

D 0Dh

0Eh

0Fh

10h周邊暫存器及狀態暫存周邊暫存器及狀態暫存

器皆為器皆為 Register files Register files 的一部份的一部份

WREG10h

Opcode d a Address

Microchip Technology Taiwan Slide 20 102_ASP-TW

指令解碼與執行

結果儲存控制 資料來源的位址

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 21: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Data Memory Data Memory 架構架構Data Memory Data Memory 架構架構

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

有16 banks 的 Data Memory

每個 Bank 由 256 Bytes 組成

Access RAM PIC16F8F25204520暫存器的分布

000h07Fh

Bank 0 GPR080h0FFh每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

每個 Bank 由 256 Bytes 組成

( Max 4K Bytes )

Special Function Registers

Bank 1GPR

0FFh100h

1FFhSpecial Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Special Function Registers (SFRs) 被安排在 Bank 15

使用 BSRlt30gt 來選擇適當的

Bank 2GPR

200h

2FFh00h

Access Bank

BANK 或使用 BANKSEL 來做

切換 BANK 的工作

BANK 或使用 BANKSEL 來做

切換 BANK 的工作Bank 13

Access RAM

Access SFRD00h

00h7Fh80hFFh

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

MOVFF 指令可免去切換 BANK 的工作 但需 2 words 的指令空

間來完成

256 BytesBank 13GPR

Bank 14

DFFh

E00h

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

間來完成

使用Access Bank 的暫存器無

須做 Bank 的切換

Bank 14GPR

Bank 15 GPR

EFFh

F00hF7Fh

Microchip Technology Taiwan Slide 21 102_ASP-TW

須做 Bank 的切換須做 Bank 的切換Access SFRFFFh

F7FhF80h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 22: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

PIC18 PIC18 系列的架構系列的架構Program MemoryProgram Memory

最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M B ( 21 bi )M B ( 21 bi )最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

最大可達最大可達 22M Bytes ( 21 bits) M Bytes ( 21 bits)

的程式空間的程式空間

PClt200gt

Stack Level 1

CALL RCALL RETURNCALL RCALL RETURNRETFIE RETLWRETFIE RETLW

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Reset Vector Reset Vector 位於程式位址位於程式位址

00x0000x0000

Stack Level 1hellipStack Level 31

R t V t000h

y Sp

ace

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

高優先權高優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

Reset Vector

High Priority Interrupt Vector 008h

Use

r Mem

ory

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

於於 00080008hh

低優先權低優先權中斷向量中斷向量 的位址位的位址位

Low Priority Interrupt Vector

On-ChipProgramming

018h

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

於於 00001188hh

3131 層獨立的位址堆疊區層獨立的位址堆疊區

Programming

Read lsquo0rsquo

Microchip Technology Taiwan Slide 22 102_ASP-TW

31 31 層獨立的位址堆疊區層獨立的位址堆疊區31 31 層獨立的位址堆疊區層獨立的位址堆疊區1FFFFFh200000h

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 23: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

PIC18 PIC18 系列的架構系列的架構Interrupt OverviewInterrupt Overview

豐富的內部與外部中斷來源

幾乎所有周邊皆有中斷幾乎所有周邊皆有中斷 CPU CPU 的能力的能力

有些有些 IOIO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)有些有些 IO IO 的變化也可構成中斷條件的變化也可構成中斷條件 ((PORTB)PORTB)

每個周邊中斷可以被設定成具有每個周邊中斷可以被設定成具有 高高低優先權低優先權 ( ( HiLow Priority)HiLow Priority)INT0INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷INT0 INT0 永遠為高優先權外部中斷永遠為高優先權外部中斷

具相同優先權設定的中斷則可由軟體來決定其優先順序具相同優先權設定的中斷則可由軟體來決定其優先順序

有整體及各別的中斷致能控制位元有整體及各別的中斷致能控制位元

每個中斷都有其獨立的中斷旗標可用軟體詢問的方式每個中斷都有其獨立的中斷旗標可用軟體詢問的方式

大部份的中斷可以用來喚醒處於大部份的中斷可以用來喚醒處於 SLEEP SLEEP 狀態的狀態的 PICmicroPICmicro

中斷延遲固定為中斷延遲固定為 3 3 個個 cyclecycle

Microchip Technology Taiwan Slide 23 102_ASP-TW

容易以軟體來判斷中斷的經過時間容易以軟體來判斷中斷的經過時間

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 24: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

睡眠模式睡眠模式 (Sleep Mode)(Sleep Mode)睡眠模式睡眠模式 ( p )( p )指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率

系統主振盪時脈停止運作系統主振盪時脈停止運作

處理器進入靜態的狀態 ndash 資料保持不變

Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響

處理器進入最低耗電模式 ndash 通常是漏電流 (01 - 20μA typical)

一些可以喚醒睡眠模式的外部觸發事件一些可以喚醒睡眠模式的外部觸發事件

MCLRWDT

Master Clear Pin Asserted (pulled low)Watchdog Timer Timeout

INTTMR1ADC

gINT Pin InterruptTimer 1 Interrupt (or also TMR3 on PIC18)AD Conversion Complete Interrupt

CMPCCP

PORTB

Comparator Output Change InterruptInput Capture EventPORTB Interrupt on Change

Microchip Technology Taiwan Slide 24 102_ASP-TW

SSPPSP

Synchronous Serial Port (I2C Mode) Start Stop Bit Detect InterruptParallel Slave Port Read or Write

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 25: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18 PIC18 系列的架構系列的架構高優先全中斷邏輯架構高優先全中斷邏輯架構

TMR0IFTMR0IFTMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

RBIFRBIFRBIFRBIFRBIFRBIF

Interrupt to CPUInterrupt to CPUVector to locationVector to location0008h0008h(High Priority (High Priority InterruptInterruptRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

INT1IFINT1IFINT1IEINT1IE

InterruptInterruptVector Address)Vector Address)

High Priority Interrupt GenerationHigh Priority Interrupt GenerationINT1IEINT1IEINT1IPINT1IPINT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bit GIEHGIEGIEHGIEPeripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

Peripheral Interrupt Priority bitPeripheral Interrupt Priority bit

IPENIPEN

GIEHGIEGIEHGIE

High Priority Interrupt initializedHigh Priority Interrupt initialized(Disable low priority interrupts)(Disable low priority interrupts)

Additional Peripheral InterruptsAdditional Peripheral InterruptsIPENIPEN

GIELPEIEGIELPEIE

From (a)From (a)IPENIPEN

To (c)To (c)

Microchip Technology Taiwan Slide 25 102_ASP-TW

From (a)From (a)

From (b)From (b)

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 26: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18 PIC18 系列的架構系列的架構低優先全中斷邏輯架構低優先全中斷邏輯架構

Peripheral Interrupt Enabled bitPeripheral Interrupt Enabled bitPeripheral Interrupt Flag bitPeripheral Interrupt Flag bit

To (a)To (a) Wake-up(if in SLEEP mode)

p p gp p gPeripheral Interrupt Priority bitPeripheral Interrupt Priority bit

Additional Peripheral InterruptsAdditional Peripheral Interrupts From (c)From (c)

Interrupt to CPUVector to Location 0018h (Low Priority Interrupt

TMR0IFTMR0IFTMR0IETMR0IETMR0IPTMR0IP

To (b)To (b) Vector Address)

RBIFRBIFRBIFRBIFRBIFRBIF

INT0IFINT0IFINT0IEINT0IE

GIEHGIEGIEHGIE

INT1IFINT1IFINT1IEINT1IEINT1IPINT1IPINT2IFINT2IF

Microchip Technology Taiwan Slide 26 102_ASP-TW

INT2IFINT2IFINT2IEINT2IEINT2IPINT2IP

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 27: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

1818F4520F4520 中斷處理中斷處理1818F4520 F4520 中斷處理中斷處理

18F4520有兩個中斷向量點

高優先權 ==gt 中斷向量位址 0x0008

低優先權 ==gt 中斷向量位址 0x0018

每個中斷源均可選擇其中斷優先權(二選一)

每個中斷源均有獨立的中斷旗標(Flag)

中斷旗標的清除 ==gt 自行用軟體清除清 清

每個中斷源均可 Enable 或 Disable

當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容當然PIC18系列也可設定與PIC16Fxxx系列的中斷相容

(關掉優先權的設定)

Microchip Technology Taiwan Slide 27 102_ASP-TW

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 28: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

以以Timer1Timer1 的中斷設定為範例的中斷設定為範例以以Timer1 Timer1 的中斷設定為範例的中斷設定為範例

有關 T1CON 的控制位元請參考 PIC18F4520 Data SheetRCON 的 ltIPENgt 位元 啟動高低權位的中斷功能

GIEH 允許高優先權中斷

GIEL 允許低優先權中斷

TMR1IE 打開 Timer1 的中斷設定

TMR1IP = 1設定 Timer1 為高優先權中斷

Microchip Technology Taiwan Slide 28 102_ASP-TW

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 29: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18PIC18 系列的架構系列的架構PIC18PIC18 系列的架構系列的架構PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

PIC18 PIC18 系列的架構系列的架構Clocking SchemeClocking Scheme

一個指令周期等於輸入 頻率的一個指令周期等於輸入 頻率的一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期

一個指令周期等於輸入 CPU 頻率的 14 若 CPU 操作於 40 Mhz 時其指令周期為 100 nsFosc 振盪頻率 4 = Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期Fosc 振盪頻率 4 Tosc 指令週期

Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4

OSC1

Q1

Q2Q2

Q4

Q3

指令週期

OSC2

Microchip Technology Taiwan Slide 29 102_ASP-TW

一般性的動作 Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 30: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18FxxxxPIC18Fxxxx

定址模式定址模式定址模式定址模式

Microchip Technology Taiwan Slide 30 102_ASP-TW

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 31: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

18Fxxxx18Fxxxx 定址模式定址模式18Fxxxx 18Fxxxx 定址模式定址模式

四種基本的定址模式四種基本的定址模式

立即定址模式

直接定址模式

間接定址模式間接定址模式

相對定址模式

Microchip Technology Taiwan Slide 31 102_ASP-TW

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 32: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18 PIC18 定址模式定址模式立即數的操作

PIC18 PIC18 定址模式定址模式立即數的操作立即數的操作立即數的操作

8-bit 的常數值 (literal value) 被包含於程式碼之中8-bit 的常數值 (literal value) 被包含於程式碼之中

使用於 literal instructions 例如

movlw addlw retlw etc使用於 literal instructions 例如

movlw addlw retlw etcmovlw addlw retlw etcmovlw addlw retlw etc

1616--bit Instruction for Literal Instructionsbit Instruction for Literal Instructions

OP CODEOP CODE kk kk kk kk kk kk kk kk

Microchip Technology Taiwan Slide 32 102_ASP-TW

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 33: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ((Banked)Banked)

Data Memory 總共有效位址為 12 Bits ( 4K Bytes )Data Memory 總共有效位址為 12 Bits ( 4K Bytes )8-bit 的直接位址由指令中得到

其他高位的 4 i 由 S 0 3 獲得其他高位的 4-bits 由 BSRlt03gt 獲得

BSR RegisterBSR Register 1616--bit Instructionbit Instruction

BSRlt74gtBSRlt74gt OP CODEOP CODE

gg

ff ff ff ff ff ff ff ffBSRlt30gtBSRlt30gt

88--bits from Instruction Wordbits from Instruction Word

44--bits frombits fromBSR BSR RegisterRegister

Effective 12Effective 12--bit Register Addressbit Register Address

BSRlt30gtBSRlt30gt ff ff ff ff ff ff ff ff

Microchip Technology Taiwan Slide 33 102_ASP-TW

Effective 12Effective 12 bit Register Addressbit Register Address

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 34: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 直接定址直接定址 ( ( Non Banked)Non Banked)

對 的資料搬移MOVFF src dest ( RAM 對 RAM 的資料搬移)2 Word 的指令直接指定兩個 4K 的位址

1 1 0 01 1 0 0 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(S )

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

來源位址(Source)

1 1 1 11 1 1 1 nn nn nn nn nn nn nn nn nn nn nn nn

目的位址(Destination)

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體

Microchip Technology Taiwan Slide 34 102_ASP-TW

可增加執行效能及佔用較少的程式記憶體可增加執行效能及佔用較少的程式記憶體

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 35: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18PIC18 定址模式定址模式PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect addressing)Indirect addressing)

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

三個 12-bit 寬的間接定址暫存器

FSR0 FSR1 FSR2

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容

FSRn (File Select Register) 的內容可做以下調整Pre-Incremented (PREINCx) 先被加一再存取其內容Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1

Pre Incremented (PREINCx) 先被加一再存取其內容

Post-Incremented (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1

Post-Decremented (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

( )

Offset by WREG value (signed)(PLUAWx) FSRn 不變 以 W 為偏移值

相加後以產生新的索引指標

保持不變 (INDFx)保持不變 (INDFx)

12-bit FSRn Register

Microchip Technology Taiwan Slide 35 102_ASP-TW

ff ff ff ff ff ff ff ffff ff ff ff

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 36: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18 PIC18 定址模式定址模式Data Memory Data Memory 間接定址間接定址 ((Indirect Addressing)Indirect Addressing)

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中

範例 清除由位址 0x000 to 0x5FF 的資料內容

間接位址被寫入間接位址被寫入 FSRn FSRn 中中每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全

每當每當 INDFn INDFn 被當成運算元被當成運算元((operand)operand) 實際上被操作的位址乃是由實際上被操作的位址乃是由

FSRn FSRn 內容所指的位址內容所指的位址

類似類似 80518051 的的 R0R0 R1R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全類似類似 8051 8051 的的 R0R0R1 R1 定址法定址法 但功能更齊全但功能更齊全

movlwmovlw 0x050x05 Value to compare Value to comparemovlwmovlw 0x050x05 Value to compare Value to comparelfsr lfsr 10x00010x000 Clear address from 0 Clear address from 0

_Loop_Loop clrfclrf POSTINC1POSTINC1 Clear increment FSR1 Clear increment FSR1cpfsgtcpfsgt FSR1HFSR1H FSR1H gt 5 ( 0x600 ) FSR1H gt 5 ( 0x600 )gotogoto _Loop_Loop NO loop NO loop YES next instruction YES next instruction

Microchip Technology Taiwan Slide 36 102_ASP-TW

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 37: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PCPC 相對定址相對定址 計算計算 GOTOGOTO 位址位址PC PC 相對定址相對定址 計算計算 GOTO GOTO 位址位址

計算 GOTO 位址使用索引 查WREGUSER INPUT

org 0x002180 PCLATU PCLATH

表方式建立跳躍表格

08WREGUSER_INPUT

02

JTS movlw UPPER JUMP_TABLEmovwf PCLATUmovlw HIGH JUMP TABLE

g

00 21PCLATU PCLATH

movlw HIGH JUMP_TABLEmovwf PCLATHmovlw 0x04mulwf USER INPUT

FF FF FFPCU PCH PCL

mulwf USER_INPUTmovf PRODL Waddwf PCL f

計算跳躍的偏移量位址

0x002190

JUMP_TABLE goto DoUserInput0goto DoUserInput1goto DoUserInput2

每一個 goto 指令佔 4 bytes 所以要乘四後才可以得到正確

的偏移位址

Microchip Technology Taiwan Slide 37 102_ASP-TW

goto DoUserInput3goto DoUserInput4

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 38: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18FXXXPIC18FXXX

指令集指令集指令集指令集

Microchip Technology Taiwan Slide 38 102_ASP-TW

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 39: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCUMCU 指令集指令集PIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set OverviewPIC18PIC18系列系列 MCU MCU 指令集指令集Instruction Set OverviewInstruction Set Overview

77 個指令

73 個是 Single Word 指令g4 個 two-word 指令 MOVFF LFSR CALL GOTO

55 的指令只需 single cycle 即可完

4 個 two word 的指令須耗費 2 周期

10 個 branches calls 會依情況耗費一或兩個周期

8 個對 Table 操做的指令為 2 周期

3 個 return 的指令需耗費 2 周期

10 個條件式的 SKIP 指令依狀況需耗費 1 or 2 周期

Microchip Technology Taiwan Slide 39 102_ASP-TW

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 40: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18系列 MCU 指令集PIC18系列 MCU 指令集PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

B tB t O i t d O tiO i t d O ti B tB t O i t d O tiO i t d O ti

ADDWFADDWF f d af d a Add WREG and fAdd WREG and fADDWFCADDWFC f d af d a Add WREG and fAdd WREG and fANDWFANDWF f d af d a AND WREG and fAND WREG and f

MULWFMULWF f af a Multiple WREG with fMultiple WREG with fNEGFNEGF f af a Negate f Negate f RLCFRLCF f d af d a Rotate left f through carryRotate left f through carry

ByteByte--Oriented OperationsOriented Operations Byte Byte --Oriented OperationsOriented Operations

ANDWFANDWF f d af d a AND WREG and fAND WREG and fCLRFCLRF f af a Clear fClear fCOMFCOMF f d af d a Complement f Complement f CPFSEQCPFSEQ f af a Compare f with WREG skip =Compare f with WREG skip =CPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

g yg yRLNCFRLNCF f d af d a Rotate left f no carryRotate left f no carryRRCFRRCF f d af d a Rotate right f through carryRotate right f through carryRRNCFRRNCF f d af d a Rotate right f no carryRotate right f no carrySETFSETF f af a Set fSet fCPFSGTCPFSGT f af a Compare f with WREG skip gtCompare f with WREG skip gt

CPFSLTCPFSLT f af a Compare f with WREG skip ltCompare f with WREG skip ltDECFDECF f d af d a Decrement fDecrement fDECFSZDECFSZ f d af d a Decrement f skip if zeroDecrement f skip if zeroDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

SUBFWBSUBFWB f d af d a Subtract f from WREGSubtract f from WREG

with borrowwith borrowSUBWFSUBWF f d af d a Subtract WREG from fSubtract WREG from fSUBWFBSUBWFB f d af d a Subtract WREG from fSubtract WREG from fDCFSNZDCFSNZ f d af d a Decrement f skip if not zeroDecrement f skip if not zero

INCFINCF f d af d a Increment fIncrement fINCFSZINCFSZ f d af d a Increment f skip if zeroIncrement f skip if zeroINFSNZINFSNZ f d af d a Increment f skip if not zeroIncrement f skip if not zeroIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

with borrowwith borrowSWAPFSWAPF f d af d a Swap nibbles of fSwap nibbles of fTSTFSZTSTFSZ f af a Test f skip if zero Test f skip if zero XORWFXORWF f d af d a Exclusive OR WREG and fExclusive OR WREG and fIORWFIORWF f d af d a Inclusive OR WREG and fInclusive OR WREG and f

MOVFMOVF f d af d a Move fMove fMOVFFMOVFF fs fd fs fd Move fs (source) to fd Move fs (source) to fd

(destination)(destination)MOVWFMOVWF f af a Move WREG to fMove WREG to f

Microchip Technology Taiwan Slide 40 102_ASP-TW

MOVWFMOVWF f af a Move WREG to fMove WREG to f

f = File Register d = destination (1=f 0=WREG) a = access bank (0=access bank 1=BSR)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 41: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

BitBit Oriented OperationsOriented Operations Literal OperationsLiteral Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

BCFBCF f b af b a Bit clear fBit clear fBSFBSF f b af b a Bit set fBit set fBTGBTG f b af b a Bit toggle fBit toggle f

ADDLWADDLW kk Add literal with WREGAdd literal with WREGANDLWANDLW kk AND literal with WREGAND literal with WREGIORLWIORLW kk Inclusive OR literal withInclusive OR literal with

Bit Bit --Oriented OperationsOriented Operations Literal OperationsLiteral Operations

BTGBTG f b af b a Bit toggle fBit toggle fBTFSCBTFSC f b af b a Bit test f skip if clearBit test f skip if clearBTFSSBTFSS f b af b a Bit test f skip if setBit test f skip if set

IORLWIORLW kk Inclusive OR literal with Inclusive OR literal with WREGWREG

LFSRLFSR f kf k Move literal to FSRnMove literal to FSRnMOVLBMOVLB kk Move literal to BSRMove literal to BSRMOVLWMOVLW kk Move literal to WREGMove literal to WREGMOVLWMOVLW kk Move literal to WREGMove literal to WREGMULLWMULLW kk Multiply literal to WREGMultiply literal to WREGRETLWRETLW kk Return place literal in WREGReturn place literal in WREGSUBLWSUBLW kk Subtract WREG from literalSubtract WREG from literalXORLWXORLW kk Exclusive OR literal withExclusive OR literal with

Data Memory ltData Memory lt----gt Program Memory gt Program Memory OperationsOperations

TBLRDTBLRD -- Table ReadTable ReadXORLWXORLW kk Exclusive OR literal with Exclusive OR literal with

WREGWREGTBLRD+TBLRD+ -- Table Read with postTable Read with post--incincTBLRDTBLRD-- -- Table Read with postTable Read with post--decdecTBLRD+TBLRD+ -- Table Read with preTable Read with pre--incincTBLWTTBLWT -- Table WriteTable Write

TBLWT+TBLWT+ -- Table Write with postTable Write with post--incincTBLWTTBLWT-- -- Table Write with postTable Write with post--decdecTBLWT+TBLWT+ -- Table Write with preTable Write with pre--incinc

Microchip Technology Taiwan Slide 41 102_ASP-TW

f = File Register k = literal value (8-bit) b = bit address (0 - 7)

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 42: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18系列 MCU 指令集PIC18系列 MCU 指令集

Control OperationsControl Operations Control OperationsControl Operations

PIC18系列 MCU 指令集Summary (16-bit core)

PIC18系列 MCU 指令集Summary (16-bit core)

DAWDAW -- Decimal Adjust WREGDecimal Adjust WREGGOTOGOTO nn Go to address (n is 20Go to address (n is 20--bit)bit)NOPNOP -- No OperationNo Operation

Control OperationsControl OperationsBCBC nn Branch if Carry (n is 8Branch if Carry (n is 8--bit)bit)BNBN nn Branch if Negative (n is 8Branch if Negative (n is 8--bit)bit)BNCBNC nn Branch if No Carry (n is 8Branch if No Carry (n is 8--bit)bit)

pp

POPPOP -- Pop top of return stack (TOS)Pop top of return stack (TOS)PUSHPUSH -- Push top of return stack (TOS)Push top of return stack (TOS)RCALLRCALL nn Relative call (n is 11Relative call (n is 11--bit) bit) RESETRESET -- Software device resetSoftware device reset

BNNBNN nn Branch if Not Negative Branch if Not Negative (n is 8(n is 8--bit)bit)

BNOVBNOV nn Branch if No Overflow Branch if No Overflow (n is 8(n is 8--bit)bit)

RETFIERETFIE ss Return from interrupt enableReturn from interrupt enableRETLWRETLW kk Return place literal in WREGReturn place literal in WREGRETURNRETURN ss Return from subroutineReturn from subroutineSLEEPSLEEP -- Go into standby modeGo into standby mode

BNZBNZ nn Branch if Not Zero (n is 8Branch if Not Zero (n is 8--bit)bit)BOVBOV nn Branch if Overflow (n is 8Branch if Overflow (n is 8--bit)bit)BRABRA nn Branch (n is 11Branch (n is 11--bit)bit)BZBZ nn Branch if Zero (n is 8Branch if Zero (n is 8--bit)bit)CC C ( 20C ( 20 ))CALLCALL n sn s Call subroutine (n is 20Call subroutine (n is 20--bit)bit)CLRWDTCLRWDT -- Clear Watchdog TimerClear Watchdog Timer

Microchip Technology Taiwan Slide 42 102_ASP-TW

n = absolute address or address offset k = literal value (8-bit) s = fast restore

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 43: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

ByteByte--OrientedOriented16-bit Instruction for Byte Oriented Operations

OP CODEOP CODE ddADDWFADDWF f d af d aADDWFCADDWFC f d af d aANDWFANDWF f df d

yyOperationsOperations

aa ff ff ff ff ff ff ff ffOP CODEOP CODE dd

d = Destination Bit

ANDWFANDWF f d af d aCLRFCLRF f af aCOMFCOMF f d af d aCPFSEQCPFSEQ f a f a

aa ff ff ff ff ff ff ff ff

d = 0 運算結果置於 Wd = 1 運算結果置於 F

(Default d=1)

QQ CPFSGTCPFSGT f af aCPFSLTCPFSLT f af aDECFDECF f d af d aDECFSZDECFSZ f df d

f = 8f = 8--bit bit 暫存器位址暫存器位址

DECFSZDECFSZ f d af d aDCFSNZDCFSNZ f d af d aINCFINCF f d af d aINCFSZINCFSZ f d af d a

a = Access Bita = 0 指定置於 Access

ExampleADDWF 0 0

INFSNZINFSNZ f d af d aIORWFIORWF f d af d aMOVFMOVF f d af d aMOVFFMOVFF fs fdfs fd

Bank 的暫存器(Default a=0或不寫)

a = 1 實際的位址由 BSR 與 的暫存器位址組成

Microchip Technology Taiwan Slide 43 102_ASP-TW

ADDWF 0 0ADDWF f d a

MOVFFMOVFF fs fdfs fdMOVWFMOVWF f af a

與 8-bit 的暫存器位址組成

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 44: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18系列 MCU 指令集範例PIC18系列 MCU 指令集範例系列 指令集範例以位元組為操作的對象

系列 指令集範例以位元組為操作的對象

1616--bit bit Instruction for Bit Oriented Operationsfor Bit Oriented Operations

OP CODEOP CODE bb bb bb

BitBit--OrientedOrientedOperationsOperations

aa ff ff ff ff ff ff ff ff

b = 3b = 3--Bit Bit 用來指定用來指定欲操作位元的位址欲操作位元的位址

BCFBCF f b af b aBSFBSF f b af b aBTGBTG f b af b a

(Bit Number)(Bit Number)

BTFSCBTFSC f b af b aBTFSSBTFSS f b af b a a = Access Bita = Access Bit

00 指定置於指定置於 AA

f = 8f = 8--bit bit 的暫存器位址的暫存器位址

a = 0 a = 0 指定置於指定置於 Access Access Bank Bank 的暫存器的暫存器a = 1 a = 1 實際的位址由實際的位址由 BSR BSR 與與 88 bitbit 的暫存器位址組成的暫存器位址組成

EExampleampleBTFSCBTFSC STATUS C 0STATUS C 0

與與 88--bit bit 的暫存器位址組成的暫存器位址組成

Microchip Technology Taiwan Slide 44 102_ASP-TW

BTFSCBTFSC FLAGS BIT_0 aFLAGS BIT_0 a

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 45: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18系列 MCU 指令集PIC18系列 MCU 指令集Data Memory ltgt Program MemoryData Memory ltgt Program Memory

Table PointerTable PointerOperationsOperations

16-bit Instruction for TBLRD or TBLWTTBLRDTBLRD --TBLRD+ TBLRD+ --TBLRDTBLRD

pp

OP CODEOP CODETBLRDTBLRD-- --TBLRD+TBLRD+ --TBLWT TBLWT --TBLWTTBLWT

nn nn

nn = 00 ( 指標不變)TBLWT+TBLWT+ --TBLWTTBLWT-- --TBLWT+TBLWT+ --

( )

nn = 01 + ( 查表後指標加一 )

10 ( 查表後指標減一 )nn = 10 - ( 查表後指標減一 )

nn = 11 + ( 先將指標加一再查表 )

Microchip Technology Taiwan Slide 45 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 46: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份

相當有彈性的資料指標

3 個資料指標 各有五種操作模式

更方便且有效率的 Table ReadWrite 功能更方便且有效率的 Table ReadWrite 功能

以 Byte 為操作單位

多種的指標更新選項

用簡單的指令集來支援複雜的架構用簡單的指令集來支援複雜的架構

新的 MOVFFBRA 以及條件式分支指令

Microchip Technology Taiwan Slide 46 102_ASP-TW

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 47: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18FXXXPIC18FXXX 加強的部份加強的部份PIC18FXXX PIC18FXXX 加強的部份加強的部份資料指標資料指標( ( Data Pointer )Data Pointer )

總共有三個獨立的指標FSR0 FSR1 FSR2

指標為 12 Bits 可定址 4K 的資料空間

指標有兩個暫存器組成 (例 FSR0L FSR0H)

五種指標的操作方式 (使用不同的虛擬暫存器)INDFn 直接操作指標所指的內容

POSTINCn 操作指標所指內容後將指標加一

POSTDECn 操作指標所指內容後將指標減一

PREINCn 將指標先加一後再對指標所指位址操作

PLUSWn 將 WREG 的內容作為 Offset 值與 FSRn 相加後

Microchip Technology Taiwan Slide 47 102_ASP-TW

以其結果為指標來存取記憶體

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 48: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18FxxxxPIC18Fxxxx

IOIO 介紹與運用介紹與運用IO IO 介紹與運用介紹與運用

Microchip Technology Taiwan Slide 48 102_ASP-TW

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 49: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO PortsPIC18 PIC18 系列的周邊系列的周邊 IO PortsIO Ports

輸入輸出腳為一般 MCU 最輸入輸出腳 為一般 MCU 最

基本的工作

PIC18 系列的 IO 多了一個PIC18 系列的 IO 多了一個

LATx 可有效解決所謂 Read-

M dif W it 指令動作帶來Modify-Write 指令動作帶來

的困擾

每個 皆有三個獨立每個 IO Port 皆有三個獨立

的控制暫存器

PORTx

LATx

TRIS

Microchip Technology Taiwan Slide 49 102_ASP-TW

TRISx

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 50: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

IO IO 的輸出入暫存器的輸出入暫存器的輸出入暫存器的輸出入暫存器

Internal Data Bus

Write PORTxWrite LATx Read LATx Read PORTx

LATx 拴鎖暫存器或

PORTx 輸出暫存器

PORTx IO Pins

Microchip Technology Taiwan Slide 50 102_ASP-TW

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 51: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

為什麼加入為什麼加入 LATx LATx 拴鎖暫存器拴鎖暫存器1 如果工作在較低頻率 或 較小的負載電容環境

BSF PORTxPINy BSF PORTxPINz

電壓的呈現在

Q1 Q2 Q3Q4BSF PORTxPINy BSF PORTxPINz

VILPORTxPINy

t

2 如果工作在較高頻率 或 較大的負載電容環境

Read-Modify-Write 操作時的取樣點

Q1 Q2 Q3Q4 Q4BSF PORTxPINy BSF PORTxPINz

如果工作在較高頻率 或 較大的負載電容環境

VIL

t

電壓的呈現在PORTxPINy

此時電壓還沒升到VIL轉態點

Microchip Technology Taiwan Slide 51 102_ASP-TW

Read-Modify-Write 操作時的取樣點

t

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 52: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

如何解決如何解決 ReadRead--ModifyModify--WriteWrite如何解決如何解決 ReadRead ModifyModify WriteWrite

如果是對相同的 PORTx 連續操作如果是對相同的 PORTx 連續操作解決之道是在連續兩個 BSFBCF 指令間加入適當個 NOP 指令

bull BSF PORTA5bull NOPbull BSF PORTA 0bull BSF PORTA0

使用 C18 則可加入 Nop( ) 的巨集

建議直接使用 LATx 的來取代 PORTx 的操作這樣直接就對拴鎖暫存器操作而不是對外面的 PORT 操作也就沒有因電容所造成的延遲作也就沒有因電容所造成的延遲

bull BSF LATA5bull BSF LATA0

Microchip Technology Taiwan Slide 52 102_ASP-TW

S 0

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 53: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

IO IO 輸出輸出 輸入控制輸入控制

IO Pin 方向控制1 1 1 10 0 0 0TRISB

IO Pin 方向控制

In In In InOut Out Out OutPORTB

TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能)位元的資料方向 (Reset 的初始定義為輸入功能)1 = 輸入 0 = 輸出

Microchip Technology Taiwan Slide 53 102_ASP-TW

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 54: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

類比類比 或或 數位數位 腳位腳位 類比類比 或或 數位數位 腳位腳位

有些接腳是類比輸入與數位共用同一隻接腳有些接腳是類比輸入與數位共用同一隻接腳Power-On 後這些接腳的預設值為 ldquo類比輸入rdquo

類比輸入有 ADC 輸入與電壓比較器輸入類比輸入有 ADC 輸入與電壓比較器輸入

ADCON1 暫存器用來設定腳位是類比輸入還是數位 IO通常我們不需如此多的 AD Input Channel 通常我們不需如此多的 AD Input Channel

Bit 0Bit 7

- - X X 1 1 1 1VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0

設定最低 4 bits 為 lsquo1rsquo 時關閉所有 ADC 輸入腳功能

Microchip Technology Taiwan Slide 54 102_ASP-TW

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 55: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

MPLABMPLAB IDE IDE -- 整合式的發展環境整合式的發展環境

MPLABreg

Integrated Development Environment

內含多功能內含多功能程式編輯器程式編輯器

單一系統專案單一系統專案管理模式管理模式

原始檔案程式原始檔案程式偵錯功能偵錯功能

語言工具 軟體模擬 模擬器及除錯器 燒錄器協力廠商支援工具Compilers

程式編輯器程式編輯器 管理模式管理模式 偵錯功能偵錯功能

MPASMtradeAssembler

MPLAB SIMSofware

Simulator

MPLABREAL ICE

PICkit 1PICSTARTreg

Plus

CompilersIAR Hi-Tech

CCSME Labs

Green Hills

MPLINKtradeMPLIBtrade

MPLAB ICE2000 amp 4000 MPLAB PM3

Real-time OperatingSystems

CMX VectorRealogy Express

Logic

MPLAB C18MPLAB C30MPLAB C32

PICkit 2MPLAB ICD 2

MATLABLive Devices CMXMomentum Data

Systems

Microchip Technology Taiwan Slide 55 102_ASP-TW

Uniquely supporting 8 16 and 32 bit MCUS within one integrated development Environment

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 56: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

MPLAB IDEMPLAB IDE 的下載的下載MPLAB IDE MPLAB IDE 的下載的下載

由 Microchip 的首頁即可連結到 MPLAB IDE 的下由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 ndash Itrsquos free

Microchip Technology Taiwan Slide 56 102_ASP-TW

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 57: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

現有硬體除錯工具現有硬體除錯工具

MPLAB ICE 2000MPLAB ICE 2000舊版d

Trac

e MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLABREAL ICE

High SpeedCustom USBRun Halt SS

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

MPLAB ICE 2000Parallel

USB OptionRun Halt SS

Complex T i B k

舊版

現有

sSp

eed Complex

TriggerBreakReal Time Watch

StopwatchProgram Read

Complex TriggerBreak

Real Time WatchStopwatch

Program Read

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2

TriggerBreakStatic TraceLogic Probes

Flexible circuit cable20 MIPS Emul

PIC10F 12F 16C2MPLAB ICD 2

S i l F ll S dMPLAB ICD 2

S i l F ll S d

Feat

ures Dynamic Trace

Logic ProbesLVDS

Full Speed EmulPIC16F1 PIC18F

Dynamic TraceLogic Probes

LVDSFull Speed Emul

PIC16F1 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

PIC10F 12F 16C2 PIC16F PIC17C2

PIC18C2 PIC18F

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreak

Serial Full Speed Custom USBRun Halt SS

Complex TriggerBreakF

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PICkit 2Full Speed HID

USB1 Device Family per

Loader

PIC24 dsPIC

PIC24 dsPICStopwatch

Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24

Stopwatch Program ReadFull Speed EmulPIC10F 12F 16F PIC18F PIC24Loader

Run Halt SSProgram ReadFull Speed EmulSelect Devices

Loader Run Halt SSProgram ReadFull Speed EmulSelect Devices

PIC18F PIC24 dsPIC

PIC18F PIC24 dsPIC

Microchip Technology Taiwan Slide 57 102_ASP-TW

$50 $150 $500 $1500

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 58: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

新推出的硬體除錯工具新推出的硬體除錯工具e

edT

race MPLAB REAL ICEtrade

EmulatorHigh Speed

USB 2 0

MPLAB REAL ICEtrade EmulatorHigh Speed

USB 2 0

esS

pee

MPLABreg ICD 3High Speed USB 20

Run Halt SS

MPLABreg ICD 3High Speed USB 20

Run Halt SS

USB 20Run Halt SS

Software Breakpoints Complex

USB 20Run Halt SS

Software Breakpoints Complex

Feat

ure

Software Breakpoints Complex

BreakpointsSt t h

Software Breakpoints

Complex BreakpointsSt t h

PICkittrade 3PICkittrade 3

pBreakpointsTriggers

StopwatchProgram Read

Real Time Watch

pBreakpointsTriggers

StopwatchProgram Read

Real Time WatchStopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

StopwatchProgram Read

All of Microchiprsquos Flash PICreg

MCUs and dsPICreg DSCs

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Full Speed USB HIDRun Halt SS

Simple BreakpointsProgram Read

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUs

Real-Time WatchTrace

Logic ProbesPerformance Pak

All of Microchiprsquos Flash PIC MCUsProgram ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

Program ReadAll of Microchiprsquos Flash PICreg MCU and dsPIC

DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

All of Microchip s Flash PIC MCUs and dsPIC DSCs

Microchip Technology Taiwan Slide 58 102_ASP-TW

$70 $220 $500

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 59: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

串列燒錄與除錯功能串列燒錄與除錯功能InIn--Circuit Serial ProgrammingCircuit Serial Programmingtrade amp Debuggingtrade amp Debugging

燒錄時只用到兩支腳位 (請勿接電阻到地)

PinPinVPP

VDD

V

FunctionFunctionProgramming Voltage = 13VSupply VoltageGround

RB6 PGCRB7 PGD

Vpp 會有 13V 的觸發電壓輸入

VSS

RB6RB7

GroundClock InputData IO amp Command Input

Vpp 會有 13V 的觸發電壓輸入要使用10K ohm 電阻接 Vdd最好不要再接電容到地以免影響 V 上升時間造成燒錄錯誤

MCLRVPP

VDD VDDApplication PCB

響 Vpp 上升時間造成燒錄錯誤

這種燒錄介面可以使用在

PP

VDD

C18

Fxxx

x ICSP ConnectorICSPtrade Connector

Microchip 所有的開發工具裏

ICD2 ICD3 Real ICEPICkit2 PM3

VSS

RB6

RB7

PIC

Microchip Technology Taiwan Slide 59 102_ASP-TW

PICkit2 PM3To application circuit Isolation

circuits

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 60: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接Microchip MCU 使用 ICDICSP 界面與 DebuggerProgrammer 連接

PICkit 2 與 PICkit 3 的介面定義如下

Microchip Technology Taiwan Slide 60 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 61: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 使用的 RJ-11 連接器上的 接腳與信號位置 ( 使用上簧片之 6P6C )

Microchip Technology Taiwan Slide 61 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 62: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

MPLAB ICD2ICD3 隨附的 Modular Cable 示意MPLAB ICD2ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 )

Microchip Technology Taiwan Slide 62 102_ASP-TW

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 63: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

DebuggerProgrammer DebuggerProgrammer 的界面的界面gg ggg g與與 Tool Tool 的連接的連接

自行 Layout 時 若採用市售常見下簧片的 6P6C自行 Layout 時 若採用市售常見下簧片的 6P6C Connector 與 MPLAB ICD2ICD3 間的信號關係如下 ( 零件的 Layout 圖 正面 )如下 ( 零件的 Layout 圖 正面 )

Microchip Technology Taiwan Slide 63 102_ASP-TW

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 64: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

MPLAB IDE MPLAB IDE 畫面畫面工具圖示區

C 的原始程式

工具圖示區

工作項目

編譯輸出

設定中斷點

編譯輸出

變數觀察視窗暫存器顯示

Microchip Technology Taiwan Slide 64 102_ASP-TW狀態顯示列

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 65: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18 PIC18 振盪器選擇振盪器選擇振盪器選擇振盪器選擇XT

HS

Standard frequency crystal oscillator

High frequency crystal oscillator

100kHz - 4MHz

DC - 40MHz

HS+PLL High frequency crystal with 4x PLL 4MHz - 10MHz

LP

RC

Low frequency crystal oscillator

External RC oscillator

5kHz - 200kHz

DC - 4MHzRC External RC oscillator DC 4MHz

RCIO External RC oscillator OSC2=RA6 DC - 4MHz

INTRC Internal RC oscillator Various

EC E t l Cl k OSC2 f 4 DC 40MHEC External Clock OSC2=fosc4 DC - 40MHz

ECIO External Clock OSC2=RA6 DC - 40MHz

提供多樣的振盪器選擇方式 讓設計更加彈性提供多樣的振盪器選擇方式讓設計更加彈性 LP 振盪模式提供了低功耗功能

RC 或 INTRC 振盪模式提供了低成本的效益RC 或 INTRC 振盪模式提供了低成本的效益

XT 提供一般使用的工作頻率

HS 提供需高速處理所需的工作頻率

Microchip Technology Taiwan Slide 65 102_ASP-TW

速度範圍參考資料手冊所建議的範圍

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 66: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

設定設定 Configuration Word Configuration Word 的方式的方式設定設定 gg 的方式的方式

直接在 MPLAB IDE 下設定先確定 使用的元件名稱先確定 MPLAB IDE 使用的元件名稱

MPLAB IDE 目錄下 Configure Configuration Bits取消 Configuration Bits Set in code取消 Configuration Bits Set in code

Microchip Technology Taiwan Slide 66 102_ASP-TW

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 67: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令組合語言必需要使用的虛擬指令

LISTLIST 目錄控制目錄控制 (Listing Control)(Listing Control)LISTLIST -- 目錄控制目錄控制 (Listing Control)(Listing Control)list p=PIC18F4520list p=PIC18F4520

INCLUDEINCLUDE -- 加入一原始檔定義檔或敘述檔加入一原始檔定義檔或敘述檔INCLUDEINCLUDE 加入一原始檔 定義檔或敘述檔加入一原始檔 定義檔或敘述檔

include lt18f4520incgtinclude lt18f4520incgt

EQUEQU -- 宣告宣告常數變數常數變數 ((不可重新定位不可重新定位))memorymemory equ equ 0x3f0x3fcount count equequ 100100i ti t Brsquo11000011rsquoBrsquo11000011rsquoio_set equio_set equ Brsquo11000011rsquoBrsquo11000011rsquo

ORGORG -- 設定程式組譯的起始位址設定程式組譯的起始位址

org 0x00 組譯位址從ldquo00hrdquo開始org 0x00 組譯位址從 00h 開始

org 0x30 組譯位址從ldquo30hrdquo開始

ENDEND -- 程式結束程式結束

Microchip Technology Taiwan Slide 67 102_ASP-TW

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 68: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例虛擬指令的使用範例

list p=18F4520 定義使用的 MCU 為 PIC18F4520使用 的標準定義檔include ltp18F4520incgt 使用 18F4520 的標準定義檔

定義變數` 常數參數區 T_DELAY EQU Drsquo100rsquo 設定常數值

dly count EQU Hrsquo00 設定變數位址dly_count EQU H 00 設定變數位址

ORG 0x000 設定程式執行位址從ldquo0000開始

clrf PCLATUclrf PCLATHgoto main

0 0008 高優先權中斷向量進入位址ORG 0x0008 高優先權中斷向量進入位址

中斷處理副程式區 retfie s 中斷返回

main movlw T_DELAY 主程式開始

movwf dly_count remaining code goes here

Microchip Technology Taiwan Slide 68 102_ASP-TW

END 程式結束

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 69: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

MPASMMPASM 的範本程式的範本程式MPASM MPASM 的範本程式的範本程式

安裝了 MPLAB IDE 後 MPASM 也一併安裝安裝了 MPLAB IDE 後 MPASM 也一併安裝

目錄 CProgram FilesMicrochipMPASM Suite在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式在此目錄可找到各 PIC16F amp PIC18F 的組合語言範本程式

CProgram FilesMicrochipMPASM SuiteTemplateCodeCProgram FilesMicrochipMPASM SuiteTemplateObjectC og a es c oc p S Su te e p ateObject

Code 子目錄的寫法為 ldquo單一rdquo組合語言程式的範本一個 Project 只包含 1 個 asm 的檔案 定址都用 ORG amp EQUj

Object 子目錄的寫法為 ldquoRelocatable code rdquo的組合語言程式的範本

一個 Project 只包含多個 asm 的檔案

使用 CODE RES hellip 等 Directives 來定址

Mi hi 提供 102ASP (W400) 一天的訓練課程

Microchip Technology Taiwan Slide 69 102_ASP-TW

Microchip 提供 102ASP (W400) 一天的訓練課程

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 70: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

MPLAB C18MPLAB C18MPLAB C18MPLAB C18

須另外安裝的語言工具須另外安裝的語言工具

可由 wwwmicrochipcom 免費下載也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

提供完整的 Peripheral Library 讓周邊的操控更簡便

Microchip 提供 W401 兩天的訓練課程Microchip 提供 W401 兩天的訓練課程

可在 wwwmicrochipcomtw 的下載中新下載講義及練習檔也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到也可在 Microchip Taiwan 提供的 RTC 及 產品 DVD 光碟找到

搭配 MPLAB IDE 可以做到 Source Level Debugging

Microchip Technology Taiwan Slide 70 102_ASP-TW

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 71: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

認識認識 APP001 APP001 實驗板實驗板AD 輸入

VR1ICD2接頭

Reset按鍵

9V電源輸入 RS-232 CAN

RS-485Debug amp Prog選擇 Jumper

PI

5V電源穩壓

IC18F4

I2C amp SPIEEPROM

CCP24520CCP1蜂鳴器

PWM LED

PORTDLED x8 外部震盪

器插座

16MH

2 x 16 LCD

Microchip Technology Taiwan Slide 71 102_ASP-TW

按鍵開關SW3 amp SW2

16MHzCrystal

溫度感測器

LCD反襯度調整

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 72: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

APP026APP026--3X3X 實驗板實驗板APP026APP026 3X 3X 實驗板實驗板

Microchip RTC 課程及菁英班課程的專用實驗板

可以搭配 64 to 44 Pin 的轉接板 使用來開發 PIC18F4520

Microchip Technology Taiwan Slide 72 102_ASP-TW

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 73: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

APP026APP026 3X3X 內建的內建的 PICkit 2PICkit 2 相容相容 modulemoduleAPP026APP026--3X 3X 內建的內建的 PICkit 2 PICkit 2 相容相容 modulemodule

位置在 LCD Display 的下方位置在 LCD Display 的下方

功能與 PICkit 2 相容

無法提供 P 至 T t無法提供 Power 至 Target Board沒有 EEPROM 24LC512 2沒有 EEPROM 24LC512 2 所以無 Programmer to go 功能

24LC512 可自行加入24LC512 可自行加入

有 PICkit 2 相容的單排2 54mm 6 Pin 與 ICD 2 相254mm 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面

Microchip Technology Taiwan Slide 73 102_ASP-TW

及燒錄介面

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 74: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PICKit 2PICKit 2 的重要功能的重要功能PICKit 2 PICKit 2 的重要功能的重要功能

與 MPLAB ICD 2 類似 可以搭配與 C 類似 可以搭配MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯

只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可

大部分的 PIC16F PIC18F大部分的 PIC16F PIC18F大部分的 PIC24FJ PIC24HJ amp dsPIC33FJ大部分的 dsPIC30F

使用 HID Device Class 安裝及使用比 MPLAB ICD 2 容易用比 容易

操作介面及方法與 ICD 2 一致

Microchip Technology Taiwan Slide 74 102_ASP-TW

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 75: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026--3X3X 實驗板的範例實驗板的範例使用使用 APP026APP026 3X 3X 實驗板的範例實驗板的範例

APP026-3X 主要目的在提供一個穩定的基礎電路給User 要使用的 CPUAPP026-3X 只將 Power 及DebugProgram 所需信號接妥

User 可依時際需求將CPU 的 功能 Pin 腳接至實驗板上的周邊 ( VR Switch LED RS-232 收發器)若 On-Board 的周邊功能不足 可輕易地以杜邦跳線連接其他的控制板

APP026-3X 的電路與APP026-3 相同 說明書可

Microchip Technology Taiwan Slide 75 102_ASP-TW

相同 說明書可於 Microchip 台灣網站下載

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 76: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

APP026APP026--3X3X 重要的重要的 ConnectorsConnectorsAPP026APP026 3X 3X 重要的重要的 ConnectorsConnectorsAPP026-3X 的設計原責 僅將必要信號定位 其餘交由 User 自定

CON1-9V DC 插孔

JP5ampJP6 ndash CPU 選擇 JUMPER

VR1 amp VR2 的插針

JP2- Vdd 電壓選擇(33V5V) JP20 - USB 信號

JP1- 5V Source

5V 電壓的來源

64-Pin

X2 ndash

CPU 模組CON4-LCD 模組的控制

信號

X1 ndash 8Mhz 震盪器

X2 32768Hhz

內建PICKit 2

JP19- 按鍵及LED 的插針

震盪器PICKit 2 的 USB

Connector

Microchip Technology Taiwan Slide 76 102_ASP-TW

ICD2 amp PICKit 2 Connector

Debug 腳位選擇 JP8-ENVREG for PIC18J CON11 47K

Pull-up 4按鍵 S1-S4 LED1 ndash LED4

的插針

內建 PICKit 2 module的 Debug 輸出

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 77: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源接上電源前需確認 JP2 VDD 的選用是正確的 ( 33V or 5V)不同的 MCU 有不同的工作電壓範圍 Power On 前先確定電源設定是對不同的 MCU 有不同的工作電壓範圍 Power-On 前先確定電源設定是對的

Microchip Technology Taiwan Slide 77 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 78: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源APP026-3 不管 JP2 VDD 設為 33V or 5V 都需要一個 5V 的周邊電壓被產生 因為 3 3V 是經由 MCP170X LDO 由 5V 輸入獲得 3 3V 輸出產生 因為 33V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 輸出

使用 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB or 外接電源經7805 而來

Microchip Technology Taiwan Slide 78 102_ASP-TW

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 79: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026 3X3X 的注意事項的注意事項 電源電源使用使用 APP026APP026--3X 3X 的注意事項的注意事項 -- 電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為 USB 則 APP026-3X 的CON15 USB 必需被連上 PC 的 USB P t 或 USB t 相容的供電設備CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備

Microchip Technology Taiwan Slide 79 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 80: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源

若以 JP1 5V-SOURCE來選擇 周邊用5V的來源為外接電源經 7805 則 APP026-3X 的 CON1 必需被連上 外接的 9V DC Power3X 的 CON1 必需被連上 外接的 9V DC Power

Microchip Technology Taiwan Slide 80 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 81: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026--3X 3X 的注意事項的注意事項電源電源電源電源

如果你要使用 APP026-3X 上的 PICkit2 來 Debug 則 CON18-USB1 需接上 PC 端的USB Port 如此一來實驗板上的周邊用 可以考慮共同使用 上的如此一來實驗板上的周邊用 5V 可以考慮共同使用 CON18-USB1 上的 5V VBUS可以使用隨附的短杜邦線將 CON18A Pin2 的 5V 跳接至 +5V 的 Connector

Microchip Technology Taiwan Slide 81 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 82: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026--3X 3X 的注意事項的注意事項連接連接連接連接 PICKitPICKit 22

APP026-3X 上的 PICKit 2 工具使用 HID 類別 無需安裝 Driver 請使用隨附的 線將 與 連接請使用隨附的USB 線將 PC 與 APP026-3X 連接

CON19 與 CON19A 是並聯的 都可用來 Debug請用隨附的 6P 電話線將 CON19A 與 CON12 連接請用隨附的 6P 電話線將 CON19A 與 CON12 連接

參考前幾頁的電源設定來選擇要用何種供電方式

Microchip Technology Taiwan Slide 82 102_ASP-TW

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 83: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

使用使用 APP026APP026--3X 3X 的注意事項的注意事項Deb gger PinDeb gger Pin 腳的選擇腳的選擇Debugger Pin Debugger Pin 腳的選擇腳的選擇

如果使用 CON18-USB 的 5V VBUS 來供電 則基本連線將如下圖

APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCUPIC24FJ128GB106 有 3 組 DebugProgram 腳位可選 而 APP026-3X 有將第 1 2 組引出

請將 JP15 ampJP16 的 JUMPER 插上 以便使用 PGEC1PGED1 來 Debug

Microchip Technology Taiwan Slide 83 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 84: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIMAPP026-3X 可以使用 PIC18F4520 的APP026 3X 可以使用 PIC18F4520 的PIM ( Plug-In Module )PIC18F4520 與 LCD 的連線已完成

接至RD0 RD3 接至 LCD DataRD4 amp RD5 接至 LCD 的 RS 及 RW 接腳

RA2 被接至 LCD 的 E 腳

PIC18F4520 的 Power ICSP 連線亦已經完成

可以用 JP2 選 33 or 5V 的 Vdd可以用 JP2 選 33 or 5V 的 VddPIC18F4520 的 ICSP 連線被接至APP026-3X 原有 64-pin 腳位的 Pin-15 amp Pin16Pin16

其它的腳位皆為 available User 可以自行決定如何運用

Microchip Technology Taiwan Slide 84 102_ASP-TW

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 85: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

在在 APP026APP026 3X3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM在在 APP026APP026--3X 3X 使用使用 PIC18F4520 PIMPIC18F4520 PIM

APP026 3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例

Microchip Technology Taiwan Slide 85 102_ASP-TW

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 86: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

PIC18F4520PIC18F4520 IPTIPT 的腳位圖的腳位圖PIC18F4520PIC18F4520--IPT IPT 的腳位圖的腳位圖

Microchip Technology Taiwan Slide 86 102_ASP-TW

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 87: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Demo 1Demo 1 IOIO 的操作的操作Demo 1 Demo 1 IO IO 的操作的操作

使用組合語言使程式能依按鍵的選擇來控制 LED使用 MPASM 提供的範本程式為基礎 練習組合語言

練習使用 APP026-3X 上的 PICkit 2 來 DebugProgram

設定 RB0 RA4 為輸入

設定 PORTD 為輸出 ( RD0 RD3 驅動 LED1 ~ LED4 )

按 S1 (RB0) 時點亮 LED1 ndash LED2

按 S2 (RA4) 時點亮 LED3ndash LED4

使用中斷點來觀查程式的運作

Microchip Technology Taiwan Slide 87 102_ASP-TW

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 88: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Demo 2Demo 2使用使用 Timer1Timer1 與與中斷中斷Demo 2Demo 2使用使用 Timer1 Timer1 與與中斷中斷

使用 MPLAB C18 來撰寫 D 2使用 MPLAB C18 來撰寫 Demo 2

讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock

並啟動 Timer1 的中斷

設定 PIC18F4520 的工作模式後進入主程式的永久迴圈

檢查 ONE_SEC 旗標 若為 1 則顯示 MMSS 於 LCD 第一行_

將 Timer1 設定為 10 ms 秒中斷 CPU 一次

Timer1 每次中斷後判斷是否已累積達 1 秒的時間後

Update Min Sec 變數 並設定 ONE_SEC 旗標

Microchip Technology Taiwan Slide 88 102_ASP-TW

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20

Page 89: PIC18F 39th設計競賽 [相容模式] · PIC18 系列MCUMCU 的主要特色 PIC18Fxxxx ¾提供較多的指令(77個基本指令) ¾更大的程式與資料記憶體的定址能力

Demo 3Demo 3ADCADC 的操作的操作Demo 3Demo 3ADC ADC 的操作的操作承接 Demo 2 但在主程示迴圈不斷的讀取 VR1 amp VR2 的

ADC 讀值顯示於 LCD 第二行

將 ADC 規劃為 將 ADC 規劃為

AN0 amp AN1 enabled

Right Justified

Internal Reference

Proper TAD and TACQ

LCD 若 update 太快會有閃爍的現象 所以要以程式處理LCD 若 update 太快會有閃爍的現象 所以要以程式處理

每 100 ms 才update LCD 的 ADC 讀值

AN0 Pi 19 AN1 Pi 20

Microchip Technology Taiwan Slide 89 102_ASP-TW

AN0 Pin-19 AN1 Pin-20