Microsoft PowerPoint - PIC18F_39th設計競賽 [相容模式]

Similar documents
ICD ICD ICD ICD ICD

PIC16F F MPLAB 08 16F LED 15 LED

第5章:汇编语言程序设计

文件名

2.5 idlocs PIC idloc idloc 0x1234 ; 0x config idloc HEX errorlevel errorlevel Error (Warning) Message HEX 3-14 errorlevel

INCF SEC1,F ; 將 SEC1+1 作查表取回對應數字 MOVF SEC1,W banksel STRING1 banksel PORTB RETFIE run_sec2: MOVF SEC2,W SUBLW.5 GOTO run_min ; 將秒個位數歸 0 CLRF SEC1 MOVL

USB解决方案.ppt

2 PIC PIC 1 / CPU PIC MCU PIC RC

C PICC C++ C++ C C #include<pic.h> C static volatile unsigned char 0x01; static volatile unsigned char 0x02; static volatile unsigned cha

Section1_16bit Arc.ppt

1-1 SH79F6431 A. 2( ) 9~15V ( 12V) U2 U3 3.3V SH79F B. 1(VCC/GND) SH79F6431 C. VDDIO SH79F6431 P4 P5 P0.6 P0.7 VDDIO VDDIO=5V D. 2 V 1.0

ARM Cortex-M3 (STM32F) STMicroelectronics ( ST) STM32F103 Core: ARM 32-bit Cortex -M3 CPU 72 MHz, 90 DMIPS with 1.25 DMIPS/MHz Single-cycle multiplica

(Microsoft PowerPoint - 102_ASP TW_CH [\254\333\256e\274\322\246\241])

2 Keil µ vision 2.1 1) Keil µ vision2 V2.34 µ vision3 2) Sino_Keil.exe Keil c:\keil\ 3) JET51 USB PC C:\Keil\ USB PC 4) S-L

PROTEUS VSM

目 录

(Load Project) (Save Project) (OffLine Mode) (Help) Intel Hex Motor

AN579

PICkit2 燒寫器編程器調試器

ARM JTAG实时仿真器安装使用指南

EK-STM32F

untitled

目录

Tel:

投影片 1

SPHE8202R Design Guide Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provi

FM1935X智能非接触读写器芯片

1 TPIS TPIS 2 2

2 14 PORTC.1 PORTB.3 PORTA.2/T0 GND PORTB.2 PORTA.0 PORTC.3 PORB.0/OSCO PORTB.1/OSCI PORTC.0 PORTC.2 SH69P21 /SOP PORTA

33023A.book(31001A_cn.fm)

P4V88+_BIOS_CN.p65

PIC16F87X基本指令說明

audiogram3 Owners Manual

C语言的应用.PDF


DPJJX1.DOC

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

第 15 章 程 式 編 写 語 言 15.1 程 式 編 写 語 言 的 角 色 程 式 編 寫 語 言 是 程 式 編 寫 員 與 電 腦 溝 通 的 界 面 語 法 是 一 組 規 則 讓 程 式 編 寫 員 將 字 詞 集 合 起 來 電 腦 是 處 理 位 元 和 字 節 的 機 器, 與

Cube20S small, speedy, safe Eextremely modular Up to 64 modules per bus node Quick reaction time: up to 20 µs Cube20S A new Member of the Cube Family

General Description: Preliminary TTP916 Consumer IC VCD/DVD LCD Green mode Stand-by mode( 1W ) Features: 2.2V-5.5V LCD RAM read condition 2.6V-5.5V RC

P4VM800_BIOS_CN.p65

1.1 ML_ONOFF = 1 Q 3 Q 8 C 0.3V M 2 L 1 ML_ONOFF = 0 Q 3 Q 8 C 1. + R31 VCC R21 10K ML_ONOFF R15 0:off 1:on 1K Green Light VCC=5V L1 Q VDD=12V C

1 CPU

51 C 51 isp 10 C PCB C C C C KEIL

12232A LED LED LED EL EL CCFL EL CCF

行业

邏輯分析儀的概念與原理-展示版

Bus Hound 5

逢 甲 大 學

行业

Microsoft PowerPoint - CA_02 Chapter5 Part-I_Single _V2.ppt

P3B-F Pentium III/II/Celeron TM

System Design and Setup of a Robot to Pass over Steps Abstract In the research, one special type of robots that can pass over steps is designed and se

《计算机应用基础》学习材料(讲义)

RESET OSCO OSCI OSC CPU WDTEN RC RESET PRESCALER PORTA (4-BITS) WATCHDOG TIMER Power on PORTB (4-BITS) PORTA [0:3] PORTB [0:3] LPD PORTC (4-BITS) LPDO

ATMEL AT90S8515 AVR CPU AVR AVR AVR ATMEL RISC 32 8 r0 r X Y Z R0 R1 R2 R13 R14 R15 R16 R17 R26 R27 R28 R29 R30 R31 0x00 0x

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

四位微控制器ML64168_ _.doc

Microsoft PowerPoint - C15_LECTURE_NOTE_04.ppt

<4D F736F F D20B3E6B4B9A4F930365F32A443AC71C5E3A5DCBEB9B1B1A8EE2E646F63>

Microsoft PowerPoint - C15_LECTURE_NOTE_04.ppt

JTAG ICE PC JTAG ICE JTAG ISP... 5 IDE AVR STUDIO JTAGICE JTAGICE... 12

Microsoft PowerPoint - C15_LECTURE_NOTE_05.ppt

Ác Åé å Serial ATA ( Sil3132) S A T A (1) SATA (2) BIOS SATA (3)* RAID BIOS RAID (4) SATA (5) SATA (a) S A T A ( S A T A R A I D ) (b) (c) Windows XP

ARK-14013/14017/14018

Microsoft Word - LD5515_5V1.5A-DB-01 Demo Board Manual

untitled

701PIC Introduction to PICMicro MCUs

Pin Configurations Figure2. Pin Configuration of FS2012 (Top View) Table 1 Pin Description Pin Number Pin Name Description 1 GND 2 FB 3 SW Ground Pin.

1

2/80 2

: WICE-PIC 1.1 WICE-PIC 1.2 WICE-PIC : WICE-PIC WICE-PIC 2.5 WICE-PIC 2.6 : : : : A. B.E.V.Board 1. Internet Internet WWW: http//

A Preliminary Implementation of Linux Kernel Virus and Process Hiding

untitled

2_dvdr3380_97_CT_21221b.indd

行业

(02) (02) (02) (02) (02

els0xu_zh_nf_v8.book Page Wednesday, June, 009 9:5 AM ELS-0/0C.8

,Microchip Technology PIC LCD, PIC16F913/914/ 916/917/946 PIC18F6390/6490/8390/8490 PIC16F65J90/85J90 Microchip LCD LCD, Microchip 的优势 LCD PIC, LCD LC

68369 (ppp quickstart guide)

学习MSP430单片机推荐参考书

untitled

iml v C / 0W EVM - pplication Notes. IC Description The iml8683 is a Three Terminal Current Controller (TTCC) for regulating the current flowin

untitled

iml v C / 4W Down-Light EVM - pplication Notes. IC Description The iml8683 is a Three Terminal Current Controller (TTCC) for regulating the cur

Microsoft Word - WAP001 Workshop Board 中文 Manual VER.3.doc

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

untitled

Microsoft Word - MSP430 Launchpad 指导书.docx

52C

Chroma 61500/ bit / RMS RMS VA ()61500 DSP THD /61508/61507/61609/61608/ (61500 ) Chroma STEP PULSE : LISTLIST 100 AC DC

行业

Serial ATA ( nvidia nforce4 Ultra/SLI)...2 (1) SATA... 2 (2) B I O S S A T A... 3 (3) RAID BIOS RAID... 6 (4) S A T A... 9 (5) S A T A (6) Micro

MATLAB 1

Aquasnap Junior 30RH/RA RH/RA

Transcription:

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

開發 PIC18F4520 程式相關的 軟 / 硬體開發工具 使用軟體工具 ( 包含於 Microchip 產品 DVD 中 ) MPLAB IDE v8.xx ( 或更新版本 ) MPASM, MPLINK, MPLIB 使用硬體工具 MPLAB ICD2 Microchip APP001 Workshop Board (PIC18F4520 inside) APP026-3X 泛用型實驗板 + PIC18F4520 PIM Module 參考書籍 ( 可以從網站上下載 ) MPASM User s Guide with MPLINK and MPLIB (DS33014J) MPLAB IDE User ss Guide PIC18F4520 Data Sheet ( DS39631A ) Microchip Technology Taiwan Slide 2

包含於 Microchip 產品 DVD V4.0 Microchip Technology Taiwan Slide 3

PIC18F 系列 PIC18F4520 開發講座內容 PIC18F MCU 的架構 PIC18F MCU 主要周邊功能 開發工具介紹與使用 撰寫 PIC18 系列組合語言及參考資源 使用 MPLAB C18 於程式開發 Demo 1 : I/O 的操作 Demo 2 : 使用 Timer1 與中斷 Demo 3 : 使用 A/D 轉換器 Microchip Technology Taiwan Slide 4

新改良 PIC18Fxxxx ( 四碼 ) PIC18F4520 是廣泛使用的 40-Pin PIC18F 系列 MCU Extended Instruction Sets for C18 Compiler Efficiency Support nano-watt Technology 內建 31KHz ~ 8MHz 自由切換的 RC 震盪器 快速 RC 啟動模式 雙速啟動模式 系統時脈監視 / 故障切換功能 Low-Power Watch-Dog Timer Ultra Low-Power Wake-Up 省電模式 : Idle, Sleep Microchip Technology Taiwan Slide 5

PIC18Fxxxx Enhanced PICmicro MCU 的架構及主要特色 Microchip Technology Taiwan Slide 6

PIC18Fxxxx PIC18 系列 MCU 的主要特色 提供較多的指令 (77 個基本指令 ) 更大的程式與資料記憶體的定址能力 Program Memory : Up to 2M Bytes Data Memory : Up to 4K 程式記憶體使用線性排列方式存取 提供 Access Bank 設計觀念 內建 PLL 4 倍頻線路, 用 10Mhz 就得到 10 MIPs 的執行效 能 ( 100 ns 的指令執行時間 ) 有 8 * 8 的硬體乘法器 全系列提供 10 Bits 的 A/D 轉換器及 ICD 除錯功能 Microchip Technology Taiwan Slide 7

PIC18F4520 增加的功能 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

PIC18F4520 nanowatt 技術 什麼是 nano-watt 技術 Sleep Mode 下消耗功率低於 1uW 以下 PIC18F4520 Sleep Mode 下低於 0.1uA 內建的電源管理 內建 31KHz ~ 8MHz 自由切換的 RC 震盪器 快速 RC 啟動模式 雙速啟動模式 系統時脈監視 / 故障切換功能 Low-Power Watch-Dog Timer Ultra Low-Power Timer1 硬體設定或軟體控制的 Watch-Dog Timer 省電模式 : Idle (5.8uA), Sleep (0.1uA) Microchip Technology Taiwan Slide 9

PIC18F4520 主要的內建周邊 最多可提供 36 個 I/O 腳位 3 個可程式規劃的外部中斷 4 個可偵測 Pin-Change 的腳位 增強型 Input Capture/Output Compare/PWM 模組 MSSP module (Master Synchronous Serial Port) 支援硬體 I2C, SPI 增強型 Addressable USART module (EUSART) 支援高達 13 個 Channel 的 AD Converter module 兩個 Channel 的類比比較器 1 個 8-bit timer, 3 個 16-bit Timer Microchip Technology Taiwan Slide 10

Instruction Pipelining 預提取指令執行指令 movlw 0x0505 - 指令週期 1 2 3 4 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 addwf REG2 T0 Fetch 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 11

Instruction Pipelining 預提取指令執行指令 movwf REG1 movlw 0x0505 指令週期 1 2 3 4 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 addwf REG2 T0 Fetch T1 Execute Fetch 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 12

Instruction Pipelining 預提取指令 rcall SUB1 執行指令 movwf REG1 指令週期 1 2 3 4 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 addwf REG2 T0 T1 T2 Fetch Execute Fetch Execute Fetch 此階段執行一般指令 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 13

Instruction Pipelining 預提取指令 addwf REG2 執行指令 rcall SUB1 指令週期 1 2 3 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 T0 T1 T2 T3 Fetch Execute Fetch Execute Fetch Execute 4 addwf REG2 Fetch 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 14

Instruction Pipelining 預提取指令 movf PORTB,w 執行指令 rcall SUB1 指令週期 1 2 3 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 T0 T1 T2 T3 T4 Fetch Execute Fetch Execute Fetch Execute 4 addwf REG2 Fetch Flush 放棄 rcall 指令, 重新提取下一個指令 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 15 Fetch

Instruction Pipelining 預提取指令 return 執行指令 movf PORTB,w 指令週期 1 2 3 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 T0 T1 T2 T3 T4 T5 Fetch Execute Fetch Execute Fetch Execute 4 addwf REG2 Fetch Flush 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 16 Fetch Execute Fetch

Instruction Pipelining 預提取指令 movf PORTC,w 執行指令 return 指令週期 1 2 3 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 T0 T1 T2 T3 T4 T5 T6 Fetch Execute Fetch Execute Fetch Execute 4 addwf REG2 Fetch Flush 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 17 Fetch Execute Fetch Execute Fetch

Instruction Pipelining 預提取指令 addwf REG2 執行指令 return 指令週期 1 2 3 MAIN 程式範例 movlw 0x0505 movwf REG1 rcall SUB1 T0 T1 T2 T3 T4 T5 T6 T7 Fetch Execute Fetch Execute Fetch Execute 4 addwf REG2 Fetch Flush Fetch 51 SUB1 movf PORTB,w 52 53 SUB2 return movf PORTC,w 54 return Microchip Technology Taiwan Slide 18 Fetch Execute Fetch Execute Fetch Flush

21 21 Program Memory 16 PIC18F452 基本架構圖 Table Pointer <2> Inc/dec logic Program Bus OSC2/CLK0 OSC1/CLK1 VDD,VSS MCLR T1OS1 T1OSO 5 8 8 PCLATU PCLATH PCU PCH 31 Level Stack PCL Program Counter TABLELATCH Timing Generation 4X PLL Precision Voltage Reference 8 Data RAM 12 (2) Address<12> 4 12 Data Bus BIT OP PRODH PRODL 8 x 8 Multiply WREG BSR FSR0 8 8 FSR1 FSR2 Instruction Register Power-up Timer Oscillator Start-up Timer Power-on Reset Watchdog Timer Brown-out Reset In-circuit it Debugger 12 Instruction Decode and Control 8 ALU<8> MSSP USART CCP 8 8 8 PORTA PORTB PORTC PORTD PORTE TIMER A/D Microchip Technology Taiwan Slide 19

引用 Register File 的觀念 Data Memory (Register File) SFR s 位於 Bank 15 所 有指令皆可對任一暫存 w ALU f 07h 08h 09h 器操作, 無須切換 Bank d w f Data Bus 0Ah 0Bh 0Ch 0Dh Long word 的指令讓各暫存器可被直接定址 0Eh MOVFF src, dest 0Fh WREG 10h 周邊暫存器及狀態暫存 器皆為 Register files 的一部份 Opcode d a Address 指令解碼與執行 結果儲存控制 Microchip Technology Taiwan Slide 20 資料來源的位址

Data Memory 架構 有 16 banks 的 Data Memory, 每個 Bank 由 256 Bytes 組成 ( Max. 4K Bytes ) Special Function Registers (SFRs) 被安排在 Bank 15 使用 BSR<3:0> 來選擇適當的 BANK 或使用 BANKSEL 來做切換 BANK 的工作 MOVFF 指令可免去切換 BANK 的工作, 但需 2 words 的指令空間來完成 使用 Access Bank 的暫存器, 無須做 Bank 的切換 000h 07Fh 080h 0FFh 100h 1FFh 200h 2FFh D00h DFFh E00h EFFh F00h F7Fh F80h FFFh Access RAM Bank 0 GPR Bank 1 GPR Bank 2 GPR Bank 13 GPR Bank 14 GPR Bank 15 GPR Access SFR PIC16F8F2520/4520 暫存器的分布 Access Bank Access RAM Access SFR 256 Bytes 00h 7Fh 80h FFh Microchip Technology Taiwan Slide 21

PIC18 系列的架構 Program Memory 最大可達 2M MB Bytes (21bi bits) PC<20:0> 的程式空間 Reset Vector 位於程式位址 0x0000 高優先權中斷向量的位址位於 0008h CALL, RCALL, RETURN RETFIE, RETLW Stack Level 1 Stack Level 31 Reset Vector High Priority Interrupt Vector Low Priority Interrupt Vector 000h 008h 018h User Memory Space 低優先權中斷向量的位址位於 0018h On-Chip Programming 31 層獨立的位址堆疊區 Microchip Technology Taiwan Slide 22 Read 0 1FFFFFh 200000h

豐富的內部與外部中斷來源 PIC18 系列的架構 Interrupt Overview 幾乎所有周邊皆有中斷 CPU 的能力 有些 I/O 的變化也可構成中斷條件 (PORTB) 每個周邊中斷可以被設定成具有高 / 低優先權 ( Hi/Low Priority) INT0 永遠為高優先權外部中斷 具相同優先權設定的中斷則可由軟體來決定其優先順序 有整體及各別的中斷致能控制位元 每個中斷都有其獨立的中斷旗標, 可用軟體詢問的方式 大部份的中斷可以用來喚醒處於 SLEEP 狀態的 PICmicro 中斷延遲固定為 3 個 cycle 容易以軟體來判斷中斷的經過時間 Microchip Technology Taiwan Slide 23

睡眠模式 (Sleep Mode) 指微控制器可以執行 Sleep 指令進入休眠狀態節省消耗功率 系統主振盪時脈停止運作 處理器進入靜態的狀態 資料保持不變 Watch-Dog Timer 因使用特定的 RC 振盪可以持續工作不受影響 處理器進入最低耗電模式 通常是漏電流 (0.1-2.0μA typical) MCLR WDT INT TMR1 ADC CMP CCP PORTB SSP PSP 一些可以喚醒睡眠模式的外部觸發事件 Master Clear Pin Asserted (pulled low) Watchdog Timer Timeout INT Pin Interrupt Timer 1 Interrupt (or also TMR3 on PIC18) A/D Conversion Complete Interrupt Comparator Output Change Interrupt Input Capture Event PORTB Interrupt on Change Synchronous Serial Port (I 2 C Mode) Start / Stop Bit Detect Interrupt Parallel Slave Port Read or Write Microchip Technology Taiwan Slide 24

PIC18 系列的架構 高優先全中斷邏輯架構 TMR0IF TMR0IE TMR0IP RBIF RBIF RBIF INT0IF INT0IE INT1IF INT1IE INT1IP INT2IF INT2IE INT2IP Peripheral Interrupt Enabled bit Peripheral Interrupt Flag bit Peripheral Interrupt Priority bit Additional Peripheral Interrupts High Priority Interrupt Generation IPEN From (a) IPEN GIEL/PEIE IPEN Interrupt to CPU Vector to location 0008h (High Priority Interrupt Vector Address) GIEH/GIE High Priority Interrupt initialized (Disable low priority interrupts) To (c) From (b) Microchip Technology Taiwan Slide 25

PIC18 系列的架構 低優先全中斷邏輯架構 Peripheral Interrupt Enabled bit Peripheral Interrupt Flag bit Peripheral Interrupt Priority bit Additional Peripheral Interrupts To (a) To (b) From (c) Wake-up (if in SLEEP mode) Interrupt to CPU Vector to Location 0018h (Low Priority Interrupt Vector Address) TMR0IF TMR0IE TMR0IP RBIF RBIF RBIF INT0IF INT0IE GIEH/GIE INT1IF INT1IE INT1IP INT2IF INT2IE INT2IP Microchip Technology Taiwan Slide 26

18F4520 中斷處理 18F4520 有兩個中斷向量點 高優先權 ==> 中斷向量位址 0x0008 低優先權 ==> 中斷向量位址 0x0018 每個中斷源均可選擇其中斷優先權 ( 二選一 ) 每個中斷源均有獨立的中斷旗標 (Flag) 中斷旗標的清除 ==> 自行用軟體清除 每個中斷源均可 Enable 或 Disable 當然 PIC18 系列也可設定與 PIC16Fxxx 系列的中斷相容 ( 關掉優先權的設定 ) Microchip Technology Taiwan Slide 27

以 Timer1 的中斷設定為範例 有關 T1CON 的控制位元, 請參考 PIC18F4520 Data Sheet RCON 的 <IPEN> 位元 : 啟動高 低權位的中斷功能 GIEH : 允許高優先權中斷 GIEL : 允許低優先權中斷 TMR1IE : 打開 Timer1 的中斷設定 TMR1IP = 1, 設定 Timer1 為高優先權中斷 Microchip Technology Taiwan Slide 28

PIC18 系列的架構 Clocking Scheme 一個指令周期等於輸入 CPU 頻率的 1/4 若 CPU 操作於 40 Mhz 時, 其指令周期為 100 ns Fosc 振盪頻率 /4= Tosc 指令週期 OSC1 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 OSC2 指令週期 一般性的動作 : Q1 = 解碼 Q2 = 讀取 Q3 = 執行 Q4 = 寫入 Microchip Technology Taiwan Slide 29

PIC18Fxxxx 定址模式 Microchip Technology Taiwan Slide 30

18Fxxxx 定址模式 四種基本的定址模式 立即定址模式 直接定址模式 間接定址模式 相對定址模式 Microchip Technology Taiwan Slide 31

PIC18 定址模式立即數的操作 8-bit 的常數值 (literal value) 被包含於程式碼之中 使用於 literal instructions, 例如 movlw, addlw, retlw, etc. 16-bit Instruction for Literal Instructions OP CODE k k k k k k k k Microchip Technology Taiwan Slide 32

PIC18 定址模式 Data Memory: 直接定址 (Banked) Data Memory 總共有效位址為 12 Bits ( 4K Bytes ) 8-bit 的直接位址由指令中得到 其他高位的 4-bits 由 BSR<0..3> 獲得 BSR Register 16-bit Instruction BSR<7:4> BSR<3:0> OP CODE f f f f f f f f 4-bits from BSR Register 8-bits from Instruction Word BSR<3:0> f f f f f f f f Effective 12-bit Register Address Microchip Technology Taiwan Slide 33

PIC18 定址模式 Data Memory : 直接定址 ( Non Banked) MOVFF src, dest ( RAM 對 RAM 的資料搬移 ) 2 Word 的指令, 直接指定兩個 4K 的位址 1 1 0 0 n n n n n n n n n n n n 來源位址 (Source) 1 1 1 1 n n n n n n n n n n n n 目的位址 (Destination) 善用 Access BANK 或選擇 BANKED 的 Single Word 指令可增加執行效能及佔用較少的程式記憶體 Microchip Technology Taiwan Slide 34

PIC18 定址模式 Data Memory: 間接定址 (Indirect addressing) 三個 12-bit 寬的間接定址暫存器 FSR0, FSR1, FSR2 FSRn (File Select Register) 的內容可做以下調整 : Pre-Incremented : (PREINCx) 先被加一再存取其內容 Post-Incremented : (POSTINCx) 存取完 FSRn 所指內容後將 FSRn 加 1 Post-Decremented : (POSTDECx) 存取完 FSRn 所指內容後將 FSRn 減 1 Offset by WREG value (signed):(pluawx) FSRn 不變, 以 W 為偏移值相加後以產生新的索引指標 保持不變 : (INDFx) 12-bit FSRn Register Microchip Technology Taiwan Slide 35 f f f f f f f f f f f f

PIC18 定址模式 Data Memory: 間接定址 (Indirect Addressing) 範例 : 清除由位址 0x000 to 0x5FF 的資料內容 間接位址被寫入 FSRn 中 每當 INDFn 被當成運算元 (operand) operand), 實際上被操作的位址乃是由 FSRn 內容所指的位址 類似 8051 的 @R0 @R1 定址法, 但功能更齊全 _Loop movlw 0x05 ; Value to compare lfsr 1,0x000 ; Clear address from 0 clrf POSTINC1 ; Clear, increment FSR1 cpfsgt FSR1H ; FSR1H > 5? ( 0x600 ) goto _Loop ; NO, loop : ; YES, next instruction Microchip Technology Taiwan Slide 36

PC 相對定址 : 計算 GOTO 位址 計算 GOTO 位址, 使用索引查 表方式建立跳躍表格 org 0x002180 JTS movlw UPPER JUMP_TABLE movwf PCLATU movlw HIGH JUMP_TABLE movwf PCLATH movlw 0x04 mulwf USER_INPUT movf PRODL W @ 0x002190 addwf PCL, f JUMP_TABLE goto DoUserInput0 goto DoUserInput1 goto DoUserInput2 goto DoUserInput3 goto DoUserInput4 Microchip Technology Taiwan Slide 37 USER_INPUT 02 WREG 08 PCLATU PCLATH 00 21 FF FF FF PCU PCH PCL 計算跳躍的偏移量位址每一個 goto 指令佔 4 bytes: 所以要乘四後才可以得到正確的偏移位址

PIC18FXXX 指令集 Microchip Technology Taiwan Slide 38

PIC18 系列 MCU 指令集 Instruction Set Overview 77 個指令 73 個是 Single Word 指令 4 個 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

PIC18 系列 MCU 指令集 Summary (16-bit core) Byte-Oriented Oi t do Operations Byte -Oriented Oi do Operations ADDWF f, d, a Add WREG and f MULWF f, a Multiple WREG with f ADDWFC f, d, a Add WREG and f NEGF f, a Negate f ANDWF f, d, a AND WREG and f RLCF f, d, a Rotate left f through carry CLRF f, a Clear f RLNCF f, d, a Rotate left f, no carry COMF f, d, a Complement f RRCF f, d, a Rotate right f through carry CPFSEQ f, a Compare f with WREG, skip = RRNCF f, d, a Rotate right f, no carry CPFSGT f, a Compare f with WREG, skip > SETF f, a Set f CPFSLT f, a Compare f with WREG, skip < SUBFWB f, d, a Subtract f from WREG DECF f, d, a Decrement f with borrow DECFSZ f, d, a Decrement f, skip if zero SUBWF f, d, a Subtract WREG from f DCFSNZ f, d, a Decrement f, skip if not zero SUBWFB f, d, a Subtract WREG from f INCF f, d, a Increment f with borrow INCFSZ f, d, a Increment f, skip if zero SWAPF f, d, a Swap nibbles of f INFSNZ f, d, a Increment f, skip if not zero TSTFSZ f, a Test f, skip if zero IORWF f, d, a Inclusive OR WREG and f XORWF f, d, a Exclusive OR WREG and f MOVF f, d, a Move f MOVFF fs, fd Move fs (source) to fd (destination) MOVWF f, a Move WREG to f f = File Register, d = destination (1=f, 0=WREG), a = access bank (0=access bank, 1=BSR) Microchip Technology Taiwan Slide 40

PIC18 系列 MCU 指令集 Summary (16-bit core) Bit -Oriented Operations BCF f, b, a Bit clear f BSF f, b, a Bit set f BTG f, b, a Bit toggle f BTFSC f, b, a Bit test f, skip if clear BTFSS f, b, a Bit test f, skip if set Data Memory <--> Program Memory Operations TBLRD* - Table Read TBLRD*+ - Table Read with post-inc. TBLRD*- - Table Read with post-dec. TBLRD+* - Table Read with pre-inc. TBLWT* - Table Write TBLWT*+ - Table Write with post-inc. TBLWT*- - Table Write with post-dec. TBLWT+* - Table Write with pre-inc. Literal Operations ADDLW k Add literal with WREG ANDLW k AND literal with WREG IORLW k Inclusive OR literal with WREG LFSR f, k Move literal to FSRn MOVLB k Move literal to BSR MOVLW k Move literal to WREG MULLW k Multiply literal to WREG RETLW k Return, place literal in WREG SUBLW k Subtract WREG from literal XORLW k Exclusive OR literal with WREG f = File Register, k = literal value (8-bit), b = bit address (0-7) Microchip Technology Taiwan Slide 41

PIC18 系列 MCU 指令集 Summary (16-bit core) Control Operations Control Operations BC n Branch if Carry (n is 8-bit) BN n Branch if Negative (n is 8-bit) BNC n Branch if No Carry (n is 8-bit) BNN n Branch if Not Negative (n is 8-bit) BNOV n Branch if No Overflow (n is 8-bit) BNZ n Branch if Not Zero (n is 8-bit) BOV n Branch if Overflow (n is 8-bit) BRA n Branch (n is 11-bit) BZ n Branch if Zero (n is 8-bit) CALL n, s Call subroutine (n is 20-bit) CLRWDT - Clear Watchdog Timer DAW - Decimal Adjust WREG GOTO n Go to address (n is 20-bit) NOP - No Operation POP - Pop top of return stack (TOS) PUSH - Push top of return stack (TOS) RCALL n Relative call (n is 11-bit) RESET - Software device reset RETFIE s Return from interrupt enable RETLW k Return, place literal in WREG RETURN s Return from subroutine SLEEP - Go into standby mode n = absolute address or address offset, k = literal value (8-bit), s = fast restore Microchip Technology Taiwan Slide 42

Byte-Oriented Operations ADDWF f, d, a ADDWFC f, d, a ANDWF f, d, a CLRF f, a COMF f, d, a CPFSEQ f, a CPFSGT f, a CPFSLT f, a DECF f, d, a DECFSZ f, d, a DCFSNZ f, d, a INCF f, d, a INCFSZ f, d, a INFSNZ f, d, a IORWF f, d, a MOVF f, d, a MOVFF fs, fd MOVWF f, a PIC18 系列 MCU 指令集範例 以位元組為操作的對象 16-bit Instruction for Byte Oriented Operations OP CODE d = Destination Bit d = 0 運算結果置於 W d = 1 運算結果置於 F (Default d=1) a = Access Bit a = 0 指定置於 Access Bank 的暫存器 (Default a=0, 或不寫 ) a = 1 實際的位址由 BSR 與 8-bit 的暫存器位址組成 d a f f f f f f f f f = 8-bit 暫存器位址 Example: ADDWF 0, 0 ADDWF f, d, a Microchip Technology Taiwan Slide 43

Bit-Oriented Operations BCF BSF BTG BTFSC BTFSS f, b, a f, b, a f, b, a f, b, a f, b, a PIC18 系列 MCU 指令集範例 以位元組為操作的對象 16-bit Instruction for Bit Oriented Operations OP CODE b b b b = 3-Bit, 用來指定欲操作位元的位址 (Bit Number) a = Access Bit a = 0 指定置於 Access Bank 的暫存器 a = 1 實際的位址由 BSR 與 8-bit 的暫存器位址組成 Example: BTFSC STATUS, C, 0 BTFSC FLAGS, BIT_0, a a f f f f f f f f f = 8-bit 的暫存器位址 Microchip Technology Taiwan Slide 44

PIC18 系列 MCU 指令集 Data Memory <> Program Memory Table Pointer Operations TBLRD* - TBLRD*+ - TBLRD*- - TBLRD+* - TBLWT* - TBLWT*+ - TBLWT*- - TBLWT+* - 16-bit Instruction for TBLRD* or TBLWT* OP CODE nn = 00 * ( 指標不變 ) nn = 01 *+ ( 查表後指標加一 ) nn = 10 *- ( 查表後指標減一 ) n n nn = 11 +* ( 先將指標加一再查表 ) Microchip Technology Taiwan Slide 45

PIC18FXXX 加強的部份 相當有彈性的資料指標 3 個資料指標, 各有五種操作模式 更方便且有效率的 Table Read/Write 功能 以 Byte 為操作單位 多種的指標更新選項 用簡單的指令集來支援複雜的架構 新的 MOVFF,BRA, 以及條件式分支指令 Microchip Technology Taiwan Slide 46

PIC18FXXX 加強的部份 資料指標 ( Data Pointer ) 總共有三個獨立的指標 FSR0, FSR1, FSR2 指標為 12 Bits, 可定址 4K 的資料空間 指標有兩個暫存器組成 ( 例 : FSR0L, FSR0H) 五種指標的操作方式 ( 使用不同的虛擬暫存器 ) INDFn : 直接操作指標所指的內容 POSTINCn : 操作指標所指內容後將指標加一 POSTDECn : 操作指標所指內容後將指標減一 PREINCn : 將指標先加一後再對指標所指位址操作 PLUSWn : 將 WREG 的內容作為 Offset 值與 FSRn 相加後以其結果為指標來存取記憶體 Microchip Technology Taiwan Slide 47

PIC18Fxxxx I/O 介紹與運用 Microchip Technology Taiwan Slide 48

PIC18 系列的周邊 : I/O Ports 輸入 / 輸出腳, 為一般 MCU 最 基本的工作 PIC18 系列的 I/O 多了一個 LATx, 可有效解決所謂 Read- Modify-Write Wit 指令動作帶來 的困擾 每個 I/O Port 皆有三個獨立 的控制暫存器 PORTx LATx TRISx Microchip Technology Taiwan Slide 49

I/O 的輸出入暫存器 Internal Data Bus Write PORTx Write LATx Read LATx Read PORTx LATx 拴鎖暫存器或 PORTx 輸出暫存器 PORTx I/O Pins Microchip Technology Taiwan Slide 50

為什麼加入 LATx 拴鎖暫存器 1. 如果工作在較低頻率或較小的負載電容環境 BSF PORTx,PINy Q4 BSF PORTx,PINz Q1 Q2 Q3 電壓的呈現在 PORTx,PINy V IL t Read-Modify-Write 操作時的取樣點 2. 如果工作在較高頻率或較大的負載電容環境 BSF PORTx,PINy BSF PORTx,PINz Q4 Q1 Q2 Q3 Q4 電壓的呈現在 PORTx,PINy V IL 此時電壓還沒升到 V IL 轉態點 t Read-Modify-Write 操作時的取樣點 Microchip Technology Taiwan Slide 51

如何解決 Read-Modify Modify-Write 如果是對相同的 PORTx 連續操作 解決之道是在連續兩個 BSF/BCF 指令間加入適當個 NOP 指令 BSF PORTA,5 NOP BSF PORTA,0 使用 C18 則可加入 Nop( ) 的巨集 建議直接使用 LATx 的來取代 PORTx 的操作 這樣直接就對拴鎖暫存器操作, 而不是對外面的 PORT 操作也就沒有因電容所造成的延遲 BSF LATA,5 BSF LATA,0 Microchip Technology Taiwan Slide 52

I/O 輸出 / 輸入控制 I/O Pin 方向控制 TRISB 1 1 1 1 0 0 0 0 PORTB In In In In Out Out Out Out TRISx 的每一獨立位元可單獨去控制相對應 PORTx 裡的位元的資料方向 (Reset 的初始定義為輸入功能 ) 1 = 輸入, 0 = 輸出 Microchip Technology Taiwan Slide 53

類比或數位腳位? 有些接腳是類比輸入與數位共用同一隻接腳 Power-On 後這些接腳的預設值為 類比輸入 類比輸入有 ADC 輸入與電壓比較器輸入 ADCON1 暫存器用來設定腳位是類比輸入還是數位 I/O 通常我們不需如此多的 AD Input Channel!! Bit 7 Bit 0 - - X X 1 1 1 1 VCFG1 VCFG0 PCFG3 PCFG2 PCFG1 PCFG0 設定最低 4 bits 為 1 時, 關閉所有 ADC 輸入腳功能 Microchip Technology Taiwan Slide 54

內含多功能程式編輯器 MPLAB IDE - 整合式的發展環境 MPLAB Integrated Development Environment 單一系統專案管理模式 語言工具軟體模擬模擬器及除錯器燒錄器 MPASM Assembler MPLAB SIM Sofware Simulator MPLAB REAL ICE PICkit 1 PICSTART Plus 原始檔案程式偵錯功能 協力廠商支援工具 Compilers IAR, Hi-Tech, CCS, ME Labs, Green Hills MPLINK MPLIB MPLAB ICE 2000 & 4000 MPLAB PM3 Real-time Operating Systems CMX, Vector, Realogy, Express Logic MPLAB C18 MPLAB C30 MPLAB C32 PICkit 2 MPLAB ICD 2 MATLAB Live Devices, CMX, Momentum Data Systems Uniquely supporting 8, 16 and 32 bit MCUS within one integrated development Environment! Microchip Technology Taiwan Slide 55

MPLAB IDE 的下載 由 Microchip 的首頁即可連結到 MPLAB IDE 的下載中心 It s free Microchip Technology Taiwan Slide 56

現有硬體除錯工具 Features s/speed d/trace 舊版現有 PICkit 2 Full Speed HID USB, 1 Device Family per Loader, Run, Halt, SS, Program, Read Full Speed Emul Select Devices MPLAB ICE 2000 MPLAB Parallel, REAL ICE USB Option High Speed Custom USB, Run, Halt, SS, Run, Halt, SS Complex Complex Ti Trigger/Break, Trigger/Break, Static Trace, Real Time Watch, Logic Probes, Stopwatch, Flexible circuit cable MPLAB ICD 2 Program, Read, 20 MIPS Emul Serial, Full Speed Dynamic Trace, PIC10F, 12F, 16C 2, Custom USB, Logic Probes, PIC16F, PIC17C 2, Run, Halt, SS, PIC18C LVDS 2, PIC18F Complex Full Speed Emul Trigger/Break PIC16F 1, PIC18F, Stopwatch, PIC24, dspic Program, Read Full Speed Emul PIC10F, 12F, 16F PIC18F, PIC24, dspic $50 $150 $500 $1500 Microchip Technology Taiwan Slide 57

新推出的硬體除錯工具 Feature es/spee ed/trace e PICkit 3 Full Speed USB HID, Run, Halt, SS, Simple Breakpoints, Program, Read All of Microchip s Flash PIC MCU and dspic DSCs MPLAB ICD 3 High Speed USB 2.0, Run, Halt, SS Software Breakpoints, Complex Breakpoints, Stopwatch, t Program, Read, All of Microchip s Flash PIC MCUs and dspic DSCs MPLAB REAL ICE Emulator High Speed USB 2.0, Run, Halt, SS Software Breakpoints, Complex Breakpoints/Triggers, Stopwatch, Program, Read, Real-Time Watch, Trace, Logic Probes, Performance Pak All of Microchip s Flash PIC MCUs and dspic DSCs $70 $220 $500 Microchip Technology Taiwan Slide 58

串列燒錄與除錯功能 In-Circuit Serial Programming & Debugging 燒錄時只用到兩支腳位 ( 請勿接電阻到地 ) RB6 PGC RB7 PGD Vpp 會有 13V 的觸發電壓輸入, 要使用 10K ohm 電阻接 Vdd, 最好不要再接電容到地以免影響 Vpp 上升時間造成燒錄錯誤 這種燒錄介面可以使用在 Microchip 所有的開發工具裏 ICD2, ICD3, Real ICE PICkit2, PM3 Pin V PP V DD V SS RB6 RB7 Application PCB MCLR/V PP PIC C18Fxxxx Function Programming Voltage = 13V Supply Voltage Ground Clock Input Data I/O & Command Input V DD V SS RB6 RB7 To application circuit V DD V DD Isolation circuits ICSP Connector Microchip Technology Taiwan Slide 59

Debugger/Programmer g 的界面與 Tool 的連接 Microchip MCU 使用 ICD/ICSP 界面與 Debugger/Programmer 連接 PICkit 2 與 PICkit 3 的介面定義如下 Microchip Technology Taiwan Slide 60

Debugger/Programmer g 的界面與 Tool 的連接 MPLAB ICD2/ICD3 使用的 RJ-11 連接器上的接腳與信號位置 ( 使用上簧片之 6P6C ) Microchip Technology Taiwan Slide 61

Debugger/Programmer g 的界面與 Tool 的連接 MPLAB ICD2/ICD3 隨附的 Modular Cable 示意圖 ( 注意 6P6C 排線上的順序兩端是相反的 ) Microchip Technology Taiwan Slide 62

Debugger/Programmer g 的界面與 Tool 的連接 自行 Layout 時, 若採用市售常見下簧片的 6P6C Connector, 與 MPLAB ICD2/ICD3 間的信號關係如下 ( 零件的 Layout 圖正面 ) Microchip Technology Taiwan Slide 63

MPLAB IDE 畫面 工具圖示區 C 的原始程式 工作項目 設定中斷點 編譯輸出 暫存器顯示 變數觀察視窗 Microchip Technology Taiwan Slide 64 狀態顯示列

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 Low frequency crystal oscillator 5kHz - 200kHz RC External RC oscillator DC - 4MHz RCIO External RC oscillator, OSC2=RA6 DC - 4MHz INTRC Internal RC oscillator Various EC External Clock, OSC2=f osc /4 DC - 40MHz ECIO External Clock, OSC2=RA6 DC - 40MHz 提供多樣的振盪器選擇方式, 讓設計更加彈性 : LP 振盪模式提供了低功耗功能 RC 或 INTRC 振盪模式提供了低成本的效益 XT 提供一般使用的工作頻率 HS 提供需高速處理所需的工作頻率 速度範圍參考資料手冊所建議的範圍 Microchip Technology Taiwan Slide 65

設定 Configuration Word 的方式 直接在 MPLAB IDE 下設定 先確定 MPLAB IDE 使用的元件名稱 MPLAB IDE 目錄下 : Configure Configuration Bits 取消 Configuration Bits Set in code Microchip Technology Taiwan Slide 66

組合語言必需要使用的虛擬指令 LIST - 目錄控制 (Listing Control) list p=pic18f4520 #INCLUDE - 加入一原始檔 定義檔或敘述檔 #include <18f4520.inc> EQU - 宣告常數 變數 ( 不可重新定位 ) memory equ count 0x3f equ.100 io_set equ B 11000011 ORG - 設定程式組譯的起始位址 org 0x00 ; 組譯位址從 00h 開始 org 0x30 ; 組譯位址從 30h 開始 END - 程式結束 Microchip Technology Taiwan Slide 67

虛擬指令的使用範例 list #include p=18f4520 ; 定義使用的 MCU 為 PIC18F4520 <p18f4520.inc> ; 使用 18F4520 的標準定義檔 ;***** 定義變數 ` 常數 參數區 ****** T_DELAY EQU D 100 ; 設定常數值 dly_count EQU H 00 ; 設定變數位址 ;********************************************************************** ORG 0x000 ; 設定程式執行位址從 0000" 開始 clrf PCLATU clrf PCLATH goto main ; ORG 0x00080008 ; 高優先權中斷向量進入位址 ;******* 中斷處理副程式區 ****** retfie s ; 中斷返回 ; main movlw T_DELAY ; 主程式開始 movwf dly_count ;** remaining code goes here END ; 程式結束 Microchip Technology Taiwan Slide 68

MPASM 的範本程式 安裝了 MPLAB IDE 後, MPASM 也一併安裝 目錄 : C:\Program Files\Microchip\MPASM Suite\ 在此目錄可找到各 PIC16F & PIC18F 的組合語言範本程式 C:\Program Files\Microchip\MPASM Suite\Template\Code C:\Program Files\Microchip\MPASM coc Suite\Template\Object e Code 子目錄的寫法為 單一 組合語言程式的範本 一個 Project 只包含 1 個.asm 的檔案, 定址都用 ORG & EQU Object 子目錄的寫法為 Relocatable code 的組合語言程式的範本 一個 Project 只包含多個.asm 的檔案 使用 CODE, RES 等 Directives 來定址 Microchip 提供 102ASP (W400), 一天的訓練課程 Microchip Technology Taiwan Slide 69

須另外安裝的語言工具 MPLAB C18 可由 www.microchip.com 免費下載 也可在 Microchip Taiwan 提供的 RTC 及產品 DVD 光碟找到 提供完整的 Peripheral Library, 讓周邊的操控更簡便 Microchip 提供 W401, 兩天的訓練課程 可在 www.microchip.com.tw 的下載中新下載講義及練習檔 也可在 Microchip Taiwan 提供的 RTC 及產品 DVD 光碟找到 搭配 MPLAB IDE 可以做到 Source Level Debugging Microchip Technology Taiwan Slide 70

9V 電源輸入 RS-232 認識 APP001 實驗板 CAN RS-485 AD 輸入 VR1 ICD2 接頭 Reset 按鍵 Debug & Prog. 選擇 Jumper 5V 電源穩壓 I2C & SPI EEPROM CCP1 蜂鳴器 PI IC18F4 4520 CCP2 PWM LED PORTD LED x8 2 x 16 LCD 外部震盪器插座 LCD 反襯度調整 溫度感測器 Microchip Technology Taiwan Slide 71 16MHz Crystal 按鍵開關 SW3 & SW2

APP026-3X 實驗板 Microchip RTC 課程及菁英班課程的專用實驗板 可以搭配 64 to 44 Pin 的轉接板, 使用來開發 PIC18F4520 Microchip Technology Taiwan Slide 72

APP026-3X 內建的 PICkit 2 相容 module 位置在 LCD Display 的下方 功能與 PICkit 2 相容 無法提供 Power 至 Target Board 沒有 EEPROM 24LC512 * 2, 所以無 Programmer to go 功能 24LC512 可自行加入 有 PICkit 2 相容的單排 2.54mm * 6-Pin 與 ICD 2 相容 RJ-11 Connector 兩種除錯及燒錄介面 Microchip Technology Taiwan Slide 73

PICKit 2 的重要功能 與 MPLAB ICD 2 類似, 可以搭配 MPLAB IDE 來對 Microchip Flash MCU 進行燒錄與除錯 只要將 Programmer or Debugger 的選擇選取 PICKit 2 即可 大部分的 PIC16F, PIC18F 大部分的 PIC24FJ, PIC24HJ & dspic33fj 大部分的 dspic30f 使用 HID Device Class, 安裝及使用比 MPLAB ICD 2 容易 操作介面及方法與 ICD 2 一致 Microchip Technology Taiwan Slide 74

使用 APP026-3X 實驗板的範例 Microchip Technology Taiwan Slide 75 APP026-3X 主要目的在提供一個穩定的基礎電路給 User 要使用的 CPU APP026-3X 只將 Power 及 Debug/Program 所需信號接妥 User 可依時際需求將 CPU 的功能 Pin 腳接至實驗板上的周邊 ( VR, Switch, LED, RS-232 收發器 ) 若 On-Board 的周邊功能不足, 可輕易地以杜邦跳線連接其他的控制板 APP026-3X 的電路與 APP026-3 相同, 說明書可於 Microchip 台灣網站下載

APP026-3X 重要的 Connectors APP026-3X 的設計原責 : 僅將必要信號定位, 其餘交由 User 自定 VR1 & VR2 的插針 JP2- Vdd 電壓選擇 (3.3V/5V) JP1-5V Source 5V 電壓的來源 JP5&JP6 CPU 選擇 JUMPER CON1-9V DC 插孔 JP20 - USB 信號 CON4-LCD 模組的控制信號 64-Pin CPU 模組 X2 32.768Hhz 內建 PICKit 2 的 USB Connector 內建 PICKit 2 module 的 Debug 輸出 Debug 腳位選擇 ICD2 & PICKit 2 Connector Microchip Technology Taiwan Slide 76 JP8-ENVREG for PIC18J CON11 4.7K Pull-up * 4 按鍵 S1-S4 X1 8Mhz 震盪器 JP19- 按鍵及 LED 的插針 LED1 LED4

使用 APP026-3X 的注意事項 - 電源 接上電源前需確認 JP2 VDD 的選用是正確的 ( 3.3V or 5V) 不同的 MCU 有不同的工作電壓範圍, Power-On 前先確定電源設定是對的 Microchip Technology Taiwan Slide 77

使用 APP026-3X 的注意事項 - 電源 APP026-3 不管 JP2 VDD 設為 3.3V or 5V, 都需要一個 5V 的周邊電壓被產生, 因為 33V 3.3V 是經由 MCP170X LDO 由 5V 輸入獲得 33V 3.3V 輸出 使用 JP1 5V-SOURCE 來選擇周邊用 5V 的來源為 USB or 外接電源經 7805 而來 Microchip Technology Taiwan Slide 78

使用 APP026-3X 的注意事項 - 電源 若以 JP1 5V-SOURCE 來選擇周邊用 5V 的來源為 USB, 則 APP026-3X 的 CON15-USB 必需被連上 PC 的 USB Port 或 USB connector 相容的供電設備 Microchip Technology Taiwan Slide 79

使用 APP026-3X 的注意事項 電源 若以 JP1 5V-SOURCE 來選擇周邊用 5V 的來源為外接電源經 7805, 則 APP026-3X 的 CON1 必需被連上外接的 9V DC Power Microchip Technology Taiwan Slide 80

使用 APP026-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

使用 APP026-3X 的注意事項連接 PICKit 2 APP026-3X 上的 PICKit 2 工具使用 HID 類別, 無需安裝 Driver!! 請使用隨附的 USB 線將 PC 與 APP026-3X 連接 CON19 與 CON19A 是並聯的, 都可用來 Debug 請用隨附的 6P 電話線將 CON19A 與 CON12 連接 參考前幾頁的電源設定來選擇要用何種供電方式 Microchip Technology Taiwan Slide 82

使用 APP026-3X 的注意事項 Debugger Pin 腳的選擇 如果使用 CON18-USB 的 5V VBUS 來供電, 則基本連線將如下圖 APP026-3X 隨附的是 PIC24FJ128GB106 64-pin TQFP 包裝的 MCU PIC24FJ128GB106 有 3 組 Debug/Program 腳位可選, 而 APP026-3X 有將第 1, 2 組引出 請將 JP15 &JP16 的 JUMPER 插上, 以便使用 PGEC1/PGED1 來 Debug.. Microchip Technology Taiwan Slide 83

在 APP026-3X 使用 PIC18F4520 PIM APP026-3X 可以使用 PIC18F4520 的 PIM ( Plug-In Module ) PIC18F4520 與 LCD 的連線已完成 RD0.. RD3 接至 LCD Data RD4 & RD5 接至 LCD 的 RS 及 RW 接腳 RA2 被接至 LCD 的 E 腳 PIC18F4520 的 Power, ICSP 連線亦已經完成 可以用 JP2 選 3.3 or 5V 的 Vdd PIC18F4520 的 ICSP 連線被接至 APP026-3X 原有 64-pin 腳位的 Pin-15 & Pin16 其它的腳位皆為 available, User 可以自行決定如何運用 Microchip Technology Taiwan Slide 84

在 APP026-3X 使用 PIC18F4520 PIM APP026-3X 使用 PIC18F4520 連接外部按鍵與 LED 的範例 Microchip Technology Taiwan Slide 85

PIC18F4520-I/PT I/PT 的腳位圖 Microchip Technology Taiwan Slide 86

Demo 1 : I/O 的操作 使用組合語言, 使程式能依按鍵的選擇來控制 LED 使用 MPASM 提供的範本程式為基礎, 練習組合語言 練習使用 APP026-3X 上的 PICkit 2 來 Debug/Program 設定 RB0, RA4 為輸入 設定 PORTD 為輸出 ( RD0.. RD3 驅動 LED1 ~ LED4 ) 按 S1 (RB0) 時, 點亮 LED1 LED2 按 S2 (RA4) 時, 點亮 LED3 LED4 使用中斷點來觀查程式的運作 Microchip Technology Taiwan Slide 87

Demo 2: 使用 Timer1 與中斷 使用 MPLAB C18 來撰寫 Demo 2 讓 Timer1 使用內部經倍頻後的 40 Mhz 為 clock 並啟動 Timer1 的中斷 : 設定 PIC18F4520 的工作模式後, 進入主程式的永久迴圈 檢查 ONE_SEC 旗標, 若為 1 則顯示 MM:SS 於 LCD 第一行 將 Timer1 設定為 10 ms 秒中斷 CPU 一次 Timer1 每次中斷後, 判斷是否已累積達 1 秒的時間後 Update Min, Sec 變數, 並設定 ONE_SEC 旗標 Microchip Technology Taiwan Slide 88

Demo 3:ADC 的操作 承接 Demo 2, 但在主程示迴圈不斷的讀取 VR1 & VR2 的 ADC 讀值顯示於 LCD 第二行 將 ADC 規劃為 : AN0 & AN1 enabled Right Justified Internal Reference Proper TAD and TACQ LCD 若 update 太快會有閃爍的現象, 所以要以程式處理 每 100 ms 才 update LCD 的 ADC 讀值 AN0 @ Pin-19, AN1 @ Pin-20 Microchip Technology Taiwan Slide 89