You are on page 1of 96

101 ASP

Getting Started with


PICmicro® Mid-Range
Architecture, Instruction Set and
Assembly Language Programming

© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 1


Class Objective
When you finish this class you will:
 Understand the basics of the inner
workings of a PIC16
 Understand most instructions
 Understand memory organization
 Understand how to write simple
programs

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 2
Agenda
 Architecture Basics
 Instruction Set Overview
 Memory Organization and
Addressing Modes
 Special Features
 Hands-on Exercises

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 3
Architecture
 The high performance of the PICmicro®
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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 4
Harvard Architecture
Von Neumann
Architecture
 Von Neumann
Architecture:
 Fetches instructions and
8-bit Program
data from a single memory
Bus & Data space
Memory
CPU  Limits operating bandwidth

Harvard
Architecture
 Harvard Architecture:
 Uses two separate memory
spaces for program
8-bit Data instructions and data
Bus Memory
CPU  Improved operating
14-bit bandwidth
Bus
Program  Allows for different bus
Memory widths

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 5
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

movlw 0x05 -
Instruction Cycles

T0
Example Program
1 MAIN movlw 0x05 Fetch

2 movwf REG1
3 call SUB1
4 addwf REG2

51 SUB1 movf PORTB,w


52 return
53 SUB2 movf PORTC,w
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 7
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

movwf REG1 movlw 0x05


Instruction Cycles

T0 T1
Example Program
1 MAIN movlw 0x05 Fetch Execute

2 movwf REG1 Fetch


3 call SUB1
4 addwf REG2

51 SUB1 movf PORTB,w


52 return
53 SUB2 movf PORTC,w
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 8
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

call SUB1 movwf REG1


Instruction Cycles

T0 T1 T2
Example Program
1 MAIN movlw 0x05 Fetch Execute Time to execute normal instruction

2 movwf REG1 Fetch Execute


3 call SUB1 Fetch

4 addwf REG2

51 SUB1 movf PORTB,w


52 return
53 SUB2 movf PORTC,w
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 9
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

addwf REG2 call SUB1


Instruction Cycles

T0 T1 T2 T3
Example Program
1 MAIN movlw 0x05 Fetch Execute

2 movwf REG1 Fetch Execute


3 call SUB1 Fetch Execute

4 addwf REG2 Fetch

51 SUB1 movf PORTB,w


52 return
53 SUB2 movf PORTC,w
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 10
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

movf PORTB,w call SUB1


Instruction Cycles

T0 T1 T2 T3 T4
Example Program
1 MAIN movlw 0x05 Fetch Execute

2 movwf REG1 Fetch Execute Time to execute call


instruction includes
3 call SUB1 Fetch Execute pipeline flush

4 addwf REG2 Fetch Flush

51 SUB1 movf PORTB,w Fetch

52 return
53 SUB2 movf PORTC,w
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 11
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

return movf PORTB,w


Instruction Cycles

T0 T1 T2 T3 T4 T5
Example Program
1 MAIN movlw 0x05 Fetch Execute

2 movwf REG1 Fetch Execute


3 call SUB1 Fetch Execute

4 addwf REG2 Fetch Flush

51 SUB1 movf PORTB,w Fetch Execute

52 return Fetch
53 SUB2 movf PORTC,w
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 12
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

movf PORTC,w return


Instruction Cycles

T0 T1 T2 T3 T4 T5 T6
Example Program
1 MAIN movlw 0x05 Fetch Execute

2 movwf REG1 Fetch Execute


3 call SUB1 Fetch Execute

4 addwf REG2 Fetch Flush

51 SUB1 movf PORTB,w Fetch Execute

52 return Fetch Execute


53 SUB2 movf PORTC,w Fetch
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 13
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction

addwf REG2 return


Instruction Cycles

T0 T1 T2 T3 T4 T5 T6 T7
Example Program
1 MAIN movlw 0x05 Fetch Execute

2 movwf REG1 Fetch Execute


3 call SUB1 Fetch Execute

4 addwf REG2 Fetch Flush Fetch

51 SUB1 movf PORTB,w Fetch Execute

52 return Fetch Execute


53 SUB2 movf PORTC,w Fetch Flush
54 return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 14
Long Word Instruction
8-bit Program Memory 8-bit Instruction on typical 8-bit MCU
Example: Freescale ‘Load Accumulator A’:
• 2 Program Memory Locations
• 2 Instruction Cycles to Execute
 Limits Bandwidth
ldaa #k  Increases Memory
1 0 0 0 0 1 1 0 Size Requirements
k k k k k k k k

14-bit Program Memory 14-bit Instruction on PIC16 8-bit MCU


Example: ‘Move Literal to Working Register’
• 1 Program Memory Location
• 1 Instruction Cycle to Execute

movlw k
1 1 0 0 0 0 k k k k k k k k

 Separate busses allow different widths


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

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 15
Register File Concept
Data Memory  Register File Concept:
(Register File)
All of data memory is
part of the register
w f 07h
file, so any location in
ALU 08h
data memory may be
09h
operated on directly

Data Bus
0Ah

0Bh  All peripherals are


d
w f 0Ch mapped into data
0Dh memory as a series of
0Eh registers
0Fh
 Orthogonal
10h
W Instruction Set: ALL
instructions can
operate on ANY data
memory location
Decoded Instruction
from Program Opcode d Address  The Long Word
Memory: Instruction format
Arithmetic/Logic Address of Second allows a directly
Function to be Performed Result Source Operand addressable register
Destination
file
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 16
Instruction Set Overview
Byte Oriented Operations
13 8 7 6 0

Opcode f f f f f f f

Opcode d f f f f f f f
File Register Address

Destination (W or F)

ADDWF 0x25, W

File Register Address Destination

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 17
Instruction Set Overview
Bit Oriented Operations

13 10 9 7 6 0

Opcode b b b f f f f f f f
File Register Address

Bit Position (0-7)

BSF 0x25, 3

File Register Address Bit Position

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 18
Instruction Set Overview
Literal and Control Operations
13 10 8 7 0

Opcode

Opcode k k k k k k k k

Opcode k k k k k k k k k k k
Literal Value

MOVLW 0x55

Literal Value

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 19
PIC16 Instruction Set
Byte Oriented Operations Bit Oriented Operations
addwf f,d Add W and f bcf f,b Bit Clear f
andwf f,d AND W with f bsf f,b Bit Set f
clrf f Clear f btfsc f,b Bit Test f, Skip if Clear
clrw - Clear W btfss f,b Bit Test f, Skip if Set
comf f,d Complement f Literal and Control Operations
decf f,d Decrement f addlw k Add literal and W
decfsz f,d Decrement f, Skip if 0 andlw k AND literal with W
incf f,d Increment f call k Call subroutine
incfsz f,d Increment f, Skip if 0 clrwdt - Clear Watchdog Timer
iorwf f,d Inclusive OR W with f goto k Go to address
movf f,d Move f iorlw k Inclusive OR literal with W
movwf f Move W to f movlw k Move literal to W
nop - No Operation retfie - Return from interrupt
rlf f,d Rotate Left f through Carry retlw k Return with literal in W
rrf f,d Rotate Right f through Carry return - Return from Subroutine
subwf f,d Subtract W from f sleep - Go into standby mode
swapf f,d Swap nibbles in f sublw k Subtract W from literal
xorwf f,d Exclusive OR W with f xorlw k Exclusive OR literal with W
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 20
PIC16 Visual Interpreter
ADDLW 0x0A , Execute Reset

Literal Data from Data


Hex Bus Register File Address
Dec
Instruction Word
Bin FF 00h
FF 01h STATUS
FF 02h 2 1 0
18 03h 1 0 0
w f
FF 04h Z DC C
ALU
FF 05h
d 06h
w f 07h
FF 08h
FF 09h
FF
FF 0Ah
W Register
FF 0Bh
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 21
Data Memory Organization
PIC16F876/877 Register File Map
368 Bytes of General Purpose RAM Plus Special Function Registers

000h 080h 100h 180h


SFR SFR SFR SFR
10Fh 18Fh
110h 190h

01Fh 09Fh
020h 0A0h

128 Bytes

GPR GPR GPR GPR


96 Bytes 80 Bytes 96 Bytes 96 Bytes

0EFh 16Fh 1EFh


Accesses Accesses Accesses
07Fh 0FFh 70h – 7Fh 17Fh 70h – 7Fh 1FFh 70h – 7Fh
Bank 0 Bank 1 Bank 2 Bank 3

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 22
Data Memory Organization
Bank 0 Bank 1 Bank 2 Bank 3
000 INDF 080 INDF 100 INDF 180 INDF

001 TMR0 081 OPTION_REG 101 TMR0 181 OPTION_REG

002 PCL 082 PCL 102 PCL 182 PCL

003 STATUS 083 STATUS 103 STATUS 183 STATUS

004 FSR 084 FSR 104 FSR 184 FSR

005 PORTA 085 TRISA 105 185


006 PORTB 086 TRISB 106 PORTB 186 TRISB

007 PORTC 087 TRISC 107 187


008 PORTD 088 TRISD 108 188
009 PORTE 089 TRISE 109 189
00A PCLATH 08A PCLATH 10A PCLATH 18A PCLATH

00B INTCON 08B INTCON 10B INTCON 18B INTCON

00C PIR1 08C PIE1 10C EEDATA 18C EECON1

00D PIR2 08D PIE2 10D EEADR 18D EECON2

Device Specific Registers


© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 23
STATUS Register
IRP RP1 RP0 TO PD Z DC C
bit 7 bit 0

IRP: Register Bank Select (used for Indirect addressing)


0 = Bank 0, 1 1 = Bank 2, 3
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 4th low order bit occurred / No borrow occurred
C: Carry / borrow bit
1 = Carry out of MSb occurred / No borrow occurred

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 24
PIC16 Addressing Modes
 Data Memory Access:
 Directaddwf <data_address>, <d>

 Indirect addwf INDF, <d>

 Immediate (Literal) movlw <constant>

 Program Memory Access:


 Absolute goto <program_address>

 Relative addwf PCL,f

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 25
Register Direct Addressing
2-bits from 9-bit Effective Address
STATUS Register 7-bits Encoded in Instruction (Use this when coding)

0 0 0 0 0 0 0 0 0 0x183
RP1 RP0 ‘f’ Operand

Bank 0 Bank 1 Bank 2 Bank 3


00h FF FF
01h FF FF
02h FF FF
03h 18 1C
04h FF FF
Address

05h FF FF

7Ah FF FF FF FF Register File


Address Bus
7Bh FF FF FF FF
7Ch FF FF FF FF
7Dh FF FF FF
7Eh FF FF FF
7Fh FF FF FF
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 26
Register Direct Addressing
Example: Initialize bits 0-3 of Register File
PORTB as outputs Address Bank 0 Bank 1 Address
INDF: 00h FF
FF FF 80h : INDF
W Register:
TMR0: 01h FF
FF FF 81h : OPTION
F0
PCL : 02h FF
FF FF 82h : PCL
9-Bit Effective Address: STATUS: 03h 38 38 83h : STATUS
38
00 1 00 00 0 0 0 0 0 FSR: 04h FF
FF FF 84h : FSR
RP1 RP0 7-bits from Instruction
PORTA: 05h FF
FF FF 85h : TRISA
PORTB: 06h FF
FF FF 86h : TRISB

bsf STATUS,RP0 PORTC: 07h FF


FF FF 87h : TRISC

movlw b’11110000’ 20h FF


FF FF A0h
movwf TRISB 21h FF
FF FF A1h
bcf STATUS,RP0 22h FF
FF FF A2h
clrf PORTB 23h FF
FF FF A3h

Bin Dec Hex

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 27
Register Indirect
1-bit from
Addressing 9-bit Effective Address
STATUS Register 8-bits from FSR Register (Use this when coding)

0 0 0 0 0 0 0 0 0 0x1FC
IRP FSR

Bank 0,1 Bank 2,3


000h 100h FF
001h 101h FF
002h 102h FF
003h 103h 1C
004h 104h FF
005h FF 105h FF

0FAh FF 1FAh FF Register File


Address Bus
0FBh FF 1FBh FF
0FCh FF 1FCh FF
0FDh FF 1FDh FF
0FEh FF 1FEh FF
0FFh FF 1FFh FF

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 28
Register Indirect Addressing
Register File Address
Example: Clear all RAM locations from 20h to 7Fh
00 00h : INDF
W Register:
FF 01h : TMR0
20
FF 02h : PCL
9-Bit Effective Address:
18 03h : STATUS
0 0 0 0 0 0 0 0 0
80 04h : FSR
IRP FSR

bcf STATUS,IRP 00 20h


00 21h
movlw 0x20
00 22h
movwf FSR 00 23h
LOOP clrf INDF
incf FSR,f 00 7Dh

btfss FSR,7 00 7Eh


00 7Fh
goto LOOP
FF 80h
<next instruction>
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 29
Program Memory Organization
14-bits
0000h Reset Vector
 Program memory is
divided into four 2k×14 0004h Interrupt Vector 2k

pages Page 0
PCH = 00h
0800h
 Required to maintain
single word/single cycle Page 1 2k
PCH = 08h
execution
1000h
 Paging is only a
concern when using the Page 2 2k
PCH = 10h
call or goto instructions,
or when directly 1800h

modifying the program Page 3 2k


PCH = 18h
counter
1FFFh
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 30
Program Counter
PCH PCL
12 11 10 9 8 7 6 5 4 3 2 1 0

Program Counter 0 0 0 0 0 0 0 0 0 0 0 0 0

 13-bit PC can access up to 213 = 8192 words


 Contains address of NEXT instruction (pipelining)
 Lower byte accessible in data memory as PCL
 Upper byte indirectly accessible via PCLATH
 Runs freely across page boundaries
 Events that modify PC out of sequence:
 Interrupts
 Instructions: CALL, GOTO, RETURN, RETLW, RETFIE
 Any instruction that uses the PCL register as an operand

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 31
PC Absolute Addressing
CALL and GOTO Instructions:
13 12 11 10 9 8 7 6 5 4 3 2 1 0

Opcode 0 0 0 0 0 0 0 0 0 0 0

 PC Absolute Addressing (Program Memory)


 Jump to another program memory location out of PC sequence
 Call a subroutine
 Used by the CALL and GOTO instructions
 11-bits of the required 13 address bits are encoded in the instruction
 2 additional bits will come from the PCLATH register
 Used when performing Computed Goto operation
 Address to jump to is calculated by the program
 Computed address is written directly into the Program Counter

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 32
PC Absolute Addressing
14-Bit CALL or GOTO Instruction in Program Memory
13 12 11 10 9 8 7 6 5 4 3 2 1 0

Opcode 0 0 0 0 0 0 0 0 0 0 0

PCLATH Register in Data Memory


7 6 5 4 3 2 1 0

- - - 0 0 0 0 0

11-Bits From Instruction


2-Bits From PCLATH
12 11 10 9 8 7 6 5 4 3 2 1 0

0 0 0 0 0 0 0 0 0 0 0 0 0
PCH
PCH PCL
PCL
13-Bit Program Counter

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 33
PC Absolute Addressing
Example: Jumping to code located in a different program memory page.

PCLATH Register CALL Instruction in Program Memory


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

- - - 0 0 0 0 0 Opcode 0 0 0 0 0 0 0 0 0 0 0

W Register Program Counter - PCH:PCL

FF - 0 0 0 0 0 0 0 0 0 0 0 0 0

org 0x0020
movlw HIGH MySubroutine
movwf PCLATH
call MySubroutine

org 0x1250
MySubroutine <do something useful>

return
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 34
CALL / RETURN Stack
13-bit Program Counter
0020 movlw HIGH MySub1
0021 movwf PCLATH 0020
0022 call MySub1
0023 call MySub4
0
0024 bsf PORTB,7
… … 1
1000 MySub1 bsf PORTB,0
2
1001 call MySub2
1002 return 3
1003 MySub2 bsf PORTB,1 4
1004 call MySub3
5
1005 return
1006 MySub3 bsf PORTB,2 6
1007 return
7
1008 MySub4 bsf PORTB,3
13-bit x 8-Level
1009 call MySub2 Return Address Stack
100A return

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 35
PC Relative Addressing
W Register FF
To write to PC:
8-bit Data Bus
 Write high byte to
PCLATH PCLATH
FF
 Write low byte to PCL PCH PCL

(PCH will be loaded with FF FF


value from PCLATH)
movlw HIGH 0x1250
movwf PCLATH
movlw LOW 0x1250
movwf PCL

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 36
PC Relative Addressing: Lookup
Table
ORG 0x0020 ;Page 0
movlw HIGH SevenSegDecode
Example: Use a lookup movwf PCLATH
table with relative movlw .5
addressing to retrieve the call SevenSegDecode
bit pattern to display a digit movwf PORTB
on a 7-segment LED …
ORG 0x1800 ;Page 3
SevenSegDecode:
addwf PCL,f
retlw b’11000000’ ;0 = C0
retlw b’11111001’ ;1 = F9
retlw b’10100100’ ;2 = A4
PIC® MCU

retlw b’10110000’ ;3 = B0
retlw b’10011001’ ;4 = 99
retlw b’10010010’ ;5 = 92
retlw b’10000010’ ;6 = 82
retlw b’11111000’ ;7 = F8
retlw b’10000000’ ;8 = 80
retlw b’10010000’ ;9 = 90
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 37
Special Features
Overview

© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 38


Configuration Word
CP - DEBUG WRT1 WRT0 CPD LVP BOREN - - PWRTEN WDTEN FOSC1 FOSC0
bit 1 bit 0

 Located in program memory space, outside the reach


of the program counter
 Used to setup device options:
 Code Protection
 Oscillator Mode
 Watchdog Timer
 Power Up Timer
 Brown Out Reset
 Low Voltage Programming
 Flash Program Memory Write
 Only readable at program time on most PIC16 devices
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 39
PIC16 Oscillator Options
XT
XT Standard frequency crystal oscillator 100kHz - 4MHz
4MHz
HS
HS High frequency crystal
crystal oscillator
oscillator 4MHz - 20MHz
LP
LP Low frequency crystal
crystal oscillator
oscillator 5kHz - 200kHz
RC
RC External RC oscillator
oscillator DC - 4MHz
INTRC
INTRC Internal RC oscillator
oscillator 4 or 8 MHz  2%
2%

 Selectable clock options provide greater flexibility for


the designer:
 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
 Speed ranges are guidelines only

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 40
POR, OST, PWRT
 POR: Power On Reset
VDD
DD
 With MCLR tied to VDD, a reset
pulse is generated when VDD MCLR
rise is detected TPWRT
PWRT
PWRT
 PWRT: Power Up Timer
TOST

OST
Device is held in reset for 72ms OST
(nominal) to allow VDD to rise to
an acceptable level (after POR
only) Reset Execution
 OST: Oscillator Start-up Timer
 Holds device in reset for 1024
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

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 41
Sleep Mode
 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 - 2.0A
typical)

Events that wake processor from sleep


MCLR Master Clear Pin Asserted (pulled low)
WDT Watchdog Timer Timeout
INT INT Pin Interrupt
TMR1 Timer 1 Interrupt (or also TMR3 on PIC18)
ADC A/D Conversion Complete Interrupt
CMP Comparator Output Change Interrupt
CCP Input Capture Event
PORTB PORTB Interrupt on Change
SSP Synchronous Serial Port (I22C Mode) Start / Stop Bit Detect Interrupt
PSP Parallel Slave Port Read or Write
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 42
Watchdog Timer
 Helps recover from software malfunction
 Uses its own free-running on-chip RC oscillator
 WDT is cleared by CLRWDT instruction
 Enabled WDT cannot be disabled by software
 WDT overflow resets the chip
 Programmable timeout period: 18ms to 3.0s typical
 Operates in SLEEP; on time out, wakes up CPU

WDT Ripple
Postscaler
Counter

8:1 Mux
WDTEN
Configuration Bit
WDT Timeout
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 43
BOR –Brown Out Reset
 When voltage drops below a
particular threshold, the device is
held in reset
 Prevents erratic or unexpected
operation
 Eliminates need for external BOR
circuitry
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 44
PBOR – Programmable
Brown Out Reset
 Configuration Option (set at program time)
 Cannot be enabled / disabled in software
 Four selectable BVDD trip points:
 2.5V – Minimum VDD for OTP PICmicro® MCUs
 2.7V
 4.2V
 4.5V
 For other thresholds, use an external supervisor
(MCP1xx, MCP8xx/TCM8xx, or TC12xx)

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 45
(P)BOR – Brown Out Reset
 Holds PICmicro® MCU in reset until ~72ms after VDD rises back above
threshold
VDD
BVDD

72ms
Internal
Reset

VDD
BVDD

72ms
Internal
Reset <72ms

VDD
BVDD

72ms
Internal
Reset

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 46
PLVD – Programmable Low
Voltage Detect
 Early warning VDD LVDIN

before brown out LVDCON

 16 selectable trip

16-bit Multiplexer
points:
 1.8V up to 4.5V LVDIF
in 0.1 to 0.2V
steps
 External analog
input VREF
LVDIN
 Internal VREF
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 47
In-Circuit Serial
Programming™
Pin
Pin Function
Function
 Only two pins required for VPP Programming Voltage = 13V
programming PP
VDD Supply Voltage
DD
 Convenient for In-System VSS
SS Ground
Programming of RB6 Clock Input
 Calibration Data RB7 Data I/O & Command Input
 Serialization Data
 Supported by MPLAB® PM3 & ICD2
Application PCB VDD VDD

MCLR/VPP

ICSP™ Connector
ICSP Connector
PIC16Fxxx

VDD

VSS

RB6

RB7

Isolation
To application circuit
circuits

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 48
I/O Ports
VDD
Data Bus D Q
 High Drive Capability Write PORTx
Q

 Can directly drive LEDs RESET


I/O Pin
D Q
 Direct, single cycle Write TRISx
Q
bit manipulation VSS

 Each pin has individual Read PORTx

direction control under software


 All pins have ESD protection diodes
 Pin RA4 is usually open drain
 All I/O pins default to inputs (high impedance) on startup
 All pins multiplexed with analog functions default to
analog inputs on startup

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 49
I/O Pin Conceptual Diagram

Bit 1 of TRISB
Register
1 = RB1 is input
movwf PORTB PORTB 0 = RB1 is output
Write Bit 1
Operation Latch

RB1
Bit 1 of
Data Bus

Read
Operation
movf PORTB,w

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 50
I/O Ports
I/O Pin Direction Control
TRISB 1 1 0 1 1 0 0 0

PORTB In In Out In In Out Out Out

 Bit n in TRISx controls the data


direction of Bit n in PORTx
 1 = Input, 0 = Output
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 51
Hands-on
Exercises

© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 52


MPLAB® ICD2

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 53
PICDEM™ 2 Plus Board

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 54
MPASM™ Assembler
Template
MPASM
MPASM Program
Program Template
Template
11 LIST
LIST p=16f877a
p=16f877a ;Explicitly
;Explicitly declare
declare processor
processor
22
33 #include
#include <p16f877a.inc>
<p16f877a.inc> ;Include
;Include register
register label
label definitions
definitions
44
55 org
org 0x0000
0x0000 ;Put
;Put next
next line
line of
of code
code at
at address
address 0x0000
0x0000
66 RESET_V
RESET_V goto
goto START
START ;Reset Vector
;Reset Vector
77
88 org
org 0x0004
0x0004 ;Put
;Put next
next line
line of
of code
code at
at address
address 0x0004
0x0004
99 INT_V
INT_V retfie
retfie ;Interrupt
;Interrupt Vector
Vector
10
10
11
11 START
START {Begin
{Begin your
your code
code here}
here} ;Your
;Your code
code goes
goes here
here
12
12
13
13 END
END ;Tell
;Tell MPASM
MPASM that
that this
this is
is the
the end
end

 If not using interrupts, lines 8 and 9 could be omitted


 The labels in the left column may be anything you
want; these are just examples

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 55
Specifying the Radix
 By default, MPASM™ assembler expects numbers in hexadecimal
 Default can be changed through IDE or by adding r=hex or r=dec
as a parameter to the LIST directive:

LIST p=16f877a, r=dec

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


specified explicitly:

Radix MPASM Syntax

Binary b’10101010’

Decimal d’25’ or .25

Hexadecimal h’2A’ or 0x2A

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 56
Lab 1: The Task
 Turn on LED connected to bit 0
of PORTB (RB0)
470
37
RB0
R21
D2
PIC16F877A

J6

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 57
Lab 1: Program Structure
START

Switch to Bank 1
Clear PORTB
1 Instruction Output Latches
Load number into W
Make 4 low bits of
4 Instructions
PORTB outputs
Move value to
TRISB
Turn on LED on
1 Instruction
RB0 (PORTB,0)
Switch to Bank 0

1 Instruction: goto $ Loop Here Forever


(Go to self)

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 58
Lab 1: Template
Lab 1: “Hello, world!” for Microcontrollers
1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 org 0x0000
6 RESET_V goto START ;Reset Vector
7
8 START {1st
st
Instruction} ;Clear PORTB output latches
nd
9 {2 nd Instruction} ;Switch to bank 1
10 {3rd
rd
Instruction} ;Load value to make lower 4 bits outputs
th
th
11 {4 Instruction} ;Move value to TRISB
12 {5th
th
Instruction} ;Switch to bank 0
th
th
13 {6 Instruction} ;Turn on LED on RB0
14
15 goto $ ;Loop here forever
16
17 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 59
Lab 1: Solution
Lab 1: “Hello, world!” for Microcontrollers
1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 org 0x0000
6 RESET_V goto START ;Reset Vector
7
8 START clrf PORTB ;Clear PORTB output latches
9 bsf STATUS,RP0 ;Switch to bank 1
10 movlw b’11110000' ;Load value to make lower 4 bits outputs
11 movwf TRISB ;Move value to TRISB
12 bcf STATUS,RP0 ;Switch to bank 0
13 bsf PORTB,0 ;Turn on LED on RB0
14
15 goto $ ;Loop here forever
16
17 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 60
Lab 1: Results
 You have learned:
 How to program a device and run the
code using the MPLAB® ICD2
 How to configure an I/O port
 How to manipulate I/O pins
 How to code an infinite loop (the
equivalent of while(1) in C)

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 61
Lab 2: The Task
 Make the LED connected to bit 0
of PORTB (RB0) blink

470
37
RB0
R21
D2
PIC16F877A

J6

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 62
Lab 2: The Task
 A delay is required to make the
blinking slow enough for the
human eye
 At 4MHz, one instruction
executes in 1s
 A 16-bit software counter is
sufficient to implement the delay

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 63
Naming
Registers/Constants
 Equate Method:
MyReg0 equ 0x20 ;MyReg0 = 0x20
MyReg1 equ 0x21 ;MyReg1 = 0x21
MyReg2 equ 0x23 ;MyReg2 = 0x23

 Constant Block Method:


CBLOCK 0x20
MyReg0 ;MyReg0 = 0x20
MyReg1: 2 ;MyReg1 = 0x21
MyReg2 ;MyReg2 = 0x23
ENDC
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 64
Lab 2: Program Structure
START

Taken from Lab 1 Setup PORTB

Turn on LED on
1 Instruction RB0

1 Instruction
Delay
Subroutine Call

Turn off LED on


1 Instruction RB0

1 Instruction
Delay
Subroutine Call

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 65
Lab 2: Program Structure
Delay
Delay Subroutine START

1 Instruction COUNTERL--

Hint: Use decfsz No


1 Instruction COUNTERL
= 0?
Yes
1 Instruction COUNTERH--

No
1 Instruction COUNTERH
= 0?
Yes
1 Instruction RETURN

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 66
Lab 2: Template – Part 1
Lab 2: Blinking LED
1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 cblock 0x020
6 COUNTERL
7 COUNTERH
8 endc
9
10 org 0x0000
11 RESET_V goto START ;Reset Vector
12
13 START clrf PORTB ;Clear PORTB output latches
14 bsf STATUS,RP0 ;Switch to bank 1
15 movlw b’11110000' ;Load value to make lower 4 bits outputs
16 movwf TRISB ;Move value to TRISB
17 bcf STATUS,RP0 ;Switch to bank 0

;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 67
Lab 2: Template – Part 2

Lab 2: Blinking LED - Continued

18 LOOP {1st
st
Instruction} ;Turn on LED on RB0
nd
19 {2 nd Instruction} ;Call delay routine
20 {3rd
rd
Instruction} ;Turn off LED on RB0
th
th
21 {4 Instruction} ;Call delay routine
22 {5th
th
Instruction} ;Repeat main loop
23
24 DELAY {6th
th
Instruction} ;Decrement COUNTERL
th
th
25 {7 Instruction} ;If not zero, keep decrementing COUNTERL
26 {8th
th
Instruction} ;Decrement COUNTERH
th
th
27 {9 Instruction} ;If not zero, decrement COUNTERL again
28 {10thth
Instruction} ;Return to main routine
29
30 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 68
Lab 2: Solution – Part 1
Lab 2: Blinking LED
1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 cblock 0x020
6 COUNTERL
7 COUNTERH
8 endc
9
10 org 0x0000
11 RESET_V goto START ;Reset Vector
12
13 START clrf PORTB ;Clear PORTB output latches
14 bsf STATUS,RP0 ;Switch to bank 1
15 movlw b’11110000' ;Load value to make lower 4 bits outputs
16 movwf TRISB ;Move value to TRISB
17 bcf STATUS,RP0 ;Switch to bank 0

;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 69
Lab 2: Solution – Part 2
Lab 2: Blinking LED - Continued

18 LOOP bsf PORTB,0 ;Turn on LED on RB0


19 call DELAY ;Call delay routine
20 bcf PORTB,0 ;Turn off LED on RB0
21 call DELAY ;Call delay routine
22 goto LOOP ;Repeat main loop
23
24 DELAY decfsz COUNTERL ;Decrement COUNTERL
25 goto DELAY ;If not zero, keep decrementing COUNTERL
26 decfsz COUNTERH ;Decrement COUNTERH
27 goto DELAY ;If not zero, decrement COUNTERL again
28 return
29
30 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 70
Lab 2: Results
 You have learned:
 How to define register labels
 How to implement a loop
 How to implement software delays
 How to use a “skip” instruction
 How to call a subroutine

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 71
Lab 3: The Task
 Using one of the rotate
instructions, “move” the
illuminated LED across the lower
4 bits of PORTB. When it
reaches one side, send it back to
the start.
RB3 RB2 RB1 RB0

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 72
Lab 3: Program Structure
START

Same setup code from Lab 1 Setup PORTB

1 Instruction Set Carry Bit


Rember: The rotate
instructions operate 1 Instruction
Rotate Left
PORTB
on 9-bits, with the
Carry bit in the Yes
1 Instruction RB3 = 0?
STATUS register as
No
the 9th bit
1 Instruction Set Carry Bit

1 Instruction Delay
Call same subroutine from Lab 2

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 73
Lab 3: Template – Part 1
Lab 3: Rotating LED
1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 cblock 0x020
6 COUNTERL
7 COUNTERH
8 endc
9
10 org 0x0000
11 RESET_V goto START ;Reset Vector
12
13 START clrf PORTB ;Clear PORTB output latches
14 bsf STATUS,RP0 ;Switch to bank 1
15 movlw b’11110000' ;Load value to make lower 4 bits outputs
16 movwf TRISB ;Move value to TRISB
17 bcf STATUS,RP0 ;Switch to bank 0

;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 74
Lab 3: Template – Part 2
Lab 3: Rotating LED - Continued

18 {1st
st
Instruction} ;Set carry bit for initial rotate
nd
nd
19 LOOP {2 Instruction} ;Rotate PORTB to left
20 {3rd
rd
Instruction} ;Call delay routine
th
th
21 {4 Instruction} ;Is the LED on RB3 (PORTB,3) on?
22 {5th
th
Instruction} ;If yes, set the Carry bit
th
th
23 {6 Instruction} ;Repeat main loop
24
25 DELAY decfsz COUNTERL ;Decrement COUNTERL
26 goto DELAY ;If not zero, keep decrementing COUNTERL
27 decfsz COUNTERH ;Decrement COUNTERH
28 goto DELAY ;If not zero, decrement COUNTERL again
29 return ;Return to main subroutine
30
31 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 75
Lab 3: Solution – Part 1
Lab 3: Rotating LED
1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 cblock 0x020
6 COUNTERL
7 COUNTERH
8 endc
9
10 org 0x0000
11 RESET_V goto START ;Reset Vector
12
13 START clrf PORTB ;Clear PORTB output latches
14 bsf STATUS,RP0 ;Switch to bank 1
15 movlw b’11110000' ;Load value to make lower 4 bits outputs
16 movwf TRISB ;Move value to TRISB
17 bcf STATUS,RP0 ;Switch to bank 0

;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 76
Lab 3: Solution – Part 2
Lab 3: Rotating LED - Continued

18 bsf STATUS,C ;Set carry bit for initial rotate


19 LOOP rlf PORTB,f ;Rotate PORTB to left
20 call DELAY ;Call delay routine
21 btfsc PORTB,3 ;Is the LED on RB3 (PORTB,3) on?
22 bsf STATUS,C ;If yes, set the Carry bit
23 goto LOOP ;Repeat main loop
24
25 DELAY decfsz COUNTERL ;Decrement COUNTERL
26 goto DELAY ;If not zero, keep decrementing COUNTERL
27 decfsz COUNTERH ;Decrement COUNTERH
28 goto DELAY ;If not zero, decrement COUNTERL again
29 return ;Return to main subroutine
30
31 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 77
Lab 3: Results
 You have learned:
 How to use the rotate instructions
 How to use the bit test & skip
instructions

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 78
Lab 4: The Task
 Same as Lab 3, but this time
make the direction of rotation
change when the LED is rotated
to either end
Rotate Left

RB3 RB2 RB1 RB0

Rotate Right

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 79
Lab 4: Program Structure START

Same setup code from Lab 1 Setup PORTB

bsf STATUS,C Set Carry Bit

Rotate Left
1 Instruction PORTB

1 Instruction Delay
Subroutine Call
No
2 Instructions RB3 = 1?

Yes
Rotate Right
1 Instruction PORTB

1 Instruction Delay
Subroutine Call
No
2 Instructions RB0 = 1?

Yes

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 80
Lab 4: Template
 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


19
20 LEFT {1st
st
Instruction} ;Rotate PORTB to left
nd
nd
21 {2 Instruction} ;Call delay routine
22 {3rd
rd
Instruction} ;Is the LED on RB3 (PORTB,3) on?
th
th
23 {4 Instruction} ;if no, rotate left again
24
25 RIGHT {5th
th
Instruction} ;Rotate PORTB to right
th
th
26 {6 Instruction} ;Call delay routine
27 {7th
th
Instruction} ;Is the LED on RB0 (PORTB,0) on?
th
th
28 {8 Instruction} ;if no, rotate right again
29 {9th
th
Instruction} ;if yes, rotate left
30
31 DELAY decfsz COUNTERL ;Decrement COUNTERL
32 goto DELAY ;If not zero, keep decrementing COUNTERL
33 decfsz COUNTERH ;Decrement COUNTERH
34 goto DELAY ;If not zero, decrement COUNTERL again
35 return ;Return to main subroutine
36
37 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 81
Lab 4: Solution
Lab 3: Rotating LED - Continued

18 bsf STATUS,C ;Set carry bit for initial rotate


19
20 LEFT rlf PORTB,f ;Rotate PORTB to left
21 call DELAY ;Call delay routine
22 btfss PORTB,3 ;Is the LED on RB3 (PORTB,3) on?
23 goto LEFT ;if no, rotate left again
24
25 RIGHT rrf PORTB,f ;Rotate PORTB to right
26 call DELAY ;Call delay routine
27 btfss PORTB,0 ;Is the LED on RB0 (PORTB,0) on?
28 goto RIGHT ;if no, rotate right again
29 goto LEFT ;if yes, rotate left
30
31 DELAY decfsz COUNTERL ;Decrement COUNTERL
32 goto DELAY ;If not zero, keep decrementing COUNTERL
33 decfsz COUNTERH ;Decrement COUNTERH
34 goto DELAY ;If not zero, decrement COUNTERL again
35 return ;Return to main subroutine
36
37 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 82
Lab 4: Results
 You have learned:
 How to make decisions in software
and take different courses of action

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 83
Lab 5: The Task
 Use a lookup table to obtain the
bit pattern to be displayed on
PORTB

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 84
Lab 5: Program Structure
START
Move W Reg to
PORTB
1 Instruction
Lab 1 Setup Code Setup PORTB

Delay
1 Instruction
1 Instruction Clear INDEX Subroutine Call

INDEX ++ 1 Instruction
2 Instructions Setup PCLATH

No
Move INDEX to INDEX = 7? 3 Instructions
1 Instruction W Register
Yes
1 Instruction Call TABLE Clear INDEX 1 Instruction

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 85
Lab 5: Template – Part 1
Lab 5: Lookup Table

1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 cblock 0x020
6 COUNTERL
7 COUNTERH
8 endc
9
10 org 0x0000
11 RESET_V goto START ;Reset Vector
12
13 START clrf PORTB ;Clear PORTB output latches
14 bsf STATUS,RP0 ;Switch to bank 1
15 movlw b’11110000' ;Load value to make lower 4 bits outputs
16 movwf TRISB ;Move value to TRISB
17 bcf STATUS,RP0 ;Switch to bank 0
st
st
18 {1 Instruction} ;Clear index into table
19 {2nd
nd
Instruction} ;Load W with high byte of TABLE address
rd
rd
20 {3 Instruction} ;Move W to PCLATH

;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 86
Lab 5: Template – Part 2
Lab 3: Lookup Table - Continued

22 LOOP {4th
th
Instruction} ;Move INDEX to W
th
th
23 {5 Instruction} ;Call TABLE
24 {6th
th
Instruction} ;Move W to PORTB
th
th
25 {7 Instruction} ;Call delay
26 {8th
th
Instruction} ;Increment INDEX
th
th
27 {9 Instruction} ;Load W with 0x07
28 {10thth
Instruction} ;Subtract W from INDEX, result in W
th
th
29 {11 Instruction} ;Is Z bit in STATUS set?
30 {12thth
Instruction} ;if yes, clear INDEX
31 th
th
{13 Instruction} ;Repeat loop
32
33 DELAY decfsz COUNTERL ;Decrement COUNTERL
34 goto DELAY ;If not zero, keep decrementing COUNTERL
35 decfsz COUNTERH ;Decrement COUNTERH
36 goto DELAY ;If not zero, decrement COUNTERL again
37 return ;Return to main subroutine
38
;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 87
Lab 5: Template – Part 3

Lab 3: Lookup Table - Continued


39 TABLE addwf PCL,f ;Add offset to program counter
40 retlw b’00000001' ;Table entry 0
41 retlw b’00000011' ;Table entry 1
42 retlw b’00000111' ;Table entry 2
43 retlw b’00001111' ;Table entry 3
44 retlw b’00001110' ;Table entry 4
45 retlw b’00001100' ;Table entry 5
56 retlw b’00001000' ;Table entry 6
58
59 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 88
Lab 5: Solution – Part 1
Lab 5: Lookup Table

1 LIST p=16f877a
2
3 #include <p16f877a.inc>
4
5 cblock 0x020
6 COUNTERL
7 COUNTERH
8 endc
9
10 org 0x0000
11 RESET_V goto START ;Reset Vector
12
13 START clrf PORTB ;Clear PORTB output latches
14 bsf STATUS,RP0 ;Switch to bank 1
15 movlw b’11110000' ;Load value to make lower 4 bits outputs
16 movwf TRISB ;Move value to TRISB
17 bcf STATUS,RP0 ;Switch to bank 0
18 clrf INDEX ;Clear index into table
19 movlw HIGH TABLE ;Load W with high byte of TABLE address
20 movwf PCLATH ;Move W to PCLATH

;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 89
Lab 5: Solution – Part 2
Lab 3: Lookup Table - Continued

22 LOOP movf INDEX,w ;Move INDEX to W


23 call TABLE ;Call TABLE
24 movwf PORTB ;Move W to PORTB
25 call DELAY ;Call delay
26 incf INDEX,f ;Increment INDEX
27 movlw 0x07 ;Load W with 0x07
28 subwf INDEX,w ;Subtract W from INDEX, result in W
29 btfsc STATUS,Z ;Is Z bit in STATUS set?
30 clrf INDEX ;if yes, clear INDEX
31 goto LOOP ;Repeat loop
32
33 DELAY decfsz COUNTERL ;Decrement COUNTERL
34 goto DELAY ;If not zero, keep decrementing COUNTERL
35 decfsz COUNTERH ;Decrement COUNTERH
36 goto DELAY ;If not zero, decrement COUNTERL again
37 return ;Return to main subroutine
38
;CONTINUED ON NEXT SLIDE

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 90
Lab 5: Solution – Part 3

Lab 3: Lookup Table - Continued


39 TABLE addwf PCL,f ;Add offset to program counter
40 retlw b’00000001' ;Table entry 0
41 retlw b’00000011' ;Table entry 1
42 retlw b’00000111' ;Table entry 2
43 retlw b’00001111' ;Table entry 3
44 retlw b’00001110' ;Table entry 4
45 retlw b’00001100' ;Table entry 5
56 retlw b’00001000' ;Table entry 6
58
59 END

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 91
Lab 5: Results
 You have learned:
 How to implement a lookup table
 How to retrieve data from a lookup
table
 How to call a subroutine on another
page
 How to perform a computed goto

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 92
Summary
 PIC16 Architecture
 PIC16 Instruction Set
 PIC16 Memory Organization
 Simple Programming Techniques

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 93
References
 PICmicro® MCU Mid-Range Family
Reference Manual (DS33023A)
Microchip Technology
 Programming and Customizing
PICmicro Microcontrollers
by Myke Predko
 Design with PIC® Microcontrollers
by John B. Peatman

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 94
References
 123 PIC® Microcontroller
Experiments for the Evil Genius
by Myke Predko

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 95
Thank You

© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 96


Trademarks
The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KeeLoq,
microID, MPLAB, PIC, PICmicro, PICSTART, PRO MATE, PowerSmart,
rfPIC and SmartShunt are registered trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB, SEEVAL,
SmartSensor and The Embedded Control Solutions Company are
registered trademarks of Microchip Technology Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, dsPICDEM, dsPICDEM.net,
dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM, fuzzyLAB, In-
Circuit Serial Programming, ICSP, ICEPIC, Linear Active Thermistor,
Mindi, MiWi, MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM,
PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo, PowerMate,
PowerTool, REAL ICE, rfLAB, rfPICDEM, Select Mode, Smart Serial,
SmartTel, Total Endurance, UNI/O, WiperLock and ZENA are trademarks of
Microchip Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
All other trademarks mentioned herein are property of their respective
companies.

© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 97

You might also like