1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Getting Started with PIC® MCU Mid-Range docx

97 323 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Getting Started with PIC® MCU Mid-Range Architecture, Instruction Set and Assembly Language Programming
Thể loại tài liệu hướng dẫn
Năm xuất bản 2007
Định dạng
Số trang 97
Dung lượng 1,38 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Instruction PipeliningO Instruction fetch is overlapped with execution of previously fetched instruction call SUB1 addwf REG2 movf PORTB,w return movf PORTC,w return SUB1 SUB2 movlw 0x0

Trang 2

Class Objective

When you finish this class you will:

workings of a PIC16

programs

Trang 3

Addressing Modes

Trang 4

microcontroller can be attributed to the following architectural features:

− Harvard Architecture

− Instruction Pipelining

− Large Register File

− Single Cycle Instructions

− Single Word Instructions

− Long Word Instructions

− Reduced Instruction Set

− Orthogonal Instruction Set

Trang 5

Harvard Architecture

Architecture:

− Fetches instructions and

data from a single memory space

− Limits operating bandwidth

− Uses two separate memory

spaces for program instructions and data

− Improved operating

bandwidth

− Allows for different bus

widths

Trang 6

Instruction Pipelining

O Instruction fetch is overlapped with execution of previously

fetched instruction

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Time to execute normal instruction

Time to execute call instruction includes pipeline flush

Trang 7

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Instruction Cycles

-Pre-Fetched Instruction Executing Instruction

Trang 8

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Instruction Cycles

Fetch

Pre-Fetched Instruction Executing Instruction

Trang 9

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Instruction Cycles

Fetch Execute

Fetch

Pre-Fetched Instruction Executing Instruction

Time to execute normal instruction

Trang 10

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Instruction Cycles

Fetch Execute

Fetch Execute

Fetch

Pre-Fetched Instruction Executing Instruction

Trang 11

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Pre-Fetched Instruction Executing Instruction

Time to execute call instruction includes pipeline flush

Trang 12

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Pre-Fetched Instruction Executing Instruction

Trang 13

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Pre-Fetched Instruction Executing Instruction

Trang 14

Instruction Pipelining

call SUB1 addwf REG2

movf PORTB,w return

movf PORTC,w return

SUB1

SUB2

movlw 0x05 MAIN

Pre-Fetched Instruction Executing Instruction

Trang 15

Long Word Instruction

8-bit Program Memory

14-bit Program Memory

1

1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 k

k k k k k k k k k k k k k k k

1

1 1 1 0 0 0 0 0 0 0 0 k k k k k k k k k k k k k k k k

8-bit Instruction on typical 8-bit MCU

Example: Freescale ‘Load Accumulator A’:

• 2 Program Memory Locations

• 2 Instruction Cycles to Execute

14-bit Instruction on PIC16 8-bit MCU

Example: ‘Move Literal to Working Register’

• 1 Program Memory Location

• 1 Instruction Cycle to Execute

O Limits

Bandwidth

O Increases

Memory Size Requirements

O Separate busses allow different widths

O 2k x 14 is roughly equivalent to 4k x 8

Trang 16

Register File Concept

Destination

Address of Second Source Operand

O Register File Concept:

All of data memory is part of the register file, so any location in data memory may be operated on directly

O All peripherals are

mapped into data memory as a series of registers

O Orthogonal

Instruction Set: ALL instructions can operate on ANY data memory location

O The Long Word

Instruction format allows a directly addressable register file

07h 08h 09h 0Ah 0Bh 0Ch 0Dh 0Eh 0Fh 10h

Trang 17

Instruction Set Overview

Trang 18

Instruction Set Overview

Trang 19

Instruction Set Overview

Trang 20

PIC16 Instruction Set

Byte Oriented Operations Bit Oriented Operations

Clear W Complement f Decrement f Decrement f, Skip if 0 Increment f

Increment f, Skip if 0 Inclusive OR W with f Move f

Move W to f

No Operation Rotate Left f through Carry Rotate Right f through Carry Subtract W from f

Swap nibbles in f Exclusive OR W with f

bcf f,b bcf f,b bsf f,b bsf f,b btfsc f,b btfsc f,b btfss f,b btfss f,b

Bit Clear f Bit Set f Bit Test f, Skip if Clear Bit Test f, Skip if Set

Literal and Control Operations

addlw k addlw k andlw k andlw k call k call k clrwdt - clrwdt - goto k goto k iorlw k iorlw k movlw k movlw k retfie - retfie - retlw k retlw k return - return - sleep - sleep - sublw k sublw k xorlw k xorlw k

Add literal and W AND literal with W Call subroutine Clear Watchdog Timer

Go to address Inclusive OR literal with W Move literal to W

Return from interrupt Return with literal in W Return from Subroutine

Go into standby mode Subtract W from literal Exclusive OR literal with W

Trang 21

PIC16 Visual Interpreter

Register File Address

d

FF

W Register

FF FF FF 18 FF FF

FF FF FF FF

00h 01h 02h 03h 04h 05h 06h 07h 08h 09h 0Ah 0Bh

Data Bus

Trang 22

Data Memory Organization

Accesses 70h – 7Fh

Accesses 70h – 7Fh

Accesses 70h – 7Fh

Accesses 70h – 7Fh

Accesses 70h – 7Fh

Accesses 70h – 7Fh Bank 0 Bank 1 Bank 2 Bank 3

PIC16F876/877 Register File Map

368 Bytes of General Purpose RAM Plus Special Function Registers

Trang 23

Data Memory Organization

INDF TMR0 PCL STATUS

FSR PORTA PORTB PORTC PORTD PORTE PCLATH

INTCON

INDF OPTION_REG PCL STATUS FSR TRISA TRISB TRISC TRISD TRISE PCLATH INTCON

INDF TMR0 PCL STATUS FSR

PORTB

PCLATH INTCON

INDF OPTION_REG PCL STATUS FSR

TRISB

PCLATH INTCON PIR1 PIE1 EEDATA EECON1

Bank 0 Bank 1 Bank 2 Bank 3 000

100 101 102 103 104 105 106 107 108 109 10A 10B 10C 10D

180 181 182 183 184 185 186 187 188 189 18A 18B 18C 18D Device Specific Registers

Trang 24

RP1:RP0: Register Bank Select Bits (used for direct addressing)

00 = Bank 0, 01 = Bank 1, 10 = Bank 2, 11 = Bank 3

TO: Time-out bit

0 = A WDT time-out occurred

PD: Power-down bit

0 = SLEEP instruction executed

Z: Zero bit

1 = Result of arithmetic operation is zero

DC: Digit cary / borrow bit

1 = Carry out of 4 th low order bit occurred / No borrow occurred

C: Carry / borrow bit

1 = Carry out of MSb occurred / No borrow occurred

Trang 25

PIC16 Addressing Modes

− Immediate (Literal) movlw <constant>

Trang 26

Register Direct Addressing

FF FF FF 18 FF FF

FF FF FF 1C FF FF

00h 01h 02h 03h 04h 05h

7Ah 7Bh 7Ch 7Dh 7Eh 7Fh

RP1

Bank 1 Bank 2 Bank 3

2-bits from

STATUS Register 7-bits Encoded in Instruction

9-bit Effective Address (Use this when coding)

Bank 0

RP0 ‘f’ Operand

FF FF FF

FF FF FF FF FF FF

FF FF FF FF FF FF

FF FF FF FF FF FF

0 0 0 0 0 0

Register File Address Bus

Trang 27

FF FF FF 38 FF FF FF FF

FF FF FF FF

Register Direct Addressing

80h : INDF 81h : OPTION 82h : PCL 83h : STATUS

Bank 1

FF FF FF 38

Address

FF FF FF FF

FF FF FF FF

84h : FSR 85h : TRISA 86h : TRISB 87h : TRISC

Bank 0 INDF: 00h

TMR0: 01h PCL : 02h STATUS: 03h Address

FSR: 04h PORTA: 05h PORTB: 06h PORTC: 07h

20h

22h 21h

23h

A0h

A2h A1h

9-Bit Effective Address:

7-bits from Instruction RP0

RP1

Bin Dec Hex

Example: Initialize bits 0-3 of

PORTB as outputs

F0

W Register:

Trang 28

Register Indirect Addressing

FF

FF FF FF 1C FF FF

000h 001h 002h 003h 004h 005h

0FAh 0FBh 0FCh 0FDh 0FEh 0FFh

IRP

Bank 2,3

1-bit from

STATUS Register 8-bits from FSR Register

9-bit Effective Address (Use this when coding)

Bank 0,1

FSR

FF FF FF FF FF FF

FF FF FF FF FF FF

0 0 0 0 0 0

Register File Address Bus

100h 101h 102h 103h 104h 105h

1FAh 1FBh 1FCh 1FDh 1FEh 1FFh

Trang 29

00 FF

Register Indirect Addressing

00h : INDF

02h : PCL 03h : STATUS 04h : FSR

20h 21h 22h 23h

80h 7Fh 7Eh 7Dh

Register File

LOOP

movlw 0x20 movwf FSR clrf INDF incf FSR,f btfss FSR,7 goto LOOP

<next instruction>

FF 18 80

Address

00 00 00 00

00 00 00 FF

01h : TMR0 Example: Clear all RAM locations from 20h to 7Fh

bcf STATUS,IRP

0 0 0 0 0 0 0 0 0

9-Bit Effective Address:

FSR IRP

20

W Register:

Trang 30

Program Memory Organization

concern when using

the call or goto

Trang 31

Program Counter

0 0 0 0 0

0 1 2 3 4 5 6 7 8 9 10 11 12

PCL PCH

0 0 0 0 0 0 0 0

Program Counter

− Interrupts

− Instructions: CALL, GOTO, RETURN, RETLW, RETFIE

− Any instruction that uses the PCL register as an operand

Trang 32

PC Absolute Addressing

0 1 2 3 4 5 6 7 8 9 10 11 12

Opcode 0 0 0 0 0

CALL and GOTO Instructions:

13

0 0 0 0 0 0

− Jump to another program memory location out of PC sequence

− Call a subroutine

− 11-bits of the required 13 address bits are encoded in the

instruction

− 2 additional bits will come from the PCLATH register

− Address to jump to is calculated by the program

− Computed address is written directly into the Program Counter

Trang 33

0 0

11 12

PC Absolute Addressing

0 1 2 3 4 5 6 7 8 9 10 11 12

PCLATH Register in Data Memory

14-Bit CALL or GOTO Instruction in Program Memory

0 1 2 3 4 5 6 7 8 9 10

0 0 0 0 0 0 0 0 0 0 0

13-Bit Program Counter

2-Bits From PCLATH

11-Bits From Instruction

Trang 34

<do something useful>

… return

Example: Jumping to code located in a different program memory page.

0 0 0 0 0

0 1 2 3 4 5 6

7

PCLATH Register

0 1 2 3 4 5 6 7 8 9 10 11 12

Opcode 0 0 0 0 0

13

0 0 0 0 0 0 CALL Instruction in Program Memory

FF

W Register Program Counter - PCH:PCL

0 0 0 0 0 0 0 0 0 0 0

org 0x0020

Trang 35

CALL / RETURN Stack

13-bit Program Counter

13-bit x 8-Level Return Address Stack

movlw HIGH MySub1 movwf PCLATH

call MySub1 call MySub4

… bsf PORTB,0 call MySub2 return

bsf PORTB,1 call MySub3 return

bsf PORTB,2 return

bsf PORTB,3 call MySub2 return

bsf PORTB,7 MySub1

0 1 2 3 4 5 6 7

Trang 36

8-bit Data Bus

(PCH will be loaded with

value from PCLATH)

PCLATH

movlw HIGH 0x1250 movwf PCLATH

movlw LOW 0x1250 movwf PCL

FF

W Register

Trang 37

PC Relative Addressing: Lookup

Table

Example: Use a lookup

table with relative

addressing to retrieve the

bit pattern to display a digit

on a 7-segment LED

ORG 0x0020 ;Page 0 movlw HIGH SevenSegDecode movwf PCLATH

movlw 5 call SevenSegDecode movwf PORTB

… ORG 0x1800 ;Page 3 SevenSegDecode:

addwf PCL,f retlw b’00111111’ ;0 retlw b’00000110’ ;1 retlw b’01011011’ ;2 retlw b’01001111’ ;3 retlw b’01100110’ ;4 retlw b’01101101’ ;5 retlw b’01111101’ ;6 retlw b’00000111’ ;7 retlw b’01111111’ ;8 retlw b’01101111’ ;9

Trang 38

Special Features

Overview

Trang 39

Configuration Word

bit 0 bit 1

of the program counter

− Code Protection

− Oscillator Mode

− Watchdog Timer

− Power Up Timer

− Brown Out Reset

− Low Voltage Programming

− Flash Program Memory Write

Trang 40

PIC16 Oscillator Options

− LP Oscillator designed to draw least amount of current

− RC or INTRC provide ultra low cost oscillator solution

− XT optimized for most commonly used oscillator frequencies

− HS optimized to drive high frequency crystals or resonators

Trang 41

O OST: Oscillator Start-up Timer

cycles to allow crystal or resonator to stabilize in frequency and amplitude; not active in RC modes; used only after POR or Wake Up from SLEEP

Trang 42

Events that wake processor from sleep

O The processor can be put into a power-down

mode by executing the SLEEP instruction

− System oscillator is stopped

− Processor status is maintained (static design)

− Watchdog timer continues to run, if enabled

− Minimal supply current is drawn mostly due to leakage (0.1

Comparator Output Change Interrupt Input Capture Event

PORTB Interrupt on Change Synchronous Serial Port (I 2 C™ Mode) Start / Stop Bit Detect Interrupt

Synchronous Serial Port (I 2 C™ Mode) Start / Stop Bit Detect Interrupt

Parallel Slave Port Read or Write

Trang 43

Watchdog Timer

Trang 44

BOR –Brown Out Reset

particular threshold, the device is held in reset

operation

circuitry

Trang 45

PBOR – Programmable

Brown Out Reset

O Configuration Option (set at program time)

− Cannot be enabled / disabled in software

O Four selectable BV DD trip points:

− 2.5V – Minimum V DD for OTP PIC ® MCUs

Trang 46

(P)BOR – Brown Out Reset

O Holds PIC ® MCU in reset until ~72ms after V DD rises back above threshold

Trang 48

In-Circuit Serial Programming™

O Only two pins required for

Function

Programming Voltage = 13V Supply Voltage

Ground Clock Input Data I/O & Command Input

MCLR/V PP

V DD

V SS

RB6 RB7

ICSP™ Connector

Trang 49

I/O Ports

bit manipulation

direction control under software

startup

analog inputs on startup

Trang 50

I/O Pin Conceptual Diagram

Bit 1 of TRISB Register

PORTB Bit 1 Latch

RB1 Bit 1 of

Data Bus

Read Operation

Write Operation

movwf PORTB

movf PORTB,w

1 = RB1 is input

0 = RB1 is output

Trang 51

I/O Ports

direction of Bit n in PORTx

Trang 52

Hands-on Exercises

Trang 53

MPLAB ® ICD2

Trang 54

PICDEM™ 2 Plus Board

Trang 55

MPASM™ Assembler

Template

want; these are just examples

LIST p=16f877a ;Explicitly declare processor

#include <p16f877a.inc> ;Include register label definitions

org 0x0000 ;Put next line of code at address 0x0000 RESET_V goto START ;Reset Vector

org 0x0004 ;Put next line of code at address 0x0004 INT_V retfie ;Interrupt Vector

START {Begin your code here} ;Your code goes here

END ;Tell MPASM that this is the end

LIST p=16f877a ;Explicitly declare processor

#include <p16f877a.inc> ;Include register label definitions

org 0x0000 ;Put next line of code at address 0x0000

RESET_V goto START ;Reset Vector

org 0x0004 ;Put next line of code at address 0x0004

INT_V retfie ;Interrupt Vector

START {Begin your code here} ;Your code goes here

END ;Tell MPASM that this is the end

Trang 56

Radix MPASM Syntax

Specifying the Radix

O By default, MPASM™ assembler expects numbers in hexadecimal

O Default can be changed through IDE or by adding r=hex or r=dec

as a parameter to the LIST directive:

O Good programming practice suggests that a number’s radix be

specified explicitly:

LIST p=16f877a, r=dec LIST p=16f877a, r=dec

Binary Binary b’10101010’ b’10101010’

Decimal Decimal d’25’ or 25 d’25’ or 25 Hexadecimal

Hexadecimal h’2A’ or 0x2A h’2A’ or 0x2A

Trang 57

Lab 1: The Task

of PORTB (RB0)

Trang 58

Lab 1: Program Structure

Switch to Bank 0

Trang 59

Lab 1: Template

Trang 60

Lab 1: Solution

Trang 61

Lab 1: Results

equivalent of while(1) in C)

Trang 62

Lab 2: The Task

of PORTB (RB0) blink

Trang 63

Lab 2: The Task

blinking slow enough for the

human eye

sufficient to implement the delay

Trang 64

Naming Registers/Constants

MyReg0 equ 0x20 ;MyReg0 = 0x20

MyReg1 equ 0x21 ;MyReg1 = 0x21

MyReg2 equ 0x23 ;MyReg2 = 0x23

Trang 65

Lab 2: Program Structure

Taken from Lab 1

1 Instruction

1 Instruction Subroutine Call

1 Instruction

1 Instruction Subroutine Call

Trang 66

Lab 2: Program Structure

Trang 67

Lab 2: Template – Part 1

Trang 68

Lab 2: Template – Part 2

Trang 69

Lab 2: Solution – Part 1

Trang 70

Lab 2: Solution – Part 2

Trang 71

Lab 2: Results

Trang 72

Lab 3: The Task

instructions, “move” the

illuminated LED across the lower

4 bits of PORTB When it

reaches one side, send it back to the start.

RB0 RB1

RB2 RB3

Trang 73

Lab 3: Program Structure

1 Instruction

1 Instruction Same setup code from Lab 1

1 Instruction

1 Instruction

1 Instruction Call same subroutine from Lab 2

Rember: The rotate

instructions operate

on 9-bits, with the

Carry bit in the

STATUS register as

the 9 th bit

Trang 74

Lab 3: Template – Part 1

Trang 75

Lab 3: Template – Part 2

Trang 76

Lab 3: Solution – Part 1

Trang 77

Lab 3: Solution – Part 2

Trang 78

Lab 3: Results

instructions

Trang 79

Lab 4: The Task

make the direction of rotation

change when the LED is rotated

to either end

Trang 80

Lab 4: Program Structure

Set Carry Bit

Delay

Rotate Left PORTB

RB3 = 1?

Yes No

Setup PORTB START

Rotate Right PORTB

2 Instructions

1 Instruction

1 Instruction Subroutine Call

2 Instructions

Trang 81

Lab 4: Template

O Setup is identical to Lab 3 up to the LOOP

Lab 3: Rotating LED - Continued 18

bsf STATUS,C ;Set carry bit for initial rotate

LEFT {1 st Instruction} ;Rotate PORTB to left

{2 nd Instruction} ;Call delay routine {3 rd Instruction} ;Is the LED on RB3 (PORTB,3) on?

{4 th Instruction} ;if no, rotate left again

RIGHT {5 th Instruction} ;Rotate PORTB to right

{6 th Instruction} ;Call delay routine {7 th Instruction} ;Is the LED on RB0 (PORTB,0) on?

{8 th Instruction} ;if no, rotate right again {9 th Instruction} ;if yes, rotate left

DELAY decfsz COUNTERL ;Decrement COUNTERL

goto DELAY ;If not zero, keep decrementing COUNTERL decfsz COUNTERH ;Decrement COUNTERH

goto DELAY ;If not zero, decrement COUNTERL again return ;Return to main subroutine

bsf STATUS,C ;Set carry bit for initial rotate

LEFT {1 st Instruction} ;Rotate PORTB to left

{2 nd Instruction} ;Call delay routine

{3 rd Instruction} ;Is the LED on RB3 (PORTB,3) on?

{4 th Instruction} ;if no, rotate left again

RIGHT {5 th Instruction} ;Rotate PORTB to right

{6 th Instruction} ;Call delay routine

{7 th Instruction} ;Is the LED on RB0 (PORTB,0) on?

{8 th Instruction} ;if no, rotate right again

{9 th Instruction} ;if yes, rotate left

DELAY decfsz COUNTERL ;Decrement COUNTERL

goto DELAY ;If not zero, keep decrementing COUNTERL decfsz COUNTERH ;Decrement COUNTERH

goto DELAY ;If not zero, decrement COUNTERL again return ;Return to main subroutine

END

Ngày đăng: 17/02/2014, 14:20

w