Professional Documents
Culture Documents
© 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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 7
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction
T0 T1
Example Program
1 MAIN movlw 0x05 Fetch Execute
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 8
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction
T0 T1 T2
Example Program
1 MAIN movlw 0x05 Fetch Execute Time to execute normal instruction
4 addwf REG2
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 9
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction
T0 T1 T2 T3
Example Program
1 MAIN movlw 0x05 Fetch Execute
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 10
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction
T0 T1 T2 T3 T4
Example Program
1 MAIN movlw 0x05 Fetch Execute
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
T0 T1 T2 T3 T4 T5
Example Program
1 MAIN movlw 0x05 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
T0 T1 T2 T3 T4 T5 T6
Example Program
1 MAIN movlw 0x05 Fetch Execute
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 13
Instruction Pipelining
Pre-Fetched Instruction Executing Instruction
T0 T1 T2 T3 T4 T5 T6 T7
Example Program
1 MAIN movlw 0x05 Fetch Execute
© 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
movlw k
1 1 0 0 0 0 k k k k k k k k
© 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
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
© 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
BSF 0x25, 3
© 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
01Fh 09Fh
020h 0A0h
128 Bytes
© 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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 24
PIC16 Addressing Modes
Data Memory Access:
Directaddwf <data_address>, <d>
© 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
05h FF FF
© 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
© 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
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
Program Counter 0 0 0 0 0 0 0 0 0 0 0 0 0
© 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
© 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
- - - 0 0 0 0 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.
- - - 0 0 0 0 0 Opcode 0 0 0 0 0 0 0 0 0 0 0
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
© 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
© 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.0A
typical)
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
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
© 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
© 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
© 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:
Binary b’10101010’
© 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
© 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 1s
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
Turn on LED on
1 Instruction RB0
1 Instruction
Delay
Subroutine Call
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--
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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 67
Lab 2: Template – Part 2
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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 69
Lab 2: Solution – Part 2
Lab 2: Blinking LED - Continued
© 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
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
© 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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 76
Lab 3: Solution – Part 2
Lab 3: Rotating LED - Continued
© 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
Rotate Right
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 79
Lab 4: Program Structure START
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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 81
Lab 4: Solution
Lab 3: Rotating LED - Continued
© 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
© 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
© 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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 89
Lab 5: Solution – Part 2
Lab 3: Lookup Table - Continued
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 90
Lab 5: Solution – Part 3
© 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
© 2006 Microchip Technology Incorporated. All Rights Reserved. 101 ASP Slide 97