701PIC Introduction to PICMicro MCUs

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

PIC16F F MPLAB 08 16F LED 15 LED

文件名

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

Microsoft PowerPoint - 701_PIC-CHN_Attendee.ppt

ICD ICD ICD ICD ICD

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

AN579

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

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

目 录

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

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

1.ai

33023A.book(31005A_cn.fm)

EK-STM32F

目录

2 PIC PIC 1 / CPU PIC MCU PIC RC

Section1_16bit Arc.ppt

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

Microsoft Word - S153B.doc

33023A.book(31006A_cn.fm)

Microsoft Word - MSP430 Launchpad 指导书.docx

AL-M200 Series

untitled

1 TPIS TPIS 2 2

Bus Hound 5

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

Tel:

C语言的应用.PDF

DPJJX1.DOC

untitled

TX-NR3030_BAS_Cs_ indd

BC04 Module_antenna__ doc

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

<4D F736F F F696E74202D B5A5C6ACBBFAD3EBB5E7D7D3CFB5CDB3C9E8BCC62E707074>

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

Microsoft PowerPoint - STU_EC_Ch08.ppt

IP505SM_manual_cn.doc

Windows XP

audiogram3 Owners Manual

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

2/80 2

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

CANVIO_AEROCAST_CS_EN.indd

Guide to Install SATA Hard Disks

Microsoft PowerPoint - ATF2015.ppt [相容模式]

Microsoft PowerPoint - C15_LECTURE_NOTE_04.ppt

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

P4V88+_BIOS_CN.p65

epub83-1

68369 (ppp quickstart guide)

PIC16F87X基本指令說明

P4VM800_BIOS_CN.p65

bingdian001.com

(Guangzhou) AIT Co, Ltd V 110V [ ]! 2

学习MSP430单片机推荐参考书

Microsoft PowerPoint - C15_LECTURE_NOTE_04.ppt

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

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

目录 1. 产品简介 功能特性 引脚图 引脚描述 中央处理器 (CPU) 程序存储器 (OTP ROM) 堆栈 数据存储器 (RAM) INDF 寄存器.

P3B-F Pentium III/II/Celeron TM

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

Microsoft Word - template.doc

33023A.book(31001A_cn.fm)

1 CPU

K7VT2_QIG_v3

USB解决方案.ppt

<4D F736F F D C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63>

PROTEUS VSM

Microsoft PowerPoint - C15_LECTURE_NOTE_05.ppt

穨control.PDF

: 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//

PIC PICC C 4K C <10% C PIC 11.3 MPLAB-IDE PICC PICC MPLAB-IDE MPLAB-IDE ICE2000 ICD2 PICC MPLAB-IDE IDE Project Set Language Tool Locations

Á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

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

<A1AAA1AA475053B4AEBFDACAFDBEDDBDD3CAD5B3CCD0F2CAB5C0FD>

行业

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

DESCRIPTION

FM1935X智能非接触读写器芯片

KDC-U5049 KDC-U4049 Made for ipod, and Made for iphone mean that an electronic accessory has been designed to connect specifically to ipod, or iphone,

PICkit2 LPC

WebSphere Studio Application Developer IBM Portal Toolkit... 2/21 1. WebSphere Portal Portal WebSphere Application Server stopserver.bat -configfile..

Edge-Triggered Rising Edge-Triggered ( Falling Edge-Triggered ( Unit 11 Latches and Flip-Flops 3 Timing for D Flip-Flop (Falling-Edge Trigger) Unit 11

Abstract arm linux tool-chain root NET-Start! 2

2_dvdr3380_97_CT_21221b.indd

Epson

untitled

12232A LED LED LED EL EL CCFL EL CCF

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

DR2010.doc

PowerPoint Presentation

安全防范

行业

PCM-3386用户手册.doc

untitled

ebook140-9

Transcription:

701 PIC PICmicro MCU 介绍 PIC16 系列产品的汇编编程 HANDS-ON Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 1

日程 讲述部分 讲述 90 分钟 休息 20 分钟 实验部分 1 2 个实验 60 分钟 休息 60 分钟 实验部分 2 10 4 个实验 2 小时 休息 20 分钟 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 2

讲述部分的内容 架构 存储器 指令集 开发软件 (MPLAB IDE) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 3

实验和演示部分的内容 Labs 在 MPLAB IDE 创建项目调试和跳转控制 PWM A/D 中断 演示 ( 时间允许的话 ) PIC16F877A 实现多任务 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 4

架构 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 5

Microchip PIC MCU 金字塔 16 bit MCU dspic 24 bits 数据存储器宽度 PIC18 16 bits 程序存储器宽度 8 bit MCU PIC16 PIC12F 14 bits PIC16C5X PIC12C5XX 12 bits Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 6

PICmicro Architecture RISC-like Features PIC 单片机之所以有很高的性能是因为其具备如下特性 : 内部为哈佛结构 指令流水线操作 寄存器文档 长字型指令 大多数单指令周期 指令数很少 指令实现的功能基本不重复 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 7

冯 - 纽曼结构 哈佛结构 程序存储器 CPU 8- 位 CPU 12/14/16- 位 程序和数据存储器 8- 位数据存储器 PIC 单片机架构 哈佛结构 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 8 从同一存储器空间取指令和取操作数据. 限制了数据流量 从两个独立的存储空间分别取指令和存取操作数. 数据流量增加针对程序区和数据区可以设计不同的数据线宽度

PIC 单片机架构 指令流水线 大部分单片机, 其取指和执行过程是顺序进行的. 指令流水线的引入允许取指和执行可以同步进行. 使得指令可以在一个指令周期内执行. 程序分支例外 ( 如 GOTO, CALL 或直接修改 PC), 这需两个指令周期. Tcy0 Tcy1 Tcy2 Tcy3 Tcy4 1. MOVLW 55h Fetch 1 2. MOVWF PORTB 3. CALL SUB_1 4. BSF PORTA, BIT3 Execute 1 Fetch 2 Execute 2 Fetch 3 Execute 3 Fetch 4 Flush Fetch 4 Fetch SUB_1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 9

ALU W Register 数据存储器 PIC 单片机架构 INDF TMR0 PCL STATUS FSR PORTA Other SFRs 寄存器文档概念 RAM 被看作是一组通用的寄存器. 周边模块 (I/O) 也作为寄存器. 所有的指令操作都可针对所有的寄存器. 长字指令使得在指令中直接寻址寄存器. 14- 位字长的指令范例 : 操作码 <7> General Purpose Registers (RAM) 直接数据地址 <7> Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 10

PICmicro 架构 指令实例 PIC MCU 指令编码为操作码和参数编码用一个字完成 立即数类指令 OP CODE k k k k k k k k Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 11

PIC MCU 晶振 指令率是输入时钟的 1/4 f osc PIC MCU 输入晶振频率叫 4 MHz (250 ns) 做 f osc T cy 一个指令周期占用 1 T cy 1 MHz (1 us) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 12

存储器 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 13

PICmicro 架构 存储器 两种类型 程序 (RAM 和 EEPROM) 组成 Pages - 页 ( 程序存储器 ) Banks 组 ( 数据存储器 ) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 14

PICmicro 架构 存储器表 PICmicro MCU FLASH 程序存储器 大小 (words) RAM 数据存储器 GPR 大小 (bytes) PIC12F675 1 K 64 PIC16F877A 8 K 368 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 15

分页的程序存储器 (14bit 核 ) CALL or GOTO instruction OP CODE k k k k k k k k k k k 3 bits for instruction 11 bits for destination 在 14-bit 核的器件中 : 11 位访问 2 或 2048 (2k) 空间 访问更多的空间, 需要更多的位 程序存储器的分页提供这些位 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 16

14-bit 核架构 程序存储器 PCLATH<4:3> = 00 Reset Interrupt Page 0 PCLATH<4:3> = 01 0000h 0004h 07FFh 0800h PCLATH<4:3> = 10 Page 2 PCLATH<4:3> = 11 1000h 17FFh 1800h 程序存储器最大 8K 字四页, 每页 2k 使用 PCLATH<4:3> 访问页复位地址 0000h 中断地址 0004h Page 1 Page 3 0FFFh 1FFFh Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 17

PICmicro 架构 程序存储器 : PC 绝对寻址 用 CALL 和 GOTO 指令修改 PC PCLATH Register x x x k k x x x 14-bit Instruction for call and goto OP CODE k k k k k k k k k k k 2-bits From PCLATH k k k k k k k k k 有效的 13-bit 程序存储器地址 11-bits From Instruction k k k k Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 18

PICmicro 架构分页的程序 分页的程序存储器 当执行 CALL or GOTO 指令时, 需考虑分页 当跳转到不同的页时, 修改页位 调用页位 : GOTO < 地址 > CALL < 地址 > <Instruction> PCL,F ; e.g. ADDWF PCL,F 返回时不需考虑页 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 19

PICmicro 架构 PC 相对寻址 (14-bit core) 高字节写入 PCLATH. 然后低字节写入 PCL, 将装入 13-bit 值到 PC. Internal Data Bus <8> movlw movwf movlw movwf Note: PCH cannot be read HIGH Delay PCLATH LOW Delay PCL 5 PCLATH <5> 5 PCH <5> PCL <8> 13-Bit Program Counter 8 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 20

分组的数据存储器 (14 bit core) Data Modifying Instruction OP CODE f f f f f f f 7 位指令编码 7 位目标地址 在 14-bit 核的器件中 : 7 位访问 2 7 或 128 空间访问更多的空间, 需要更多的位 程序存储器的分组提供这些位 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 21

PICmicro 架构 数据存储器 数据存储器 : 直接寻址 低 7 位的数据地址直接在指令中描述高 2 位在 STATUS 寄存器中 ( 又称寄存器组号 BANK) STATUS 寄存器 14 位长指令 IRP RP1RP0 RP0 TO PD Z DC C OP CODE f f f f f f f 2 位来自 STATUS 寄存器 RP1 RP0 f f f f f f f 7 位来自指令 指向目标寄存器的 9 位有效地址 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 22

PICmicro 架构 数据存储器组成 Bank0 SFRs 000h 01Fh 020h Bank1 SFRs 080h 09Fh 0A0h Bank2 SFRs 100h 11Fh 120h Bank3 SFRs 180h 19Fh 1A0h 分成 4 个组 SFR 映射到最顶端 32 位置通过 RP0,1 和 IRP 选择组 GPRs GPRs GPRs GPRs 07Fh 0F0h 0FFh 170h 17Fh 1F0h 1FFh RP<1:0> RP<1:0> RP<1:0> RP<1:0> = 00 = 01 = 10 = 11 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 23

File Address (hex) File Address (hex) 00 Indirect addr. (1) Indirect addr. (1) 80 01 TMR0 OPTION_REG 81 02 PCL PCL 82 03 STATUS STATUS 83 04 FSR FSR 84 05 GPIO TRISIO 85 PICmicro 架构数据存储器 0A PCLATH 0B INTCON 0C PIR1 0D 0E TMR1L 0F TMR1H 10 T1CON 15 16 PCLATH INTCON PIE1 PCON OSCCAL WPU IOC 8A 8B 8C 8D 8E 8F 90 95 96 寄存器图 PIC12F675 Unimplemented data memory location; read as '0'. Special Function Registers (SFR). General Purpose Registers (GPR), user RAM. 1 Not a Physical Register. 19 1A 1B 1C 1D 1E 1F 20 5F CMCON ADRESH ADCON0 General Purpose Registers (64 Bytes) VRCON EEDATA EEADR EECON1 EECON2 (1) ADRESL ANSEL accesses 20h-5Fh 99 9A 9B 9C 9D 9E 9F A0 DF Bank 0 Bank 1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 24

PICmicro MCU 架构 PC 相对寻址 movlw HIGH Decode movwf PCLATH movf DisplayValue,W call Decode movwf PORTB goto Continue Decode addwf PCL,F retlw B 00111111 ;decode 0 retlw B 00000110 ;decode 1 retlw B 01011011 ;decode 2 retlw B 01001111 ;decode 3 retlw B 01100110 ;decode 4 retlw B 01101101 01101101 ;decode 5 retlw B 01111101 ;decode 6 retlw B 00000111 ;decode 7 retlw B 01111111 ;decode 8 retlw B 01101111 ;decode 9 Continue 实现查找表范例 RB0 RB1 RB2 RB3 RB4 RB5 RB6 XXXXXXXX 01101101 00000101 W Register a b c d e f g f e a g d 00000000 01101101 I/O Port B b c Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 25

PICmicro 架构 间接寻址 用 FSR 寄存器作为 8 位地址指针 另外 1 位在 STATUS 中描述 STATUS 寄存器 r 8 位 FSR 寄存器 1 位来自 STATUS 寄存器 IRP RP1 RP0 TO PD Z DC C f f f f f f f f IRP f f f f f f f f 8 位来自 FSR 有效的 9 位地址 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 26

PICmicro 架构 间接寻址 FSR 和 INDF 用于间接寻址 FSR 是地址指针 INDF 保存移动的值 Writing Reading 22 FSR 5 INDF 21 22 23 24 movlw 22 movwf FSR movlw 5 movwf INDF 5 22 FSR 5 INDF 21 22 23 24 movlw 22 movwf FSR movf INDF,W movwf 5 5 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 27

清除 0x20 to 0x7F 的 RAM. PICmicro 架构 间接寻址 00h 04h 20h 7Fh Data Memory INDF FSR = 20h 0000 0000 0000 0000 movlw 0x20 movwf FSR LOOP clrf INDF incf FSR,F btfss FSR,7 goto LOOP <next instruction> Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 28

指令系统 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 29

35 条指令 易学紧凑单字指令向上兼容 PICmicro MCU 指令集 (14-bit core) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 30

PICmicro 指令集 字节操作 NOP - MOVWF f CLRW - CLRF f SUBWF f,d DECF f,d IORWF f,d ANDWF f,d XORWF f,d ADDWF f,d MOVF f,d COMF f,d INCF f,d DECFSZ f,d RRF f,d RLF f,d SWAPF f,d INCFSZ f,d 14 位内核之字节操作指令构成 OP CODE d f f f f f f f d = Destination Bit d = 0 for destination W d = 1 for destination F f = 7-bit 7 Register Address Example: ADDWF ADDWF f, d REG, W Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 31

移动数据 : 立即数, W and F incf MyVar,F movwf MyVar MyVar 12 12 W or WREG movf MyVar,W movlw 1 File Registers 1 立即数 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 32

PICmicro 指令集 汇总 Byte-Oriented Operations ADDWF f,d Add W and f ANDWF f,d AND W and f CLRF f Clear f CLRW - Clear W COMF f,d Complement f DECF f,d Decrement f DECFSZ f,d Decrement f, skip if zero INCF f,d Increment f INCFSZ f,d Increment f, skip if zero IORWF f,d Inclusive OR W and f MOVF f,d Move f MOVWF f Move W to f NOP - No Operation RRF f,d Rotate right f through carry RLF f,d Rotate left f through carry SUBWF f,d Subtract W from f SWAPF f,d Swap nibbles of f XORWF f,d Exclusive OR W and f BCF BSF BTFSC BTFSS Bit-Oriented Operations f,b f,b f,b f,b Bit clear f Bit set f Bit test f, skip if clear Bit test f, skip if set Literal and Control Operations SLEEP - Go into standby mode CLRWDT - Clear watchdog timer RETLW k Return, place literal in W RETFIE - Return from interrupt RETURN - Return from subroutine CALL k Call subroutine GOTO k Go to address (k is 9-bit) 9 MOVLW k Move literal to W IORLW k Inclusive OR literal with W ADDLW k Add literal with W SUBLW k Subtract W from literal ANDLW k AND literal with W XORLW k Exclusive OR literal with W f = File Register, k = literal value (8-bit), b = bit address <0 to 7>, d = destination (W or file register) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 33

PICmicro 指令集 位操作 位操作 BCF f,b BSF f,b BTFSC f,b BTFSS f,b 14 位内核之位操作指令构成 OP CODE b b b f f f f f f f b = 3-Bit 3 Address (Bit Number) 0000 0000 f = 7-bit 7 Register Address Bit 7 Bit 0 例 : BTFSC BTFSC STATUS, C f, b Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 34

PICmicro 指令集 立即数和控制操作 立即数和控制操作 SLEEP - CLRWDT - RETLW k RETFIE - RETURN - CALL k GOTO k MOVLW k IORLW k ADDLW k SUBLW k ANDLW k XORLW k 14 位内核之立即数和控制指令构成 OP CODE k k k k k k k k k = 8-bit 8 Immediate Value 例 : MOVLW 0x2F MOVLW k Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 35

14-bit 单片机指令集 操作实例 NOP No Operation Syntax: NOP Operands: None Operation: No operation Status: None Encoding: 00 0000 0000 0000 Words: 1 Cycles: 1 例 : NOP Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 36

14-bit 核指令集 字节操作 Byte-Oriented Operations NOP - MOVWF f CLRW - CLRF f SUBWF f,d DECF f,d IORWF f,d ANDWF f,d XORWF f,d ADDWF f,d MOVF f,d COMF f,d INCF f,d DECFSZ f,d RRF f,d RLF f,d SWAPF f,d INCFSZ f,d 14 位内核之字节操作指令构成 OP CODE d f f f f f f f d = Destination Bit d = 0 for destination W d = 1 for destination F f = 7-bit 7 Register Address Example: ADDWF REG, W ADDWF f, d Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 37

PICmicro MCU 指令集 字节操作 MOVWF Move W to f Syntax: MOVWF f Operands: 0 <= f <= 127 Operation: (W) -> > (f) Status: None Encoding: 00 0000 1fff 1 ffff Words: 1 Cycles: 1 例 : MOVWF FSR 执行前 FSR = 0xFF W = 0x4F 执行后 FSR = 0x4F W = 0x4F Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 38

PICmicro MCU 指令集 字节操作 CLRW Clear W Syntax: CLRW Operands: None Operation: 00h -> > (W) 1 -> > Z Status: Z Encoding: 00 0001 0000 0000 Words: 1 Cycles: 1 例 : CLRW 执行前 W = 0x4F 执行后 W = 0x00 Z = 1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 39

PICmicro MCU 指令集 字节操作 CLRF Clear f Syntax: CLRF f Operands: 0 <= f <= 127 Operation: 00h -> > (f) 1 -> > Z Status: Z Encoding: 00 0001 1fff 1 ffff Words: 1 Cycles: 1 例 : CLRF FSR 执行前 FSR = 0x4F 执行后 FSR = 0x00 Z = 1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 40

PICmicro MCU 指令集 字节操作 SUBWF Subtract W from f Syntax: SUBWF f,d Operands: 0 <= f <= 127 d = {0,1} Operation: (f) - (W) -> dest Status: C,DC,Z Encoding: 00 0010 dfff ffff Words: 1 Cycles: 1 例 : SUBWF FSR,W 执行前 FSR = 0x03 W = 0x02 C =? Z =? 执行后 FSR = 0x03 W = 0x01 C = 1 Z = 0 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 41

PICmicro MCU 指令集 字节操作 DECF Decrement f Syntax: DECF f,d Operands: 0 <= f <= 127 d = {0,1} Operation: (f) - 1 -> dest Status: Z Encoding: 00 0011 dfff ffff Words: 1 Cycles: 1 例 : DECF FSR,F 执行前 FSR = 0x01 Z = 0 执行后 FSR = 0x00 Z = 1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 42

PICmicro MCU 指令集 字节操作 MOVF Move f Syntax: MOVF f,d Operands: 0 <= f <= 127 d = {0,1} Operation: (f) -> dest Status: Z Encoding: 00 1000 dfff ffff Words: 1 Cycles: 1 例 : MOVF FSR,F 执行前 FSR = 0x00 Z = 0 执行后 FSR = 0x00 Z = 1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 43

PICmicro MCU 指令集 字节操作 DECFSZ Dec. f, Skip if 0 Syntax: DECFSZ f,d Operands: 0 <= f <= 127 d = {0,1} Operation: (f) - 1 -> dest skip if result = 0 Status: None Encoding: 00 1011 dfff ffff Words: 1 Cycles: 1(2) 例 : Loop DECFSZ CNT,F GOTO Loop Continue 执行前 PC = address Loop 执行后 CNT = CNT - 1 if CNT = 0, PC = address Continue else CNT!= 0, PC = address Loop+1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 44

PICmicro MCU 指令集 字节操作 RRF Rotate Right f -> > C Syntax: RRF f,d Operands: 0 <= f <= 127 d = {0,1} Operation: Status: C Encoding: 00 1100 dfff ffff Words: 1 Cycles: 1 C Register f 例 : RRF CNT,W 执行前 CNT = 1110 0110 C = 0 执行后 CNT = 1110 0110 W = 0111 0011 C = 0 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 45

PICmicro MCU 指令集 位操作 BCF f,b BSF f,b BTFSC f,b BTFSS f,b 14 位内核之位操作指令构成 OP CODE b b b f f f f f f f b = 3-Bit 3 Address (Bit Number) f = 7-bit 7 Register Address Example: BTFSC BTFSC STATUS, C f, b Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 46

PICmicro MCU 指令集 位操作 BCF Bit Clear f Syntax: BCF f,b Operands: 0 <= f <= 127 0 <= b <= 7 Operation: 0 -> > (f<b>) Status: None Encoding: 01 00bb bfff ffff Words: 1 Cycles: 1 例 : BCF FSR,4 执行前 FSR = 0011 0000 执行后 FSR = 0010 0000 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 47

PICmicro MCU 指令集 位操作 BTFSS Bit Test f, Skip if 1 Syntax: BTFSS f,b Operands: 0 <= f <= 127 0 <= b <= 7 Operation: skip if (f<b>) = 1 Status: None Encoding: 01 11bb bfff ffff Words: 1 Cycles: 1(2) 例 : Here BTFSS CNT,2 False GOTO Elsewhere True 执行前 PC = address Here 执行后 if CNT<2> = 1, PC = address True if CNT<2> = 0, PC = address False Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 48

PICmicro MCU 指令集 立即数操作 立即数操作 MOVLW IORLW ADDLW SUBLW ANDLW XORLW k k k k k k 14 位内核之立即数操作指令构成 OP CODE k k k k k k k k k = 8-bit 8 Immediate Value 例 : MOVLW 0x2F MOVLW k Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 49

PICmicro MCU 指令集 立即数操作 MOVLW 立即数送给 W Syntax: MOVLW k Operands: 0 <= k <= 255 Operation: k -> > (W) Status: None Encoding: 11 0000 kkkk kkkk Words: 1 Cycles: 1 例 : MOVLW 0x5A 执行后 W = 0x5A Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 50

PICmicro MCU 指令集 立即数操作 ADDLW W 与立即数相加 Syntax: ADDLW k Operands: 0 <= k <= 255 Operation: (W) + k -> > (W) Status: C,DC,Z Encoding: 11 1110 kkkk kkkk Words: 1 Cycles: 1 例 : ADDLW 0x15 执行前 W = 0x10 执行后 W = 0x25 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 51

PICmicro MCU 指令集 立即数操作 ANDLW W 与立即数相与 Syntax: ANDLW k Operands: 0 <= k <= 255 Operation: (W) AND k -> > (W) Status: Z Encoding: 11 1001 kkkk kkkk Words: 1 Cycles: 1 例 : ANDLW 0x5F 执行前 W = 0xA3 执行后 W = 0x03 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 52

PICmicro MCU 指令集 控制类操作 控制类操作 SLEEP - CLRWDT - RETLW k RETFIE - RETURN - CALL k GOTO k 14 位内核之 RETLW 指令构成 OP CODE k k k k k k k k k = 8-bit 8 Immediate Value 14 位内核之 CALL /GOTO 指令构成 OP CODE k k k k k k k k k k k k = 11-bit Immediate Value Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 53

PICmicro MCU 指令集 控制类操作 SLEEP 进入 SLEEP Syntax: SLEEP Operands: None Operation: 00h -> > WDT 1 -> > TO 0 -> > PD Status: TO, PD Encoding: 00 0000 0110 0011 Words: 1 Cycles: 1 例 : SLEEP Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 54

PICmicro 指令集 控制类操作 CLRWDT 清 Watchdog 例 : Syntax: CLRWDT CLRWDT Operands: None Operation: 00h -> > WDT 0 -> > WDT prescaler 1 -> > TO 1 -> > PD Status: TO, PD Encoding: 00 0000 0110 0100 Words: 1 Cycles: 1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 55

PICmicro MCU 指令集 控制类操作 RETLW 返回 Syntax: RETLW k Operands: 0 <= k <= 255 Operation: k -> > (W) TOS -> > PC Status: None Encoding: 11 0100 kkkk kkkk Words: 1 Cycles: 2 例 : RETLW 0x5A 执行后 W = 0x5A Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 56

PICmicro MCU 指令集 控制类操作 RETFIE 中断返回 Syntax: RETFIE Operands: None Operation: TOS -> > PC 1 -> > GIE Status: None Encoding: 00 0000 0000 1001 Words: 1 Cycles: 2 例 : RETFIE Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 57

PICmicro MCU 指令集 控制类操作 CALL 调用子程序 Syntax: CALL k Operands: 0 <= f <= 2047 Operation: (PC) + 1 -> > TOS k -> > PC<10:0> PCLATH<4:3 -> PC<12:11> Status: None Encoding: 10 0kkk 0 kkkk kkkk Words: 1 Cycles: 2 例 : Here CALL There 执行前 PC = address Here 执行后 PC = address There TOS = address Here+1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 58

PICmicro MCU 指令集 控制类操作 GOTO 无条件转移 Syntax: GOTO k Operands: 0 <= f <= 2047 Operation: k -> > PC<10:0> PCLATH<4:3> -> PC<12:11> Status: None Encoding: 10 1kkk 1 kkkk kkkk Words: 1 Cycles: 2 例 : GOTO There 执行后 PC = address There Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 59

使用 MPLAB 集成开发环境 (IDE) MPASM 编译器 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 60

汇编指令 #INCLUDE 语法 : #include# 文件名.* 或 < 文件名.*> 或无括号或引号 : 示例 : #INCLUDE p12f675.inc Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 61

汇编指令 #DEFINE 用于定义变量和分配初始值 用户友好的名字被分配给常数, 寄存器, 管脚名等. 使程序易读 例 : #DEFINE MYCONSTANT H 5A CONFIG 设置配置位 见 Lab 2 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 62

汇编指令 ORG 语法 : ORG <address> 例 : ORG 0x0000 描述 : 定义起始地址 (0x0000). Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 63

汇编指令 EQU 语法 : <label> < equ <expr> 例 : Seconds EQU 0x24 描述 : 定义常数或变量的替换名. 可用做常数 movlw SECONDS ; put 24 (hex) into W... 或当作寄存器地址 movwf SECONDS ; put value in W into ; RAM location 24 (hex). Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 64

汇编指令 BANKSEL 语法 : BANKSEL [<variable>] 例 : BANKSEL TRISA movwf TRISA 设置相关的 bank 位 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 65

汇编指令 END 语法 : END 例 : 表示原代码结束. 你必须在想结束汇编的地方有一条 END 语句. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 66

休息 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 67

Lab 1 在 MPLAB 7.x 中创建一个项目 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 68

Lab 1 MPLAB 7.xx 布局 图标栏 Buttons are added to the Toolbar as features are enabled 项目窗口 Workspace 输出窗口 状态栏 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 69

Lab 1 目标 使用 Project Wizard 编写简单的 MPASM 代码 创建项目 编译代码介绍输出窗口和 Program Memory 窗口选择 Build 选项 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 70

创建项目 Project -> > Project Wizard... 打开 Project Wizard, 创建一个项目 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 71

Lab 1 从任一项目开始 按照下列步骤创建项目 : 点击 Next 继续. 选择 MCU 型号 使用最新的 MPLAB IDE 版本 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 72

Lab 1 从任一项目开始 选择语言工具 : MPASM 汇编器免费, 本课将使用. 显示安装后子目录, 你也可以更改. 当然你可以点出 Help 或 Show all 查看所有的语言工具. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 73

Lab 1 从任一项目开始 选择路径 : 键入项目名字. 键入项目路径. 注意路径尽可能短. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 74

Lab 1 从任一项目开始 增加文件 增加文件到项目中. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 75

Lab 1 从任一项目开始 完成 创建的项目的小结被显示, 点击 finish 完成. 记住, Project Wizard 新版本将会增加一些新的功能. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 76

Lab 1 从任一项目开始 (*.mcp) 和 (*.mcw) 文件名将被显示. 在窗口你可以添加任何类型相关的文件到项目中 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 77

Lab 1 从任一项目开始 选择 File > New 创建新文件 键入右图所示的代码. * 表示文件已被改变. 键入的代码执行 nop s 功能. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 78

Lab 1 从任一项目开始 选择 File > Save As 保存文件.. 保存例子文件为 Lab1.asm. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 79

Lab 1 从任一项目开始 在 source code 栏增加文件鼠标右击 Source Files 选择 Add Files. 增加 Lab1.asm 文件. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 80

Lab 1 从任一项目开始 文件现在增加在 Project 中注意 * 表示文件已被改变未保存. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 81

Lab 1 选择 File > Save Workspace 保存项目 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 82

编译代码 选择 Project -> > Build All 编译项目 Build All 表示每一个文件被改变, 因此重新编译所有项目 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 83

Lab 1 从任一项目开始 输出窗口显示 build 的过程. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 84

Lab 1 从任一项目开始 选择 View > Program Memory 可以查看程序区 Your Code Unprogrammed locations (3FFF) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 85

Workspace: 包含下列信息 : MCU 型号 Project: MPLAB IDE 设置信息 调试工具或编程器打开的窗口 IDE configuration 的设置值 包含所有的源文件 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 86

MPLAB IDE 窗口 新的工作界面有下列窗口 Project - 工程项目 Output - 输出信息 Disassembly - 反汇编 Watch - 变量观察 Hardware Stack - 硬件堆栈信息 Program memory - 程序空间内容 File Registers - 寄存器内容 EEPROM data - EEPROM 数据内容 Special Function Registers - 特殊功能寄存器 Note: There are often more windows, but they will depend on the device e used and debugger/programmer tool. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 87

Lab 1 设置 Build 选项 在 Project Window 右击项目文件 ( 也可以选择 Project 菜单 ) 选择 Build Options... 更改设置 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 88

Lab 1 设置 Build 选项 The Build Options 窗口有多个设置窗口使用下拉菜单改变类型选择你需要的选项通常采用缺省值 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 89

Lab 1 注意事项 首先必须生成项目或文件.asm 文件必须添加到项目使用 Configuration 菜单选择型号编译项目生成.hex 文件 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 90

Lab 2 使用 MPLAB ICD 2 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 91

开始 Lab 2 Lab 2 目标 介绍 MPLAB ICD 2 和 PICDEM 2 Plus 验证逻辑跳转编写代码测试按键输入 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 92

套件包含 : MPLAB ICD 2 套件 (DV164006) MPLAB ICD 2 所有电缆包括 USB 电缆 电源 CD-ROM 资料 PICDEM 2 Plus 演示板及 : 样品 (PIC18F452, PIC16F877) 功能! (LCD, ( 温度传感器, 等 ) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 93

可单独购买 DV164006 ICD 2 套件内含 与 ICD 2 直接相连 ICD 2 供电或外接电源 可演示许多功能 PICDEM 2 Plus 演示板 注意订单时订购 DV164006! DV164007 不包含 PICDEM 2 Plus 和和样品 DV164005 仅包含 ICD2, 无无电缆, 电源, 样品等. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 94

跳转 循环种类 : 定时嵌套计数延时 指令 : BTFSS Bit Test File Skip if Set BTFSC Bit Test File Skip if Clear DECFSZ DECrement File Skip when Zero INCFSZ INCrement File Skip when Zero Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 95

跳转 Delay ; Delay code using decfsz instruction decfsz MyVar,F ; Decrement MyVar,, skip if 0 goto Delay ; since MyVar is not 0, go back bsf PORTB, RB0 ; MyVar now 0, light LED and continue 210 MyVar 指令 BTFSS Bit Test File Skip if Set BTFSC Bit Test File Skip if Clear DECFSZ DECrement File Skip when Zero INCFSZ INCrement File Skip when Zero Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 96

跳转指令 - - - N OV Z DC C STATUS Status Bits N Negative OV Overflow Z Zero DC Digit Carry C Carry 使用跳转指令测试任何一个位 : SFR 变量 (GPR) STATUS 寄存器 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 97

Lab 2: 内容 测试按键输入 读 PORTx,y x 是口的名字 ( 例 PORTA ) y 是第几位 (0 到 7) 程序跳转 通过 btfsc or btfss 跳转 指令跳过或或不跳过下一条指令 根据跳转点亮 LED 用 MPLAB SIM, MPLAB ICD2 和 PICDEM 2 Plus 验证 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 98

LAB 2 测试按键输入 #INCLUDE p16f877a.inc ; Include the standard definitions clrf PORTB ; Initialize PORTB so that all LEDs are off by default BANKSEL TRISB ; BANKSEL is used to get to bank with TRISB in it (bank 1) clrf TRISB ; Clear TRISB to make all PORTB pins outputs movlw B 00010000 ; Make pin RA4 an input (rest of PORTA pins are outputs) movwf TRISA ; This is redundant as TRISA bits are 1 s 1 s by default (inputs) ; (the rest of the pins are set low) movlw H 07 ; Writing 7 into ADCON1 causes all analog pins to allow movwf ADCON1 ; digital input. You must set ADCON1 when using pins with ; A/D functions as they are analog mode by default. ; This code isn t t needed in this example - do you know why? Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 99

LAB 2 BANKSEL PORTA ; BANKSEL is used to return to bank 0 (for PORTA, PORTB) Loop btfss PORTA,4 ; Test RA4 on PORTA, skip if pin is high goto On ; go to On code if button is pressed (pressed button is low) Off movlw B 00000010 ; On code is skipped so Off code runs - it writes a value to movwf PORTB ; PORTB to turn on RB1 (high) and sets rest of PORTB low goto Loop ; The test of the button happens again by branching to Loop On movlw B 00001000 ; On code writes a value to PORTB to turn on RB3 (high) movwf PORTB ; and sets rest of PORTB low, including RB1. goto Loop ; The test of the button happens again by branching to Loop Here goto Here ; This code is not needed either. Why? END ; All programs must have an END directive Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 100

Lab 2: 开始 打开 Lab2.asm 使用 File -> > Open 创建项目 使用 Project Wizard Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 101

ICD 2 与计算机相连, 目标板上电, 然后 ICD 2 与目标板相连 使能 MPLAB ICD 2 Lab 2: 设置 MPLAB ICD 2 USB 1 Connect to PC ICD 2 3 6 Pin Phone jack to PICDEM 2 Plus 2 Connect Power Power Supply PICDEM 2 Plus Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 102

Lab 2 用 MPLAB ICD 2 调试 使能 ICD2, 编译代码. 你必须先编程器件 编程后, 可尝试全速运行, 单步走. 查看 watch 窗口. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 103

Lab 2 使能 MPLAB ICD 2 一旦使能, 许多选项将允许. 你可编程或调试记住设置 Configuration Bits! Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 104

Lab 2 配置位 PIC MCU 有 Configuration Bits Configuration bits 在编程或调试时被设置. 打开 Configure -> Configuration Bits, 先设置后编 程. Configuration Bits 非常重要! Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 105

用 CONFIG 设置配置位 CONFIG _ CONFIG 字键入两个下划线 使用 INCLUDE(*.inc) 用来设置 CONFIGURATION 寄存器 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 106

例 :(PIC16F877A): 用 CONFIG 设置配置位 CONFIG _XT_OSC & _BODEN_OFF &... Bitwise AND (&)( ) together any desired terms (may grow long on some parts) Open the Processor Include file (*.INC) ) and scroll to end to see the configuration bit options Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 107

使用 Watch Window View -> > Watch 键入观察的寄存器 / 变量点击 Add SFR 或 Add Symbol Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 108

用 MPLAB IDE 调试 MPLAB IDE 界面 工程项目窗口 源代码窗口 输出信息窗口 变量观察窗口 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 109

MPLAB IDE 快捷键 - 调试快捷键 F5 Halt - 暂停 F6 Reset - 复位 F7 Step - 单步运行 F9 Run - 全速运行 或使用图标栏! Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 110

Lab 2 注意 MPLAB IDE 功能强大, 下列免费 编辑器编译器模拟器与其它工具的接口 使用 Watch Windows 调试你的代码编程前先设置 configuration bits 跳转指令 btfss, btfscc, incfsz, decfsz Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 111

Lab 3 使用 CCP 模块 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 112

Lab 3 Lab 3 目标 介绍 CCP 和 PWM 让蜂鸣器鸣叫 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 113

Lab 3: CCP CCPR1L CCP1CON<5:4> CCPR1H Duty_Cycle 00 (copy) TMR2 COMPARE R Q Output Fosc/4 Prescaler (counter) S Divide by 1, 4 or 16 PR2 COMPARE Period (rate) Note: These 2 bits can be Q cyles, or the counter divided by 4 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 114

Lab 3: CCP CCPR1H CCPR1L Duty_Cycle 5 00 5 00 CCP1CON<5:4> Transition at 5 due to match of CCPR1x and TMR2 TMR2 COMPARE R Q Output Fosc/4 Prescaler Divide by 1, 4 or 16 (counter) 1234567890 0 S Output goes low and TMR2 is reset after PR2 matches TMR2 PR2 COMPARE Rate 9 Note: These 2 bits can be Q cyles, or the counter divided by 4 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 115

Fosc/4 Prescaler Divide by 1, 4 or 16 Lab 3: CCP 预分频 Prescaler 使用 TMR2 预分频 例 : Fosc = 4 MHz Fcy = 1 MHz Tcy = 1 us T1CKPS1:0 00 01 Divides by 1 4 1x 16 Count Rate 1.00 us 4.00 us 16.00 us Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 116

例 : 880 Hz( 4 MHz 的主晶振 ) 选择 TMR2 预分频值 16 Lab 3: 计算 CCP 值 计算 PR2 (4 MHz / 16) / 4 = 62500 Hz 62500 Hz / 880 = 71.022727-1 = 70 计算 CCPR1L : 50% 的占空比是 35. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 117

为何选择 16 作为预分频? 我们提前计算 : 回忆 PR2 的结果 (70 ) 如果预分频选择 4, Lab 3: 问题 计算 PR2 : (4 MHz / 4) / 4 = 250000 Hz 250000 Hz / 880 = 284.09 = 284 = >255 结果比之前的预分频值更糟 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 118

Lab 3: 内容 设置 PWM 为 880Hz, 50% 占空比 这个频率可听得见, 计算 : PR2 CCPR1L 设置 T2CON TMR2 用于 PWM 选择 TMR2 预分频 RA4 按下鸣叫 在 LAB 3 基础上基于 RA4 输入进行跳转 设置 CCP1CON 打开或关闭 PWM 用 MPLAB SIM, MPLAB ICD2, PICDEM 2 Plus 验证调试 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 119

LAB 3 让 PIC16F877A 发出音乐 #INCLUDE p16f877a.inc ; Include the standard definitions Start BANKSEL TRISC ; BANKSEL to get to TRISC bank (bank 1) bcf TRISC,2 ; Set CCP1 as an output movlw D'70' ; Manually Calculated Period movwf PR2 ; loaded into PR2 (also in bank1) BANKSEL CCPR1L ; BANKSEL is needed to get bank to bank 0. movlw D'35' ; Manually Calculated Duty Cycle (50%) movwf CCPR1L ; loaded into CCPR1L movlw 0x06 ; Turn on TMR2 (for PWM) movwf T2CON ; and /16 prescale Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 120

LAB 3 CheckButton btfsc PORTA,4 ; Test the button - if pressed, its a low signal. goto ButtonOff ; if high (not pressed), go to Button Off code ButtonOn ButtonOff movlw H'0C ; if low (pressed), fall here to Button On code. movwf CCP1CON ; this turns on CCP1CON by writing 0C (hex) to it. goto CheckButton ; go check the button again (back to CheckButton) clrf CCP1CON ; Turn off CCP1 module by writing 0 to it goto CheckButton ; go check the button again (back to CheckButton) END ; All programs must have an END directive Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 121

尝试一下! Lab 3 提高 尝试改变频率. 能改变成 440 Hz? 计算 : Timer 2 预分频值 (1,4 or 16) PR2 值, 然后减 1 CCPR1L 值 测试 : 使用 ICD 2 and F7, F5 进行调试. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 122

休息 (II) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 123

Lab 4 使用 A/D Converter (PIC16F877A) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 124

Lab 4 目标 Lab 4 目标 A/D Converter 在 PIC16F877A 执行 A/D 转换基于模拟输入点亮 LEDs 学习使用宏命令 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 125

A/D 使用 A/D 设置口方向 (TRISx ) 设置模拟和数字通道 (ADCON1 或 ANSEL) 设置 A/D 时钟 (ADCON0 / ADCON1) 执行转换循环 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 126

A/D 设置 A/D 时钟 ADCON0 ( 内部 RC 或 Fosc ) ADCON1 通道 (AN0, AN7) ADCON0 打开 A/D (ADON 置 1) ADCON0 设置参考电压 (VDD 或外部.) ADCON1 选择对齐 ADCON1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 127

设置 TRISx 设置口方向 通过 TRISA, TRISE 设置为输入或输出. 相关的模拟口必须为输入. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 128

A/D 设置模拟和数字通道 必须设置口为数字或模拟口通过 ADCON1 和 ANSEL 来设置 This chart is a sample of modes, there are 15 modes for this part Bit Setting PCFG<3:0> 0000 0001 AN7 A A 0010 D AN6 A A D Channel Mode Options AN5 A A D AN4 A A A AN3 A Vref+ A AN2 A A A AN1 A A A AN0 A A A Voltage References Vref+ VDD AN3 VDD Vref- Vss Vss Vss C/R 8/0 7/1 5/0 We will be using mode 1110 (1 A/D channel, VDD and VSS reference) 1110 D D D D D D D A Vdd Vss 1/0 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 129

A/D 选择参考电源 参考电源可为 VDD VSS 或外接当设置数字和模拟引脚模式时同时设置 Bit Setting PCFG<3:0> AN7 AN6 Channel Mode Options AN5 AN4 AN3 AN2 AN1 AN0 Vref+ 1110 D D D D D D D A VDD 高参考源 低参考源 A/D 通道 / 参考输入 Voltage References Vref- C/R Vss 1/0 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 130

A/D 设置 A/D 时钟两个主要选项 - 内部 RC 和 Fosc Fosc 有许多分频选项内部 RC 是独立的 A/D 振荡器 ADCON0 ( 有时 ADCON1) 设置 A/D 时钟 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 131

A/D 时钟计算 A/D 假设 4 MHz 主频, PIC18F877A 则 Tosc = 250 ns T AD 必须 1.6 us 到 20 us 250 ns / 1.6 us = 1/6.4 因此选择 Fosc/8 ADCON1 <ADCS2> 0 0 0 1 1 Bit Setting ADCON0 <ADCS1:0> 00 01 10 00 01 Clock Mode Fosc/2 Fosc/8 Fosc/32 Fosc/4 Fosc/16 1 10 Fosc/64 x 11 FRC (internal RC) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 132

A/D 选择对齐 结果是 10 位宽结果存于 : ADRESH,ADRESL ADRESH ADRESL ADRESH ADRESL bbbb bbbb bb00 0000 0000 00bb bbbb bbbb 左对齐右对齐 打开 A/D ADCON0 的 ADON 位置 1 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 133

A/D 循环转换 插入取样时间 ( 你的代码 ) 开始转换 (GO/DONE 置 1) 等待 GO/DONE 清零 ( 或 ADIF=1) 读 A/D 值 (ADRESH / ADRESL) 选择通道 置 GO/DONE 位 PIC 清除 GO/DONE 位 通道选择 取样时间 AD 转换 如果为多通道输入 ADRESH ADRESL 你的 A/D 结果 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 134

宏命令 宏命令用 MACRO 开始, 用 ENDM 结束 Name_Of_Macro MACRO Variables nop ;My Code Goes Here ENDM Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 135

宏命令 宏命令 例 : AddW MACRO Input addlw Input ENDM 你的代码中 : AddW 5 ; Add 5 to W Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 136

Lab 4: 内容 读 A/D 值, 用 ADRESH 值点亮 LEDs 当值小于 100 点亮 LED 0 和关闭 LED 1 当值大于 100 点亮 LED 1 和关闭 LED 0 别忘了取样时间 GO/DONE 置 1 前必须延时, 1 ms 足够例 : Dly32 D 50 延时 1 ms Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 137

可使用下列宏命令 : Lab 4: 宏命令 LEDEnable <bitpattern> 1 输入, 0 输出. 允许 LED 0 和 1, 使用 : LEDEnable B 11111100 or LEDEnable H FC LEDOn <LED number> <LED number> 必须在 0 和 3 之间. 例, 点亮 LED 0 LEDOn 0 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 138

Lab 4: 宏命令 LEDOff <LED number> LEDOff 关闭 LED 例 LEDOff 2 Dly32 <Cycles x 20> 延时子程序 例延时 1 ms (1000 cycles at 4 MHz) : Dly32 D 50 ; Delay of 50 x 20 cycles = 1000 Tcy Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 139

LAB 4 Start #INCLUDE p16f877a.inc ; Include the standard definitions #INCLUDE 701PIC.inc ; Include MACROs from MASTER s 03, PIC 102 class #DEFINE TESTVAL D 100 ; High Value to compare A/D reading against LEDEnable B 11111100 ; MACRO to setup I/O port to drive LEDs RB1 and RB0 BANKSEL TRISA ; BANKSEL to TRISA in bank 1 movlw H 01 ; Initialize PORTA so that RA0 (analog channel 0) is an input, movwf TRISA ; all other PORTA pins are outputs movlw H 0E ; Initialize the A/D to be analog on, partly selects clock mode movwf ADCON1 ; channel 0 (RA0), all other pins are digital mode BANKSEL ADCON0 ; BANKSEL to ADCON0 in bank 0 movlw H 41 ; Also, A/D clock is Fosc/8 (good for up to 5 MHz, 5V) and, movwf ADCON0 ; A/D is turned on but not yet converting and ready on Ch 0. CheckAgain Dly32 D 50 ; MACRO to Wait 1 ms (at 4 MHz) bsf ADCON0,GO ; Start the Conversion Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 140

LAB 4 PollAD btfsc ADCON0,GO ; Wait for Conversion to finish goto PollAD ; go back and poll the A/D GO/DONE bit again ConvertDone movf ADRESH,W ; Move A/D result (high byte) into W for later testing sublw TESTVAL ; Test A/D result against test value btfss STATUS,C ; go to over if A/D result larger than test value goto Over ; value is too large, run Over code Ok LEDOn 0 ; MACRO to turn on LED 0 LEDOff 1 ; MACRO to turn off LED 1 (this shows Ok condition) goto CheckAgain ; Do A/D read and value test over again Over LEDOn 1 ; MACRO to turn on LED 1 LEDOff 0 ; MACRO to turn off LED 0 (this shows Over condition) goto CheckAgain ; Do A/D read and value test over again END Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 141

试一试! Lab 4 警示灯 调节电位器 RA0. LEDs 改变吗? Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 142

Lab 4b 报警! 尝试下列功能 : 复制 Lab 4 代码粘贴到 Lab 5. 如果值大于 TESTVAL 鸣叫 Ok Over New Lab 5 Lab 5 (A/D Test) 使用 GOTO or CALL/RETURN 指令 ButtonOn (start beeping) Lab 4 (Beep) ButtonOff (stop beeping) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 143

Lab 5 定时器及中断 (on the PIC16F877A) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 144

Lab 5 的目标 Lab 5 的目标 学习 TMR0 理解查询和中断写一个基于中断的延时程序 LED 以中断设定的速度闪烁 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 145

Lab 5: TMR0 外设 可读写 从 FF 到 00 (hex)( 溢出时产生中断 预分频可选择 2, 4, 8, 16... 直到 256 定时模式 : 最快速度是 OSC/4 ( 对于 20 MHz 晶振来说是 5 MHz) 外部事件计数模式 : 上升沿或下降沿触发可供选择 TMR0 位宽是 8 TMR0IF overflow flag output Ext clock input Internal clock (Tcy( Tcy) 预分频 Divide by 1, 4 or 16 8-bit timer/counter Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 146

TMR0 时钟源是 Fosc / 4 使用 TMR0 您的 Demo 板上使用 4 MHz 时钟 (Fosc) 当使用预分频时, 输入可进一步除上 2, 4, 8, 16... 直到 256. TMR0 从 00 到 FF (hex) 向上计数, 计到 FF 时溢出从 00 开始重新计数. 溢出时标志位置 1 以供中断或查询使用. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 147

计算 TMR0 的溢出 使用 4 MHz 时钟 TMR0 计数时钟速率是 Fosc/4 对于一 200 ms (0.2 second) 速率中断, 您将如何配置 TMR0 的预分频? TMR0 初值置多少? 考虑 : 计数器以 1 us 速率计数, 无预分频, 能计到多少? Time = 预分频 x maximum count (8 bits) x rate Time = 256 x 256 x 1 us Time = 65536 但我们需 200000 来得到 0.2 秒! Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 148

计算 TMR0 的溢出 ( 第二部分 ) 将溢出时间作为计数单元 本例 50000 us 以整数的倍数 (4) 计数来得到总时间. 这种设定中能计到 50000 us 吗? 除数是 (256), 时间 (50000) 得到商 : 500000 / 256 = 195.3125 counts 选择 : 忽略相对较小的误差 (195 * 256 = 49920 us) 改变主晶振以得到整数 改变除数, 获得快中断等诸多方法. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 149

计算 TMR0 的溢出 ( 第三部分 ) 本例中我们采用忽略误差的方法, 当然在您的应用中方法会不一样 故, 这里 TMR0 是 : 256 预分频速率 195 计数值 4 个溢出 这样, 我们需给定时器预装一个初值, 定时器从该值开始向上计到 256 时, 给出溢出信号 : 这个值是 256-195 = 61 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 150

TMR0 中断 当定时器溢出时, 会将标志位置 1 举个例子, TMR0 将 TMR0IF 置 1 中断标志位 xxif 产生标志时, 既可用来查询, 同时也是中断产生的基础在您的软件中一旦检测到 xxif 标志位, 必须清零 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 151

中断... Lab 5 - 中断 使 CPU 在执行主程序时转而执行其他任务 中断源有多种 : 定时器外设外部信号 配置方式 NTCON 寄存器 其他寄存器 ( 随芯片不同而不同 详见数据手册 ) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 152

中断 T0IF T0IE 唤醒 ( 如果在 SLEEP 模式 ) INTF INTE RBIF RBIE 到 CPU 的中断 EEIF EEIE GIE Note: There is often a PEIE bit which enables/disable further 中断 sources Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 153

R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-x INTCON REG PIC16F877A GIE PEIE T0IE INTE RBIE T0IF INTF RBIF R= Readable bit W= Writable bit bit7 bit 7: GIE: Global 中断 Enable bit 1 = Enables all un-masked 中断 0 = Disables all 中断 bit0 U= Unimplemented bit, read as 0 - n= Value at POR reset bit 5: T0IE : TMR0 Overflow 中断 Enable bit 1 = Enables the TMR0 中断 0 = Disables the TMR0 中断 bit 2: T0IF : TMR0 Overflow 中断 Flag bit 1 = TMR0 has overflowed (must be cleared in software) 0 = TMR0 did not overflow Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 154

为什么要用中断? 更好的利用 CPU 资源快速响应多任务固定的或已知的间隔处理异常处理 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 155

许多程序不需中断 逻辑流程中不用中断 不需现场保护... 我需要中断吗? Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 156

中断 : ISR 现场保护 Saving Push MACRO ; PUSH W and STATUS movwf W_TEMP ; movf STATUS,W ; (swapf( will also work here) movwf STATUS_TEMP ; ENDM Restoring Pop MACRO ; POP W and STATUS movf STATUS_TEMP,W ; (swapf( will also work here) movwf STATUS ; swapf W_TEMP,F ; movf changes STATUS bit Z, but swapf W_TEMP,W ; swapf does not change STATUS ENDM Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 157

中断 : ISR 现场保护 Push MACRO ; PUSH W and STATUS movwf W_TEMP ; movf STATUS,W ; (swapf( will also work here) movwf movwf STATUS_TEMP STATUS_TEMP ; ENDM Saving Saving STATUS W 12 34 XX 12 W W_TEMP 34 STATUS XX 34 STATUS_TEMP Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 158

中断 : ISR 现场保护 Pop MACRO ; POP W and STATUS movf STATUS_TEMP,W ; (swapf( will also work here) movwf STATUS ; swapf swapf W_TEMP,F W_TEMP,F ; movf changes STATUS bit Z, but swapf W_TEMP,W ; swapf does not change STATUS swapf W_TEMP,W ENDM Restoring STATUS W XX 12 34 W 12 21 W_TEMP XX 34 34 STATUS STATUS_TEMP Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 159

Lab 5: 内容 TMR0 预分频 256, 每个循环计 195 次以产生 0.05 秒 (50000 us) 中断 每四个循环 (0.2 seconds) 触发 LED 0. LED 0 (RB0) 以此速率闪烁 在改变 LED 状态前你要读它的当前状态, 或采用其他方式对该口写一完整的值, 不对该口寄存器使用 bsf/bcf 指令 使用 MPLAB SIM, MPLAB ICD2 以及 PICDEM 2 Plus 验证你的结果 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 160

LAB 5 第一部分 #INCLUDE p16f877a.inc #INCLUDE 701PIC.inc ; Include the standard definitions ; Include MACROs from MASTER s 03, PIC 102 class ISRCtr EQU 0x30 ; location for ISRCtr variable (countdown for ISR loops) Start ISR ORG 0 ; Reset Vector Location (0) goto Initialize ; On start-up go to Initialization code ORG 4 ; 中断 Vector Location (4) ; Macro to context save in ISR ; Clear the TMR0 中断 condition ; Reload the Timer for accurate timing ; Decrement the ISRCounter (from 4 to 0) and test for 0 ; if the counter is not yet 0, then toggling is not needed ; if the counter is 0, run toggle code Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 161

LAB 5 第二部分 ToggleLED ; put 4 into W to reload into the ; 中断 counter (after 4 counts we need to reset it) ; write 1 to W in order to, ; toggle LED 0 of PORTB - XOR with 1 will toggle the bit SkipToggle ; Done 中断, so restore and exit ; Macro to context restore when leaving ISR ; Return from 中断 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 162

LAB 5 第三部分 Initialize goto $ ; Ensure LED 0 is off ; MACRO to setup I/O port to drive LED RB0 ; Write 4 to W to ; preload the ISR counter with ; Preload the timer for appropriate time delay ; Turn on GIE and ; TMR0 中断 (T0IE) ; Set up banking to access OPTION_REG in bank 1 ; Set up TMR0 for 256 prescale and ; internal clock mode ; return to accessing BANK 0 ; Wait forever for 中断 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 163

LAB 5 第四部分 ; Subroutine to reload TMR0 DoReloadTimer END ; Preload Timer to ; count 195 times before roll-over over ; return from subroutine Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 164

定时器 : Lab 5 注意点 PIC 微控制器带预分频可选的至少 8 位 (TMR0) 定时器 需要重装初值以得到期望时间 可以通过计几次溢出以得到期望时间. 中断 : 判断是否是您应用中所要的中断, 如果是 不要忘了保护和恢复现场置 GIE, 及其他 xxie 位来打开中断中断向量从程序段的 0004h 开始 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 165

Lab 6 时间片系统 ( 实时多任务 ) Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 166

Lab6 目标 Lab6 目标 简单的时间片系统 (Time -Slicing Systems ) 代码迅速处理两个 ( 或以上的 ) 任务! Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 167

Lab6: 任务 LED 0 以 0.15 秒点亮, 0.25 秒熄灭来闪烁, 并且蜂鸣器 ( 及 LED 1 闪烁 ) 以 0.5 秒打开, 1 秒关闭 : 让我们看看是什么现象 : Time 0 0.5 1 1.5 2 2.5 LED On Off On Off On Off On Off On Off On Off On O... Piezo On Off On Of... Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 168

Lab6: 计算 分时 找出最小的时间单元 : 秒数 : 0.15, 0.25, 0.5, 1.0 0.05 秒贯穿所有这些整数时间段 ( 使用 GCF 来计算 ) 计算任务数 ( 及变量 ): 2 然后生成一计划来显示这两个软件计数器是怎样 控制这些任务的. Note: : GCF stands for Greatest Common Factor Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 169

Lab6: 分时 (Animated) TMR0 中断每 0.05 s 在每个计数寄存器中产生一次计数 LED 计数器 Piezo 计数器 0: ON 0: ON 3: OFF 6 10: OFF 0 8: 复位到 0 30: 复位到 0 ( 计数器 67 01 52 34 ) 计数器 29 10 11 12 01 23 45 67 89 ) LEDCtr PiezoCtr OFF ON OFF ON LED Piezo Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 170

Lab6: 计划 TMR0 每 0.05 秒中断一次以产生时基 该时基用于 : 点亮或熄灭 LED 0 及 LED 1 (RB0 及 RB1) 打开或关闭 Piezo 清掉 LED 及 Piezo 的时间计数器 点亮 2 个 LEDs 并使蜂鸣器发声 ( 以 500 Hz): RB0 打开时间 0.15 秒, 关闭时间 0.25 秒 ( 共 0.4s) RB1 打开时间 0.5 秒, 关闭时间 1 秒 ( 共 1.5 s) Piezo 响 0.5 秒, 关闭 1 秒 ( 同 RB1) 使用宏指令来简化代码 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 171

Lab6 第一部分 #INCLUDE p16f877a.inc ; Include the standard definitions #INCLUDE 701PIC.inc ; Include MACROs from MASTER s 03, PIC 102 class #DEFINE DEVICE_FREQ_HZ D'4000000 ; Needed by Piezo series macros LEDCtr EQU 0x30 ; Counter for LED timing PiezoCtr EQU 0x31 ; Counter for Piezo timing Start ISR ORG H'00' ; Reset Vector goto Initialize ; Jump to Initialization code ORG H'04' ; 中断 Vector ; Macro to context save in ISR ; Clear the TMR0 中断 condition ; Add 1 to LEDCtr ; Add 1 to PiezoCtr ; Reload the Timer for accurate timing ; Macro to context restore when leaving ISR ; Return from 中断 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 172

Lab6 第二部分 Initialize LEDOff 0 ; Turn off both LEDs LEDOff 1 ; " clrf LEDCtr ; Initialze Task Couters to 0 ; " (both LED and Piezo) ; MACRO to setup I/O port to drive LEDs RB1 and RB0 ; 500 Hz Beep when PiezoOn is used ; Preload the timer for appropriate time delay ; set banking to access OPTION_REG in bank 1 ; Set up TMR0 for 256 prescale and ; internal clock mode ; Return to accessing BANK 0 ; Turn on GIE and TMR0 中断 ; " Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 173

Lab6 第三部分 LEDTest NotEqual1 TurnOnLED TurnOffLED ; Test LED1 Counter Value for overflow ; by subtracting 8 from it. Leave result in W ; Test for 0 to determine if equal ; If counter is not 8, test if LED should be on or off ; If counter is 8, it is reset to 0 ; Test LED counter against "On" time ; by subtracting 3 from it. Leave result in W ; Test for negative value to determine if less than 3 ; if 3 or more, goto LEDOff Code ; else, Turn on LED 0 since counter is less than 3 ; LED Testing done, goto PiezoTest ; Turn off LED since counter is 3 or more (up to 7) ; LED Testing now done, so fall into PiezoTest Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 174

Lab6 第四部分 PiezoTest ; Test Piezo Counter Value ; by subtracting 30 from it. Leave result in W ; Test for 0 to determine if equal ; If counter is not 30, test if LED should be on or off ; If counter is 30, it is reset to 0 NotEqual2 ; Test Piezo Counter against "On" time ; by subtracting 10 from it. Leave result in W ; Test for negative value to determine if less than 3 ; if 10 or more, goto LEDOff Code TurnOnPiezo ; Turn on LED1 ; Turn on Piezo (by turning on CCP1) ; manage tasks by returning to top of list TurnOffPiezo ; Turn off LED1 ; Turn off Piezo (by turning off CCP1) goto LEDTest ; manage tasks by returning to top of list Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 175

Lab6 第五部分 ; Subroutine to reload TMR0 DoReloadTimer END ; Preload Timer to ; count 195 times before roll-over over ; return from subroutine Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 176

对您项目的 帮助 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 177

Microchip 应用支持 www.microchip.com 数据手册, 应用笔记, 设计中心 选择最好的元件 申请免费样片 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 178

Microchip 应用支持 仍有困难? 免费的电话或 e-mail 帮助! Development Systems Information Line 800-820-6247 820-6247 China.techhelp@microchip.co m 800-820-6247 820-6247 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 179

Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 180

附录 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 181

使用模板来建立文件 模板能帮助您快速建立文件 File -> > Open 进入模板路径 : C:\Program Files\MPLAB IDE\ MCHIP_Tools\ 模板 \CODE 找到您所需的文件 文件名中带有芯片型号的后几位字符举例如 PIC12F675 或 ( F675), 文件名是 f675temp.asm Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 182

模板中有些什么? 芯片名 说明 代码编写提示 文件信息 文件名, 时间, 版本等 所需的文件 说明项目中所用到的其他文件 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 183

模板中有些什么? 笔记 解释程序流程 Include, List 及 CONFIG 指令 Includes 标准定义设定配置位 定义变量 EQU 定义变量中断中用的临时变量已有定义 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 184

模板中有些什么? 向量 复位, 中断向量及代码 中断保护及恢复 在中断保护代码及恢复寄存器 用户代码 您的代码从这里开始 EEPROM 初始化 END 指令 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 185

外设 : PortB 输入中断 键盘接口 : 内部上拉 ( 软件实现 ) 使 RB4-RB7 RB7 脚为高 RB4 RB5 RB6 RB7 100 Hex keypad RB0-RB3 RB3 脚输出 0 按下任何建将使 RB 脚拉低并产生一中断 RB0 RB1 该中断能使处理器从 SLEEP 模式唤醒 PIC16CXXX RB2 RB3 R 节省了定时器资源 Resistance for ESD protection AN557 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 186

可靠性设计技术 硬件看门狗 有助于从软件故障中唤醒使用芯片自带的 RC 振荡 WDT 不能由软件关掉 WDT 溢出时使芯片复位 CLRWDT 指令清 WDT 可编程的溢出周期 : 18 ms 到 2.5 秒 SLEEP 模式时也工作在溢出时唤醒 CPU "CLRWDT" RC oscillator Reset 8-bit ripple counter Postscaler Overflow resets chip Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 187

可靠性设计技术 软件看门狗 只用软件控制, 看门狗同样有效整个程序中只使用一个 CLRWDT 指令将 CLRWDT 指令放在主循环中不要将 CLRWDT 指令放在 ISR 或其他子程序中选择主循环能容许的最小 WDT 溢出周期以 GOTO wdtreset 指令 ( 自循环 ) 初始化不用的内存来强制溢出 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 188

可靠性设计技术填充未用的程序空间 当使用看门狗时, 未用的区域填充 GOTO wdtreset 指令该自循环正常情况不会执行如果 PC 工作失常, 就很有可能会执行这条指令 通过 WDT 强制复位来初始化内核 在插入该指令时可使用 FILL ( 假设 < 最末程序地址 +1> 是 400h): FILL (GOTO wdtreset), (400h-$) ORG 3FFh Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 189

上电时强制 WDT 复位 : 上电时检查 RAM 模式 如果模式不正确 : 初始化 RAM 模式 强制 WDT 复位 可靠性设计技术干净的 WDT 复位 如果代码跳到未用的程序区强制 WDT 复位 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 190

可靠性设计技术子程序计数 设定子程序调用计数器及子程序执行计数器 每调用一次子程序, 子程序调用计数器自增 1 在每个子程序开始使执行计数器自增 1 在主循环开始, 检查调用计数器和执行计数器是否相 等 如果不等, 强制 WDT 复位 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 191

术语表 TMR0-8-bit timer peripheral w/8 bit 预分频 预分频 - peripheral which delays the TMR0 count by a prescale ratio i.e. 1:1, 1:2, 1:4 Page - memory organization for program memory Bank - memory organization for data memory Pipeline - hardware architecture for prefetching an opcode while executing the previous opcode Orthogonal instruction set - instructions work on ports and registers the same way OPTION_REG - 寄存器 of control bits for configuring tmr0, INT, and pull ups INTCON - 寄存器 of control bits for configuring peripheral 中断 STATUS - 寄存器 of bits indicating the results of an operation RP1&RP0 - direct addressing bank selection control bits IRP - indirect addressing bank selection control bit PORTB - I/O port B GPIO - I/O port on the 8 pin parts Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 192

Lab 1 Projects the Old Way 使用 Configure > Select Device 来选择处理器 注意有不同的开发工具来支持使用最新的 MPLAB IDE 确保得到最好支持 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 193

Lab 1 项目建立 点击 File > New 产生新的项目文件. 也可以通过 File > Open 打开文件完成此操作. 可以在此处保存 workspace 以及 import 和 export 文件供后面使用. Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 194

Lab 1 项目建立 点击 Project > New Project 在 Workspace 中建立新的项目文件输入项目名及路径 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 195

Lab 注意点 : 读 - 修改 - 写 Qcy = Fosc Instruction cycle = Fosc/4 PC Instruction fetched RB7:RB0 Instruction executed Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 PC PC + 1 PC + 2 PC + 3 MOVWF PORTB write to PORTB MOVF PORTB,W MOVWF PORTB write to PORTB TPD NOP Port pin sampled here MOVF PORTB,W NOP NOP 注意 : 本例显示对 PORTB 一次写指令后紧跟一条读指令. 注意到 : 数据建立时间 = (0. 25TCY-TPD ) 这里 TCY= 指令周期 TPD = 传播延时 因此, 在较高时钟频率时, 写操作后紧跟读操作可能会出现问题. 在反复读写口时要考虑口的负载 Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 196

按钮 / 按键去抖或噪声输入 Check_key ;poll for key press btfsc PORTB,1 ; goto Check_key ; ; call Debounce_delay ;debounce signal btfsc PORTB,1 ;check for bouncing input goto Check_key ; return ; Routine to give delay for key bounce routine Debounce_delay.. delay routine here. return AN 566 contains debounce Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 197

Exercise #1: 启动 PICkit TM 1 软件 Step 1: 启动软件 Start->Programs-> PICkit 1 FLASH Start Kit- > PICkit 1 FLASH Starter Kit Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 198

装载 Exercise1.hex 到缓冲 Step 2: 找到 Exercise1.hex 并装入缓冲 File->Import HEX E:/PICkit1/Exercise/ C:/Program Files/Microchip/PICkit1/Exercise/ Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 199

Step 4: 状态窗口 成功写入 Exercise1: 成功写入 Step 5: 成功装载后观察 PICkit TM 1 LED D0 应以 200ms 的速度闪烁 Write Successful Microchip Technology Incorporated. All Rights Reserved. 701 PIC PIC MCU - Introduction to Programming 200