Professional Documents
Culture Documents
26K20/43K20/44K20/45K20/
46K20
Data Sheet
28/40/44-Pin
Flash Microcontrollers
with 10-Bit A/D and nanoWatt Technology
Advance Information
DS41303B
Note the following details of the code protection feature on Microchip devices:
Microchip products meet the specification contained in their particular Microchip Data Sheet.
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
Microchip is willing to work with the customer who is concerned about the integrity of their code.
Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as unbreakable.
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Trademarks
The Microchip name and logo, the Microchip logo, Accuron,
dsPIC, KEELOQ, KEELOQ logo, 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, Linear Active Thermistor, Migratable
Memory, MXDEV, MXLAB, PS logo, 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, CodeGuard,
dsPICDEM, dsPICDEM.net, dsPICworks, ECAN,
ECONOMONITOR, FanSense, FlexROM, fuzzyLAB,
In-Circuit Serial Programming, ICSP, ICEPIC, Mindi, MiWi,
MPASM, MPLAB Certified logo, MPLIB, MPLINK, 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.
2007, Microchip Technology Incorporated, Printed in the
U.S.A., All Rights Reserved.
Printed on recycled paper.
Microchip received ISO/TS-16949:2002 certification for its worldwide
headquarters, design and wafer fabrication facilities in Chandler and
Tempe, Arizona, Gresham, Oregon and Mountain View, California. The
Companys quality system processes and procedures are for its PIC
MCUs and dsPIC DSCs, KEELOQ code hopping devices, Serial
EEPROMs, microperipherals, nonvolatile memory and analog
products. In addition, Microchips quality system for the design and
manufacture of development systems is ISO 9001:2000 certified.
DS41303B-page ii
Advance Information
PIC18F2XK20/4XK20
28/40/44-Pin Flash Microcontrollers with
10-Bit A/D and nanoWatt Technology
Power-Managed Modes:
Peripheral Highlights:
Advance Information
DS41303B-page 1
PIC18F2XK20/4XK20
Program Memory
Data Memory
(1)
Flash # Single-Word SRAM EEPROM I/O
(bytes) Instructions (bytes) (bytes)
Device
10-bit
A/D
(ch)(2)
CCP/
ECCP
(PWM)
MSSP
SPI
Master
I2C
EUSART
Comp.
Timers
8/16-bit
PIC18F23K20
8K
4096
512
256
25
11
1/1
1/3
PIC18F24K20
16K
8192
768
256
25
11
1/1
1/3
PIC18F25K20
32K
16384
1536
256
25
11
1/1
1/3
PIC18F26K20
64k
32768
3936
1024
25
11
1/1
1/3
PIC18F43K20
8K
4096
512
256
36
14
1/1
1/3
PIC18F44K20
16K
8192
768
256
36
14
1/1
1/3
PIC18F45K20
32K
16384
1536
256
36
14
1/1
1/3
PIC18F46K20
64k
32768
3936
1024
36
14
1/1
1/3
Note
1:
2:
DS41303B-page 2
Advance Information
PIC18F2XK20/4XK20
Pin Diagrams
28-pin PDIP, SOIC, SSOP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
RA1/AN1/C12IN1RA0/AN0/C12IN0-
MCLR/VPP/RE3
RA0/AN0/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RA4/T0CKI/C1OUT
RA5/AN4/SS/HLVDIN/C2OUT
RE0/RD/AN5
RE1/WR/AN6
RE2/CS/AN7
VDD
VSS
OSC1/CLKIN/RA7
OSC2/CLKOUT/RA6
RC0/T1OSO/T13CKI
RC1/T1OSI/CCP2(1)
RC2/CCP1/P1A
RC3/SCK/SCL
RD0/PSP0
RD1/PSP1
PIC18F43K20
PIC18F44K20
PIC18F45K20
PIC18F46K20
40-pin PDIP
28-pin QFN
28
27
26
25
24
23
22
21
20
19
18
17
16
15
PIC18F23K20
PIC18F24K20
PIC18F25K20
PIC18F26K20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
RB7/KBI3/PGD
RB6//KBI2/PGC
RB5/KBI1/PGM
RB4/KBI0/AN11/P1D
RB3/AN9/C12IN3-/CCP2(1)
RB2/INT2/AN8/P1B
RB1/INT1/AN10/C12IN2-/P1C
RB0/INT0/FLT0/AN12
VDD
VSS
RC7/RX/DT
RC6/TX/CK
RC5/SDO
RC4/SDI/SDA
RB7/KBI3/PGD
RB6/KBI2/PGC
RB5/KBI1/PGM
RB4/KBI0/AN11
RB3/AN9/C12IN3-/CCP2(1)
RB2/INT2/AN8
RB1/INT1/AN10/C12IN2RB0/INT0/FLT0/AN12
VDD
VSS
RD7/PSP7/P1D
RD6/PSP6/P1C
RD5/PSP5/P1B
RD4/PSP4
RC7/RX/DT
RC6/TX/CK
RC5/SDO
RC4/SDI/SDA
RD3/PSP3
RD2/PSP2
MCLR/VPP/RE3
RB7/KBI3/PGD
RB6/KBI2/PGC
RB5/KBI1/PGM
RB4/KBI0/AN11/P1D
MCLR/VPP/RE3
RA0/AN0/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RA4/T0CKI/C1OUT
RA5/AN4/SS/HLVDIN/C2OUT
VSS
OSC1/CLKIN/RA7
OSC2/CLKOUT/RA6
RC0/T1OSO/T13CKI
RC1/T1OSI/CCP2(1)
RC2/CCP1/P1A
RC3/SCK/SCL
28 27 26 25 24 23 22
RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RA4/T0CKI/C1OUT
RA5/AN4/SS/HLVDIN/C2OUT
VSS
OSC1/CLKIN/RA7
OSC2/CLKOUT/RA6
1
2
3
4
5
6
7
PIC18F23K20
PIC18F24K20
PIC18F25K20
PIC18F26K20
21
20
19
18
17
16
15
RB3/AN9/C12IN3-/CCP2(1)
RB2/INT2/AN8/P1B
RB1/INT1/AN10/C12IN2-/P1C
RB0/INT0/FLT0/AN12
VDD
VSS
RC7/RX/DT
Note
1:
RC0/T1OSO/T13CKI
RC1/T1OSI/CCP2(1)
RC2/CCP1/P1A
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
8 9 10 11 12 13 14
Advance Information
DS41303B-page 3
PIC18F2XK20/4XK20
RC6/TX/CK
RC5/SDO
RC4/SDI/SDA
RD3/PSP3
RD2/PSP2
RD1/PSP1
RD0/PSP0
RC3/SCK/SCL
RC2/CCP1/P1A
RC1/T1OSI/CCP2(1)
NC
44
43
42
41
40
39
38
37
36
35
34
44-pin TQFP
12
13
14
15
16
17
18
19
20
21
22
PIC18F43K20
PIC18F44K20
PIC18F45K20
PIC18F46K20
NC
RC0/T1OSO/T13CKI
OSC2/CLKOUT/RA6
OSC1/CLKIN/RA7
VSS
VDD
RE2/CS/AN7
RE1/WR/AN6
RE0/RD/AN5
RA5/AN4/SS/HLVDIN/C2OUT
RA4/T0CKI/C1OUT
33
32
31
30
29
28
27
26
25
24
23
44
43
42
41
40
39
38
37
36
35
34
44-pin QFN
1
2
3
4
5
6
7
8
9
10
11
NC
NC
RC6/TX/CK
RB4/KBI0/AN11
RC5/SDO
RB5/KBI1/PGM
RC4/SDI/SDA
RB6/KBI2/PGC
RD3/PSP3
RB7/KBI3/PGD
RD2/PSP2
MCLR/VPP/RE3
RD1/PSP1
RA0/AN0/C12IN0RD0/PSP0
RA1/AN1/C12IN1RC3/SCK/SCL
RA2/AN2/VREF-/CVREF/C2IN+
RC2/CCP1/P1A
RA3/AN3/VREF+/C1IN+
RC1/T1OSI/CCP2(1)
RC0/T1OSO/T13CKI
RC7/RX/DT
RD4/PSP4
RD5/PSP5/P1B
RD6/PSP6/P1C
RD7/PSP7/P1D
VSS
VDD
RB0/INT0/FLT0/AN12
RB1/INT1/AN10/C12IN2RB2/INT2/AN8
RB3/AN9/C12IN3-/CCP2(1)
Note
1:
DS41303B-page 4
PIC18F43K20
PIC18F44K20
PIC18F45K20
PIC18F46K20
33
32
31
30
29
28
27
26
25
24
23
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
OSC2/CLKOUT/RA6
OSC1/CLKIN/RA7
VSS
VSS
VDD
VDD
RE2/CS/AN7
RE1/WR/AN6
RE0/RD/AN5
RA5/AN4/SS/HLVDIN/C2OUT
RA4/T0CKI/C1OUT
RB3/AN9/C12IN3-/CCP2(1)
NC
RB4/KBI0/AN11
RB5/KBI1/PGM
RB6/KBI2/PGC
RB7/KBI3/PGD
MCLR/VPP/RE3
RA0/AN0/C12IN0RA1/AN1/C12IN1RA2/AN2/VREF-/CVREF/C2IN+
RA3/AN3/VREF+/C1IN+
RC7/RX/DT
RD4/PSP4
RD5/PSP5/P1B
RD6/PSP6/P1C
RD7/PSP7/P1D
VSS
VDD
VDD
RB0/INT0/FLT0/AN12
RB1/INT1/AN10/C12IN2RB2/INT2/AN8
Advance Information
PIC18F2XK20/4XK20
21
RA2
AN2
C2IN+
VREF-/
CVREF
22
22
RA3
AN3
23
23
RA4
C1IN+
VREF+
C1OUT
DIL Pin
7
24
24
RA5
AN4
C2OUT
HLVDIN
14
31
33
RA6
13
30
32
RA7
33
RB0
AN12
34
10
RB1
AN10
C12IN2-
35
10
11
RB2
AN8
36
11
12
RB3
AN9
C12IN3-
37
14
14
RB4
AN11
38
15
15
RB5
39
16
16
RB6
40
17
17
RB7
15
32
34
RC0
Basic
21
Pull-up
Interrupts
Slave
C12IN1-
Timers
C12IN0-
AN1
MSSP
AN0
RA1
EUSART
RA0
20
ECCP
Reference
19
20
I/O
19
QFN Pin
TQFP Pin
Comparator
TABLE 1:
T0CKI
T1OSI
FLT0
CCP2(1)
SS
T1OSO/
T13CKI
OSC2/
CLKOUT
OSC1/CLKIN
INT0
Yes
INT1
Yes
INT2
Yes
Yes
KBI0
Yes
KBI1
Yes
PGM
KBI2
Yes
PGC
KBI3
Yes
PGD
16
35
35
RC1
36
36
RC2
CCP2(2)
17
18
37
37
RC3
SCK/
SCL
23
42
42
RC4
SDI/
SDA
SDO
TX/CK
26
RC7
19
38
38
RD0
20
39
39
RD1
21
40
40
RD2
22
41
41
RD3
27
RD4
28
RD5
29
RD6
30
RD7
25
25
RE0
AN5
26
26
RE1
AN6
10
27
27
RE2
AN7
18
18
RE3(3)
11
32
28
28
12
31
29
30
NC
NC
29
NC
31
24
43
43
RC5
25
44
44
RC6
Note 1:
2:
3:
CCP1/
P1A
P1B
P1C
P1D
RX/DT
PSP0
PSP1
PSP2
PSP3
PSP4
PSP5
PSP6
PSP7
RD
WR
CS
MCLR/VPP
VDD
VDD
VSS
VSS
VDD
VDD
VSS
Advance Information
DS41303B-page 5
PIC18F2XK20/4XK20
Pull-up
Interrupts
EUSART
Reference
C12IN0-
AN1
C12IN1-
RA2
AN2
C2IN+
VREF-/
CVREF
RA3
AN3
C1IN+
VREF+
RA4
RA5
10
RA6
OSC2/
CLKOUT
RA7
OSC1/
CLKIN
21
18
RB0
AN12
22
19
RB1
AN10
23
20
RB2
AN8
24
21
RB3
AN9
25
22
RB4
AN11
26
23
27
C1OUT
AN4
C2OUT
C12IN2C12IN3-
Basic
AN0
RA1
Slave
RA0
28
Timers
27
MSSP
ECCP
Analog
Comparator
Pin QUAD
Pin DIL
TABLE 2:
T0CKI
HLVDIN
SS
FLT0
INT0
P1C
INT1
Yes
P1B
INT2
Yes
CCP2(1)
Yes
Yes
P1D
KBI0
Yes
RB5
KBI1
Yes
24
RB6
KBI2
Yes
PGC
28
25
RB7
KBI3
Yes
PGD
11
RC0
PGM
T1OSO/
T13CKI
12
RC1
CCP2(2)
13
10
RC2
CCP1/
P1A
14
11
RC3
SCK/
SCL
15
12
RC4
SDI/
SDA
T1OSI
16
13
RC5
17
14
RC6
TX/CK
SDO
18
15
RC7
RX/DT
26
RE3(3)
MCLR/
VPP
VSS
19
16
VSS
20
17
VDD
Note 1:
2:
3:
DS41303B-page 6
Advance Information
PIC18F2XK20/4XK20
Table of Contents
1.0 Device Overview .......................................................................................................................................................................... 9
2.0 Oscillator Module (With Fail-Safe Clock Monitor)....................................................................................................................... 25
3.0 Power-Managed Modes ............................................................................................................................................................. 41
4.0 Reset .......................................................................................................................................................................................... 49
5.0 Memory Organization ................................................................................................................................................................. 63
6.0 Flash Program Memory.............................................................................................................................................................. 87
7.0 Data EEPROM Memory ............................................................................................................................................................. 97
8.0 8 x 8 Hardware Multiplier.......................................................................................................................................................... 101
9.0 Interrupts .................................................................................................................................................................................. 103
10.0 I/O Ports ................................................................................................................................................................................... 117
11.0 Timer0 Module ......................................................................................................................................................................... 137
12.0 Timer1 Module ......................................................................................................................................................................... 141
13.0 Timer2 Module ......................................................................................................................................................................... 147
14.0 Timer3 Module ......................................................................................................................................................................... 149
15.0 Capture/Compare/PWM (CCP) Modules ................................................................................................................................. 153
16.0 Enhanced Capture/Compare/PWM (ECCP) Module................................................................................................................ 165
17.0 Master Synchronous Serial Port (MSSP) Module .................................................................................................................... 185
18.0 Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) ............................................................... 227
19.0 Analog-to-Digital Converter (ADC) Module .............................................................................................................................. 255
20.0 Comparator Module.................................................................................................................................................................. 269
21.0 Voltage References.................................................................................................................................................................. 279
22.0 High/Low-Voltage Detect (HLVD)............................................................................................................................................. 283
23.0 Special Features of the CPU.................................................................................................................................................... 289
24.0 Instruction Set Summary .......................................................................................................................................................... 305
25.0 Development Support............................................................................................................................................................... 355
26.0 Electrical Characteristics .......................................................................................................................................................... 359
27.0 DC and AC Characteristics Graphs and Tables....................................................................................................................... 393
28.0 Packaging Information.............................................................................................................................................................. 395
Appendix A: Revision History............................................................................................................................................................. 403
Appendix B: Device Differences ........................................................................................................................................................ 403
The Microchip Web Site ..................................................................................................................................................................... 415
Customer Change Notification Service .............................................................................................................................................. 415
Customer Support .............................................................................................................................................................................. 415
Reader Response .............................................................................................................................................................................. 416
Product Identification System ............................................................................................................................................................ 417
Advance Information
DS41303B-page 7
PIC18F2XK20/4XK20
Errata
An errata sheet, describing minor operational differences from the data sheet and recommended workarounds, may exist for current
devices. As device/documentation issues become known to us, we will publish an errata sheet. The errata will specify the revision
of silicon and revision of document to which it applies.
To determine if an errata sheet exists for a particular device, please check with one of the following:
Microchips Worldwide Web site; http://www.microchip.com
Your local Microchip sales office (see last page)
The Microchip Corporate Literature Center; U.S. FAX: (480) 792-7277
When contacting a sales office or the literature center, please specify which device, revision of silicon and data sheet (include
literature number) you are using.
DS41303B-page 8
Advance Information
PIC18F2XK20/4XK20
1.0
DEVICE OVERVIEW
1.1.2
PIC18F43K20
PIC18F24K20
PIC18F44K20
PIC18F25K20
PIC18F45K20
PIC18F26K20
PIC18F46K20
1.1
1.1.1
Advance Information
DS41303B-page 9
PIC18F2XK20/4XK20
1.2
1.3
DS41303B-page 10
Devices in the PIC18F2XK20/4XK20 family are available in 28-pin and 40/44-pin packages. Block diagrams
for the two groups are shown in Figure 1-1 and
Figure 1-2.
The devices are differentiated from each other in five
ways:
1.
2.
3.
4.
Advance Information
4
1
1
Timers
Capture/Compare/PWM Modules
Enhanced
Capture/Compare/PWM Modules
Advance Information
Yes
Note
1:
Packages
1 internal plus
10 Input
Channels
No
MSSP,
Enhanced
USART
1 internal plus
10 Input
Channels
No
MSSP,
Enhanced
USART
A, B, C, (E)(1)
19
1024
3936
32768
65536
DC 64 MHz
PIC18F26K20
1 internal plus
13 Input
Channels
Yes
MSSP,
Enhanced
USART
A, B, C, D, E
20
256
512
4096
8192
DC 64 MHz
PIC18F43K20
1 internal plus
13 Input
Channels
Yes
MSSP,
Enhanced
USART
A, B, C, D, E
20
256
768
8192
16384
DC 64 MHz
PIC18F44K20
1 internal plus
13 Input
Channels
Yes
MSSP,
Enhanced
USART
A, B, C, D, E
20
256
1536
16384
32768
DC 64 MHz
PIC18F45K20
1 internal plus
13 Input
Channels
Yes
MSSP,
Enhanced
USART
A, B, C, D, E
20
1024
3936
32768
65536
DC 64 MHz
PIC18F46K20
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
28-pin PDIP
28-pin SOIC
28-pin QFN
28-pin SSOP
28-pin PDIP
28-pin SOIC
28-pin QFN
28-pin SSOP
28-pin PDIP
28-pin SOIC
28-pin QFN
28-pin SSOP
40-pin PDIP
44-pin QFN
44-pin TQFP
PORTE contains the single RE3 read-only bit. The LATE and TRISE registers are not implemented.
28-pin PDIP
28-pin SOIC
28-pin QFN
28-pin SSOP
40-pin PDIP
44-pin QFN
44-pin TQFP
40-pin PDIP
44-pin QFN
44-pin TQFP
40-pin PDIP
44-pin QFN
44-pin TQFP
Yes
Programmable
High/Low-Voltage Detect
Instruction Set
1 internal plus
10 Input
Channels
No
MSSP,
Enhanced
USART
A, B, C, (E)(1)
19
256
1536
16384
32768
DC 64 MHz
PIC18F25K20
POR, BOR,
POR, BOR,
POR, BOR,
POR, BOR,
POR, BOR,
POR, BOR,
POR, BOR,
POR, BOR,
RESET
RESET
RESET
RESET
RESET
RESET
RESET
RESET
Instruction,
Instruction,
Instruction,
Instruction,
Instruction,
Instruction,
Instruction,
Instruction,
Stack Full, Stack Stack Full, Stack Stack Full, Stack Stack Full, Stack Stack Full, Stack Stack Full, Stack Stack Full, Stack Stack Full, Stack
Underflow
Underflow
Underflow
Underflow
Underflow
Underflow
Underflow
Underflow
(PWRT, OST),
(PWRT, OST),
(PWRT, OST),
(PWRT, OST),
(PWRT, OST),
(PWRT, OST),
(PWRT, OST),
(PWRT, OST),
MCLR (optional), MCLR (optional), MCLR (optional), MCLR (optional), MCLR (optional), MCLR (optional), MCLR (optional), MCLR (optional),
WDT
WDT
WDT
WDT
WDT
WDT
WDT
WDT
No
1 internal plus
10 Input
Channels
MSSP,
Enhanced
USART
A, B, C, (E)(1)
A, B, C, (E)(1)
I/O Ports
Serial Communications
19
19
Interrupt Sources
768
256
512
256
8192
4096
Program Memory
(Instructions)
16384
DC 64 MHz
PIC18F24K20
TABLE 1-1:
8192
DC 64 MHz
Operating Frequency
PIC18F23K20
Features
PIC18F2XK20/4XK20
DEVICE FEATURES
DS41303B-page 11
PIC18F2XK20/4XK20
FIGURE 1-1:
Table Pointer<21>
Data Latch
inc/dec logic
PORTA
Data Memory
PCLATU PCLATH
21
Address Latch
20
PCU PCH PCL
Program Counter
12
Data Address<12>
RA0/AN0
RA1/AN1
RA2/AN2/VREF-/CVREF
RA3/AN3/VREF+
RA4/T0CKI/C1OUT
RA5/AN4/SS/HLVDIN/C2OUT
OSC2/CLKOUT(3)/RA6
OSC1/CLKIN(3)/RA7
31-Level Stack
4
BSR
Address Latch
Program Memory
(8/16/32/64 Kbytes)
STKPTR
12
FSR0
FSR1
FSR2
Data Latch
4
Access
Bank
12
PORTB
inc/dec
logic
Table Latch
Address
Decode
ROM Latch
Instruction Bus <16>
RB0/INT0/FLT0/AN12
RB1/INT1/AN10
RB2/INT2/AN8
RB3/AN9/CCP2(1)
RB4/KBI0/AN11
RB5/KBI1/PGM
RB6/KBI2/PGC
RB7/KBI3/PGD
IR
8
Instruction
Decode and
Control
State machine
control signals
PRODH PRODL
PORTC
8
W
BITOP
8
Internal
Oscillator
Block
OSC1(3)
OSC2
(3)
T1OSI
LFINTOSC
Oscillator
T1OSO
16 MHz
Oscillator
Single-Supply
Programming
In-Circuit
Debugger
MCLR(2)
VDD, VSS
BOR
HLVD
FVR
CVREF Comparator
Note
Power-up
Timer
Oscillator
Start-up Timer
Power-on
Reset
RC0/T1OSO/T13CKI
RC1/T1OSI/CCP2(1)
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
8 x 8 Multiply
ALU<8>
8
Watchdog
Timer
Brown-out
Reset
Fail-Safe
Clock Monitor
Precision
Band Gap
Reference
FVR
PORTE
MCLR/VPP/RE3(2)
Data
EEPROM
Timer0
Timer1
Timer2
Timer3
ECCP1
CCP2
MSSP
EUSART
ADC
10-bit
FVR
1:
CCP2 is multiplexed with RC1 when Configuration bit CCP2MX is set, or RB3 when CCP2MX is not set.
2:
3:
OSC1/CLKIN and OSC2/CLKOUT are only available in select oscillator modes and when these pins are not being used as digital I/O.
Refer to Section 2.0 Oscillator Module (With Fail-Safe Clock Monitor) for additional information.
DS41303B-page 12
Advance Information
PIC18F2XK20/4XK20
FIGURE 1-2:
PORTA
Table Pointer<21>
RA0/AN0
RA1/AN1
RA2/AN2/VREF-/CVREF
RA3/AN3/VREF+
RA4/T0CKI/C1OUT
RA5/AN4/SS/HLVDIN/C2OUT
OSC2/CLKOUT(3)/RA6
OSC1/CLKIN(3)/RA7
Data Latch
inc/dec logic
Data Memory
PCLATU PCLATH
21
Address Latch
20
PCU PCH PCL
Program Counter
12
Data Address<12>
PORTB
31-Level Stack
4
BSR
Address Latch
Program Memory
(8/16/32/64 Kbytes)
STKPTR
FSR0
FSR1
FSR2
Data Latch
8
RB0/INT0/FLT0/AN12
RB1/INT1/AN10
RB2/INT2/AN8
RB3/AN9/CCP2(1)
RB4/KBI0/AN11
RB5/KBI1/PGM
RB6/KBI2/PGC
RB7/KBI3/PGD
4
Access
Bank
12
12
inc/dec
logic
Table Latch
PORTC
Address
Decode
ROM Latch
Instruction Bus <16>
RC0/T1OSO/T13CKI
RC1/T1OSI/CCP2(1)
RC2/CCP1/P1A
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
IR
8
Instruction
Decode and
Control
State machine
control signals
PRODH PRODL
PORTD
8 x 8 Multiply
BITOP
8
Internal
Oscillator
Block
OSC1(3)
OSC2
(3)
T1OSI
LFINTOSC
Oscillator
T1OSO
16 MHz
Oscillator
Single-Supply
Programming
In-Circuit
Debugger
MCLR(2)
VDD, VSS
BOR
HLVD
Power-up
Timer
Note
Oscillator
Start-up Timer
Power-on
Reset
ALU<8>
8
Watchdog
Timer
PORTE
Brown-out
Reset
Fail-Safe
Clock Monitor
Precision
Band Gap
Reference
RE0/RD/AN5
RE1/WR/AN6
RE2/CS/AN7
MCLR/VPP/RE3(2)
FVR
Data
EEPROM
Timer0
Timer1
Timer2
Timer3
ECCP1
CCP2
MSSP
EUSART
ADC
10-bit
FVR
CVREF Comparator
RD0/PSP0
RD1/PSP1
RD2/PSP2
RD3/PSP3
RD4/PSP4
RD5/PSP5/P1B
RD6/PSP6/P1C
RD7/PSP7/P1D
FVR
PSP
1:
CCP2 is multiplexed with RC1 when Configuration bit CCP2MX is set, or RB3 when CCP2MX is not set.
2:
3:
OSC1/CLKIN and OSC2/CLKOUT are only available in select oscillator modes and when these pins are not being used as digital I/O.
Refer to Section 2.0 Oscillator Module (With Fail-Safe Clock Monitor) for additional information.
Advance Information
DS41303B-page 13
PIC18F2XK20/4XK20
TABLE 1-2:
Pin Name
Pin Buffer
PDIP,
QFN Type Type
SOIC
MCLR/VPP/RE3
MCLR
VPP
RE3
OSC1/CLKIN/RA7
OSC1
26
I
P
I
6
ST
ST
ST
CLKOUT
RA6
I/O
TTL
RA7
OSC2/CLKOUT/RA6
OSC2
10
CLKIN
Description
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
DS41303B-page 14
Advance Information
PIC18F2XK20/4XK20
TABLE 1-2:
Pin Name
Pin Number
Pin Buffer
PDIP,
QFN Type Type
SOIC
Description
PORTA is a bidirectional I/O port.
RA0/AN0/C12IN0RA0
AN0
C12IN0-
RA1/AN1/C12IN1RA1
AN1
C12IN1-
RA2/AN2/VREF-/CVREF/
C2IN+
RA2
AN2
VREFCVREF
C2IN+
RA3/AN3/VREF+/C1IN+
RA3
AN3
VREF+
C1IN+
RA4/T0CKI/C1OUT
RA4
T0CKI
C1OUT
RA5/AN4/SS/HLVDIN/
C2OUT
RA5
AN4
SS
HLVDIN
C2OUT
27
I/O
TTL
I Analog
I Analog
Digital I/O
Analog input 0, ADC channel 0
Comparators C1 and C2 inverting input
I/O
TTL
I Analog
I Analog
Digital I/O
ADC input 1, ADC channel 1
Comparators C1 and C2 inverting input
I/O
I
I
O
I
Digital I/O
Analog input 2, ADC channel 2
A/D reference voltage (low) input
Comparator reference voltage output
Comparator C2 non-inverting input
28
1
TTL
Analog
Analog
Analog
Analog
2
I/O
TTL
I Analog
I Analog
I Analog
Digital I/O
Analog input 3, ADC channel 3
A/D reference voltage (high) input
Comparator C1 non-inverting input
I/O
ST
I
ST
O CMOS
Digital I/O
Timer0 external clock input
Comparator C1 output
I/O
TTL
I Analog
I
TTL
I Analog
O CMOS
Digital I/O
Analog input 4, ADC channel 4
SPI slave select input
High/Low-Voltage Detect input
Comparator C2 output
RA6
RA7
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
Advance Information
DS41303B-page 15
PIC18F2XK20/4XK20
TABLE 1-2:
Pin Name
Pin Number
Pin Buffer
PDIP,
QFN Type Type
SOIC
Description
PORTB is a bidirectional I/O port. PORTB can be software
programmed for internal weak pull-up on each input.
RB0/INT0/FLT0/AN12
RB0
INT0
FLT0
AN12
21
RB1/INT1/AN10/C12IN2/P1C
RB1
INT1
AN10
C12IN2P1C
22
RB2/INT2/AN8/P1B
RB2
INT2
AN8
P1B
23
RB3/AN9/C12IN3-/CCP2
RB3
AN9
C12IN3CCP2(2)
24
RB4/KBI0/AN11/P1D
RB4
KBI0
AN11
P1D
25
RB5/KBI1/PGM
RB5
KBI1
PGM
26
RB6/KBI2/PGC
RB6
KBI2
PGC
27
RB7/KBI3/PGD
RB7
KBI3
PGD
28
18
I/O
TTL
I
ST
I
ST
I Analog
Digital I/O
External interrupt 0
PWM Fault input for CCP1
Analog input 12, ADC channel 12
I/O
TTL
I
ST
I Analog
I Analog
O CMOS
Digital I/O
External interrupt 1
Analog input 10, ADC channel 10
Comparators C1 and C2 inverting input
Enhanced CCP1 PWM output
I/O
TTL
I
ST
I Analog
O CMOS
Digital I/O
External interrupt 2
Analog input 8, ADC channel 8
Enhanced CCP1 PWM output
I/O
TTL
I Analog
I Analog
I/O
ST
Digital I/O
Analog input 9, ADC channel 9
Comparators C1 and C2 inverting input
Capture 2 input/Compare 2 output/PWM 2 output
I/O
TTL
I
TTL
I Analog
O CMOS
Digital I/O
Interrupt-on-change pin
Analog input 11, ADC channel 11
Enhanced CCP1 PWM output
I/O
I
I/O
TTL
TTL
ST
Digital I/O
Interrupt-on-change pin
Low-Voltage ICSP Programming enable pin
I/O
I
I/O
TTL
TTL
ST
Digital I/O
Interrupt-on-change pin
In-Circuit Debugger and ICSP programming clock pin
I/O
I
I/O
TTL
TTL
ST
Digital I/O
Interrupt-on-change pin
In-Circuit Debugger and ICSP programming data pin
19
20
21
22
23
24
25
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
DS41303B-page 16
Advance Information
PIC18F2XK20/4XK20
TABLE 1-2:
Pin Name
Pin Number
Pin Buffer
PDIP,
QFN Type Type
SOIC
Description
PORTC is a bidirectional I/O port.
RC0/T1OSO/T13CKI
RC0
T1OSO
T13CKI
11
RC1/T1OSI/CCP2
RC1
T1OSI
CCP2(1)
12
RC2/CCP1/P1A
RC2
CCP1
P1A
13
RC3/SCK/SCL
RC3
SCK
SCL
14
RC4/SDI/SDA
RC4
SDI
SDA
15
RC5/SDO
RC5
SDO
16
RC6/TX/CK
RC6
TX
CK
17
RC7/RX/DT
RC7
RX
DT
18
RE3
VSS
VDD
8
I/O
O
I
Digital I/O
Timer1 oscillator output
Timer1/Timer3 external clock input
9
I/O
ST
I Analog
I/O
ST
Digital I/O
Timer1 oscillator input
Capture 2 input/Compare 2 output/PWM 2 output
I/O
ST
I/O
ST
O CMOS
Digital I/O
Capture 1 input/Compare 1 output
Enhanced CCP1 PWM output
10
11
I/O
I/O
I/O
ST
ST
ST
Digital I/O
Synchronous serial clock input/output for SPI mode
Synchronous serial clock input/output for I2C mode
I/O
I
I/O
ST
ST
ST
Digital I/O
SPI data in
I2C data I/O
I/O
O
ST
Digital I/O
SPI data out
I/O
O
I/O
ST
ST
Digital I/O
EUSART asynchronous transmit
EUSART synchronous clock (see related RX/DT)
I/O
I
I/O
ST
ST
ST
Digital I/O
EUSART asynchronous receive
EUSART synchronous data (see related TX/CK)
12
13
14
15
8, 19 5, 16
20
ST
ST
17
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
Advance Information
DS41303B-page 17
PIC18F2XK20/4XK20
TABLE 1-3:
Pin Name
Pin Number
PDIP
MCLR/VPP/RE3
MCLR
VPP
RE3
OSC1/CLKIN/RA7
OSC1
13
Pin Buffer
QFN TQFP Type Type
18
18
I
P
I
32
30
I
CLKIN
RA7
OSC2/CLKOUT/RA6
OSC2
I/O
14
33
ST
ST
Description
Master Clear (input) or programming voltage (input)
Active-low Master Clear (device Reset) input
Programming voltage input
Digital input
31
O
CLKOUT
RA6
I/O
TTL
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
DS41303B-page 18
Advance Information
PIC18F2XK20/4XK20
TABLE 1-3:
Pin Name
Pin Number
PDIP
Pin Buffer
QFN TQFP Type Type
Description
PORTA is a bidirectional I/O port.
RA0/AN0/C12IN0RA0
AN0
C12IN0-
RA1/AN1/C12IN0RA1
AN1
C12IN0-
RA2/AN2/VREF-/CVREF/
C2IN+
RA2
AN2
VREFCVREF
C2IN+
RA3/AN3/VREF+/
C1IN+
RA3
AN3
VREF+
C1IN+
RA4/T0CKI/C1OUT
RA4
T0CKI
C1OUT
RA5/AN4/SS/HLVDIN/
C2OUT
RA5
AN4
SS
HLVDIN
C2OUT
19
20
21
22
23
24
19
I/O
I
I
TTL
Analog
Analog
Digital I/O
Analog input 0, ADC channel 0
Comparator C1 and C2 inverting input
I/O
I
I
TTL
Analog
Analog
Digital I/O
Analog input 1, ADC channel 1
Comparator C1 and C2 inverting input
I/O
I
I
O
I
TTL
Analog
Analog
Analog
Analog
Digital I/O
Analog input 2, ADC channel 2
A/D reference voltage (low) input
Comparator reference voltage output
Comparator C2 non-inverting input
I/O
I
I
I
TTL
Analog
Analog
Analog
Digital I/O
Analog input 3, ADC channel 3
A/D reference voltage (high) input
Comparator C1 non-inverting input
I/O
I
O
ST
ST
CMOS
Digital I/O
Timer0 external clock input
Comparator C1 output
I/O
I
I
I
O
TTL
Analog
TTL
Analog
CMOS
Digital I/O
Analog input 4, ADC channel 4
SPI slave select input
High/Low-Voltage Detect input
Comparator C2 output
20
21
22
23
24
RA6
RA7
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
Advance Information
DS41303B-page 19
PIC18F2XK20/4XK20
TABLE 1-3:
Pin Name
Pin Number
PDIP
Pin Buffer
QFN TQFP Type Type
Description
PORTB is a bidirectional I/O port. PORTB can be
software programmed for internal weak pull-up on
each input.
RB0/INT0/FLT0/AN12
RB0
INT0
FLT0
AN12
33
RB1/INT1/AN10/
C12IN2RB1
INT1
AN10
C12IN2-
34
RB2/INT2/AN8
RB2
INT2
AN8
35
RB3/AN9/C12IN3-/
CCP2
RB3
AN9
C12IN3CCP2(2)
36
RB4/KBI0/AN11
RB4
KBI0
AN11
37
RB5/KBI1/PGM
RB5
KBI1
PGM
38
RB6/KBI2/PGC
RB6
KBI2
PGC
39
RB7/KBI3/PGD
RB7
KBI3
PGD
40
10
11
12
14
15
16
17
8
I/O
I
I
I
TTL
ST
ST
Analog
Digital I/O
External interrupt 0
PWM Fault input for Enhanced CCP1
Analog input 12, ADC channel 12
I/O
I
I
I
TTL
ST
Analog
Analog
Digital I/O
External interrupt 1
Analog input 10, ADC channel 10
Comparator C1 and C2 inverting input
I/O
I
I
TTL
ST
Analog
Digital I/O
External interrupt 2
Analog input 8, ADC channel 8
I/O
I
I
I/O
TTL
Analog
Analog
ST
Digital I/O
Analog input 9, ADC channel 9
Comparator C1 and C2 inverting input
Capture 2 input/Compare 2 output/PWM 2 output
I/O
I
I
TTL
TTL
Analog
Digital I/O
Interrupt-on-change pin
Analog input 11, ADC channel 11
I/O
I
I/O
TTL
TTL
ST
Digital I/O
Interrupt-on-change pin
Low-Voltage ICSP Programming enable pin
I/O
I
I/O
TTL
TTL
ST
Digital I/O
Interrupt-on-change pin
In-Circuit Debugger and ICSP programming
clock pin
I/O
I
I/O
TTL
TTL
ST
Digital I/O
Interrupt-on-change pin
In-Circuit Debugger and ICSP programming
data pin
10
11
14
15
16
17
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
DS41303B-page 20
Advance Information
PIC18F2XK20/4XK20
TABLE 1-3:
Pin Name
Pin Number
PDIP
Pin Buffer
QFN TQFP Type Type
Description
PORTC is a bidirectional I/O port.
RC0/T1OSO/T13CKI
RC0
T1OSO
T13CKI
15
RC1/T1OSI/CCP2
RC1
T1OSI
CCP2(1)
16
RC2/CCP1/P1A
RC2
CCP1
P1A
17
RC3/SCK/SCL
RC3
SCK
18
34
35
36
37
32
23
RC5/SDO
RC5
SDO
24
RC6/TX/CK
RC6
TX
CK
25
RC7/RX/DT
RC7
RX
DT
26
42
43
44
ST
ST
I/O
I
I/O
ST
CMOS
ST
Digital I/O
Timer1 oscillator input
Capture 2 input/Compare 2 output/PWM 2 output
I/O
I/O
O
ST
ST
Digital I/O
Capture 1 input/Compare 1 output/PWM 1 output
Enhanced CCP1 output
I/O
I/O
ST
ST
I/O
ST
Digital I/O
Synchronous serial clock input/output for
SPI mode
Synchronous serial clock input/output for I2C mode
I/O
I
I/O
ST
ST
ST
Digital I/O
SPI data in
I2C data I/O
I/O
O
ST
Digital I/O
SPI data out
I/O
O
I/O
ST
ST
Digital I/O
EUSART asynchronous transmit
EUSART synchronous clock (see related RX/DT)
I/O
I
I/O
ST
ST
ST
Digital I/O
EUSART asynchronous receive
EUSART synchronous data (see related TX/CK)
Digital I/O
Timer1 oscillator output
Timer1/Timer3 external clock input
35
36
37
SCL
RC4/SDI/SDA
RC4
SDI
SDA
I/O
O
I
42
43
44
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
Advance Information
DS41303B-page 21
PIC18F2XK20/4XK20
TABLE 1-3:
Pin Name
Pin Buffer
QFN TQFP Type Type
Description
PORTD is a bidirectional I/O port or a Parallel Slave
Port (PSP) for interfacing to a microprocessor port.
These pins have TTL input buffers when PSP module
is enabled.
RD0/PSP0
RD0
PSP0
19
RD1/PSP1
RD1
PSP1
20
RD2/PSP2
RD2
PSP2
21
RD3/PSP3
RD3
PSP3
22
RD4/PSP4
RD4
PSP4
27
RD5/PSP5/P1B
RD5
PSP5
P1B
28
RD6/PSP6/P1C
RD6
PSP6
P1C
29
RD7/PSP7/P1D
RD7
PSP7
P1D
30
38
39
40
41
38
I/O
I/O
ST
TTL
Digital I/O
Parallel Slave Port data
I/O
I/O
ST
TTL
Digital I/O
Parallel Slave Port data
I/O
I/O
ST
TTL
Digital I/O
Parallel Slave Port data
I/O
I/O
ST
TTL
Digital I/O
Parallel Slave Port data
I/O
I/O
ST
TTL
Digital I/O
Parallel Slave Port data
I/O
I/O
O
ST
TTL
Digital I/O
Parallel Slave Port data
Enhanced CCP1 output
I/O
I/O
O
ST
TTL
Digital I/O
Parallel Slave Port data
Enhanced CCP1 output
I/O
I/O
O
ST
TTL
Digital I/O
Parallel Slave Port data
Enhanced CCP1 output
39
40
41
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
DS41303B-page 22
Advance Information
PIC18F2XK20/4XK20
TABLE 1-3:
Pin Name
Pin Buffer
QFN TQFP Type Type
Description
PORTE is a bidirectional I/O port
RE0/RD/AN5
RE0
RD
25
25
AN5
RE1/WR/AN6
RE1
WR
26
10
27
Analog
I/O
I
ST
TTL
Analog
I/O
I
ST
TTL
Digital I/O
Read control for Parallel Slave Port
(see related WR and CS pins)
Analog input 5, ADC channel 5
Digital I/O
Write control for Parallel Slave Port
(see related CS and RD pins)
Analog input 6, ADC channel 6
27
AN7
RE3
ST
TTL
26
AN6
RE2/CS/AN7
RE2
CS
I/O
I
Digital I/O
Chip Select control for Parallel Slave Port
(see related RD and WR)
Analog input 7, ADC channel 7
Analog
6, 29
7, 8, 7, 28
28, 29
No connect
VSS
12, 31 6, 30,
31
VDD
11, 32
NC
13
12, 13,
33, 34
Note 1: Default assignment for CCP2 when Configuration bit CCP2MX is set.
2: Alternate assignment for CCP2 when Configuration bit CCP2MX is cleared.
Advance Information
DS41303B-page 23
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 24
Advance Information
PIC18F2XK20/4XK20
2.0
2.1
Overview
Low-Power Crystal
Crystal/Resonator
High-Speed Crystal/Resonator
High-Speed Crystal/Resonator
with PLL enabled
5. RC
External Resistor/Capacitor with
FOSC/4 output on RA6
6. RCIO
External Resistor/Capacitor with I/O
on RA6
7. INTOSC Internal Oscillator with FOSC/4
output on RA6 and I/O on RA7
8. INTOSCIO Internal Oscillator with I/O on RA6
and RA7
9. EC
External Clock with FOSC/4 output
10. ECIO
External Clock with I/O on RA6
LP
XT
HS
HSPLL
FIGURE 2-1:
PIC18F2XK20/4XK20
Primary Oscillator
OSC2
IDLEN
Sleep
4 x PLL
OSC1
HSPLL, HFINTOSC/PLL
Sleep
Secondary Oscillator
T1OSC
T1OSO
T1OSCEN
Enable
Oscillator
OSCCON<6:4>
16 MHz
FOSC<3:0> OSCCON<1:0>
31 kHz
Source
Main
16 MHz
(HFINTOSC)
31 kHz (LFINTOSC)
2 MHz
1 MHz
500 kHz
250 kHz
Peripherals
Internal Oscillator
CPU
111
Sleep
110
4 MHz
Postscaler
Internal
Oscillator
Block
16 MHz
Source
8 MHz
101
100
011
MUX
T1OSI
MUX
OSCTUNE<6>(1)
010
001
1 31 kHz
000
0
Clock
Control
FOSC<3:0> OSCCON<1:0>
Clock Source Option
for other Modules
OSCTUNE<7>
WDT, PWRT, FSCM
and Two-Speed Start-up
Note
1:
Advance Information
DS41303B-page 25
PIC18F2XK20/4XK20
2.2
2.2.4
Oscillator Control
2.2.1
2.2.2
INTERNAL FREQUENCY
SELECTION
2.2.3
CLOCK STATUS
2.2.5
POWER MANAGEMENT
2.2.6
DS41303B-page 26
OSCILLATOR TRANSITIONS
PIC18F2XK20/4XK20 devices contain circuitry to prevent clock glitches when switching between clock
sources. A short pause in the device clock occurs during the clock switch. The length of this pause is the sum
of two cycles of the old clock source and three to four
cycles of the new clock source. This formula assumes
that the new clock source is stable.
Advance Information
PIC18F2XK20/4XK20
REGISTER 2-1:
R/W-0
IDLEN
IRCF2
R/W-1
IRCF1
R/W-1
IRCF0
R-q
(1)
OSTS
R-0
R/W-0
R/W-0
IOFS
SCS1
SCS0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
q = depends on condition
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6-4
bit 3
bit 2
bit 1-0
Note 1:
2:
3:
Advance Information
DS41303B-page 27
PIC18F2XK20/4XK20
2.3
2.4.1
TABLE 2-1:
2.4
Switch From
Switch To
Frequency
Sleep/POR
LFINTOSC
HFINTOSC
31 kHz
250 kHz to 16 MHz
Oscillator Delay
Oscillator Warm-Up Delay (TWARM)
Sleep/POR
EC, RC
DC 64 MHz
2 instruction cycles
EC, RC
DC 64 MHz
1 cycle of each
Sleep/POR
LP, XT, HS
32 kHz to 40 MHz
Sleep/POR
HSPLL
32 MHz to 64 MHz
HFINTOSC
1 s (approx.)
2.4.2
EC MODE
FIGURE 2-2:
DS41303B-page 28
Clock from
Ext. System
PIC MCU
I/O
Note 1:
Advance Information
OSC2/CLKOUT(1)
PIC18F2XK20/4XK20
2.4.3
FIGURE 2-3:
FIGURE 2-4:
CERAMIC RESONATOR
OPERATION
(XT OR HS MODE)
QUARTZ CRYSTAL
OPERATION (LP, XT OR
HS MODE)
PIC MCU
OSC1/CLKIN
PIC MCU
C1
To Internal
Logic
OSC1/CLKIN
C1
Quartz
Crystal
C2
RP(3)
To Internal
Logic
RS(1)
RF(2)
RF(2)
Sleep
Sleep
C2 Ceramic
RS(1)
Resonator
OSC2/CLKOUT
OSC2/CLKOUT
Note 1:
2:
Advance Information
DS41303B-page 29
PIC18F2XK20/4XK20
2.4.4
EXTERNAL RC MODES
2.5
2.4.4.1
RC Mode
FIGURE 2-5:
EXTERNAL RC MODES
VDD
PIC MCU
2.
REXT
OSC1/CLKIN
Internal
Clock
CEXT
VSS
FOSC/4 or
I/O(2)
OSC2/CLKOUT(1)
2.4.4.2
RCIO Mode
2.5.1
2.5.2
HFINTOSC
DS41303B-page 30
Advance Information
PIC18F2XK20/4XK20
2.5.2.1
OSCTUNE Register
REGISTER 2-2:
(PWRT), Watchdog Timer (WDT), Fail-Safe Clock Monitor (FSCM) and peripherals, are not affected by the
change in frequency.
The OSCTUNE register also implements the INTSRC
and PLLEN bits, which control certain features of the
internal oscillator block.
The INTSRC bit allows users to select which internal
oscillator provides the clock source when the 31 kHz
frequency option is selected. This is covered in greater
detail in Section 2.2.3 Low Frequency Selection.
The PLLEN bit controls the operation of the frequency
multiplier, PLL, in internal oscillator modes. For more
details about the function of the PLLEN bit see
Section 2.6.2 PLL in HFINTOSC Modes
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
INTSRC
PLLEN(1)
TUN5
TUN4
TUN3
TUN2
TUN1
TUN0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5-0
000001 =
000000 = Oscillator module is running at the factory calibrated frequency.
111111 =
Note 1:
The PLLEN bit is active only when the HFINTOSC is the primary clock source (FOSC<2:0> = 100X) and
the selected frequency is 8 MHz or 16 MHz. Otherwise, the PLLEN bit is unavailable and always reads 0.
Advance Information
DS41303B-page 31
PIC18F2XK20/4XK20
2.5.3
LFINTOSC
2.5.5
2.5.4
16 MHz
8 MHz
4 MHz
2 MHz
1 MHz (Default after Reset)
500 kHz
250 kHz
31 kHz (LFINTOSC or HFINTOSC/512)
Note:
2.5.5.1
2.5.5.2
2.5.5.3
DS41303B-page 32
Advance Information
PIC18F2XK20/4XK20
2.6
2.6.2
2.6.1
The 4x frequency multiplier can be used with the internal oscillator block to produce faster device clock
speeds than are normally possible with an internal
oscillator. When enabled, the PLL produces a clock
speed of up to 64 MHz.
Unlike HSPLL mode, the PLL is controlled through
software. The PLLEN control bit of the OSCTUNE
register is used to enable or disable the PLL operation
when the HFINTOSC is used.
The PLL is available when the device is configured to
use the internal oscillator block as its primary clock
source (FOSC<3:0> = 1001 or 1000). Additionally, the
PLL will only function when the selected output frequency is either 8 MHz or 16 MHz (OSCCON<6:4> =
111 or 110). If both of these conditions are not met, the
PLL is disabled.
The PLLEN control bit is only functional in those internal oscillator modes where the PLL is available. In all
other modes, it is forced to 0 and is effectively
unavailable.
FIGURE 2-6:
HS Oscillator Enable
PLL Enable
(from Configuration Register 1H)
OSC2
HS Mode
OSC1 Crystal
Osc
FIN
Phase
Comparator
FOUT
Loop
Filter
VCO
MUX
SYSCLK
Advance Information
DS41303B-page 33
PIC18F2XK20/4XK20
2.7
TABLE 2-2:
2.8
Power-up Delays
OSC Mode
OSC1 Pin
OSC2 Pin
RC, INTOSC
RCIO
INTOSCIO
ECIO
EC
Note:
See Table 4-2 in Section 4.0 Reset for time-outs due to Sleep and MCLR Reset.
DS41303B-page 34
Advance Information
PIC18F2XK20/4XK20
2.9
Clock Switching
2.10
2.9.1
The System Clock Select (SCS<1:0>) bits of the OSCCON register select the system clock source that is
used for the CPU and peripherals.
When SCS<1:0> = 00, the system clock source is
determined by configuration of the FOSC<2:0>
bits in the CONFIG1H Configuration register.
When SCS<1:0> = 10, the system clock source is
chosen by the internal oscillator frequency
selected by the INTSRC bit of the OSCTUNE
register and the IRCF<2:0> bits of the OSCCON
register.
When SCS<1:0> = 01, the system clock source is
the 32.768 kHz secondary oscillator shared with
Timer1.
After a Reset, the SCS<1:0> bits of the OSCCON
register are always cleared.
Note:
2.9.2
2.10.1
Advance Information
DS41303B-page 35
PIC18F2XK20/4XK20
2.10.2
1.
2.
3.
4.
5.
6.
TWO-SPEED START-UP
SEQUENCE
2.10.4
2.10.3
4.
5.
6.
7.
DS41303B-page 36
Advance Information
PIC18F2XK20/4XK20
FIGURE 2-7:
High Speed
Old Clock
Start-up Time(1)
Clock Sync
Running
New Clock
New Clk Ready
IRCF <2:0> Select Old
Select New
System Clock
Low Speed
High Speed
Old Clock
Start-up Time(1)
Clock Sync
Running
New Clock
New Clk Ready
IRCF <2:0> Select Old
Select New
System Clock
Note 1: Start-up time includes TOST (1024 TOSC) for external clocks, plus TPLL (approx. 2 ms) for HSPLL mode.
Advance Information
DS41303B-page 37
PIC18F2XK20/4XK20
2.11
2.11.3
FIGURE 2-8:
External
Clock
LFINTOSC
Oscillator
64
31 kHz
(~32 s)
488 Hz
(~2 ms)
Sample Clock
2.11.1
2.11.4
Clock
Failure
Detected
FAIL-SAFE DETECTION
2.11.2
FAIL-SAFE OPERATION
DS41303B-page 38
Advance Information
PIC18F2XK20/4XK20
FIGURE 2-9:
Sample Clock
Oscillator
Failure
System
Clock
Output
Clock Monitor Output
(Q)
Failure
Detected
OSCFIF
Test
Note:
TABLE 2-3:
Test
Test
The system clock is normally at a much higher frequency than the sample clock. The relative frequencies in
this example have been chosen for clarity.
Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Value on
POR, BOR
Value on
all other
Resets(1)
CONFIG1H
IESO
FCMEN
FOSC3
FOSC2
FOSC1
FOSC0
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
0000 000x
0000 000x
OSCCON
IDLEN
IRCF2
IRCF1
IRCF0
OSTS
IOFS
SCS1
SCS0
0011 q000
0011 q000
OSCTUNE
INTSRC
PLLEN
TUN5
TUN4
TUN3
TUN2
TUN1
TUN0
0000 0000
000u uuuu
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
0000 0000
0000 0000
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
0000 0000
0000 0000
INTCON
PIR2
Legend:
Note 1:
x = unknown, u = unchanged, = unimplemented locations read as 0. Shaded cells are not used by oscillators.
Other (non Power-up) Resets include MCLR Reset and Watchdog Timer Reset during normal operation.
Advance Information
DS41303B-page 39
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 40
Advance Information
PIC18F2XK20/4XK20
3.0
POWER-MANAGED MODES
3.1.1
CLOCK SOURCES
3.1.2
Run modes
Idle modes
Sleep mode
3.1
ENTERING POWER-MANAGED
MODES
TABLE 3-1:
POWER-MANAGED MODES
OSCCON Bits
Mode
Module Clocking
Available Clock and Oscillator Source
IDLEN(1)
SCS<1:0>
CPU
Peripherals
N/A
Off
Off
PRI_RUN
N/A
00
Clocked
Clocked
SEC_RUN
N/A
01
Clocked
Clocked
RC_RUN
N/A
1x
Clocked
Clocked
PRI_IDLE
00
Off
Clocked
SEC_IDLE
01
Off
Clocked
RC_IDLE
1x
Off
Clocked
Sleep
Note 1:
2:
Advance Information
DS41303B-page 41
PIC18F2XK20/4XK20
3.1.3
TABLE 3-2:
Primary Oscillator
HFINTOSC
Secondary Oscillator
LFINTOSC or
HFINTOSC is not yet stable
.
Note 1: Caution should be used when modifying a
single IRCF bit. If VDD is less than 2.7V, it
is possible to select a higher clock speed
than is supported by the low VDD.
Improper device operation may result if
the VDD/FOSC specifications are violated.
2: Executing a SLEEP instruction does not
necessarily place the device into Sleep
mode. It acts as the trigger to place the
controller into either the Sleep mode or
one of the Idle modes, depending on the
setting of the IDLEN bit.
3.1.4
DS41303B-page 42
3.2
Run Modes
3.2.1
PRI_RUN MODE
3.2.2
SEC_RUN MODE
Advance Information
PIC18F2XK20/4XK20
3.2.3
RC_RUN MODE
3.3
Sleep Mode
3.4
Idle Modes
Advance Information
DS41303B-page 43
PIC18F2XK20/4XK20
FIGURE 3-1:
Q1 Q2 Q3 Q4 Q1
OSC1
CPU
Clock
Peripheral
Clock
Sleep
Program
Counter
PC
FIGURE 3-2:
PC + 2
Q1
OSC1
PLL Clock
Output
TOST(1)
TPLL(1)
CPU Clock
Peripheral
Clock
Program
Counter
PC
Wake Event
PC + 2
PC + 4
PC + 6
Note1: TOST = 1024 TOSC; TPLL = 2 ms (approx). These intervals are not shown to scale.
DS41303B-page 44
Advance Information
PIC18F2XK20/4XK20
3.4.1
PRI_IDLE MODE
3.4.2
FIGURE 3-3:
SEC_IDLE MODE
Note:
Q3
Q2
Q4
Q1
OSC1
CPU Clock
Peripheral
Clock
Program
Counter
PC
FIGURE 3-4:
PC + 2
Q2
Q3
Q4
OSC1
TCSD
CPU Clock
Peripheral
Clock
Program
Counter
PC
Wake Event
Advance Information
DS41303B-page 45
PIC18F2XK20/4XK20
3.4.3
RC_IDLE MODE
3.5.1
In RC_IDLE mode, the CPU is disabled but the peripherals continue to be clocked from the internal oscillator
block from the HFINTOSC multiplexer output. This
mode allows for controllable power conservation during
Idle periods.
From RC_RUN, this mode is entered by setting the
IDLEN bit and executing a SLEEP instruction. If the
device is in another Run mode, first set IDLEN, then set
the SCS1 bit and execute SLEEP. It is recommended
that SCS0 also be cleared, although its value is
ignored, to maintain software compatibility with future
devices. The HFINTOSC multiplexer may be used to
select a higher clock frequency by modifying the IRCF
bits before executing the SLEEP instruction. When the
clock source is switched to the HFINTOSC multiplexer,
the primary oscillator is shut down and the OSTS bit is
cleared.
If the IRCF bits are set to any non-zero value, or the
INTSRC bit is set, the HFINTOSC output is enabled.
The IOFS bit becomes set, after the HFINTOSC output
becomes stable, after an interval of TIOBST
(parameter 39, Table 26-9). Clocks to the peripherals
continue while the HFINTOSC source stabilizes. If the
IRCF bits were previously at a non-zero value, or
INTSRC was set before the SLEEP instruction was executed and the HFINTOSC source was already stable,
the IOFS bit will remain set. If the IRCF bits and
INTSRC are all clear, the HFINTOSC output will not be
enabled, the IOFS bit will remain clear and there will be
no indication of the current clock source.
When a wake event occurs, the peripherals continue to
be clocked from the HFINTOSC multiplexer output.
After a delay of TCSD following the wake event, the CPU
begins executing code being clocked by the
HFINTOSC multiplexer. The IDLEN and SCS bits are
not affected by the wake-up. The LFINTOSC source
will continue to run if either the WDT or the Fail-Safe
Clock Monitor is enabled.
3.5
3.5.2
3.5.3
EXIT BY RESET
DS41303B-page 46
EXIT BY INTERRUPT
Advance Information
PIC18F2XK20/4XK20
3.5.4
TABLE 3-3:
EXIT DELAY ON WAKE-UP BY RESET FROM SLEEP MODE OR ANY IDLE MODE
(BY CLOCK SOURCES)
Clock Source
before Wake-up
Clock Source
after Wake-up
Exit Delay
LP, XT, HS
Primary Device Clock
(PRI_IDLE mode)
HSPLL
TCSD(1)
EC, RC
HFINTOSC(2)
T1OSC or LFINTOSC(1)
HFINTOSC(2)
None
(Sleep mode)
IOFS
LP, XT, HS
TOST(3)
HSPLL
TOST + tPLL(3)
OSTS
EC, RC
HFINTOSC(1)
TCSD(1)
TIOBST(4)
IOFS
LP, XT, HS
TOST(4)
HSPLL
TOST + tPLL(3)
EC, RC
TCSD(1)
HFINTOSC(1)
None
LP, XT, HS
TOST(3)
HSPLL
TOST + tPLL(3)
OSTS
EC, RC
TCSD(1)
TIOBST(4)
IOFS
HFINTOSC(1)
Note 1:
2:
3:
4:
OSTS
OSTS
IOFS
TCSD (parameter
38) is a required delay when waking from Sleep and all Idle modes and runs concurrently
with any other required delays (see Section 3.4 Idle Modes). On Reset, HFINTOSC defaults to 1 MHz.
Includes both the HFINTOSC 16 MHz source and postscaler derived frequencies.
TOST is the Oscillator Start-up Timer (parameter 32). tPLL is the PLL Lock-out Timer (parameter F12).
Execution continues during the HFINTOSC stabilization period, TIOBST (parameter 39).
Advance Information
DS41303B-page 47
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 48
Advance Information
PIC18F2XK20/4XK20
4.0
RESET
differentiate
4.1
FIGURE 4-1:
RCON Register
RESET
Instruction
Stack Full/Underflow Reset
Stack
Pointer
External Reset
MCLRE
MCLR
( )_IDLE
Sleep
WDT
Time-out
VDD Rise
Detect
POR Pulse
VDD
Brown-out
Reset
BOREN
OST/PWRT
OST(2) 1024 Cycles
10-bit Ripple Counter
Chip_Reset
R
OSC1
32 s
LFINTOSC
PWRT(2) 65.5 ms
11-bit Ripple Counter
Enable PWRT
Enable OST(1)
Note 1:
2:
Advance Information
DS41303B-page 49
PIC18F2XK20/4XK20
REGISTER 4-1:
R/W-0
IPEN
SBOREN
U-0
(1)
R/W-1
RI
R-1
TO
R-1
R/W-0
PD
(2)
R/W-0
POR
bit 7
BOR
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
2:
3:
Note 1: Brown-out Reset is indicated when BOR is 0 and POR is 1 (assuming that both POR and BOR were set
to 1 by firmware immediately after POR).
2: It is recommended that the POR bit be set after a Power-on Reset has been detected so that subsequent
Power-on Resets may be detected.
DS41303B-page 50
Advance Information
PIC18F2XK20/4XK20
4.2
FIGURE 4-2:
4.3
PIC MCU
D
R
R1
MCLR
C
Note 1:
2:
3:
VDD
VDD
EXTERNAL POWER-ON
RESET CIRCUIT (FOR
SLOW VDD POWER-UP)
Advance Information
DS41303B-page 51
PIC18F2XK20/4XK20
4.4
4.4.1
TABLE 4-1:
4.4.2
DETECTING BOR
4.4.3
BOR CONFIGURATIONS
BOR Configuration
BOREN1
BOREN0
Status of
SBOREN
(RCON<6>)
Unavailable
Available
Unavailable
Unavailable
DS41303B-page 52
BOR Operation
BOR disabled; must be enabled by reprogramming the Configuration bits.
BOR enabled by software; operation controlled by SBOREN.
Advance Information
PIC18F2XK20/4XK20
4.5
4.5.1
The
Power-up
Timer
(PWRT)
of
PIC18F2XK20/4XK20 devices is an 11-bit counter
which uses the LFINTOSC source as the clock input.
This yields an approximate time interval of
2048 x 32 s = 65.6 ms. While the PWRT is counting,
the device is held in Reset.
The power-up time delay depends on the LFINTOSC
clock and will vary from chip-to-chip due to temperature
and process variation. See DC parameter 33 for
details.
The PWRT is enabled by clearing the PWRTEN
Configuration bit.
4.5.2
TABLE 4-2:
4.5.3
4.5.4
TIME-OUT SEQUENCE
Oscillator
Configuration
HSPLL
PWRTEN = 0
66
ms(1)
+ 1024 TOSC + 2
ms(2)
PWRTEN = 1
Exit from
Power-Managed Mode
HS, XT, LP
1024 TOSC
1024 TOSC
EC, ECIO
66 ms(1)
RC, RCIO
ms(1)
(1)
INTIO1, INTIO2
66
66 ms
Advance Information
DS41303B-page 53
PIC18F2XK20/4XK20
FIGURE 4-3:
TIME-OUT SEQUENCE ON POWER-UP (MCLR TIED TO VDD, VDD RISE < TPWRT)
VDD
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
TOST
OST TIME-OUT
INTERNAL RESET
FIGURE 4-4:
VDD
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
TOST
OST TIME-OUT
INTERNAL RESET
FIGURE 4-5:
VDD
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
TOST
OST TIME-OUT
INTERNAL RESET
DS41303B-page 54
Advance Information
PIC18F2XK20/4XK20
FIGURE 4-6:
SLOW RISE TIME (MCLR TIED TO VDD, VDD RISE > TPWRT)
5V
VDD
0V
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
TOST
OST TIME-OUT
INTERNAL RESET
FIGURE 4-7:
VDD
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
TOST
OST TIME-OUT
TPLL
PLL TIME-OUT
INTERNAL RESET
Note:
Advance Information
DS41303B-page 55
PIC18F2XK20/4XK20
4.6
TABLE 4-3:
Condition
Program
Counter
RCON Register
SBOREN
RI
TO
PD
STKPTR Register
POR BOR STKFUL
STKUNF
Power-on Reset
0000h
RESET Instruction
0000h
u(2)
Brown-out Reset
0000h
(2)
0000h
u(2)
0000h
u(2)
0000h
u(2)
0000h
u(2)
0000h
u(2)
0000h
u(2)
0000h
u(2)
PC + 2
u(2)
PC + 2(1)
u(2)
Legend: u = unchanged
Note 1: When the wake-up is due to an interrupt and the GIEH or GIEL bits are set, the PC is loaded with the
interrupt vector (008h or 0018h).
2: Reset state is 1 for POR and unchanged for all other Resets when software BOR is enabled
(BOREN<1:0> Configuration bits = 01 and SBOREN = 1). Otherwise, the Reset state is 0.
DS41303B-page 56
Advance Information
PIC18F2XK20/4XK20
TABLE 4-4:
Power-on Reset,
Brown-out Reset
MCLR Resets,
WDT Reset,
RESET Instruction,
Stack Resets
TOSU
PIC18F2XK20 PIC18F4XK20
---0 0000
---0 0000
---0 uuuu(3)
TOSH
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu(3)
TOSL
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu(3)
STKPTR
PIC18F2XK20 PIC18F4XK20
00-0 0000
uu-0 0000
uu-u uuuu(3)
PCLATU
PIC18F2XK20 PIC18F4XK20
---0 0000
---0 0000
---u uuuu
PCLATH
Register
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
PCL
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
PC + 2(2)
TBLPTRU
PIC18F2XK20 PIC18F4XK20
--00 0000
--00 0000
--uu uuuu
TBLPTRH
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
TBLPTRL
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
TABLAT
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
PRODH
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
PRODL
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
INTCON
PIC18F2XK20 PIC18F4XK20
0000 000x
0000 000u
uuuu uuuu(1)
INTCON2
PIC18F2XK20 PIC18F4XK20
1111 -1-1
1111 -1-1
uuuu -u-u(1)
INTCON3
PIC18F2XK20 PIC18F4XK20
11-0 0-00
11-0 0-00
uu-u u-uu(1)
INDF0
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
POSTINC0
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
POSTDEC0
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
PREINC0
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
PLUSW0
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
FSR0H
PIC18F2XK20 PIC18F4XK20
---- 0000
---- 0000
---- uuuu
FSR0L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
WREG
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
INDF1
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
POSTINC1
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
POSTDEC1
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
PREINC1
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
PLUSW1
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
Legend:
Note 1:
2:
3:
4:
5:
6:
Advance Information
DS41303B-page 57
PIC18F2XK20/4XK20
TABLE 4-4:
Power-on Reset,
Brown-out Reset
MCLR Resets,
WDT Reset,
RESET Instruction,
Stack Resets
FSR1H
PIC18F2XK20 PIC18F4XK20
---- 0000
---- 0000
---- uuuu
FSR1L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
BSR
Register
PIC18F2XK20 PIC18F4XK20
---- 0000
---- 0000
---- uuuu
INDF2
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
POSTINC2
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
POSTDEC2
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
PREINC2
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
PLUSW2
PIC18F2XK20 PIC18F4XK20
N/A
N/A
N/A
FSR2H
PIC18F2XK20 PIC18F4XK20
---- 0000
---- 0000
---- uuuu
FSR2L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
STATUS
PIC18F2XK20 PIC18F4XK20
---x xxxx
---u uuuu
---u uuuu
TMR0H
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
TMR0L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
T0CON
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
OSCCON
PIC18F2XK20 PIC18F4XK20
0011 qq00
0011 qq00
uuuu uuuu
HLVDCON
PIC18F2XK20 PIC18F4XK20
0-00 0101
0-00 0101
u-uu uuuu
WDTCON
PIC18F2XK20 PIC18F4XK20
---- ---0
---- ---0
---- ---u
PIC18F2XK20 PIC18F4XK20
0q-1 11q0
0q-q qquu
uq-u qquu
RCON
(4)
TMR1H
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
TMR1L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
T1CON
PIC18F2XK20 PIC18F4XK20
0000 0000
u0uu uuuu
uuuu uuuu
TMR2
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
PR2
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
1111 1111
T2CON
PIC18F2XK20 PIC18F4XK20
-000 0000
-000 0000
-uuu uuuu
SSPBUF
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
SSPADD
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
SSPSTAT
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
SSPCON1
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
SSPCON2
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
Legend:
Note 1:
2:
3:
4:
5:
6:
DS41303B-page 58
Advance Information
PIC18F2XK20/4XK20
TABLE 4-4:
Power-on Reset,
Brown-out Reset
MCLR Resets,
WDT Reset,
RESET Instruction,
Stack Resets
ADRESH
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
ADRESL
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
ADCON0
Register
PIC18F2XK20 PIC18F4XK20
--00 0000
--00 0000
--uu uuuu
ADCON1
PIC18F2XK20 PIC18F4XK20
--00 0qqq
--00 0qqq
--uu uuuu
ADCON2
PIC18F2XK20 PIC18F4XK20
0-00 0000
0-00 0000
u-uu uuuu
CCPR1H
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
CCPR1L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
CCP1CON
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
CCPR2H
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
CCPR2L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
CCP2CON
PIC18F2XK20 PIC18F4XK20
--00 0000
--00 0000
--uu uuuu
PSTRCON
PIC18F2XK20 PIC18F4XK20
---0 0001
---0 0001
---u uuuu
BAUDCTL
PIC18F2XK20 PIC18F4XK20
0100 0-00
0100 0-00
uuuu u-uu
PWM1CON
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
ECCP1AS
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
CVRCON
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
CVRCON2
PIC18F2XK20 PIC18F4XK20
00-- ----
00-- ----
uu-- ----
TMR3H
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
TMR3L
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
T3CON
PIC18F2XK20 PIC18F4XK20
0000 0000
uuuu uuuu
uuuu uuuu
SPBRGH
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
SPBRG
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
RCREG
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
TXREG
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
TXSTA
PIC18F2XK20 PIC18F4XK20
0000 0010
0000 0010
uuuu uuuu
RCSTA
PIC18F2XK20 PIC18F4XK20
0000 000x
0000 000x
uuuu uuuu
EEADR
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
EEADRH
PIC18F26K20
PIC18F46K20
---- --00
---- --00
---- --uu
EEDATA
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
EECON2
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
0000 0000
PIC18F2XK20 PIC18F4XK20
xx-0 x000
uu-0 u000
uu-0 u000
EECON1
Legend:
Note 1:
2:
3:
4:
5:
6:
Advance Information
DS41303B-page 59
PIC18F2XK20/4XK20
TABLE 4-4:
Register
Applicable Devices
MCLR Resets,
WDT Reset,
RESET Instruction,
Stack Resets
IPR2
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
PIR2
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu(1)
PIE2
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
PIC18F2XK20 PIC18F4XK20
-111 1111
-111 1111
-uuu uuuu
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu(1)
PIC18F2XK20 PIC18F4XK20
-000 0000
-000 0000
-uuu uuuu(1)
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
PIC18F2XK20 PIC18F4XK20
-000 0000
-000 0000
-uuu uuuu
OSCTUNE
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
TRISE
IPR1
PIR1
PIE1
PIC18F2XK20 PIC18F4XK20
---- -111
---- -111
---- -uuu
TRISD
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
TRISC
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
TRISB
PIC18F2XK20 PIC18F4XK20
1111 1111
(5)
1111 1111
(5)
uuuu uuuu
(5)
uuuu uuuu(5)
TRISA
PIC18F2XK20 PIC18F4XK20
1111 1111
LATE
PIC18F2XK20 PIC18F4XK20
---- -xxx
---- -uuu
---- -uuu
LATD
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
LATC
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
LATB
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
PIC18F2XK20 PIC18F4XK20
xxxx xxxx(5)
uuuu uuuu(5)
uuuu uuuu(5)
PIC18F2XK20 PIC18F4XK20
---- x000
---- u000
---- uuuu
PIC18F2XK20 PIC18F4XK20
---- x---
---- u---
---- u---
LATA(5)
PORTE
PORTD
1111 1111
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
PORTC
PIC18F2XK20 PIC18F4XK20
xxxx xxxx
uuuu uuuu
uuuu uuuu
PORTB
PIC18F2XK20 PIC18F4XK20
xxx0 0000
(5)
PORTA
PIC18F2XK20 PIC18F4XK20
ANSELH(6)
xx0x 0000
uuu0 0000
(5)
uu0u 0000
uuuu uuuu
(5)
uuuu uuuu(5)
PIC18F2XK20 PIC18F4XK20
---1 1111
---1 1111
---u uuuu
ANSEL
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
IOCB
PIC18F2XK20 PIC18F4XK20
0000 ----
0000 ----
uuuu ----
WPUB
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
CM1CON0
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
PIC18F2XK20 PIC18F4XK20
0000 0000
0000 0000
uuuu uuuu
CM2CON0
Legend:
Note 1:
2:
3:
4:
5:
6:
DS41303B-page 60
Advance Information
PIC18F2XK20/4XK20
TABLE 4-4:
Power-on Reset,
Brown-out Reset
MCLR Resets,
WDT Reset,
RESET Instruction,
Stack Resets
CM2CON1
PIC18F2XK20 PIC18F4XK20
0000 ----
0000 ----
uuuu ----
SLRCON
PIC18F2XK20 PIC18F4XK20
---1 1111
---1 1111
---u uuuu
PIC18F2XK20 PIC18F4XK20
1111 1111
1111 1111
uuuu uuuu
Register
SSPMSK
Legend:
Note 1:
2:
3:
4:
5:
6:
Advance Information
DS41303B-page 61
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 62
Advance Information
PIC18F2XK20/4XK20
5.0
MEMORY ORGANIZATION
5.1
FIGURE 5-1:
CALL,RCALL,RETURN
RETFIE,RETLW
Stack Level 1
Stack Level 31
2000h
0000h
0008h
0018h
On-Chip
Program Memory
3FFFh
4000h
PIC18F23K20/
43K20
On-Chip
Program Memory
User Memory Space
On-Chip
Program Memory
1FFFh
Reset Vector
On-Chip
Program Memory
PIC18F24K20/
44K20
7FFFh
8000h
PIC18F25K20/
45K20
Read 0
Read 0
FFFFh
10000h
Read 0
PIC18F26K20/
46K20
Read 0
Advance Information
1FFFFFh
200000h
DS41303B-page 63
PIC18F2XK20/4XK20
5.1.1
PROGRAM COUNTER
5.1.2
FIGURE 5-2:
5.1.2.1
Top-of-Stack Access
Top-of-Stack Registers
TOSU
00h
TOSH
1Ah
DS41303B-page 64
STKPTR<4:0>
00010
TOSL
34h
Top-of-Stack
Stack Pointer
001A34h
000D58h
00011
00010
00001
00000
Advance Information
PIC18F2XK20/4XK20
5.1.2.2
Note:
5.1.2.3
REGISTER 5-1:
The POP instruction discards the current TOS by decrementing the Stack Pointer. The previous value pushed
onto the stack then becomes the TOS value.
R/C-0
R/C-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
STKFUL(1)
STKUNF(1)
SP4
SP3
SP2
SP1
SP0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
U = Unimplemented
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
Unimplemented: Read as 0
bit 4-0
Note 1:
Advance Information
DS41303B-page 65
PIC18F2XK20/4XK20
5.1.2.4
5.1.3
5.1.4
5.1.4.1
Computed GOTO
EXAMPLE 5-2:
EXAMPLE 5-1:
CALL SUB1, FAST
RETURN, FAST
SUB1
DS41303B-page 66
ORG
TABLE
MOVF
CALL
nn00h
ADDWF
RETLW
RETLW
RETLW
.
.
.
5.1.4.2
Advance Information
PIC18F2XK20/4XK20
5.2
5.2.2
5.2.1
CLOCKING SCHEME
FIGURE 5-3:
INSTRUCTION FLOW/PIPELINING
CLOCK/INSTRUCTION CYCLE
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
OSC1
Q1
Q2
Internal
Phase
Clock
Q3
Q4
PC
PC
PC + 2
PC + 4
OSC2/CLKOUT
(RC mode)
Execute INST (PC 2)
Fetch INST (PC)
EXAMPLE 5-3:
TCY0
TCY1
Fetch 1
Execute 1
2. MOVWF PORTB
4. BSF
1. MOVLW 55h
3. BRA
SUB_1
PORTA, BIT3 (Forced NOP)
Fetch 2
TCY2
TCY3
TCY4
TCY5
Execute 2
Fetch 3
Execute 3
Fetch 4
Flush (NOP)
Fetch SUB_1 Execute SUB_1
All instructions are single cycle, except for any program branches. These take two cycles since the fetch instruction
is flushed from the pipeline while the new instruction is being fetched and then executed.
Advance Information
DS41303B-page 67
PIC18F2XK20/4XK20
5.2.3
INSTRUCTIONS IN PROGRAM
MEMORY
FIGURE 5-4:
LSB = 0
0Fh
EFh
F0h
C1h
F4h
55h
03h
00h
23h
56h
Program Memory
Byte Locations
5.2.4
Instruction 1:
Instruction 2:
MOVLW
GOTO
055h
0006h
Instruction 3:
MOVFF
123h, 456h
TWO-WORD INSTRUCTIONS
EXAMPLE 5-4:
Word Address
000000h
000002h
000004h
000006h
000008h
00000Ah
00000Ch
00000Eh
000010h
000012h
000014h
TWO-WORD INSTRUCTIONS
CASE 1:
Object Code
0110 0110 0000
1100 0001 0010
1111 0100 0101
0010 0100 0000
0000
0011
0110
0000
Source Code
TSTFSZ
REG1
; is RAM location 0?
MOVFF
REG1, REG2 ; No, skip this word
; Execute this word as a NOP
ADDWF
REG3
; continue code
0000
0011
0110
0000
Source Code
TSTFSZ
REG1
; is RAM location 0?
MOVFF
REG1, REG2 ; Yes, execute this word
; 2nd word of instruction
ADDWF
REG3
; continue code
CASE 2:
Object Code
0110 0110 0000
1100 0001 0010
1111 0100 0101
0010 0100 0000
DS41303B-page 68
Advance Information
PIC18F2XK20/4XK20
5.3
Note:
5.3.1
Advance Information
DS41303B-page 69
PIC18F2XK20/4XK20
FIGURE 5-5:
BSR<3:0>
= 0000
00h
Access RAM
FFh
00h
GPR
Bank 0
= 0001
= 0011
= 0100
= 0101
= 0110
= 0111
= 1000
= 1001
= 1010
= 1011
= 1100
= 1101
= 1110
= 1111
DS41303B-page 70
000h
05Fh
060h
0FFh
100h
GPR
Bank 1
= 0010
When a = 0:
FFh
00h
1FFh
200h
FFh
00h
2FFh
300h
FFh
00h
3FFh
400h
FFh
00h
4FFh
500h
FFh
00h
5FFh
600h
FFh
00h
6FFh
700h
Bank 2
Bank 3
Bank 4
Bank 5
When a = 1:
The BSR specifies the Bank
used by the instruction.
Bank 6
Bank 7
Bank 8
Bank 9
7FFh
800h
FFh
00h
FFh
00h
Unused
Read 00h
9FFh
A00h
FFh
00h
AFFh
B00h
FFh
00h
BFFh
C00h
FFh
Bank 13 00h
CFFh
D00h
FFh
00h
DFFh
E00h
Bank 11
Bank 12
00h
5Fh
Access RAM High 60h
(SFRs)
FFh
8FFh
900h
FFh
00h
Bank 10
Access Bank
Bank 14
FFh
00h
Unused
FFh
SFR
Bank 15
EFFh
F00h
F5Fh
F60h
FFFh
Advance Information
PIC18F2XK20/4XK20
FIGURE 5-6:
BSR<3:0>
= 0000
00h
Access RAM
FFh
00h
GPR
Bank 0
= 0001
= 0011
= 0100
= 0101
= 0110
= 0111
= 1000
= 1001
= 1010
= 1011
= 1100
= 1101
= 1110
= 1111
1FFh
200h
FFh
00h
Bank 2
Bank 3
Bank 4
Bank 5
000h
05Fh
060h
0FFh
100h
GPR
Bank 1
= 0010
When a = 0:
GPR
FFh
00h
2FFh
300h
FFh
00h
3FFh
400h
FFh
00h
4FFh
500h
FFh
00h
5FFh
600h
FFh
00h
6FFh
700h
When a = 1:
The BSR specifies the Bank
used by the instruction.
Bank 6
Bank 7
Bank 8
Bank 9
7FFh
800h
FFh
00h
FFh
00h
Unused
Read 00h
9FFh
A00h
FFh
00h
AFFh
B00h
FFh
00h
BFFh
C00h
FFh
Bank 13 00h
CFFh
D00h
FFh
00h
DFFh
E00h
Bank 11
Bank 12
00h
5Fh
Access RAM High 60h
(SFRs)
FFh
8FFh
900h
FFh
00h
Bank 10
Access Bank
Bank 14
FFh
00h
Unused
FFh
SFR
Bank 15
EFFh
F00h
F5Fh
F60h
FFFh
Advance Information
DS41303B-page 71
PIC18F2XK20/4XK20
FIGURE 5-7:
BSR<3:0>
= 0000
00h
Access RAM
FFh
00h
GPR
Bank 0
= 0001
= 0011
= 0100
= 0101
= 0110
= 0111
= 1000
= 1001
= 1010
= 1011
= 1100
= 1101
= 1110
= 1111
DS41303B-page 72
1FFh
200h
FFh
00h
Bank 2
Bank 3
Bank 4
Bank 5
000h
05Fh
060h
0FFh
100h
GPR
Bank 1
= 0010
When a = 0:
GPR
FFh
00h
2FFh
300h
GPR
3FFh
400h
FFh
00h
When a = 1:
The BSR specifies the Bank
used by the instruction.
GPR
4FFh
500h
FFh
00h
GPR
FFh
00h
5FFh
600h
FFh
00h
6FFh
700h
Bank 6
Bank 7
Bank 8
Bank 9
Bank 10
FFh
00h
7FFh
800h
FFh
00h
8FFh
900h
FFh
00h
Unused
Read 00h
AFFh
B00h
FFh
00h
BFFh
C00h
FFh
Bank 13 00h
CFFh
D00h
FFh
00h
DFFh
E00h
Bank 12
00h
5Fh
Access RAM High 60h
(SFRs)
FFh
9FFh
A00h
FFh
00h
Bank 11
Access Bank
Bank 14
FFh
00h
Unused
FFh
SFR
Bank 15
EFFh
F00h
F5Fh
F60h
FFFh
Advance Information
PIC18F2XK20/4XK20
FIGURE 5-8:
BSR<3:0>
= 0000
00h
Access RAM
FFh
00h
GPR
Bank 0
= 0001
= 0011
= 0100
= 0101
= 0110
= 0111
= 1000
= 1001
= 1010
= 1011
= 1100
= 1101
= 1110
= 1111
1FFh
200h
FFh
00h
Bank 2
Bank 3
Bank 4
Bank 5
2FFh
300h
GPR
3FFh
400h
FFh
00h
Bank 12
5FFh
600h
GPR
6FFh
700h
GPR
7FFh
800h
FFh
00h
GPR
Access Bank
Access RAM Low
00h
5Fh
Access RAM High 60h
(SFRs)
FFh
8FFh
900h
FFh
00h
GPR
9FFh
A00h
FFh
00h
GPR
AFFh
B00h
FFh
00h
GPR
FFh
00h
FFh
Bank 13 00h
BFFh
C00h
GPR
CFFh
D00h
GPR
DFFh
E00h
FFh
00h
GPR
Bank 14
FFh
00h
GPR
FFh
SFR
Bank 15
When a = 1:
GPR
Bank 7
Bank 11
4FFh
500h
FFh
00h
Bank 10
GPR
FFh
00h
Bank 6
Bank 9
GPR
FFh
00h
FFh
00h
Bank 8
000h
05Fh
060h
0FFh
100h
GPR
Bank 1
= 0010
When a = 0:
EFFh
F00h
F5Fh
F60h
FFFh
Advance Information
DS41303B-page 73
PIC18F2XK20/4XK20
FIGURE 5-9:
Data Memory
BSR(1)
000h
00h
Bank 0
100h
Bank 1
Bank Select(2)
FFh
00h
From Opcode(2)
FFh
00h
200h
Bank 2
300h
FFh
00h
Bank 3
through
Bank 13
FFh
00h
E00h
Bank 14
F00h
Bank 15
FFFh
Note 1:
2:
FFh
00h
FFh
The Access RAM bit of the instruction can be used to force an override of the selected bank (BSR<3:0>) to
the registers of the Access Bank.
The MOVFF instruction embeds the entire 12-bit address in the instruction.
DS41303B-page 74
Advance Information
PIC18F2XK20/4XK20
5.3.2
ACCESS BANK
5.3.3
5.3.4
Advance Information
DS41303B-page 75
PIC18F2XK20/4XK20
TABLE 5-1:
Address
Name
Address
Name
Address
Name
Address
Name
FFFh
TOSU
FD7h
TMR0H
FAFh
SPBRG
F87h
(2)
FFEh
TOSH
FD6h
TMR0L
FAEh
RCREG
F86h
(2)
FFDh
TOSL
FD5h
T0CON
FADh
TXREG
F85h
(2)
FFCh
STKPTR
FD4h
(2)
FACh
TXSTA
F84h
PORTE
FFBh
PCLATU
FD3h
OSCCON
FABh
RCSTA
F83h
PORTD(3)
FFAh
PCLATH
FD2h
HLVDCON
FAAh
EEADRH
F82h
PORTC
FF9h
PCL
FD1h
WDTCON
FA9h
EEADR
F81h
PORTB
FF8h
TBLPTRU
FD0h
RCON
FA8h
EEDATA
F80h
PORTA
FF7h
TBLPTRH
FCFh
TMR1H
FA7h
EECON2(1)
F7Fh
ANSELH
FF6h
TBLPTRL
FCEh
TMR1L
FA6h
EECON1
F7Eh
ANSEL
FF5h
TABLAT
FCDh
T1CON
FA5h
(2)
F7Dh
IOCB
FF4h
PRODH
FCCh
TMR2
FA4h
(2)
F7Ch
WPUB
FF3h
PRODL
FCBh
PR2
FA3h
(2)
F7Bh
CM1CON0
FF2h
INTCON
FCAh
T2CON
FA2h
IPR2
F7Ah
CM2CON0
FF1h
INTCON2
FC9h
SSPBUF
FA1h
PIR2
F79h
CM2CON1
FF0h
INTCON3
FC8h
SSPADD
FA0h
PIE2
F78h
SLRCON
FEFh
INDF0(1)
FC7h
SSPSTAT
F9Fh
IPR1
F77h
SSPMSK
FEEh POSTINC0(1)
FC6h
SSPCON1
F9Eh
PIR1
F76h
(2)
FEDh
POSTDEC0(1)
FC5h
SSPCON2
F9Dh
PIE1
F75h
(2)
FECh
PREINC0(1)
FC4h
ADRESH
F9Ch
(2)
F74h
(2)
FEBh
PLUSW0(1)
FC3h
ADRESL
F9Bh
OSCTUNE
F73h
(2)
FEAh
FSR0H
FC2h
ADCON0
F9Ah
(2)
F72h
(2)
F71h
(2)
FE9h
FSR0L
FC1h
ADCON1
F99h
(2)
FE8h
WREG
FC0h
ADCON2
F98h
(2)
F70h
(2)
FE7h
INDF1(1)
F6Fh
(2)
FE6h POSTINC1(1)
FBFh
CCPR1H
F97h
(2)
FBEh
CCPR1L
F96h
TRISE(3)
F6Eh
(2)
F6Dh
(2)
FE5h
POSTDEC1(1)
FBDh
CCP1CON
F95h
TRISD(3)
FE4h
PREINC1(1)
FBCh
CCPR2H
F94h
TRISC
F6Ch
(2)
FE3h
PLUSW1(1)
FBBh
CCPR2L
F93h
TRISB
F6Bh
(2)
FE2h
FSR1H
FBAh
CCP2CON
F92h
TRISA
F6Ah
(2)
(2)
FE1h
FSR1L
FB9h
PSTRCON
F91h
F69h
(2)
FE0h
BSR
FB8h
BAUDCTL
F90h
(2)
F68h
(2)
FDFh
INDF2(1)
F67h
(2)
FDEh POSTINC2(1)
FB7h
PWM1CON
F8Fh
(2)
FB6h
ECCP1AS
F8Eh
(2)
F66h
(2)
F65h
(2)
FDDh
POSTDEC2(1)
FB5h
CVRCON
F8Dh
LATE(3)
FDCh
PREINC2(1)
FB4h
CVRCON2
F8Ch
LATD(3)
F64h
(2)
FDBh
PLUSW2(1)
FB3h
TMR3H
F8Bh
LATC
F63h
(2)
FDAh
FSR2H
FB2h
TMR3L
F8Ah
LATB
F62h
(2)
FD9h
FSR2L
FB1h
T3CON
F89h
LATA
F61h
(2)
FD8h
STATUS
FB0h
SPBRGH
F88h
(2)
F60h
(2)
Note 1:
2:
3:
DS41303B-page 76
Advance Information
PIC18F2XK20/4XK20
TABLE 5-2:
File Name
TOSU
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Details
on page:
---0 0000
57, 64
TOSH
0000 0000
57, 64
TOSL
0000 0000
57, 64
00-0 0000
57, 65
---0 0000
57, 64
STKPTR
STKFUL
STKUNF
PCLATU
Value on
POR, BOR
SP4
SP3
SP2
SP1
SP0
PCLATH
0000 0000
57, 64
PCL
0000 0000
57, 64
--00 0000
57, 90
TBLPTRU
bit 21
TBLPTRH
0000 0000
57, 90
TBLPTRL
0000 0000
57, 90
TABLAT
0000 0000
57, 90
PRODH
xxxx xxxx
57, 101
PRODL
xxxx xxxx
57, 101
INTCON
GIE/GIEH
PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
0000 000x
57, 105
INTCON2
RBPU
INTEDG0
INTEDG1
INTEDG2
TMR0IP
RBIP
1111 -1-1
57, 106
INT2IP
INT1IP
INT2IE
INT1IE
INT2IF
INT1IF
11-0 0-00
57, 107
INTCON3
INDF0
Uses contents of FSR0 to address data memory value of FSR0 not changed (not a physical register)
N/A
57, 82
POSTINC0
Uses contents of FSR0 to address data memory value of FSR0 post-incremented (not a physical register)
N/A
57, 82
POSTDEC0
Uses contents of FSR0 to address data memory value of FSR0 post-decremented (not a physical register)
N/A
57, 82
PREINC0
Uses contents of FSR0 to address data memory value of FSR0 pre-incremented (not a physical register)
N/A
57, 82
PLUSW0
Uses contents of FSR0 to address data memory value of FSR0 pre-incremented (not a physical register)
value of FSR0 offset by W
N/A
57, 82
FSR0H
---- 0000
57, 82
FSR0L
xxxx xxxx
57, 82
WREG
Working Register
xxxx xxxx
57
INDF1
Uses contents of FSR1 to address data memory value of FSR1 not changed (not a physical register)
N/A
57, 82
POSTINC1
Uses contents of FSR1 to address data memory value of FSR1 post-incremented (not a physical register)
N/A
57, 82
POSTDEC1
Uses contents of FSR1 to address data memory value of FSR1 post-decremented (not a physical register)
N/A
57, 82
PREINC1
Uses contents of FSR1 to address data memory value of FSR1 pre-incremented (not a physical register)
N/A
57, 82
PLUSW1
Uses contents of FSR1 to address data memory value of FSR1 pre-incremented (not a physical register)
value of FSR1 offset by W
N/A
57, 82
---- 0000
58, 82
FSR1H
FSR1L
BSR
xxxx xxxx
58, 82
---- 0000
58, 69
INDF2
Uses contents of FSR2 to address data memory value of FSR2 not changed (not a physical register)
N/A
58, 82
POSTINC2
Uses contents of FSR2 to address data memory value of FSR2 post-incremented (not a physical register)
N/A
58, 82
POSTDEC2
Uses contents of FSR2 to address data memory value of FSR2 post-decremented (not a physical register)
N/A
58, 82
PREINC2
Uses contents of FSR2 to address data memory value of FSR2 pre-incremented (not a physical register)
N/A
58, 82
PLUSW2
Uses contents of FSR2 to address data memory value of FSR2 pre-incremented (not a physical register)
value of FSR2 offset by W
N/A
58, 82
---- 0000
58, 82
FSR2H
FSR2L
STATUS
Legend:
Note 1:
2:
3:
4:
5:
6:
OV
DC
xxxx xxxx
58, 82
---x xxxx
58, 80
Advance Information
DS41303B-page 77
PIC18F2XK20/4XK20
TABLE 5-2:
File Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Value on
POR, BOR
Details
on page:
TMR0H
0000 0000
58, 139
TMR0L
xxxx xxxx
58, 139
58, 137
T0CON
TMR0ON
T08BIT
T0CS
T0SE
PSA
T0PS2
T0PS1
T0PS0
1111 1111
OSCCON
IDLEN
IRCF2
IRCF1
IRCF0
OSTS
IOFS
SCS1
SCS0
0011 qq00
27, 58
HLVDCON
VDIRMAG
IRVST
HLVDEN
HLVDL3
HLVDL2
HLVDL1
HLVDL0
0-00 0101
58, 283
SWDTEN
--- ---0
58, 299
IPEN
SBOREN(1)
RI
TO
PD
POR
BOR
0q-1 11q0
49, 56,
114
WDTCON
RCON
TMR1H
xxxx xxxx
58, 146
TMR1L
xxxx xxxx
58, 146
T1CON
RD16
T1RUN
T1CKPS1
T1CKPS0
T1OSCEN
T1SYNC
0000 0000
58, 141
TMR2
Timer2 Register
0000 0000
58, 148
PR2
1111 1111
58, 148
-000 0000
58, 147
xxxx xxxx
58, 193,
194
T2CON
T2OUTPS3
T2OUTPS2
T2OUTPS1
T2OUTPS0
TMR2ON
TMR1CS
TMR1ON
T2CKPS1
T2CKPS0
SSPBUF
SSPADD
SSP Address Register in I2C Slave Mode. SSP Baud Rate Reload Register in I2C Master Mode.
0000 0000
58, 194
SSPSTAT
SMP
CKE
D/A
R/W
UA
BF
0000 0000
58, 187,
196
SSPCON1
WCOL
SSPOV
SSPEN
CKP
SSPM3
SSPM2
SSPM1
SSPM0
0000 0000
58, 187,
196
GCEN
ACKSTAT
ACKDT
ACKEN
RCEN
PEN
RSEN
SEN
SSPCON2
0000 0000
58, 197
ADRESH
xxxx xxxx
59, 267
ADRESL
xxxx xxxx
59, 267
ADCON0
CHS3
CHS2
CHS1
CHS0
GO/DONE
ADON
--00 0000
59, 261
ADCON1
VCFG1
VCFG0
--00 ----
59, 262
ADFM
ACQT2
ACQT1
ACQT0
ADCS2
ADCS1
ADCS0
ADCON2
0-00 0000
59, 263
CCPR1H
xxxx xxxx
59, 154
CCPR1L
xxxx xxxx
59, 154
0000 0000
59, 165
59, 154
CCP1CON
P1M1
P1M0
DC1B1
DC1B0
CCP1M3
CCP1M2
CCP1M1
CCP1M0
CCPR2H
xxxx xxxx
CCPR2L
xxxx xxxx
59, 154
CCP2M0
--00 0000
59, 153
CCP2CON
DC2B1
DC2B0
CCP2M3
CCP2M2
CCP2M1
PSTRCON
STRSYNC
STRD
STRC
STRB
STRA
---0 0001
59, 179
BAUDCTL
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
0100 0-00
59, 238
PWM1CON
PRSEN
PDC6
PDC5
PDC4
PDC3
PDC2
PDC1
PDC0
0000 0000
59, 178
ECCP1AS
ECCPASE
ECCPAS2
ECCPAS1
ECCPAS0
PSSAC1
PSSAC0
PSSBD1
PSSBD0
0000 0000
59, 175
CVRCON
CVREN
CVROE
CVRR
CVRSS
CVR3
CVR2
CVR1
CVR0
0000 0000
59, 281
CVRCON2
FVREN
FVRST
00-- ----
59, 282
59, 151
TMR3H
xxxx xxxx
TMR3L
xxxx xxxx
59, 151
0000 0000
59, 149
T3CON
RD16
Legend:
Note 1:
2:
3:
4:
5:
6:
T3CCP2
T3CKPS1
T3CKPS0
T3CCP1
T3SYNC
TMR3CS
TMR3ON
DS41303B-page 78
Advance Information
PIC18F2XK20/4XK20
TABLE 5-2:
File Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Value on
POR, BOR
Details
on page:
SPBRGH
0000 0000
59, 227
SPBRG
0000 0000
59, 227
RCREG
0000 0000
59, 228
TXREG
0000 0000
59, 227
TXSTA
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
0000 0010
59, 236
RCSTA
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
0000 000x
59, 237
EEADR
EEADR7
EEADR6
EEADR5
EEADR4
EEADR3
EEADR2
EEADR1
EEADR0
EEADR9
EEADR8
EEADRH
EEDATA
EECON2
EECON1
EEPGD
CFGS
FREE
WRERR
WREN
WR
RD
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
1111 1111
60, 113
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
0000 0000
60, 109
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
0000 0000
60, 111
IPR1
PSPIP(2)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
1111 1111
60, 112
PIR1
PSPIF(2)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
0000 0000
60, 108
PIE1
PSPIE(2)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
0000 0000
60, 110
OSCTUNE
INTSRC
PLLEN(3)
TUN5
TUN4
TUN3
TUN2
TUN1
TUN0
0q00 0000
31, 60
IBF
OBF
IBOV
PSPMODE
TRISE2
TRISE1
TRISE0
0000 -111
60, 130
TRISE(2)
TRISD(2)
1111 1111
60, 126
TRISC
1111 1111
60, 123
TRISB
1111 1111
60, 120
TRISA
TRISA7(5)
TRISA6(5)
LATE(2)
1111 1111
60, 117
---- -xxx
60, 129
LATD(2)
xxxx xxxx
60, 126
LATC
xxxx xxxx
60, 123
LATB
xxxx xxxx
60, 120
LATA
PORTE
LATA7(5)
LATA6(5)
RE3(4)
xxxx xxxx
60, 117
RE2(2)
RE1(2)
RE0(2)
---- x000
60, 129
PORTD(2)
RD7
RD6
RD5
RD4
RD3
RD2
RD1
RD0
xxxx xxxx
60, 126
PORTC
RC7
RC6
RC5
RC4
RC3
RC2
RC1
RC0
xxxx xxxx
60, 123
PORTB
RB7
RB6
RB5
RB4
RB3
RB2
RB1
RB0
xxx0 0000
60, 120
PORTA
RA7(5)
RA6(5)
RA5
RA4
RA3
RA2
RA1
RA0
xx0x 0000
60, 117
ANS12
ANS11
ANS10
ANS9
ANS8
---1 1111
60, 133
ANSEL
ANS7(2)
ANS6(2)
ANS5(2)
ANS4
ANS3
ANS2
ANS1
ANS0
1111 1111
60, 132
IOCB
IOCB7
IOCB6
IOCB5
IOCB4
0000 ----
60, 120
WPUB
WPUB7
WPUB6
WPUB5
WPUB4
WPUB3
WPUB2
WPUB1
WPUB0
1111 1111
60, 120
CM1CON0
C1ON
C1OUT
C1OE
C1POL
C1SP
C1R
C1CH1
C1CH0
0000 0000
60, 274
CM2CON0
C2ON
C2OUT
C2OE
C2POL
C2SP
C2R
C2CH1
C2CH0
0000 0000
60, 275
CM2CON1
MC1OUT
MC2OUT
C1RSEL
C2RSEL
0000 ----
61, 277
ANSELH(6)
SLRCON
SLRE
SLRD
SLRC
SLRB
SLRA
---1 1111
61, 134
SSPMSK
MSK7
MSK6
MSK5
MSK4
MSK3
MSK2
MSK1
MSK0
1111 1111
61, 204
Legend:
Note 1:
2:
3:
4:
5:
6:
Advance Information
DS41303B-page 79
PIC18F2XK20/4XK20
5.3.5
STATUS REGISTER
REGISTER 5-2:
U-0
U-0
R/W-x
R/W-x
OV
R/W-x
R/W-x
R/W-x
(1)
DC
bit 7
C(1)
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7-5
Unimplemented: Read as 0
bit 4
N: Negative bit
This bit is used for signed arithmetic (twos complement). It indicates whether the result was negative
(ALU MSB = 1).
1 = Result was negative
0 = Result was positive
bit 3
bit 2
Z: Zero bit
1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not zero
bit 1
bit 0
Note 1:
For Borrow, the polarity is reversed. A subtraction is executed by adding the twos complement of the
second operand. For rotate (RRF, RLF) instructions, this bit is loaded with either the high-order or low-order
bit of the source register.
DS41303B-page 80
Advance Information
PIC18F2XK20/4XK20
5.4
Note:
Inherent
Literal
Direct
Indirect
5.4.3
5.4.1
Many PIC18 control instructions do not need any argument at all; they either perform an operation that globally affects the device or they operate implicitly on one
register. This addressing mode is known as Inherent
Addressing. Examples include SLEEP, RESET and DAW.
Other instructions work in a similar way but require an
additional explicit argument in the opcode. This is
known as Literal Addressing mode because they
require some literal value as an argument. Examples
include ADDLW and MOVLW, which respectively, add or
move a literal value to the W register. Other examples
include CALL and GOTO, which include a 20-bit
program memory address.
EXAMPLE 5-5:
NEXT
5.4.2
INDIRECT ADDRESSING
LFSR
CLRF
DIRECT ADDRESSING
BTFSS
BRA
CONTINUE
In the core PIC18 instruction set, bit-oriented and byteoriented instructions use some version of direct
addressing by default. All of these instructions include
some 8-bit literal address as their Least Significant
Byte. This address specifies either a register address in
one of the banks of data RAM (Section 5.3.3 General
Purpose Register File) or a location in the Access
Bank (Section 5.3.2 Access Bank) as the data
source for the instruction.
Advance Information
DS41303B-page 81
PIC18F2XK20/4XK20
5.4.3.1
5.4.3.2
At the core of indirect addressing are three sets of registers: FSR0, FSR1 and FSR2. Each represents a pair
of 8-bit registers, FSRnH and FSRnL. Each FSR pair
holds a 12-bit value, therefore the four upper bits of the
FSRnH register are not used. The 12-bit FSR value can
address the entire range of the data memory in a linear
fashion. The FSR register pairs, then, serve as pointers
to data memory locations.
FIGURE 5-10:
INDIRECT ADDRESSING
000h
Bank 0
ADDWF, INDF1, 1
100h
Bank 1
200h
Bank 2
300h
FSR1H:FSR1L
7
x x x x 1 1 1 0
Bank 3
through
Bank 13
1 1 0 0 1 1 0 0
Bank 14
F00h
Bank 15
FFFh
Data Memory
DS41303B-page 82
Advance Information
PIC18F2XK20/4XK20
Operations on the FSRs with POSTDEC, POSTINC
and PREINC affect the entire register pair; that is, rollovers of the FSRnL register from FFh to 00h carry over
to the FSRnH register. On the other hand, results of
these operations do not change the value of any flags
in the STATUS register (e.g., Z, N, OV, etc.).
The PLUSW register can be used to implement a form
of indexed addressing in the data memory space. By
manipulating the value in the W register, users can
reach addresses that are fixed offsets from pointer
addresses. In some applications, this can be used to
implement some powerful program control structure,
such as software stacks, inside of data memory.
5.4.3.3
5.5
5.5.1
5.5.2
INSTRUCTIONS AFFECTED BY
INDEXED LITERAL OFFSET MODE
Advance Information
DS41303B-page 83
PIC18F2XK20/4XK20
FIGURE 5-11:
000h
060h
Bank 0
100h
00h
Bank 1
through
Bank 14
60h
Valid range
for f
Access RAM
F00h
FFh
Bank 15
F60h
SFRs
FFFh
Data Memory
000h
060h
Bank 0
100h
001001da ffffffff
Bank 1
through
Bank 14
FSR2H
FSR2L
F00h
Bank 15
F60h
SFRs
FFFh
Data Memory
BSR
00000000
000h
060h
Bank 0
100h
Bank 1
through
Bank 14
001001da ffffffff
F00h
Bank 15
F60h
SFRs
FFFh
Data Memory
DS41303B-page 84
Advance Information
PIC18F2XK20/4XK20
5.5.3
FIGURE 5-12:
Remapping of the Access Bank applies only to operations using the Indexed Literal Offset mode. Operations
that use the BSR (Access RAM bit is 1) will continue
to use direct addressing as before.
5.6
Example Situation:
ADDWF f, d, a
FSR2H:FSR2L = 120h
Locations in the region
from the FSR2 pointer
(120h) to the pointer plus
05Fh (17Fh) are mapped
to the bottom of the
Access RAM (000h-05Fh).
000h
Bank 0
100h
120h
17Fh
200h
Bank 1
Window
Bank 1
00h
Bank 1 Window
5Fh
60h
Bank 2
through
Bank 14
SFRs
FFh
Access Bank
F00h
Bank 15
F60h
SFRs
FFFh
Data Memory
Advance Information
DS41303B-page 85
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 86
Advance Information
PIC18F2XK20/4XK20
6.0
TABLE 6-1:
Erase Block
Size (bytes)
PIC18F43K20,
PIC18F23K20
64
PIC18F24K20,
PIC18F25K20,
PIC18F44K20,
PIC18F45K20
32
64
PIC18F26K20,
PIC18F46K20
64
64
Device
FIGURE 6-1:
6.1
Program Memory
Table Pointer(1)
TBLPTRU
TBLPTRH
Program Memory
(TBLPTR)
Advance Information
DS41303B-page 87
PIC18F2XK20/4XK20
FIGURE 6-2:
Program Memory
Table Pointer(1)
TBLPTRU
TBLPTRH
Holding Registers
Table Latch (8-bit)
TBLPTRL
TABLAT
Program Memory
(TBLPTR<MSBs>)
Note 1: During table writes the Table Pointer does not point directly to Program Memory. The LSBs of TBLPRTL
actually point to an address within the write block holding registers. The MSBs of the Table Pointer determine where the write block will eventually be written. The process for writing the holding registers to the
program memory array is discussed in Section 6.5 Writing to Flash Program Memory.
6.2
Control Registers
EECON1 register
EECON2 register
TABLAT register
TBLPTR registers
6.2.1
The FREE bit allows the program memory erase operation. When FREE is set, an erase operation is initiated
on the next WR command. When FREE is clear, only
writes are enabled.
The WREN bit, when set, will allow a write operation.
The WREN bit is clear on power-up.
DS41303B-page 88
Advance Information
PIC18F2XK20/4XK20
REGISTER 6-1:
R/W-x
R/W-x
U-0
R/W-0
R/W-x
R/W-0
R/S-0
R/S-0
EEPGD
CFGS
FREE
WRERR
WREN
WR
RD
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
0 = Bit is cleared
1 = Bit is set
x = Bit is unknown
bit 7
bit 6
bit 5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
When a WRERR occurs, the EEPGD and CFGS bits are not cleared. This allows tracing of the
error condition.
Advance Information
DS41303B-page 89
PIC18F2XK20/4XK20
6.2.2
6.2.3
When a TBLWT is executed the byte in the TABLAT register is written, not to Flash memory but, to a holding
register in preparation for a program memory write. The
holding registers constitute a write block which varies
depending on the device (See Table 6-1).The 3, 4, or 5
LSbs of the TBLPTRL register determine which specific
address within the holding register block is written to.
The MSBs of the Table Pointer have no effect during
TBLWT operations.
6.2.4
TABLE 6-2:
Example
TBLRD*
TBLWT*
TBLRD*+
TBLWT*+
TBLRD*TBLWT*-
TBLRD+*
TBLWT+*
FIGURE 6-3:
21
16
15
TBLPTRH
TABLE ERASE/WRITE
TBLPTR<21:n+1>(1)
TBLPTRL
TABLE WRITE
TBLPTR<n:0>(1)
DS41303B-page 90
Advance Information
PIC18F2XK20/4XK20
6.3
FIGURE 6-4:
Program Memory
TBLPTR = xxxxx1
Instruction Register
(IR)
EXAMPLE 6-1:
FETCH
TBLRD
TBLPTR = xxxxx0
TABLAT
Read Register
MOVLW
MOVWF
MOVLW
MOVWF
MOVLW
MOVWF
CODE_ADDR_UPPER
TBLPTRU
CODE_ADDR_HIGH
TBLPTRH
CODE_ADDR_LOW
TBLPTRL
READ_WORD
TBLRD*+
MOVF
MOVWF
TBLRD*+
MOVFW
MOVF
TABLAT, W
WORD_EVEN
TABLAT, W
WORD_ODD
Advance Information
DS41303B-page 91
PIC18F2XK20/4XK20
6.4
6.4.1
3.
4.
5.
6.
7.
8.
EXAMPLE 6-2:
CODE_ADDR_UPPER
TBLPTRU
CODE_ADDR_HIGH
TBLPTRH
CODE_ADDR_LOW
TBLPTRL
BSF
BCF
BSF
BSF
BCF
MOVLW
MOVWF
MOVLW
MOVWF
BSF
BSF
EECON1,
EECON1,
EECON1,
EECON1,
INTCON,
55h
EECON2
0AAh
EECON2
EECON1,
INTCON,
;
;
;
;
;
ERASE_BLOCK
Required
Sequence
DS41303B-page 92
EEPGD
CFGS
WREN
FREE
GIE
; write 55h
WR
GIE
; write 0AAh
; start erase (CPU stall)
; re-enable interrupts
Advance Information
PIC18F2XK20/4XK20
6.5
FIGURE 6-5:
The long write is necessary for programming the internal Flash. Instruction execution is halted during a long
write cycle. The long write will be terminated by the
internal programming timer.
The EEPROM on-chip timer controls the write time.
The write/erase voltages are generated by an on-chip
charge pump, rated to operate over the voltage range
of the device.
Note:
8
TBLPTR = xxxx00
TBLPTR = xxxx01
Holding Register
8
TBLPTR = xxxxYY(1)
TBLPTR = xxxx02
Holding Register
Holding Register
Holding Register
Program Memory
Note 1: YY = x7, xF, or 1F for 8, 16 or 32 byte write blocks, respectively.
6.5.1
8.
9.
10.
11.
12.
Disable interrupts.
Write 55h to EECON2.
Write 0AAh to EECON2.
Set the WR bit. This will begin the write cycle.
The CPU will stall for duration of the write (about
2 ms using internal timer).
13. Re-enable interrupts.
14. Repeat steps 6 to 13 for each block until all 64
bytes are written.
15. Verify the memory (table read).
This procedure will require about 6 ms to update each
write block of memory. An example of the required code
is given in Example 6-3.
Note:
Advance Information
DS41303B-page 93
PIC18F2XK20/4XK20
EXAMPLE 6-3:
D'64
COUNTER
BUFFER_ADDR_HIGH
FSR0H
BUFFER_ADDR_LOW
FSR0L
CODE_ADDR_UPPER
TBLPTRU
CODE_ADDR_HIGH
TBLPTRH
CODE_ADDR_LOW
TBLPTRL
TBLRD*+
MOVF
MOVWF
DECFSZ
BRA
TABLAT, W
POSTINC0
COUNTER
READ_BLOCK
MOVLW
MOVWF
MOVLW
MOVWF
MOVLW
MOVWF
MOVLW
MOVWF
BUFFER_ADDR_HIGH
FSR0H
BUFFER_ADDR_LOW
FSR0L
NEW_DATA_LOW
POSTINC0
NEW_DATA_HIGH
INDF0
MOVLW
MOVWF
MOVLW
MOVWF
MOVLW
MOVWF
BSF
BCF
BSF
BSF
BCF
MOVLW
MOVWF
MOVLW
MOVWF
BSF
BSF
TBLRD*MOVLW
MOVWF
MOVLW
MOVWF
CODE_ADDR_UPPER
TBLPTRU
CODE_ADDR_HIGH
TBLPTRH
CODE_ADDR_LOW
TBLPTRL
EECON1, EEPGD
EECON1, CFGS
EECON1, WREN
EECON1, FREE
INTCON, GIE
55h
EECON2
0AAh
EECON2
EECON1, WR
INTCON, GIE
MOVLW
MOVWF
MOVLW
MOVWF
BlockSize
COUNTER
D64/BlockSize
COUNTER2
MOVF
MOVWF
TBLWT+*
POSTINC0, W
TABLAT
;
;
;
;
; point to buffer
READ_BLOCK
;
;
;
;
;
MODIFY_WORD
; point to buffer
ERASE_BLOCK
Required
Sequence
BUFFER_ADDR_HIGH
FSR0H
BUFFER_ADDR_LOW
FSR0L
;
;
;
;
;
; write 55h
;
;
;
;
;
write 0AAh
start erase (CPU stall)
re-enable interrupts
dummy read decrement
point to buffer
WRITE_BUFFER_BACK
WRITE_BYTE_TO_HREGS
DS41303B-page 94
Advance Information
PIC18F2XK20/4XK20
EXAMPLE 6-3:
COUNTER
WRITE_WORD_TO_HREGS
BSF
BCF
BSF
BCF
MOVLW
MOVWF
MOVLW
MOVWF
BSF
DCFSZ
BRA
BSF
BCF
EECON1, EEPGD
EECON1, CFGS
EECON1, WREN
INTCON, GIE
55h
EECON2
0AAh
EECON2
EECON1, WR
COUNTER2
WRITE_BYTE_TO_HREGS
INTCON, GIE
EECON1, WREN
;
;
;
;
PROGRAM_MEMORY
Required
Sequence
6.5.2
WRITE VERIFY
UNEXPECTED TERMINATION OF
WRITE OPERATION
TABLE 6-3:
; write 55h
;
;
;
;
;
;
write 0AAh
start program (CPU stall)
repeat for remaining write blocks
re-enable interrupts
disable write to memory
6.5.4
6.5.3
PROTECTION AGAINST
SPURIOUS WRITES
6.6
Name
Bit 7
Bit 6
Bit 5
TBLPTRU
bit 21
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values on
page
57
57
57
TABLAT
INTCON
57
EECON2
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
59
59
EECON1
EEPGD
CFGS
FREE
WRERR
WREN
WR
RD
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
Legend: = unimplemented, read as 0. Shaded cells are not used during Flash/EEPROM access.
Advance Information
DS41303B-page 95
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 96
Advance Information
PIC18F2XK20/4XK20
7.0
The data EEPROM is a nonvolatile memory array, separate from the data RAM and program memory, which
is used for long-term storage of program data. It is not
directly mapped in either the register file or program
memory space but is indirectly addressed through the
Special Function Registers (SFRs). The EEPROM is
readable and writable during normal operation over the
entire VDD range.
Four SFRs are used to read and write to the data
EEPROM as well as the program memory. They are:
EECON1
EECON2
EEDATA
EEADR
EEADRH
7.2
7.1
The EECON1 register (Register 7-1) is the control register for data and program memory access. Control bit
EEPGD determines if the access will be to program or
data EEPROM memory. When the EEPGD bit is clear,
operations will access the data EEPROM memory.
When the EEPGD bit is set, program memory is
accessed.
Advance Information
DS41303B-page 97
PIC18F2XK20/4XK20
REGISTER 7-1:
R/W-x
R/W-x
U-0
R/W-0
R/W-x
R/W-0
R/S-0
R/S-0
EEPGD
CFGS
FREE
WRERR
WREN
WR
RD
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
0 = Bit is cleared
1 = Bit is set
x = Bit is unknown
bit 7
bit 6
bit 5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
When a WRERR occurs, the EEPGD and CFGS bits are not cleared. This allows tracing of the
error condition.
DS41303B-page 98
Advance Information
PIC18F2XK20/4XK20
7.3
7.4
7.5
EXAMPLE 7-1:
MOVLW
MOVWF
BCF
BCF
BSF
MOVF
EXAMPLE 7-2:
Required
Sequence
Write Verify
;
;
;
;
;
;
DATA_EE_ADDR_LOW
EEADR
DATA_EE_ADDR_HI
EEADRH
DATA_EE_DATA
EEDATA
EECON1, EEPGD
EECON1, CFGS
EECON1, WREN
INTCON, GIE
55h
EECON2
0AAh
EECON2
EECON1, WR
INTCON, GIE
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
BCF
EECON1, WREN
Advance Information
DS41303B-page 99
PIC18F2XK20/4XK20
7.6
7.8
7.7
EXAMPLE 7-3:
CLRF
BCF
BCF
BCF
BSF
EEADR
EECON1,
EECON1,
INTCON,
EECON1,
BSF
MOVLW
MOVWF
MOVLW
MOVWF
BSF
BTFSC
BRA
INCFSZ
BRA
EECON1, RD
55h
EECON2
0AAh
EECON2
EECON1, WR
EECON1, WR
$-2
EEADR, F
LOOP
BCF
BSF
EECON1, WREN
INTCON, GIE
;
;
;
;
;
;
;
;
;
;
;
;
;
CFGS
EEPGD
GIE
WREN
Loop
TABLE 7-1:
Start at address 0
Set for memory
Set for Data EEPROM
Disable interrupts
Enable writes
Loop to refresh array
Read current address
Write 55h
Write 0AAh
Set WR bit to begin write
Wait for write to complete
; Increment address
; Not zero, do it again
; Disable writes
; Enable interrupts
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INTCON
GIE/GIEH
PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
EEADR
EEADR7
EEADR6
Name
EEADRH
EEADR9 EEADR8
59
59
EEDATA
59
EECON2
59
EECON1
EEPGD
CFGS
FREE
WRERR
WREN
WR
RD
59
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
Legend: = unimplemented, read as 0. Shaded cells are not used during Flash/EEPROM access.
DS41303B-page 100
Advance Information
PIC18F2XK20/4XK20
8.0
8 x 8 HARDWARE MULTIPLIER
8.1
Introduction
EXAMPLE 8-1:
MOVF
MULWF
ARG1, W
ARG2
;
; ARG1 * ARG2 ->
; PRODH:PRODL
EXAMPLE 8-2:
8.2
8 x 8 UNSIGNED
MULTIPLY ROUTINE
8 x 8 SIGNED MULTIPLY
ROUTINE
MOVF
MULWF
ARG1, W
ARG2
BTFSC
SUBWF
ARG2, SB
PRODH, F
MOVF
BTFSC
SUBWF
ARG2, W
ARG1, SB
PRODH, F
;
;
;
;
;
Operation
TABLE 8-1:
Routine
8 x 8 unsigned
8 x 8 signed
16 x 16 unsigned
16 x 16 signed
Program
Memory
(Words)
Cycles
(Max)
13
Hardware multiply
33
Hardware multiply
Multiply Method
Time
@ 40 MHz
@ 10 MHz
@ 4 MHz
69
6.9 s
27.6 s
69 s
100 ns
400 ns
1 s
91
9.1 s
36.4 s
91 s
600 ns
2.4 s
6 s
21
242
24.2 s
96.8 s
242 s
28
28
2.8 s
11.2 s
28 s
52
254
25.4 s
102.6 s
254 s
Hardware multiply
35
40
4.0 s
16.0 s
40 s
Advance Information
DS41303B-page 101
PIC18F2XK20/4XK20
Example 8-3 shows the sequence to do a 16 x 16
unsigned multiplication. Equation 8-1 shows the
algorithm that is used. The 32-bit result is stored in four
registers (RES<3:0>).
EQUATION 8-1:
RES3:RES0
=
=
EXAMPLE 8-3:
16 x 16 UNSIGNED
MULTIPLICATION
ALGORITHM
ARG1H:ARG1L ARG2H:ARG2L
(ARG1H ARG2H 216) +
(ARG1H ARG2L 28) +
(ARG1L ARG2H 28) +
(ARG1L ARG2L)
EQUATION 8-2:
EXAMPLE 8-4:
16 x 16 UNSIGNED
MULTIPLY ROUTINE
MOVF
MULWF
ARG1L, W
ARG2L
MOVFF
MOVFF
PRODH, RES1
PRODL, RES0
MOVF
MULWF
ARG1H, W
ARG2H
MOVFF
MOVFF
PRODH, RES3
PRODL, RES2
MOVF
MULWF
ARG1L, W
ARG2H
MOVF
ADDWF
MOVF
ADDWFC
CLRF
ADDWFC
PRODL, W
RES1, F
PRODH, W
RES2, F
WREG
RES3, F
MOVF
MULWF
ARG1H, W
ARG2L
MOVF
ADDWF
MOVF
ADDWFC
CLRF
ADDWFC
PRODL, W
RES1, F
PRODH, W
RES2, F
WREG
RES3, F
; ARG1L * ARG2L->
; PRODH:PRODL
;
;
ARG1L * ARG2H->
PRODH:PRODL
Add cross
products
ARG1H * ARG2L->
PRODH:PRODL
Add cross
products
DS41303B-page 102
ARG1L, W
ARG2L
MOVFF
MOVFF
PRODH, RES1
PRODL, RES0
MOVF
MULWF
ARG1H, W
ARG2H
MOVFF
MOVFF
PRODH, RES3
PRODL, RES2
MOVF
MULWF
ARG1L, W
ARG2H
MOVF
ADDWF
MOVF
ADDWFC
CLRF
ADDWFC
PRODL, W
RES1, F
PRODH, W
RES2, F
WREG
RES3, F
MOVF
MULWF
ARG1H, W
ARG2L
MOVF
ADDWF
MOVF
ADDWFC
CLRF
ADDWFC
PRODL, W
RES1, F
PRODH, W
RES2, F
WREG
RES3, F
BTFSS
BRA
MOVF
SUBWF
MOVF
SUBWFB
ARG2H, 7
SIGN_ARG1
ARG1L, W
RES2
ARG1H, W
RES3
; ARG2H:ARG2L neg?
; no, check ARG1
;
;
;
ARG1H, 7
CONT_CODE
ARG2L, W
RES2
ARG2H, W
RES3
; ARG1H:ARG1L neg?
; no, done
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
MOVF
MULWF
;
;
;
;
;
;
;
;
;
16 x 16 SIGNED
MULTIPLY ROUTINE
;
; ARG1H * ARG2H->
; PRODH:PRODL
;
;
16 x 16 SIGNED
MULTIPLICATION
ALGORITHM
;
;
;
;
;
;
;
;
;
;
SIGN_ARG1
BTFSS
BRA
MOVF
SUBWF
MOVF
SUBWFB
;
CONT_CODE
:
Advance Information
PIC18F2XK20/4XK20
9.0
INTERRUPTS
9.2
RCON
INTCON
INTCON2
INTCON3
PIR1, PIR2
PIE1, PIE2
IPR1, IPR2
It is recommended that the Microchip header files supplied with MPLAB IDE be used for the symbolic bit
names in these registers. This allows the assembler/
compiler to automatically take care of the placement of
these bits within the specified register.
In general, interrupt sources have three bits to control
their operation. They are:
Flag bit to indicate that an interrupt event
occurred
Enable bit that allows program execution to
branch to the interrupt vector address when the
flag bit is set
Priority bit to select high priority or low priority
9.1
Mid-Range Compatibility
Interrupt Priority
9.3
Interrupt Response
Advance Information
DS41303B-page 103
PIC18F2XK20/4XK20
FIGURE 9-1:
TMR0IF
TMR0IE
TMR0IP
RBIF
RBIE
RBIP
INT0IF
INT0IE
Wake-up if in
Idle or Sleep modes
(1)
Interrupt to CPU
Vector to Location
0008h
INT1IF
INT1IE
INT1IP
INT2IF
INT2IE
INT2IP
SSPIF
SSPIE
SSPIP
GIEH/GIE
ADIF
ADIE
ADIP
IPEN
IPEN
GIEL/PEIE
RCIF
RCIE
RCIP
IPEN
Additional Peripheral Interrupts
High Priority Interrupt Generation
Low Priority Interrupt Generation
SSPIF
SSPIE
SSPIP
ADIF
ADIE
ADIP
RCIF
RCIE
RCIP
Interrupt to CPU
Vector to Location
0018h
TMR0IF
TMR0IE
TMR0IP
RBIF
RBIE
RBIP
(1)
GIEH/GIE
GIEL/PEIE
INT1IF
INT1IE
INT1IP
Additional Peripheral Interrupts
INT2IF
INT2IE
INT2IP
Note
1:
The RBIF interrupt also requires the individual pin IOCB enables.
DS41303B-page 104
Advance Information
PIC18F2XK20/4XK20
9.4
INTCON Registers
Note:
REGISTER 9-1:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-x
GIE/GIEH
PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
2:
x = Bit is unknown
A mismatch condition will continue to set the RBIF bit. Reading PORTB will end the
mismatch condition and allow the bit to be cleared.
RB port change interrupts also require the individual pin IOCB enables.
Advance Information
DS41303B-page 105
PIC18F2XK20/4XK20
REGISTER 9-2:
R/W-1
R/W-1
R/W-1
R/W-1
U-0
R/W-1
U-0
R/W-1
RBPU
INTEDG0
INTEDG1
INTEDG2
TMR0IP
RBIP
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
Unimplemented: Read as 0
bit 2
bit 1
Unimplemented: Read as 0
bit 0
Note:
DS41303B-page 106
Advance Information
PIC18F2XK20/4XK20
REGISTER 9-3:
R/W-1
R/W-1
U-0
R/W-0
R/W-0
U-0
R/W-0
R/W-0
INT2IP
INT1IP
INT2IE
INT1IE
INT2IF
INT1IF
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
Unimplemented: Read as 0
bit 1
bit 0
Note:
x = Bit is unknown
Advance Information
DS41303B-page 107
PIC18F2XK20/4XK20
9.5
PIR Registers
The PIR registers contain the individual flag bits for the
peripheral interrupts. Due to the number of peripheral
interrupt sources, there are two Peripheral Interrupt
Request Flag registers (PIR1 and PIR2).
REGISTER 9-4:
R/W-0
R/W-0
R-0
R-0
R/W-0
R/W-0
R/W-0
R/W-0
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1: The PSPIF bit is unimplemented on 28-pin devices and will read as 0.
DS41303B-page 108
Advance Information
PIC18F2XK20/4XK20
REGISTER 9-5:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Advance Information
DS41303B-page 109
PIC18F2XK20/4XK20
9.6
PIE Registers
REGISTER 9-6:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
x = Bit is unknown
DS41303B-page 110
Advance Information
PIC18F2XK20/4XK20
REGISTER 9-7:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Advance Information
x = Bit is unknown
DS41303B-page 111
PIC18F2XK20/4XK20
9.7
IPR Registers
The IPR registers contain the individual priority bits for the
peripheral interrupts. Due to the number of peripheral
interrupt sources, there are two Peripheral Interrupt
Priority registers (IPR1 and IPR2). Using the priority bits
requires that the Interrupt Priority Enable (IPEN) bit be
set.
REGISTER 9-8:
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
x = Bit is unknown
1 = High priority
0 = Low priority
bit 3
bit 2
bit 1
bit 0
Note 1: The PSPIF bit is unimplemented on 28-pin devices and will read as 0.
DS41303B-page 112
Advance Information
PIC18F2XK20/4XK20
REGISTER 9-9:
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Advance Information
x = Bit is unknown
DS41303B-page 113
PIC18F2XK20/4XK20
9.8
RCON Register
REGISTER 9-10:
R/W-0
IPEN
SBOREN
U-0
(1)
R/W-1
R-1
RI
TO
R-1
R/W-0
PD
(1)
R/W-0
POR
bit 7
BOR
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1: Actual Reset values are determined by device configuration and the nature of the device Reset.
See Register 4-1 for additional information.
DS41303B-page 114
Advance Information
PIC18F2XK20/4XK20
9.9
9.10
TMR0 Interrupt
9.11
PORTB Interrupt-on-Change
9.12
EXAMPLE 9-1:
MOVWF
W_TEMP
MOVFF
STATUS, STATUS_TEMP
MOVFF
BSR, BSR_TEMP
;
; USER ISR CODE
;
MOVFF
BSR_TEMP, BSR
MOVF
W_TEMP, W
MOVFF
STATUS_TEMP, STATUS
; Restore BSR
; Restore WREG
; Restore STATUS
Advance Information
DS41303B-page 115
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 116
Advance Information
PIC18F2XK20/4XK20
10.0
I/O PORTS
FIGURE 10-1:
WR LAT
or Port
I/O pin(1)
CK
CK
TRIS Latch
Input
Buffer
EXAMPLE 10-1:
CLRF
RD TRIS
CLRF
ENEN
RD Port
Note 1:
10.1
Data Latch
WR TRIS
Note:
RD LAT
Data
Bus
MOVLW
MOVWF
MOVWF
MOVWF
MOVLW
PORTA
;
;
;
LATA
;
;
;
07h
;
ADCON1 ;
07h
;
CMCON
;
0CFh
;
;
;
TRISA
;
;
Advance Information
INITIALIZING PORTA
Initialize PORTA by
clearing output
data latches
Alternate method
to clear output
data latches
Configure A/D
for digital inputs
Configure comparators
for digital input
Value used to
initialize data
direction
Set RA<3:0> as inputs
RA<5:4> as outputs
DS41303B-page 117
PIC18F2XK20/4XK20
TABLE 10-1:
Pin
Function
TRIS
Setting
I/O
RA0/AN0/C12IN0-
RA0
DIG
TTL
AN0
ANA
C12IN0-
ANA
RA1
DIG
TTL
AN1
ANA
C12IN1-
ANA
RA2
DIG
TTL
AN2
ANA
C2IN+
ANA
RA1/AN1/C12IN1-
RA2/AN2/C2IN+
VREF-/CVREF
RA3/AN3/C1IN+/
VREF+
RA4/T0CKI/C1OUT
OSC2/CLKOUT/
RA6
Legend:
Description
VREF-
ANA
CVREF
ANA
RA3
DIG
TTL
AN3
ANA
C1IN+
ANA
VREF+
ANA
RA4
DIG
ST
ST
T0CKI
RA5/AN4/SS/
HLVDIN/C2OUT
I/O
Type
C1OUT
DIG
RA5
DIG
TTL
AN4
ANA
SS
TTL
HLVDIN
ANA
C2OUT
DIG
RA6
DIG
LATA<6> data output. Enabled in RCIO, INTIO2 and ECIO modes only.
TTL
OSC2
ANA
CLKOUT
DIG
DIG = Digital level output; TTL = TTL input buffer; ST = Schmitt Trigger input buffer; ANA = Analog level input/output;
x = Dont care (TRIS bit does not affect port direction or is overridden for this option).
DS41303B-page 118
Advance Information
PIC18F2XK20/4XK20
TABLE 10-1:
Pin
Function
TRIS
Setting
I/O
I/O
Type
OSC1/CLKIN/RA7
RA7
DIG
Legend:
Description
LATA<7> data output. Disabled in external oscillator modes.
TTL
OSC1
ANA
CLKIN
ANA
DIG = Digital level output; TTL = TTL input buffer; ST = Schmitt Trigger input buffer; ANA = Analog level input/output;
x = Dont care (TRIS bit does not affect port direction or is overridden for this option).
TABLE 10-2:
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
RA7(1)
RA6(1)
RA5
RA4
RA3
RA2
RA1
RA0
60
LATA
LATA7(1)
LATA6(1)
TRISA
Name
PORTA
60
60
ANS7(2)
ANS6(2)
ANS5(2)
ANS4
ANS3
ANS2
ANS1
ANS0
60
SLRCON
SLRE
SLRD
SLRC
SLRB
SLRA
61
ANSEL
CM1CON
C1ON
C1OUT
C1OE
C1POL
C1SP
C1R
C1CH1
C1CH0
60
CM2CON
C2ON
C2OUT
C2OE
C2POL
C2SP
C2R
C2CH1
C2CH0
60
CVRCON
CVREN
CVROE
CVRR
CVRSS
CVR3
CVR2
CVR1
CVR0
59
Advance Information
DS41303B-page 119
PIC18F2XK20/4XK20
10.2
10.3.2
PORTB is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISB. Setting a
TRISB bit (= 1) will make the corresponding PORTB
pin an input (i.e., disable the output driver). Clearing a
TRISB bit (= 0) will make the corresponding PORTB
pin an output (i.e., enable the output driver and put the
contents of the output latch on the selected pin).
The Data Latch register (LATB) is also memory
mapped. Read-modify-write operations on the LATB
register read and write the latched output value for
PORTB.
EXAMPLE 10-2:
CLRF
CLRF
MOVLW
MOVWF
MOVLW
MOVWF
10.3
INITIALIZING PORTB
PORTB
; Initialize PORTB by
; clearing output
; data latches
LATB
; Alternate method
; to clear output
; data latches
0Fh
; Set RB<4:0> as
ADCON1 ; digital I/O pins
;(required if config bit
; PBADEN is set)
0CFh
; Value used to
; initialize data
; direction
TRISB
; Set RB<3:0> as inputs
; RB<5:4> as outputs
; RB<7:6> as inputs
10.3.1
Note:
b)
Any read or write of PORTB to clear the mismatch condition (except when PORTB is the
source or destination of a MOVFF instruction).
Clear the flag bit, RBIF.
WEAK PULL-UPS
DS41303B-page 120
INTERRUPT-ON-CHANGE
10.3.3
Advance Information
PIC18F2XK20/4XK20
TABLE 10-3:
Pin
RB0/INT0/FLT0/
AN12
RB1/INT1/AN10/
C12IN2-/P1C
RB2/INT2/AN8/
P1B
RB3/AN9/C12IN3-/
CCP2
RB4/KBI0/AN11/
P1D
RB5/KBI1/PGM
Legend:
Note 1:
2:
3:
TRIS
Setting
I/O
I/O
Type
RB0
DIG
TTL
INT0
ST
Description
FLT0
ST
AN12
ANA
RB1
DIG
TTL
INT1
ST
AN10
ANA
C12IN2-
ANA
P1C
DIG
RB2
DIG
TTL
INT2
ST
AN8
ANA
P1B
DIG
RB3
DIG
TTL
AN9
ANA
C12IN3-
ANA
CCP2(2)
DIG
ST
DIG
TTL
RB4
KBI0
TTL
Interrupt-on-pin change.
AN11
ANA
P1D
DIG
RB5
DIG
TTL
KBI1
TTL
Interrupt-on-pin change.
PGM
ST
DIG = Digital level output; TTL = TTL input buffer; ST = Schmitt Trigger input buffer; ANA = Analog level input/output;
x = Dont care (TRIS bit does not affect port direction or is overridden for this option).
Configuration on POR is determined by the PBADEN Configuration bit. Pins are configured as analog inputs by default
when PBADEN is set and digital inputs when PBADEN is cleared.
Alternate assignment for CCP2 when the CCP2MX Configuration bit is 0. Default assignment is RC1.
All other pin functions are disabled when ICSP or ICD are enabled.
Advance Information
DS41303B-page 121
PIC18F2XK20/4XK20
TABLE 10-3:
Pin
Function
TRIS
Setting
I/O
I/O
Type
RB6
DIG
RB6/KBI2/PGC
Note 1:
2:
3:
TTL
TTL
Interrupt-on-pin change.
PGC
ST
Serial execution (ICSP) clock input for ICSP and ICD operation.(3)
RB7
DIG
TTL
KBI3
TTL
Interrupt-on-pin change.
PGD
DIG
ST
DIG = Digital level output; TTL = TTL input buffer; ST = Schmitt Trigger input buffer; ANA = Analog level input/output;
x = Dont care (TRIS bit does not affect port direction or is overridden for this option).
Configuration on POR is determined by the PBADEN Configuration bit. Pins are configured as analog inputs by default
when PBADEN is set and digital inputs when PBADEN is cleared.
Alternate assignment for CCP2 when the CCP2MX Configuration bit is 0. Default assignment is RC1.
All other pin functions are disabled when ICSP or ICD are enabled.
TABLE 10-4:
Name
PORTB
KBI2
RB7/KBI3/PGD
Legend:
Description
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
RB7
RB6
RB5
RB4
RB3
RB2
RB1
RB0
LATB
TRISB
WPUB6
WPUB5
WPUB4
IOCB
IOCB7
IOCB6
IOCB5
IOCB4
SLRE
SLRD
TMR0IE
INT0IE
INTCON
GIE/GIEH PEIE/GIEL
INTCON2
RBPU
INTCON3
INT2IP
ANSELH
60
60
60
WPUB
SLRCON
Reset
Values
on page
WPUB3
WPUB2
WPUB1
WPUB0
60
60
SLRC
SLRB
SLRA
61
RBIE
TMR0IF
INT0IF
RBIF
57
TMR0IP
RBIP
57
INT1IP
INT2IE
INT1IE
INT2IF
INT1IF
57
ANS12
ANS11
ANS10
ANS9
ANS8
60
DS41303B-page 122
Advance Information
PIC18F2XK20/4XK20
10.4
EXAMPLE 10-3:
PORTC is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISC. Setting a
TRISC bit (= 1) will make the corresponding PORTC
pin an input (i.e., disable the output driver). Clearing a
TRISC bit (= 0) will make the corresponding PORTC
pin an output (i.e., enable the output driver and put the
contents of the output latch on the selected pin).
CLRF
PORTC
CLRF
LATC
MOVLW
0CFh
MOVWF
TRISC
INITIALIZING PORTC
;
;
;
;
;
;
;
;
;
;
;
;
Initialize PORTC by
clearing output
data latches
Alternate method
to clear output
data latches
Value used to
initialize data
direction
Set RC<3:0> as inputs
RC<5:4> as outputs
RC<7:6> as inputs
Advance Information
DS41303B-page 123
PIC18F2XK20/4XK20
TABLE 10-5:
Pin
TRIS
Setting
I/O
I/O
Type
RC0
DIG
RC0/T1OSO/
T13CKI
RC1/T1OSI/CCP2
RC2/CCP1/P1A
ST
ANA
T13CKI
ST
RC1
DIG
ST
ANA
CCP2(1)
DIG
CCP2 compare and PWM output; takes priority over port data.
ST
DIG
ST
DIG
ST
P1A
DIG
RC3
DIG
ST
DIG
SPI clock output (MSSP module); takes priority over port data.
RC2
SCL
RC5/SDO
RC7/RX/DT
Legend:
Note 1:
RC4
ST
DIG
I2C clock output (MSSP module); takes priority over port data.
DIG
I2C/SMB I2C clock input (MSSP module); input type depends on module setting.
ST
SDI
ST
SDA
DIG
I2C data output (MSSP module); takes priority over port data.
DIG
ST
SDO
DIG
SPI data output (MSSP module); takes priority over port data.
RC6
DIG
RC5
RC6/TX/CK
T1OSI
SCK
RC4/SDI/SDA
T1OSO
CCP1
RC3/SCK/SCL
Description
I2C/SMB I2C data input (MSSP module); input type depends on module setting.
LATC<5> data output.
ST
TX
DIG
CK
DIG
ST
DIG
ST
RX
ST
DT
DIG
ST
RC7
DIG = Digital level output; TTL = TTL input buffer; ST = Schmitt Trigger input buffer; ANA = Analog level input/output;
I2C/SMB = I2C/SMBus input buffer; x = Dont care (TRIS bit does not affect port direction or is overridden for this option).
Default assignment for CCP2 when the CCP2MX Configuration bit is set. Alternate assignment is RB3.
DS41303B-page 124
Advance Information
PIC18F2XK20/4XK20
TABLE 10-6:
Name
PORTC
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
RC7
RC6
RC5
RC4
RC3
RC2
RC1
RC0
LATC
TRISC
T3CON
RD16
TXSTA
CSRC
TX9
60
60
60
RD16
T1CON
Reset
Values
on page
SYNC
SENDB
T3SYNC
BRGH
TMR1CS TMR1ON
TMR3CS TMR3ON
TRMT
58
59
TX9D
59
RCSTA
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
59
SSPCON1
WCOL
SSPOV
SSPEN
CKP
SSPM3
SSPM2
SSPM1
SSPM0
58
CCP1CON
P1M1
P1M0
DC1B1
DC1B0
59
CCP2CON
DC2B1
DC2B0
59
ECCP1AS
SLRCON
SLRE
PSSAC0
PSSBD1
PSSBD0
59
SLRC
SLRB
SLRA
61
SLRD
Advance Information
DS41303B-page 125
PIC18F2XK20/4XK20
10.5
Note:
PORTD is an 8-bit wide, bidirectional port. The corresponding data direction register is TRISD. Setting a
TRISD bit (= 1) will make the corresponding PORTD
pin an input (i.e., disable the output driver). Clearing a
TRISD bit (= 0) will make the corresponding PORTD
pin an output (i.e., enable the output driver and put the
contents of the output latch on the selected pin).
The Data Latch register (LATD) is also memory
mapped. Read-modify-write operations on the LATD
register read and write the latched output value for
PORTD.
All pins on PORTD are implemented with Schmitt Trigger input buffers. Each pin is individually configurable
as an input or output.
Three of the PORTD pins are multiplexed with outputs
P1B, P1C and P1D of the enhanced CCP module. The
operation of these additional PWM output pins is
covered in greater detail in Section 16.0 Enhanced
Capture/Compare/PWM (ECCP) Module.
Note:
PORTD can also be configured as an 8-bit wide microprocessor port (Parallel Slave Port) by setting control
bit, PSPMODE (TRISE<4>). In this mode, the input
buffers are TTL. See Section 10.9 Parallel Slave
Port for additional information on the Parallel Slave
Port (PSP).
Note:
EXAMPLE 10-4:
CLRF
PORTD
CLRF
LATD
MOVLW
0CFh
MOVWF
TRISD
INITIALIZING PORTD
;
;
;
;
;
;
;
;
;
;
;
;
Initialize PORTD by
clearing output
data latches
Alternate method
to clear output
data latches
Value used to
initialize data
direction
Set RD<3:0> as inputs
RD<5:4> as outputs
RD<7:6> as inputs
DS41303B-page 126
Advance Information
PIC18F2XK20/4XK20
TABLE 10-7:
Pin
RD0/PSP0
RD1/PSP1
RD2/PSP2
RD3/PSP3
RD4/PSP4
RD5/PSP5/P1B
RD6/PSP6/P1C
TRIS
Setting
I/O
I/O
Type
RD0
DIG
ST
PSP0
DIG
PSP read data output (LATD<0>); takes priority over port data.
Legend:
TTL
RD1
DIG
ST
PSP1
DIG
PSP read data output (LATD<1>); takes priority over port data.
TTL
RD2
DIG
ST
PSP2
DIG
PSP read data output (LATD<2>); takes priority over port data.
TTL
RD3
DIG
ST
PSP3
DIG
PSP read data output (LATD<3>); takes priority over port data.
TTL
RD4
DIG
ST
PSP4
DIG
PSP read data output (LATD<4>); takes priority over port data.
TTL
RD5
DIG
ST
PSP5
DIG
PSP read data output (LATD<5>); takes priority over port data.
TTL
P1B
DIG
ECCP1 Enhanced PWM output, channel B; takes priority over port and
PSP data. May be configured for tri-state during Enhanced PWM
shutdown events.
RD6
DIG
ST
PSP6
RD7/PSP7/P1D
Description
DIG
PSP read data output (LATD<6>); takes priority over port data.
TTL
P1C
DIG
ECCP1 Enhanced PWM output, channel C; takes priority over port and
PSP data. May be configured for tri-state during Enhanced PWM
shutdown events.
RD7
DIG
ST
PSP7
DIG
PSP read data output (LATD<7>); takes priority over port data.
TTL
P1D
DIG
ECCP1 Enhanced PWM output, channel D; takes priority over port and
PSP data. May be configured for tri-state during Enhanced PWM
shutdown events.
DIG = Digital level output; TTL = TTL input buffer; ST = Schmitt Trigger input buffer; x = Dont care
(TRIS bit does not affect port direction or is overridden for this option).
Advance Information
DS41303B-page 127
PIC18F2XK20/4XK20
TABLE 10-8:
Name
PORTD
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
RD7
RD6
RD5
RD4
RD3
RD2
RD1
RD0
LATD
TRISD
Reset
Values
on page
60
60
60
IBF
OBF
IBOV
PSPMODE
TRISE2
TRISE1
TRISE0
60
CCP1CON
P1M1
P1M0
DC1B1
DC1B0
CCP1M3
CCP1M2
CCP1M1
CCP1M0
59
SLRCON
SLRE
SLRD
SLRC
SLRB
SLRA
61
TRISE
DS41303B-page 128
Advance Information
PIC18F2XK20/4XK20
10.6
10.6.1
EXAMPLE 10-5:
CLRF
CLRF
MOVLW
ANDWF
MOVLW
MOVWF
PORTE
;
;
;
LATE
;
;
;
1Fh
;
ANSEL,w ;
05h
;
;
;
TRISE
;
;
;
10.6.2
INITIALIZING PORTE
Initialize PORTE by
clearing output
data latches
Alternate method
to clear output
data latches
Configure analog pins
for digital only
Value used to
initialize data
direction
Set RE<0> as input
RE<1> as output
RE<2> as input
Advance Information
DS41303B-page 129
PIC18F2XK20/4XK20
REGISTER 10-1:
R-0
R-0
R/W-0
R/W-0
U-0
R/W-1
R/W-1
R/W-1
IBF
OBF
IBOV
PSPMODE
TRISE2
TRISE1
TRISE0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
Unimplemented: Read as 0
bit 2
bit 1
bit 0
DS41303B-page 130
Advance Information
PIC18F2XK20/4XK20
TABLE 10-9:
Pin
TRIS
Setting
I/O
I/O
Type
RE0
DIG
ST
RE0/RD/AN5
RE1/WR/AN6
RE2/CS/AN7
MCLR/VPP/
RE3(1,2)
Legend:
Note 1:
2:
Description
RD
TTL
AN5
ANA
RE1
DIG
ST
WR
TTL
AN6
ANA
RE2
DIG
ST
CS
TTL
AN7
ANA
MCLR
ST
VPP
ANA
RE3
(2)
ST
DIG = Digital level output; TTL = TTL input buffer; ST = Schmitt Trigger input buffer; ANA = Analog level input/output;
x = Dont care (TRIS bit does not affect port direction or is overridden for this option).
RE3 is available on both PIC18F2XK20 and PIC18F4XK20 devices. All other PORTE pins are only implemented on
PIC18F4XK20 devices.
RE3 does not have a corresponding TRIS bit to control data direction.
Bit 6
Bit 5
Bit 4
Bit 3
PORTE
RE3(1,2)
LATE(2)
TRISE
IBF
OBF
IBOV
PSPMODE
TRISE2
TRISE1
TRISE0
60
SLRCON
SLRE
SLRD
SLRC
SLRB
SLRA
61
ANS7
ANS6
ANS5
ANS4
ANS3
ANS2
ANS1
ANS0
60
ANSEL
Bit 2
Bit 1
Bit 0
RE2
RE1
RE0
Reset
Values
on page
Bit 7
60
60
Advance Information
DS41303B-page 131
PIC18F2XK20/4XK20
10.7
REGISTER 10-2:
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
ANS7(1)
ANS6(1)
ANS5(1)
ANS4
ANS3
ANS2
ANS1
ANS0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
x = Bit is unknown
DS41303B-page 132
Advance Information
PIC18F2XK20/4XK20
REGISTER 10-3:
U-0
U-0
U-0
R/W-1(1)
R/W-1(1)
R/W-1(1)
R/W-1(1)
R/W-1(1)
ANS12
ANS11
ANS10
ANS9
ANS8
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
x = Bit is unknown
Default state is determined by the PBADEN bit of CONFIG3H. The default state is 0 When
PBADEN = 0.
Advance Information
DS41303B-page 133
PIC18F2XK20/4XK20
10.8
REGISTER 10-4:
U-0
U-0
U-0
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
SLRE(1)
SLRD(1)
SLRC
SLRB
SLRA
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
2:
x = Bit is unknown
DS41303B-page 134
Advance Information
PIC18F2XK20/4XK20
10.9
Note:
FIGURE 10-2:
Q
RDx pin
CK
Data Latch
Q
RD PORTD
TTL
D
ENEN
RD LATD
PORTE Pins
Read
TTL
RD
Chip Select
TTL
CS
Write
TTL
Note:
Advance Information
WR
DS41303B-page 135
PIC18F2XK20/4XK20
FIGURE 10-3:
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Q4
Q1
Q2
Q3
Q4
CS
WR
RD
PORTD<7:0>
IBF
OBF
PSPIF
FIGURE 10-4:
Q2
Q3
Q4
Q1
Q2
Q3
CS
WR
RD
PORTD<7:0>
IBF
OBF
PSPIF
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
RD7
RD6
RD5
RD4
RD3
RD2
RD1
RD0
LATD
TRISD
PORTE
Reset
Values
on page
60
60
60
RE3
RE2
RE1
RE0
60
TRISE1
TRISE0
60
LATE
TRISE
IBF
OBF
IBOV
PSPMODE
SLRE
SLRD
SLRC
SLRB
SLRA
61
TMR0IF
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
SLRCON
INTCON
GIE/GIEH PEIE/GIEL
60
PIR1
PSPIF
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
ANSEL
ANS7
ANS6
ANS5
ANS4
ANS3
ANS2
ANS1
ANS0
60
Legend: = unimplemented, read as 0. Shaded cells are not used by the Parallel Slave Port.
DS41303B-page 136
Advance Information
PIC18F2XK20/4XK20
11.0
TIMER0 MODULE
REGISTER 11-1:
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
TMR0ON
T08BIT
T0CS
T0SE
PSA
T0PS2
T0PS1
T0PS0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2-0
Advance Information
DS41303B-page 137
PIC18F2XK20/4XK20
11.1
Timer0 Operation
11.2
FIGURE 11-1:
0
0
1
T0CKI pin
T0SE
T0CS
Programmable
Prescaler
Sync with
Internal
Clocks
TMR0L
(2 TCY Delay)
8
T0PS<2:0>
PSA
Note:
Set
TMR0IF
on Overflow
Upon Reset, Timer0 is enabled in 8-bit mode with clock input from T0CKI max. prescale.
DS41303B-page 138
Advance Information
PIC18F2XK20/4XK20
FIGURE 11-2:
FOSC/4
0
0
Sync with
Internal
Clocks
1
Programmable
Prescaler
T0CKI pin
T0SE
T0CS
TMR0
High Byte
TMR0L
Set
TMR0IF
on Overflow
(2 TCY Delay)
Read TMR0L
T0PS<2:0>
Write TMR0L
PSA
TMR0H
8
8
Internal Data Bus
Note:
11.3
Upon Reset, Timer0 is enabled in 8-bit mode with clock input from T0CKI max. prescale.
11.3.1
Prescaler
TABLE 11-1:
Name
SWITCHING PRESCALER
ASSIGNMENT
11.4
Timer0 Interrupt
The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h in 8-bit mode, or from
FFFFh to 0000h in 16-bit mode. This overflow sets the
TMR0IF flag bit. The interrupt can be masked by clearing the TMR0IE bit of the INTCON register. Before
re-enabling the interrupt, the TMR0IF bit must be
cleared by software in the Interrupt Service Routine.
Since Timer0 is shut down in Sleep mode, the TMR0
interrupt cannot awaken the processor from Sleep.
Bit 6
Bit 5
TMR0L
TMR0H
INTCON
T0CON
TMR0ON
T08BIT
TRISA
RA7(1)
RA6(1)
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
58
58
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
T0CS
T0SE
PSA
T0PS2
T0PS1
T0PS0
58
RA5
RA4
RA3
RA2
RA1
RA0
60
Advance Information
DS41303B-page 139
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 140
Advance Information
PIC18F2XK20/4XK20
12.0
TIMER1 MODULE
REGISTER 12-1:
R/W-0
R-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
RD16
T1RUN
T1CKPS1
T1CKPS0
T1OSCEN
T1SYNC
TMR1CS
TMR1ON
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5-4
bit 3
bit 2
bit 1
bit 0
Advance Information
DS41303B-page 141
PIC18F2XK20/4XK20
12.1
Timer1 Operation
FIGURE 12-1:
On/Off
T1OSO/T13CKI
1
FOSC/4
Internal
Clock
T1OSI
Synchronize
Prescaler
1, 2, 4, 8
Detect
0
2
T1OSCEN(1)
Sleep Input
Timer1
On/Off
TMR1CS
T1CKPS<1:0>
T1SYNC
TMR1ON
Clear TMR1
(CCP Special Event Trigger)
Set
TMR1IF
on Overflow
TMR1
High Byte
TMR1L
Note 1: When enable bit, T1OSCEN, is cleared, the inverter and feedback resistor are turned off to eliminate power drain.
FIGURE 12-2:
T1OSO/T13CKI
1
FOSC/4
Internal
Clock
T1OSI
Synchronize
Prescaler
1, 2, 4, 8
Detect
0
2
T1OSCEN(1)
T1CKPS<1:0>
T1SYNC
TMR1ON
Sleep Input
Timer1
On/Off
TMR1CS
Clear TMR1
(CCP Special Event Trigger)
TMR1
High Byte
TMR1L
Set
TMR1IF
on Overflow
Read TMR1L
Write TMR1L
8
TMR1H
8
8
Internal Data Bus
Note 1: When enable bit, T1OSCEN, is cleared, the inverter and feedback resistor are turned off to eliminate power drain.
DS41303B-page 142
Advance Information
PIC18F2XK20/4XK20
12.2
TABLE 12-1:
Osc Type
LP
12.3
Timer1 Oscillator
FIGURE 12-3:
EXTERNAL
COMPONENTS FOR THE
TIMER1 LP OSCILLATOR
C1
27 pF
PIC MCU
Freq
32 kHz
C1
27 pF
C2
(1)
27 pF(1)
12.3.1
USING TIMER1 AS A
CLOCK SOURCE
T1OSI
XTAL
32.768 kHz
T1OSO
C2
27 pF
Note:
Advance Information
DS41303B-page 143
PIC18F2XK20/4XK20
12.3.2
12.3.3
FIGURE 12-4:
12.4
Timer1 Interrupt
12.5
OSCILLATOR CIRCUIT
WITH GROUNDED
GUARD RING
VDD
VSS
OSC1
OSC2
RC0
RC1
RC2
Note: Not drawn to scale.
DS41303B-page 144
Advance Information
PIC18F2XK20/4XK20
12.6
EXAMPLE 12-1:
RTCinit
MOVLW
MOVWF
CLRF
MOVLW
MOVWF
CLRF
CLRF
MOVLW
MOVWF
BSF
RETURN
80h
TMR1H
TMR1L
b00001111
T1CON
secs
mins
.12
hours
PIE1, TMR1IE
BSF
BCF
INCF
MOVLW
CPFSGT
RETURN
CLRF
INCF
MOVLW
CPFSGT
RETURN
CLRF
INCF
MOVLW
CPFSGT
RETURN
CLRF
RETURN
TMR1H, 7
PIR1, TMR1IF
secs, F
.59
secs
;
;
;
;
;
;
;
;
No, done
Clear seconds
Increment minutes
60 minutes elapsed?
;
;
;
;
No, done
clear minutes
Increment hours
24 hours elapsed?
RTCisr
secs
mins, F
.59
mins
mins
hours, F
.23
hours
hours
; No, done
; Reset hours
; Done
Advance Information
DS41303B-page 145
PIC18F2XK20/4XK20
TABLE 12-2:
Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INTCON
GIE/GIEH PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
(1)
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
TMR1L
58
TMR1H
58
T1CON
RD16
T1RUN
TMR1CS
TMR1ON
58
DS41303B-page 146
Advance Information
PIC18F2XK20/4XK20
13.0
TIMER2 MODULE
13.1
Timer2 Operation
REGISTER 13-1:
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
T2OUTPS3
T2OUTPS2
T2OUTPS1
T2OUTPS0
TMR2ON
T2CKPS1
T2CKPS0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
Unimplemented: Read as 0
bit 6-3
bit 2
bit 1-0
Advance Information
x = Bit is unknown
DS41303B-page 147
PIC18F2XK20/4XK20
13.2
Timer2 Interrupt
13.3
Timer2 Output
FIGURE 13-1:
1:1 to 1:16
Postscaler
T2OUTPS<3:0>
Set TMR2IF
TMR2 Output
(to PWM or MSSP)
T2CKPS<1:0>
FOSC/4
TMR2/PR2
Match
Reset
TMR2
Comparator
PR2
8
TABLE 13-1:
Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
TMR2
T2CON
PR2
Timer2 Register
60
58
T2CKPS1 T2CKPS0
58
58
Legend: = unimplemented, read as 0. Shaded cells are not used by the Timer2 module.
Note 1: These bits are unimplemented on 28-pin devices; always maintain these bits clear.
DS41303B-page 148
Advance Information
PIC18F2XK20/4XK20
14.0
TIMER3 MODULE
REGISTER 14-1:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
RD16
T3CCP2
T3CKPS1
T3CKPS0
T3CCP1
T3SYNC
TMR3CS
TMR3ON
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6,3
bit 5-4
bit 2
bit 1
bit 0
Advance Information
DS41303B-page 149
PIC18F2XK20/4XK20
14.1
Timer3 Operation
FIGURE 14-1:
T1OSO/T13CKI
1
FOSC/4
Internal
Clock
T1OSI
Synchronize
Prescaler
1, 2, 4, 8
Detect
0
2
T1OSCEN(1)
Sleep Input
TMR3CS
T3CKPS<1:0>
Timer3
On/Off
T3SYNC
TMR3ON
Clear TMR3
TMR3L
TMR3
High Byte
Set
TMR3IF
on Overflow
Note 1: When enable bit, T1OSCEN, is cleared, the inverter and feedback resistor are turned off to eliminate power drain.
DS41303B-page 150
Advance Information
PIC18F2XK20/4XK20
FIGURE 14-2:
T13CKI/T1OSO
1
FOSC/4
Internal
Clock
T1OSI
Synchronize
Prescaler
1, 2, 4, 8
Detect
0
2
T1OSCEN(1)
Sleep Input
Timer3
On/Off
TMR3CS
T3CKPS<1:0>
T3SYNC
TMR3ON
CCP1/CCP2 Special Event Trigger
CCP1/CCP2 Select from T3CON<6,3>
Clear TMR3
Set
TMR3IF
on Overflow
TMR3
High Byte
TMR3L
Read TMR1L
Write TMR1L
8
TMR3H
8
8
Internal Data Bus
Note 1: When enable bit, T1OSCEN, is cleared, the inverter and feedback resistor are turned off to eliminate power drain.
14.2
14.3
14.4
Timer3 Interrupt
Advance Information
DS41303B-page 151
PIC18F2XK20/4XK20
14.5
TABLE 14-1:
Name
INTCON
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
Bit 6
GIE/GIEH PEIE/GIEL
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
TMR3L
TMR3H
59
59
T1CON
RD16
T1RUN
TMR1CS
TMR1ON
58
T3CON
RD16
T3CCP2
T3CKPS1 T3CKPS0
TMR3CS
TMR3ON
59
T3CCP1
T3SYNC
Legend: = unimplemented, read as 0. Shaded cells are not used by the Timer3 module.
DS41303B-page 152
Advance Information
PIC18F2XK20/4XK20
15.0
CAPTURE/COMPARE/PWM
(CCP) MODULES
REGISTER 15-1:
U-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
DC2B1
DC2B0
CCP2M3
CCP2M2
CCP2M1
CCP2M0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7-6
Unimplemented: Read as 0
bit 5-4
DC2B<1:0>: PWM Duty Cycle bit 1 and bit 0 for CCP2 Module
Capture mode:
Unused.
Compare mode:
Unused.
PWM mode:
These bits are the two LSbs (bit 1 and bit 0) of the 10-bit PWM duty cycle. The eight MSbs
(DC2B<9:2>) of the duty cycle are found in CCPR2L.
bit 3-0
Advance Information
DS41303B-page 153
PIC18F2XK20/4XK20
15.1
15.1.1
TABLE 15-1:
CCP/ECCP Mode
Timer Resource
Capture
Timer1 or Timer3
Compare
Timer1 or Timer3
PWM
Timer2
TABLE 15-2:
15.1.2
Interaction
Capture
Capture
Each module can use TMR1 or TMR3 as the time base. The time base can be different
for each CCP.
Capture
Compare
CCP2 can be configured for the Special Event Trigger to reset TMR1 or TMR3
(depending upon which time base is used). Automatic A/D conversions on trigger event
can also be done. Operation of CCP1 could be affected if it is using the same timer as a
time base.
Compare
Capture
CCP1 can be configured for the Special Event Trigger to reset TMR1 or TMR3
(depending upon which time base is used). Operation of CCP2 could be affected if it is
using the same timer as a time base.
Compare
Compare
Either module can be configured for the Special Event Trigger to reset the time base.
Automatic A/D conversions on CCP2 trigger event can be done. Conflicts may occur if
both modules are using the same time base.
Capture
PWM
None
Compare
PWM
None
PWM(1)
Capture
None
PWM(1)
Compare
None
(1)
PWM
Note 1:
PWM
Both PWMs will have the same frequency and update rate (TMR2 interrupt).
DS41303B-page 154
Advance Information
PIC18F2XK20/4XK20
15.2
EXAMPLE 15-1:
Capture Mode
CLRF
MOVLW
MOVWF
CHANGING BETWEEN
CAPTURE PRESCALERS
(CCP2 SHOWN)
CCP2CON
; Turn CCP module off
NEW_CAPT_PS ; Load WREG with the
; new prescaler mode
; value and CCP ON
CCP2CON
; Load CCP2CON with
; this value
15.2.1
15.2.2
15.2.3
SOFTWARE INTERRUPT
15.2.4
CCP PRESCALER
Advance Information
DS41303B-page 155
PIC18F2XK20/4XK20
FIGURE 15-1:
TMR3L
Set CCP1IF
T3CCP2
CCP1 pin
Prescaler
1, 4, 16
and
Edge Detect
CCPR1H
T3CCP2
4
CCP1CON<3:0>
Q1:Q4
CCP2CON<3:0>
TMR3
Enable
CCPR1L
TMR1
Enable
TMR1H
TMR1L
TMR3H
TMR3L
Set CCP2IF
4
4
T3CCP1
T3CCP2
TMR3
Enable
CCP2 pin
Prescaler
1, 4, 16
and
Edge Detect
CCPR2H
CCPR2L
TMR1
Enable
T3CCP2
T3CCP1
DS41303B-page 156
Advance Information
TMR1H
TMR1L
PIC18F2XK20/4XK20
15.3
15.3.2
Compare Mode
15.3.3
driven high
driven low
toggled (high-to-low or low-to-high)
remain unchanged (that is, reflects the state of the
I/O latch)
15.3.4
15.3.1
FIGURE 15-2:
CCPR1H
Set CCP1IF
CCPR1L
Comparator
Output
Logic
Compare
Match
R
TRIS
Output Enable
4
CCP1CON<3:0>
0
TMR1H
TMR1L
TMR3H
TMR3L
T3CCP1
T3CCP2
Set CCP2IF
Comparator
CCPR2H
CCPR2L
Compare
Match
CCP2 pin
Output
Logic
4
R
TRIS
Output Enable
CCP2CON<3:0>
Advance Information
DS41303B-page 157
PIC18F2XK20/4XK20
TABLE 15-3:
Name
INTCON
Bit 6
Bit 5
Reset
Values
on page
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
RI
TO
PD
POR
BOR
56
IPEN
SBOREN
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
(1)
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
RCON
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
TRISB
60
TRISC
60
TMR1L
58
TMR1H
T1CON
RD16
T1RUN
58
TMR1CS TMR1ON
58
TMR3H
59
TMR3L
59
T3CON
RD16
T3CCP2
T3CKPS1 T3CKPS0
T3CCP1
T3SYNC
TMR3CS TMR3ON
59
CCPR1L
59
CCPR1H
59
CCP1CON
P1M1
P1M0
DC1B1
DC1B0
CCPR2L
CCPR2H
CCP2CON
DC2B1
DC2B0
CCP1M3
CCP1M2
CCP1M1
CCP1M0
59
59
59
CCP2M3
CCP2M2
CCP2M1
CCP2M0
59
Legend: = unimplemented, read as 0. Shaded cells are not used by Capture/Compare, Timer1 or Timer3.
DS41303B-page 158
Advance Information
PIC18F2XK20/4XK20
15.4
PWM Mode
FIGURE 15-4:
Period
Pulse Width
PR2
T2CON
CCPRxL
CCPxCON
TMR2 = PR2
TMR2 = CCPRxL:DCxB<1:0>
TMR2 = 0
FIGURE 15-3:
CCPRxH(2) (Slave)
CCPx
R
Comparator
TMR2
(1)
S
TRIS
Comparator
PR2
Note 1:
2:
Clear Timer2,
toggle CCPx pin and
latch duty cycle
Advance Information
DS41303B-page 159
PIC18F2XK20/4XK20
15.4.1
PWM PERIOD
15.4.2
EQUATION 15-1:
PWM PERIOD
TMR2 is cleared
The CCPx pin is set. (Exception: If the PWM duty
cycle = 0%, the pin will not be set.)
The PWM duty cycle is latched from CCPRxL into
CCPRxH.
EQUATION 15-2:
PULSE WIDTH
EQUATION 15-3:
DS41303B-page 160
Advance Information
PIC18F2XK20/4XK20
15.4.3
PWM RESOLUTION
EQUATION 15-4:
TABLE 15-4:
Note:
2.44 kHz
9.77 kHz
39.06 kHz
156.25 kHz
312.50 kHz
416.67 kHz
16
FFh
FFh
FFh
3Fh
1Fh
17h
10
10
10
6.58
PWM Frequency
Timer Prescale (1, 4, 16)
PR2 Value
Maximum Resolution (bits)
TABLE 15-6:
[ 4 ( PR2 + 1 ) ]- bits
Resolution = log
----------------------------------------log ( 2 )
PWM Frequency
TABLE 15-5:
PWM RESOLUTION
1.22 kHz
4.88 kHz
19.53 kHz
78.12 kHz
156.3 kHz
208.3 kHz
16
0xFF
0xFF
0xFF
0x3F
0x1F
0x17
10
10
10
6.6
PWM Frequency
Timer Prescale (1, 4, 16)
PR2 Value
Maximum Resolution (bits)
1.22 kHz
4.90 kHz
19.61 kHz
76.92 kHz
153.85 kHz
200.0 kHz
16
0x65
0x65
0x65
0x19
0x0C
0x09
Advance Information
DS41303B-page 161
PIC18F2XK20/4XK20
15.4.4
OPERATION IN POWER-MANAGED
MODES
15.4.7
3.
4.
15.4.5
5.
15.4.6
6.
EFFECTS OF RESET
Any Reset will force all ports to Input mode and the
CCP registers to their Reset states.
DS41303B-page 162
7.
Advance Information
PIC18F2XK20/4XK20
TABLE 15-7:
Name
INTCON
Bit 6
GIE/GIEH PEIE/GIEL
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
RI
TO
PD
POR
BOR
56
RCON
IPEN
SBOREN
PIR1
PSPIF
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
TRISB
60
TRISC
60
TMR2
Timer2 Register
58
PR2
58
T2CON
CCPR1L
CCPR1H
CCP1CON
P1M1
P1M0
DC1B1
DC1B0
58
59
59
CCP1M3
CCP1M2
CCP1M1
CCP1M0
59
CCPR2L
59
CCPR2H
59
CCP2CON
ECCP1AS
PWM1CON
ECCPASE ECCPAS2
PRSEN
PDC6
DC2B1
DC2B0
CCP2M3
CCP2M2
CCP2M1
CCP2M0
59
ECCPAS1
ECCPAS0
PSSAC1
PSSAC0
PSSBD1
PSSBD0
59
PDC5
PDC4
PDC3
PDC2
PDC
PDC0
59
Legend: = unimplemented, read as 0. Shaded cells are not used by PWM or Timer2.
Advance Information
DS41303B-page 163
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 164
Advance Information
PIC18F2XK20/4XK20
16.0
ENHANCED
CAPTURE/COMPARE/PWM
(ECCP) MODULE
REGISTER 16-1:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
P1M1
P1M0
DC1B1
DC1B0
CCP1M3
CCP1M2
CCP1M1
CCP1M0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7-6
bit 5-4
bit 3-0
Advance Information
DS41303B-page 165
PIC18F2XK20/4XK20
In addition to the expanded range of modes available
through the CCP1CON register and ECCP1AS
register, the ECCP module has two additional registers
associated with Enhanced PWM operation and
auto-shutdown features. They are:
PWM1CON (Dead-band delay)
PSTRCON (output steering)
16.1
16.3
16.1.1
16.2
16.2.1
DS41303B-page 166
Advance Information
PIC18F2XK20/4XK20
16.4
The PWM outputs are multiplexed with I/O pins and are
designated P1A, P1B, P1C and P1D. The polarity of the
PWM pins is configurable and is selected by setting the
CCP1M bits in the CCP1CON register appropriately.
Single PWM
Half-Bridge PWM
Full-Bridge PWM, Forward mode
Full-Bridge PWM, Reverse mode
FIGURE 16-1:
DC1B<1:0>
CCP1M<3:0>
4
P1M<1:0>
2
CCPR1L
CCP1/P1A
CCP1/P1A
TRIS
CCPR1H (Slave)
P1B
R
Comparator
Output
Controller
P1B
TRIS
P1C
TMR2
(1)
S
P1D
Comparator
Clear Timer2,
toggle PWM pin and
latch duty cycle
PR2
Note
1:
P1C
TRIS
P1D
TRIS
PWM1CON
The 8-bit timer TMR2 register is concatenated with the 2-bit internal Q clock, or 2 bits of the prescaler to create the 10-bit
time base.
Note 1: The TRIS register value for each PWM output must be configured appropriately.
2: Clearing the CCPxCON register will relinquish ECCP control of all PWM output pins.
3: Any pin not used by an Enhanced PWM mode is available for alternate pin functions.
TABLE 16-1:
ECCP Mode
P1M<1:0>
CCP1/P1A
P1B
P1C
P1D
Single
00
Yes(1)
Yes(1)
Yes(1)
Yes(1)
Half-Bridge
10
Yes
Yes
No
No
Full-Bridge, Forward
01
Yes
Yes
Yes
Yes
Full-Bridge, Reverse
11
Yes
Yes
Yes
Yes
Note 1:
Outputs are enabled by pulse steering in Single mode. See Register 16-4.
Advance Information
DS41303B-page 167
PIC18F2XK20/4XK20
FIGURE 16-2:
P1M<1:0>
Signal
PR2+1
Pulse
Width
Period
00
(Single Output)
P1A Modulated
Delay(1)
Delay(1)
P1A Modulated
10
(Half-Bridge)
P1B Modulated
P1A Active
01
(Full-Bridge,
Forward)
P1B Inactive
P1C Inactive
P1D Modulated
P1A Inactive
11
(Full-Bridge,
Reverse)
P1B Modulated
P1C Active
P1D Inactive
Relationships:
Period = 4 * TOSC * (PR2 + 1) * (TMR2 Prescale Value)
Pulse Width = TOSC * (CCPR1L<7:0>:CCP1CON<5:4>) * (TMR2 Prescale Value)
Delay = 4 * TOSC * (PWM1CON<6:0>)
Note 1: Dead-band delay is programmed using the PWM1CON register (Section 16.4.6 Programmable Dead-Band Delay
mode).
DS41303B-page 168
Advance Information
PIC18F2XK20/4XK20
FIGURE 16-3:
P1M<1:0>
PR2+1
Pulse
Width
Period
00
(Single Output)
P1A Modulated
P1A Modulated
Delay(1)
10
(Half-Bridge)
Delay(1)
P1B Modulated
P1A Active
01
(Full-Bridge,
Forward)
P1B Inactive
P1C Inactive
P1D Modulated
P1A Inactive
11
(Full-Bridge,
Reverse)
P1B Modulated
P1C Active
P1D Inactive
Relationships:
Period = 4 * TOSC * (PR2 + 1) * (TMR2 Prescale Value)
Pulse Width = TOSC * (CCPR1L<7:0>:CCP1CON<5:4>) * (TMR2 Prescale Value)
Delay = 4 * TOSC * (PWM1CON<6:0>)
Note
1:
Dead-band delay is programmed using the PWM1CON register (Section 16.4.6 Programmable Dead-Band Delay
mode).
Advance Information
DS41303B-page 169
PIC18F2XK20/4XK20
16.4.1
HALF-BRIDGE MODE
FIGURE 16-4:
Period
Period
Pulse Width
P1A(2)
td
td
P1B(2)
(1)
(1)
(1)
td = Dead-Band Delay
Note 1:
2:
FIGURE 16-5:
EXAMPLE OF
HALF-BRIDGE PWM
OUTPUT
P1A
Load
FET
Driver
P1B
FET
Driver
FET
Driver
P1A
FET
Driver
Load
FET
Driver
P1B
DS41303B-page 170
Advance Information
PIC18F2XK20/4XK20
16.4.2
FULL-BRIDGE MODE
FIGURE 16-6:
FET
Driver
QC
QA
FET
Driver
P1A
Load
P1B
FET
Driver
P1C
FET
Driver
QD
QB
VP1D
Advance Information
DS41303B-page 171
PIC18F2XK20/4XK20
FIGURE 16-7:
Forward Mode
Period
P1A
(2)
Pulse Width
P1B(2)
P1C(2)
P1D(2)
(1)
(1)
Reverse Mode
Period
Pulse Width
P1A(2)
P1B(2)
P1C(2)
P1D(2)
(1)
Note 1:
2:
(1)
DS41303B-page 172
Advance Information
PIC18F2XK20/4XK20
16.4.2.1
FIGURE 16-8:
Signal
Period
P1A (Active-High)
P1B (Active-High)
Pulse Width
P1C (Active-High)
(2)
P1D (Active-High)
Pulse Width
Note 1:
2:
The direction bit P1M1 of the CCP1CON register is written any time during the PWM cycle.
When changing directions, the P1A and P1C signals switch before the end of the current PWM cycle. The
modulated P1B and P1D signals are inactive at this time. The length of this time is (1/FOSC) TMR2 prescale
value.
Advance Information
DS41303B-page 173
PIC18F2XK20/4XK20
FIGURE 16-9:
t1
Reverse Period
P1A
P1B
PW
P1C
P1D
PW
TON
External Switch C
TOFF
External Switch D
Potential
Shoot-Through Current
Note 1:
16.4.3
T = TOFF TON
2:
3:
TOFF is the turn off delay of power switch QD and its driver.
START-UP CONSIDERATIONS
DS41303B-page 174
Advance Information
PIC18F2XK20/4XK20
16.4.4
ENHANCED PWM
AUTO-SHUTDOWN MODE
REGISTER 16-2:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
ECCPASE
ECCPAS2
ECCPAS1
ECCPAS0
PSSAC1
PSSAC0
PSSBD1
PSSBD0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6-4
bit 3-2
bit 1-0
Advance Information
DS41303B-page 175
PIC18F2XK20/4XK20
Note 1: The auto-shutdown condition is a
level-based signal, not an edge-based
signal. As long as the level is present, the
auto-shutdown will persist.
2: Writing to the ECCPASE bit is disabled
while an auto-shutdown condition
persists.
3: Once the auto-shutdown condition has
been removed and the PWM restarted
(either through firmware or auto-restart)
the PWM signal will always restart at the
beginning of the next PWM period.
FIGURE 16-10:
Shutdown Event
ECCPASE bit
PWM Activity
Normal PWM
ECCPASE
Cleared by
Shutdown
Shutdown Firmware PWM
Event Occurs Event Clears
Resumes
Start of
PWM Period
16.4.5
AUTO-RESTART MODE
The Enhanced PWM can be configured to automatically restart the PWM signal once the auto-shutdown
condition has been removed. Auto-restart is enabled by
setting the PRSEN bit in the PWM1CON register.
If auto-restart is enabled, the ECCPASE bit will remain
set as long as the auto-shutdown condition is active.
When the auto-shutdown condition is removed, the
ECCPASE bit will be cleared via hardware and normal
operation will resume.
FIGURE 16-11:
Shutdown Event
ECCPASE bit
PWM Activity
Normal PWM
Start of
PWM Period
DS41303B-page 176
Shutdown
Shutdown
Event Occurs Event Clears
Advance Information
PWM
Resumes
PIC18F2XK20/4XK20
16.4.6
PROGRAMMABLE DEAD-BAND
DELAY MODE
FIGURE 16-12:
Period
Period
Pulse Width
P1A(2)
td
td
P1B(2)
(1)
(1)
(1)
td = Dead-Band Delay
Note 1:
FIGURE 16-13:
EXAMPLE OF
HALF-BRIDGE PWM
OUTPUT
2:
+
V
-
P1A
Load
FET
Driver
+
V
-
P1B
V-
Advance Information
DS41303B-page 177
PIC18F2XK20/4XK20
REGISTER 16-3:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
PRSEN
PDC6
PDC5
PDC4
PDC3
PDC2
PDC1
PDC0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6-0
DS41303B-page 178
Advance Information
PIC18F2XK20/4XK20
16.4.7
REGISTER 16-4:
Note:
U-0
U-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-1
STRSYNC
STRD
STRC
STRB
STRA
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7-5
Unimplemented: Read as 0
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
The PWM Steering mode is available only when the CCP1CON register bits CCP1M<3:2> = 11 and
P1M<1:0> = 00.
Advance Information
DS41303B-page 179
PIC18F2XK20/4XK20
FIGURE 16-14:
SIMPLIFIED STEERING
BLOCK DIAGRAM
STRA
P1A Signal
CCP1M1
PORT Data
P1A pin
STRB
CCP1M0
PORT Data
CCP1M1
PORT Data
P1C pin
TRIS
STRD
PORT Data
P1B pin
TRIS
STRC
CCP1M0
TRIS
P1D pin
1
0
TRIS
Note 1:
2:
DS41303B-page 180
Advance Information
PIC18F2XK20/4XK20
16.4.7.1
Steering Synchronization
FIGURE 16-15:
PWM
STRn
P1<D:A>
PORT Data
PORT Data
P1n = PWM
FIGURE 16-16:
PWM
STRn
P1<D:A>
PORT Data
PORT Data
P1n = PWM
Advance Information
DS41303B-page 181
PIC18F2XK20/4XK20
16.4.8
OPERATION IN POWER-MANAGED
MODES
16.4.8.1
16.4.9
EFFECTS OF A RESET
DS41303B-page 182
Advance Information
PIC18F2XK20/4XK20
TABLE 16-2:
Name
INTCON
Bit 6
GIE/GIEH PEIE/GIEL
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
RCON
IPEN
SBOREN
RI
TO
PD
POR
BOR
56
PIR1
PSPIF
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
TRISB
60
TRISC
60
TRISD
60
TMR1L
58
TMR1H
58
T1CON
TMR2
T2CON
RD16
T1RUN
T1CKPS1
T1CKPS0
T1OSCEN
T1SYNC
TMR1CS
TMR1ON
Timer2 Register
58
58
58
PR2
58
TMR3L
59
TMR3H
T3CON
RD16
T3CCP2
59
T3CKPS1
T3CKPS0
CCPR1L
CCPR1H
CCP1CON
ECCP1AS
PWM1CON
Legend:
Note 1:
P1M1(1)
P1M0(1)
ECCPASE ECCPAS2
PRSEN
PDC6(1)
DC1B1
DC1B0
T3CCP1
T3SYNC
TMR3CS
TMR3ON
59
59
59
CCP1M3
CCP1M2
CCP1M1
(1)
CCP1M0
(1)
59
ECCPAS1
ECCPAS0
PSSAC1
PSSAC0
PSSBD1
PSSBD0
59
PDC5(1)
PDC4(1)
PDC3(1)
PDC2(1)
PDC1(1)
PDC0(1)
59
= unimplemented, read as 0. Shaded cells are not used during ECCP operation.
These bits are unimplemented on 28-pin devices; always maintain these bits clear.
Advance Information
DS41303B-page 183
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 184
Advance Information
PIC18F2XK20/4XK20
17.0
17.1
MASTER SYNCHRONOUS
SERIAL PORT (MSSP)
MODULE
Master SSP (MSSP) Module
Overview
17.3
SPI Mode
FIGURE 17-1:
Internal
Data Bus
Read
Master mode
Multi-Master mode
Slave mode
17.2
SSPBUF Reg
Control Registers
SDI/SDA
Write
SSPSR Reg
Shift
Clock
SDO
bit 0
SS
SS Control
Enable
Edge
Select
The use of these registers and their individual Configuration bits differ significantly depending on whether the
MSSP module is operated in SPI or I2C mode.
2
Clock Select
SSPM<3:0>
SMP:CKE 4
TMR2 Output
2
2
Edge
Select
Prescaler TOSC
4, 16, 64
Advance Information
DS41303B-page 185
PIC18F2XK20/4XK20
17.3.1
REGISTERS
REGISTER 17-1:
During
transmission,
the
SSPBUF
is
not
double-buffered. A write to SSPBUF will write to both
SSPBUF and SSPSR.
R/W-0
R/W-0
R-0
R-0
R-0
R-0
R-0
R-0
SMP
CKE
D/A
R/W
UA
BF
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
P: Stop bit
Used in I2C mode only. This bit is cleared when the MSSP module is disabled, SSPEN is cleared.
bit 3
S: Start bit
Used in I2C mode only.
bit 2
bit 1
bit 0
Note 1:
Polarity of clock state is set by the CKP bit of the SSPCON1 register.
DS41303B-page 186
Advance Information
PIC18F2XK20/4XK20
REGISTER 17-2:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
WCOL
SSPOV
SSPEN
CKP
SSPM3
SSPM2
SSPM1
SSPM0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3-0
Note 1:
2:
3:
In Master mode, the overflow bit is not set since each new reception (and transmission) is initiated by
writing to the SSPBUF register.
When enabled, these pins must be properly configured as input or output.
Bit combinations not specifically listed here are either reserved or implemented in I2C mode only.
Advance Information
DS41303B-page 187
PIC18F2XK20/4XK20
17.3.2
OPERATION
EXAMPLE 17-1:
LOOP
BTFSS
BRA
MOVF
SSPSTAT, BF
LOOP
SSPBUF, W
MOVWF
RXDATA
MOVF
MOVWF
TXDATA, W
SSPBUF
DS41303B-page 188
Advance Information
PIC18F2XK20/4XK20
17.3.3
17.3.4
TYPICAL CONNECTION
FIGURE 17-2:
SDI
SDI
Shift Register
(SSPSR)
MSb
SDO
LSb
MSb
SCK
Serial Clock
LSb
SCK
Processor 1
Shift Register
(SSPSR)
Processor 2
Advance Information
DS41303B-page 189
PIC18F2XK20/4XK20
17.3.5
MASTER MODE
FIGURE 17-3:
Write to
SSPBUF
SCK
(CKP = 0
CKE = 0)
SCK
(CKP = 1
CKE = 0)
4 Clock
Modes
SCK
(CKP = 0
CKE = 1)
SCK
(CKP = 1
CKE = 1)
SDO
(CKE = 0)
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
SDO
(CKE = 1)
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
SDI
(SMP = 0)
bit 0
bit 7
Input
Sample
(SMP = 0)
SDI
(SMP = 1)
bit 0
bit 7
Input
Sample
(SMP = 1)
SSPIF
Next Q4 Cycle
after Q2
SSPSR to
SSPBUF
DS41303B-page 190
Advance Information
PIC18F2XK20/4XK20
17.3.6
SLAVE MODE
17.3.7
SLAVE SELECT
SYNCHRONIZATION
FIGURE 17-4:
SS
SCK
(CKP = 0
CKE = 0)
SCK
(CKP = 1
CKE = 0)
Write to
SSPBUF
SDO
SDI
(SMP = 0)
bit 7
bit 6
bit 7
bit 0
bit 0
bit 7
bit 7
Input
Sample
(SMP = 0)
SSPIF
Interrupt
Flag
Next Q4 Cycle
after Q2
SSPSR to
SSPBUF
Advance Information
DS41303B-page 191
PIC18F2XK20/4XK20
FIGURE 17-5:
SS
Optional
SCK
(CKP = 0
CKE = 0)
SCK
(CKP = 1
CKE = 0)
Write to
SSPBUF
SDO
SDI
(SMP = 0)
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
bit 0
bit 7
Input
Sample
(SMP = 0)
SSPIF
Interrupt
Flag
Next Q4 Cycle
after Q2
SSPSR to
SSPBUF
FIGURE 17-6:
SS
Not Optional
SCK
(CKP = 0
CKE = 1)
SCK
(CKP = 1
CKE = 1)
Write to
SSPBUF
SDO
bit 7
SDI
(SMP = 0)
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
bit 0
Input
Sample
(SMP = 0)
SSPIF
Interrupt
Flag
Next Q4 Cycle
after Q2
SSPSR to
SSPBUF
DS41303B-page 192
Advance Information
PIC18F2XK20/4XK20
17.3.8
OPERATION IN POWER-MANAGED
MODES
17.3.9
17.3.10
EFFECTS OF A RESET
TABLE 17-1:
CKP
CKE
0, 0
0, 1
1, 0
1, 1
TABLE 17-2:
Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
INTCON
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
60
TRISA
TRISA7(2) TRISA6(2)
TRISC
SSPBUF
TRISC7
TRISC6
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
TRISA5
TRISA4
TRISA3
TRISA2
TRISA1
TRISA0
60
TRISC5
TRISC4
TRISC3
TRISC2
TRISC1
TRISC0
60
58
SSPCON1
WCOL
SSPOV
SSPEN
CKP
SSPM3
SSPM2
SSPM1
SSPM0
58
SSPSTAT
SMP
CKE
D/A
R/W
UA
BF
58
Legend: Shaded cells are not used by the MSSP in SPI mode.
Note 1: These bits are unimplemented in 28-pin devices; always maintain these bits clear.
2: PORTA<7:6> and their direction bits are individually configured as port pins based on various primary
oscillator modes. When disabled, these bits read as 0.
Advance Information
DS41303B-page 193
PIC18F2XK20/4XK20
17.4
I2C Mode
17.4.1
FIGURE 17-7:
Write
Shift
Clock
SSPSR Reg
SDI/SDA
MSb
LSb
SSPMSK Reg
Match Detect
Addr Match
SSPADD Reg
Start and
Stop bit Detect
DS41303B-page 194
SSPBUF Reg
SCK/SCL
REGISTERS
Set, Reset
S, P bits
(SSPSTAT Reg)
Advance Information
PIC18F2XK20/4XK20
REGISTER 17-3:
R/W-0
SMP
CKE
R-0
R-0
R-0
D/A
(1)
(1)
R-0
R/W
(2, 3)
R-0
R-0
UA
BF
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
P: Stop bit(1)
1 = Indicates that a Stop bit has been detected last
0 = Stop bit was not detected last
bit 3
S: Start bit(1)
1 = Indicates that a Start bit has been detected last
0 = Start bit was not detected last
bit 2
bit 1
bit 0
Note 1:
2:
3:
Advance Information
DS41303B-page 195
PIC18F2XK20/4XK20
REGISTER 17-4:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
WCOL
SSPOV
SSPEN
CKP
SSPM3
SSPM2
SSPM1
SSPM0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3-0
DS41303B-page 196
Advance Information
PIC18F2XK20/4XK20
REGISTER 17-5:
R/W-0
GCEN
ACKSTAT
R/W-0
R/W-0
(2)
(1)
ACKDT
ACKEN
R/W-0
(1)
RCEN
R/W-0
(1)
PEN
R/W-0
(1)
RSEN
R/W-0
SEN(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
2:
For bits ACKEN, RCEN, PEN, RSEN, SEN: If the I2C module is not in the Idle mode, these bits may not
be set (no spooling) and the SSPBUF may not be written (or writes to the SSPBUF are disabled).
Value that will be transmitted when the user initiates an Acknowledge sequence at the end of a receive.
Advance Information
DS41303B-page 197
PIC18F2XK20/4XK20
17.4.2
OPERATION
17.4.3.1
17.4.3
SLAVE MODE
In Slave mode, the SCL and SDA pins must be configured as inputs. The MSSP module will override the
input state with the output data when required
(slave-transmitter).
The I 2C Slave mode hardware will always generate an
interrupt on an address match. Through the mode
select bits, the user can also choose to interrupt on
Start and Stop bits
When an address is matched, or the data transfer after
an address match is received, the hardware
automatically will generate the Acknowledge (ACK)
pulse and load the SSPBUF register with the received
value currently in the SSPSR register.
Any combination of the following conditions will cause
the MSSP module not to give this ACK pulse:
The Buffer Full bit, BF bit of the SSPSTAT register, is set before the transfer is received.
The overflow bit, SSPOV bit of the SSPCON1
register, is set before the transfer is received.
In this case, the SSPSR register value is not loaded
into the SSPBUF, but bit SSPIF of the PIR1 register is
set. The BF bit is cleared by reading the SSPBUF
register, while bit SSPOV is cleared through software.
Addressing
3.
4.
5.
6.
7.
8.
9.
DS41303B-page 198
Advance Information
PIC18F2XK20/4XK20
17.4.3.2
Reception
17.4.3.3
Transmission
Advance Information
DS41303B-page 199
DS41303B-page 200
Advance Information
CKP
A6
A4
A3
Receiving Address
A5
A2
SSPOV (SSPCON1<6>)
BF (SSPSTAT<0>)
(PIR1<3>)
SSPIF
A7
A1
ACK
R/W = 0
D7
D4
D3
Receiving Data
D5
Cleared by software
SSPBUF is read
D6
D2
D1
D0
ACK
D7
D6
D4
D3
Receiving Data
D5
D2
D1
D0
Bus master
terminates
transfer
SSPOV is set
because SSPBUF is
still full. ACK is not sent.
ACK
FIGURE 17-8:
SCL
SDA
PIC18F2XK20/4XK20
I2C SLAVE MODE TIMING WITH SEN = 0 (RECEPTION, 7-BIT ADDRESS)
Advance Information
CKP
A6
Data in
sampled
BF (SSPSTAT<0>)
SSPIF (PIR1<3>)
A7
A5
A4
A3
A2
Receiving Address
A1
R/W = 0
ACK
D7
D5
D4
D3
D2
Cleared by software
D6
Transmitting Data
D0
ACK
D1
D7
D4
D3
D2
D0
ACK
D1
Transmitting Data
Cleared by software
D5
D6
FIGURE 17-9:
SCL
SDA
PIC18F2XK20/4XK20
DS41303B-page 201
DS41303B-page 202
Advance Information
A8
A9
UA (SSPSTAT<1>)
SSPOV (SSPCON1<6>)
CKP
Cleared by software
BF (SSPSTAT<0>)
(PIR1<3>)
SSPIF
SCL
ACK
R/W = 0
A7
A4
A3
A2
A0 ACK
Cleared by hardware
when SSPADD is updated
with low byte of address
A1
Cleared by software
A5
A6
D7
Cleared by software
9
1
D1 D0
Cleared by software
D3 D2
D3 D2
P
Bus master
terminates
transfer
SSPOV is set
because SSPBUF is
still full. ACK is not sent.
ACK
FIGURE 17-10:
SDA
PIC18F2XK20/4XK20
I2C SLAVE MODE TIMING WITH SEN = 0 (RECEPTION, 10-BIT ADDRESS)
Advance Information
CKP (SSPCON1<4>)
UA (SSPSTAT<1>)
BF (SSPSTAT<0>)
(PIR1<3>)
SSPIF
A9 A8
ACK
Cleared by software
A6 A5 A4 A3 A2 A1
A0
A7
ACK
Cleared by software
6
8
ACK
R/W=1
ACK
Bus master
terminates
transfer
Completion of
data transmission
clears BF flag
D4 D3 D2 D1 D0
Cleared by software
D7 D6 D5
Write of SSPBUF
BF flag is clear
initiates transmit
at the end of the
third address sequence
A9 A8
Sr
FIGURE 17-11:
SCL
SDA
R/W = 0
PIC18F2XK20/4XK20
DS41303B-page 203
PIC18F2XK20/4XK20
17.4.3.4
REGISTER 17-6:
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
R/W-1
MSK7
MSK6
MSK5
MSK4
MSK3
MSK2
MSK1
MSK0(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7-1
bit 0
Note 1: The MSK0 bit is used only in 10-bit slave mode. In all other modes, this bit has no effect.
DS41303B-page 204
Advance Information
PIC18F2XK20/4XK20
17.4.4
CLOCK STRETCHING
17.4.4.3
17.4.4.1
17.4.4.2
17.4.4.4
In 10-bit Slave Transmit mode, clock stretching is controlled during the first two address sequences by the
state of the UA bit, just as it is in 10-bit Slave Receive
mode. The first two addresses are followed by a third
address sequence which contains the high-order bits
of the 10-bit address and the R/W bit set to 1. After
the third address sequence is performed, the UA bit is
not set, the module is now configured in Transmit
mode and clock stretching is controlled by the BF flag
as in 7-bit Slave Transmit mode (see Figure 17-11).
Advance Information
DS41303B-page 205
PIC18F2XK20/4XK20
17.4.4.5
FIGURE 17-12:
Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4
SDA
DX
DX 1
SCL
CKP
Master device
asserts clock
Master device
deasserts clock
WR
SSPCON1
DS41303B-page 206
Advance Information
Advance Information
CKP
SSPOV (SSPCON1<6>)
BF (SSPSTAT<0>)
(PIR1<3>)
SSPIF
A7
A6
A5
A4
A3
A2
Receiving Address
A1
ACK
R/W = 0
D5
D4
D3
Cleared by software
D6
If BF is cleared
prior to the falling
edge of the 9th clock,
CKP will not be reset
to 0 and no clock
stretching will occur
SSPBUF is read
D7
Receiving Data
D2
D1
D7
D0
ACK
D4
D3
Receiving Data
D5
CKP
written
to 1 in
software
D6
D2
D1
D0
Bus master
terminates
transfer
SSPOV is set
because SSPBUF is
still full. ACK is not sent.
ACK
FIGURE 17-13:
SCL
SDA
PIC18F2XK20/4XK20
DS41303B-page 207
DS41303B-page 208
Advance Information
UA (SSPSTAT<1>)
SSPOV (SSPCON1<6>)
CKP
A9 A8
Cleared by software
BF (SSPSTAT<0>)
(PIR1<3>)
SSPIF
SCL
ACK
R/W = 0
A7
A4
A3
A2
Cleared by software
A5
A1
A0
A6
ACK
ACK
ACK
Bus master
terminates
transfer
SSPOV is set
because SSPBUF is
still full. ACK is not sent.
D1 D0
Cleared by software
CKP written to 1
by software
D3 D2
D1 D0
Cleared by software
D3 D2
D7 D6 D5 D4
FIGURE 17-14:
SDA
PIC18F2XK20/4XK20
I2C SLAVE MODE TIMING WITH SEN = 1 (RECEPTION, 10-BIT ADDRESS)
PIC18F2XK20/4XK20
17.4.5
FIGURE 17-15:
SDA
Receiving Data
ACK
D6
D5
D4
D3
D2
D1
D0
SCL
S
SSPIF
BF (SSPSTAT<0>)
Cleared by software
SSPBUF is read
SSPOV (SSPCON1<6>)
GCEN (SSPCON2<7>)
1
Advance Information
DS41303B-page 209
PIC18F2XK20/4XK20
MASTER MODE
Note:
FIGURE 17-16:
Start condition
Stop condition
Data transfer byte transmitted/received
Acknowledge transmit
Repeated Start
SSPM<3:0>
SSPADD<6:0>
Write
SSPBUF
Baud
Rate
Generator
Shift
Clock
SDA
SDA In
SCL In
Bus Collision
DS41303B-page 210
LSb
Clock Cntl
SCL
Receive Enable
SSPSR
MSb
17.4.6
Advance Information
PIC18F2XK20/4XK20
17.4.6.1
Advance Information
DS41303B-page 211
PIC18F2XK20/4XK20
17.4.7
BAUD RATE
FIGURE 17-17:
SSPM<3:0>
Reload
SCL
Control
CLKOUT
Reload
FOSC/2
TABLE 17-3:
Note 1:
SSPADD<7:0>
FOSC
FCY
BRG Value
FSCL
(2 Rollovers of BRG)
64 MHz
16 MHz
27h
400 kHz(1)
64 MHz
16 MHz
32h
313.7 kHz
64 MHz
16 MHz
3Fh
250 kHz
40 MHz
10 MHz
18h
400 kHz(1)
40 MHz
10 MHz
1Fh
312.5 kHz
40 MHz
10 MHz
63h
100 kHz
16 MHz
4 MHz
09h
400 kHz(1)
16 MHz
4 MHz
0Ch
308 kHz
16 MHz
4 MHz
27h
100 kHz
4 MHz
1 MHz
02h
333 kHz(1)
4 MHz
1 MHz
09h
100 kHz
4 MHz
1 MHz
00h
1 MHz(1)
I2C
I2C
The
interface does not conform to the 400 kHz
specification (which applies to rates greater than
100 kHz) in all details, but may be used with care where higher rates are required by the application.
DS41303B-page 212
Advance Information
PIC18F2XK20/4XK20
17.4.7.1
Clock Arbitration
FIGURE 17-18:
SDA
DX
DX 1
SCL
BRG decrements on
Q2 and Q4 cycles
BRG
Value
03h
02h
01h
03h
02h
Advance Information
DS41303B-page 213
PIC18F2XK20/4XK20
17.4.8
Note:
FIGURE 17-19:
17.4.8.1
TBRG
SDA
2nd bit
TBRG
SCL
TBRG
S
DS41303B-page 214
Advance Information
PIC18F2XK20/4XK20
17.4.9
17.4.9.1
FIGURE 17-20:
SDA = 1,
SCL = 1
TBRG
TBRG
SDA
RSEN bit set by hardware
on falling edge of ninth clock,
end of Xmit
SCL
TBRG
Sr = Repeated Start
Advance Information
DS41303B-page 215
PIC18F2XK20/4XK20
17.4.10
17.4.10.3
17.4.10.1
BF Status Flag
17.4.10.2
17.4.11
17.4.11.1
BF Status Flag
17.4.11.2
17.4.11.3
DS41303B-page 216
Advance Information
Advance Information
R/W
PEN
SEN
BF (SSPSTAT<0>)
SSPIF
SCL
SDA
A6
A5
A4
A3
A2
A1
Cleared by software
SSPBUF written
D7
1
SCL held low
while CPU
responds to SSPIF
ACK = 0
R/W = 0
A7
D5
D4
D3
D2
D1
D0
D6
Cleared by software
ACK
ACKSTAT in
SSPCON2 = 1
FIGURE 17-21:
SEN = 0
PIC18F2XK20/4XK20
DS41303B-page 217
DS41303B-page 218
Advance Information
ACKEN
SSPOV
BF
(SSPSTAT<0>)
SDA = 0, SCL = 1
while CPU
responds to SSPIF
SSPIF
SCL
SDA
A7
4
5
Cleared by software
A6 A5 A4 A3 A2
A1
R/W = 0
ACK
D0
ACK
Cleared by software
D7 D6 D5 D4 D3 D2 D1
Cleared in
software
ACK
P
Set SSPIF interrupt
at end of Acknowledge sequence
Bus master
terminates
transfer
Set P bit
(SSPSTAT<4>)
and SSPIF
PEN bit = 1
written here
D0
RCEN cleared
automatically
RCEN = 1, start
next receive
Cleared by software
Cleared by software
D7 D6 D5 D4 D3 D2 D1
RCEN cleared
automatically
FIGURE 17-22:
SEN = 0
Write to SSPBUF occurs here,
start XMIT
Write to SSPCON2<4>
to start Acknowledge sequence
SDA = ACKDT (SSPCON2<5>) = 0
PIC18F2XK20/4XK20
I 2C MASTER MODE WAVEFORM (RECEPTION, 7-BIT ADDRESS)
PIC18F2XK20/4XK20
17.4.12
ACKNOWLEDGE SEQUENCE
TIMING
17.4.13
17.4.12.1
17.4.13.1
FIGURE 17-23:
TBRG
SDA
ACK
D0
SCL
SSPIF
SSPIF set at
the end of receive
Cleared in
software
Cleared in
software
SSPIF set at the end
of Acknowledge sequence
FIGURE 17-24:
Write to SSPCON2,
set PEN
Falling edge of
9th clock
TBRG
SCL
SDA
ACK
P
TBRG
TBRG
TBRG
Advance Information
DS41303B-page 219
PIC18F2XK20/4XK20
17.4.14
SLEEP OPERATION
17.4.17
17.4.15
EFFECTS OF A RESET
17.4.16
MULTI-MASTER MODE
Address Transfer
Data Transfer
A Start Condition
A Repeated Start Condition
An Acknowledge Condition
Multi-Master mode support is achieved by bus arbitration. When the master outputs address/data bits onto
the SDA pin, arbitration takes place when the master
outputs a 1 on SDA, by letting SDA float high and
another master asserts a 0. When the SCL pin floats
high, data should be stable. If the expected data on
SDA is a 1 and the data sampled on the SDA pin = 0,
then a bus collision has taken place. The master will set
the Bus Collision Interrupt Flag, BCLIF and reset the
I2C port to its Idle state (Figure 17-25).
If a transmit was in progress when the bus collision
occurred, the transmission is halted, the BF flag is
cleared, the SDA and SCL lines are deasserted and the
SSPBUF can be written to. When the user services the
bus collision Interrupt Service Routine and if the I2C
bus is free, the user can resume communication by
asserting a Start condition.
If a Start, Repeated Start, Stop or Acknowledge condition was in progress when the bus collision occurred, the
condition is aborted, the SDA and SCL lines are deasserted and the respective control bits in the SSPCON2
register are cleared. When the user services the bus collision Interrupt Service Routine and if the I2C bus is free,
the user can resume communication by asserting a Start
condition.
The master will continue to monitor the SDA and SCL
pins. If a Stop condition occurs, the SSPIF bit will be set.
A write to the SSPBUF will start the transmission of
data at the first data bit, regardless of where the
transmitter left off when the bus collision occurred.
In Multi-Master mode, the interrupt generation on the
detection of Start and Stop conditions allows the determination of when the bus is free. Control of the I2C bus
can be taken when the P bit is set in the SSPSTAT
register, or the bus is Idle and the S and P bits are
cleared.
FIGURE 17-25:
SDA
SCL
BCLIF
DS41303B-page 220
Advance Information
PIC18F2XK20/4XK20
17.4.17.1
FIGURE 17-26:
SDA
SCL
Set SEN, enable Start
condition if SDA = 1, SCL = 1
SEN
BCLIF
SSPIF
Advance Information
DS41303B-page 221
PIC18F2XK20/4XK20
FIGURE 17-27:
TBRG
SDA
SCL
SEN
SCL = 0 before BRG time-out,
bus collision occurs. Set BCLIF.
BCLIF
Interrupt cleared
by software
S
SSPIF
FIGURE 17-28:
SDA
SCL
Set SSPIF
TBRG
S
SCL pulled low after BRG
time-out
SEN
BCLIF
SSPIF
SDA = 0, SCL = 1,
set SSPIF
DS41303B-page 222
Advance Information
Interrupts cleared
by software
PIC18F2XK20/4XK20
17.4.17.2
FIGURE 17-29:
If, at the end of the BRG time-out, both SCL and SDA
are still high, the SDA pin is driven low and the BRG is
reloaded and begins counting. At the end of the count,
regardless of the status of the SCL pin, the SCL pin is
driven low and the Repeated Start condition is
complete.
SDA
SCL
BCLIF
Cleared by software
0
SSPIF
FIGURE 17-30:
TBRG
SDA
SCL
BCLIF
RSEN
0
S
SSPIF
Advance Information
DS41303B-page 223
PIC18F2XK20/4XK20
17.4.17.3
b)
FIGURE 17-31:
TBRG
SDA sampled
low after TBRG,
set BCLIF
TBRG
SDA
SDA asserted low
SCL
PEN
BCLIF
P
SSPIF
FIGURE 17-32:
TBRG
TBRG
SDA
Assert SDA
SCL
PEN
BCLIF
P
SSPIF
DS41303B-page 224
Advance Information
PIC18F2XK20/4XK20
TABLE 17-4:
Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
IPR1
PSPIP
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
PIR1
PSPIF
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR2
BCLIP
60
PIR2
BCLIF
60
PIE2
BCLIE
60
SSPADD
SSP Address Register in I2C Slave Mode. SSP Baud Rate Reload Register in I2C Master Mode.
58
SSPBUF
58
SSPCON1
WCOL
SSPOV
SSPEN
CKP
SSPM3
SSPM2
SSPM1
SSPM0
58
SSPCON2
GCEN
ACKSTAT
ACKDT
ACKEN
RCEN
PEN
RSEN
SEN
58
SSPMSK
MSK7
MSK6
MSK5
MSK4
MSK3
MSK2
MSK1
MSK0
61
SSPSTAT
TRISC
Legend:
SMP
CKE
D/A
R/W
UA
BF
58
TRISC7
TRISC6
TRISC5
TRISC4
TRISC3
TRISC2
TRISC1
TRISC0
60
Advance Information
DS41303B-page 225
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 226
Advance Information
PIC18F2XK20/4XK20
18.0
ENHANCED UNIVERSAL
SYNCHRONOUS
ASYNCHRONOUS RECEIVER
TRANSMITTER (EUSART)
FIGURE 18-1:
TXIE
Interrupt
TXIF
TXREG Register
8
TX/CK pin
MSb
LSb
(8)
Pin Buffer
and Control
TRMT
SPEN
Transmit Shift Register (TSR)
TXEN
Baud Rate Generator
FOSC
TX9
BRG16
+1
SPBRGH
SPBRG
Multiplier
x4
x16 x64
SYNC
1 X 0 0
BRGH
X 1 1 0
BRG16
X 1 0 1
TX9D
Advance Information
DS41303B-page 227
PIC18F2XK20/4XK20
FIGURE 18-2:
CREN
RX/DT pin
Data
Recovery
FOSC
BRG16
SPBRGH
SPBRG
Multiplier
x4
x16 x64
SYNC
1 X 0 0
BRGH
X 1 1 0
BRG16
X 1 0 1
Stop
RCIDL
RSR Register
MSb
Pin Buffer
and Control
+1
OERR
(8)
LSb
0 START
RX9
FERR
RX9D
RCREG Register
FIFO
8
Data Bus
RCIF
RCIE
Interrupt
DS41303B-page 228
Advance Information
PIC18F2XK20/4XK20
18.1
18.1.1
EUSART ASYNCHRONOUS
TRANSMITTER
18.1.1.1
Note 1: When the SPEN bit is set the RX/DT I/O pin
is automatically configured as an input,
regardless of the state of the corresponding
TRIS bit and whether or not the EUSART
receiver is enabled. The RX/DT pin data
can be read via a normal PORT read but
PORT latch data output is precluded.
2: The TXIF transmitter interrupt flag is set
when the TXEN enable bit is set.
18.1.1.2
Transmitting Data
18.1.1.3
18.1.1.4
Advance Information
DS41303B-page 229
PIC18F2XK20/4XK20
18.1.1.5
TSR Status
18.1.1.7
18.1.1.6
1.
2.
3.
5.
8.
FIGURE 18-3:
6.
7.
ASYNCHRONOUS TRANSMISSION
Write to TXREG
BRG Output
(Shift Clock)
RC4/C2OUT/TX/CK
pin
TXIF bit
(Transmit Buffer
Reg. Empty Flag)
TRMT bit
(Transmit Shift
Reg. Empty Flag)
DS41303B-page 230
Word 1
Start bit
bit 0
bit 1
bit 7/8
Stop bit
Word 1
1 TCY
Word 1
Transmit Shift Reg
Advance Information
PIC18F2XK20/4XK20
FIGURE 18-4:
Write to TXREG
Word 1
BRG Output
(Shift Clock)
RC4/C2OUT/TX/CK
pin
Word 2
Start bit
bit 0
bit 1
Word 1
1 TCY
TXIF bit
(Interrupt Reg. Flag)
bit 7/8
Stop bit
Start bit
bit 0
Word 2
1 TCY
TRMT bit
(Transmit Shift
Reg. Empty Flag)
Note:
Word 2
Transmit Shift Reg
TABLE 18-1:
Name
Word 1
Transmit Shift Reg
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INTCON
GIE/GIEH PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
RCSTA
TXREG
TXSTA
BAUDCTL
59
59
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
59
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
59
SPBRGH
59
SPBRG
59
Legend: = unimplemented locations read as 0. Shaded cells are not used for asynchronous transmission.
Note 1: Reserved in PIC18F2XK20 devices; always maintain these bits clear.
Advance Information
DS41303B-page 231
PIC18F2XK20/4XK20
18.1.2
EUSART ASYNCHRONOUS
RECEIVER
18.1.2.1
DS41303B-page 232
18.1.2.2
Receiving Data
18.1.2.3
Advance Information
PIC18F2XK20/4XK20
18.1.2.4
Receive Interrupts
18.1.2.7
18.1.2.5
18.1.2.8
Address Detection
18.1.2.6
Advance Information
DS41303B-page 233
PIC18F2XK20/4XK20
18.1.2.9
18.1.2.10
1.
FIGURE 18-5:
Rcv Shift
Reg
Rcv Buffer Reg
RCIDL
ASYNCHRONOUS RECEPTION
Start
bit
bit 0
RX/DT pin
bit 1
Start
bit
bit 0
Word 1
RCREG
Start
bit
Word 2
RCREG
Read Rcv
Buffer Reg
RCREG
RCIF
(Interrupt Flag)
OERR bit
CREN
Note:
This timing diagram shows three words appearing on the RX input. The RCREG (receive buffer) is read after the third word,
causing the OERR (overrun) bit to be set.
DS41303B-page 234
Advance Information
PIC18F2XK20/4XK20
TABLE 18-2:
Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INTCON
GIE/GIEH PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
RCSTA
RCREG
59
59
TRISC
TRISC7
TRISC6
TRISC5
TRISC4
TRISC3
TRISC2
TRISC1
TRISC0
60
TXSTA
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
59
BAUDCTL
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
SPBRGH
59
SPBRG
59
59
Legend: = unimplemented locations read as 0. Shaded cells are not used for asynchronous reception.
Note 1: Reserved in PIC18F2XK20 devices; always maintain these bits clear.
Advance Information
DS41303B-page 235
PIC18F2XK20/4XK20
18.2
The factory calibrates the internal oscillator block output (HFINTOSC). However, the HFINTOSC frequency
may drift as VDD or temperature changes, and this
directly affects the asynchronous baud rate. Two methods may be used to adjust the baud rate clock, but both
require a reference clock source of some kind.
REGISTER 18-1:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R-1
R/W-0
CSRC
TX9
TXEN(1)
SYNC
SENDB
BRGH
TRMT
TX9D
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Note 1:
DS41303B-page 236
Advance Information
PIC18F2XK20/4XK20
REGISTER 18-2:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R-0
R-0
R-x
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
Advance Information
DS41303B-page 237
PIC18F2XK20/4XK20
REGISTER 18-3:
R-0
R-1
R/W-0
R/W-0
R/W-0
U-0
R/W-0
R/W-0
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
Unimplemented: Read as 0
bit 1
bit 0
DS41303B-page 238
Advance Information
PIC18F2XK20/4XK20
18.3
EXAMPLE 18-1:
F OS C
Desired Baud Rate = --------------------------------------------------------------------64 ( [SPBRGH:SPBRG] + 1 )
16000000
-----------------------9600
= ------------------------ 1
64
= [ 25.042 ] = 25
16000000
Calculated Baud Rate = --------------------------64 ( 25 + 1 )
= 9615
TABLE 18-3:
CALCULATING BAUD
RATE ERROR
Configuration Bits
BRG/EUSART Mode
8-bit/Asynchronous
FOSC/[64 (n+1)]
8-bit/Asynchronous
16-bit/Asynchronous
16-bit/Asynchronous
8-bit/Synchronous
16-bit/Synchronous
SYNC
BRG16
BRGH
FOSC/[16 (n+1)]
1
Legend:
TABLE 18-4:
Name
FOSC/[4 (n+1)]
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Reset Values
on page
Bit 0
TXSTA
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
59
RCSTA
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
59
BAUDCTL
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
59
SPBRGH
59
SPBRG
59
Legend: = unimplemented, read as 0. Shaded cells are not used by the BRG.
Advance Information
DS41303B-page 239
PIC18F2XK20/4XK20
TABLE 18-5:
BAUD
RATE
Actual
Rate
%
Error
SPBRG
value
(decimal)
300
1200
1200
0.00
239
1202
0.16
207
1200
0.00
143
Actual
Rate
%
Error
SPBRG
value
(decimal)
Actual
Rate
%
Error
SPBRG
value
(decimal)
Actual
Rate
%
Error
SPBRG
value
(decimal)
2400
2400
0.00
119
2404
0.16
103
2400
0.00
71
9600
9615
0.16
103
9600
0.00
29
9615
0.16
25
9600
0.00
17
10417
10417
0.00
95
10286
-1.26
27
10417
0.00
23
10165
-2.42
16
19.2k
19.23k
0.16
51
19.20k
0.00
14
19.23k
0.16
12
19.20k
0.00
57.6k
58.82k
2.12
16
57.60k
0.00
57.60k
0.00
115.2k
111.11k
-3.55
%
Error
SPBRG
value
(decimal)
%
Error
SPBRG
value
(decimal)
%
Error
SPBRG
value
(decimal)
Actual
Rate
%
Error
SPBRG
value
(decimal)
300
300
0.16
207
300
0.00
191
300
0.16
51
1200
1202
0.16
103
1202
0.16
51
1200
0.00
47
1202
0.16
12
2400
2404
0.16
51
2404
0.16
25
2400
0.00
23
9600
9615
0.16
12
9600
0.00
10417
10417
0.00
11
10417
0.00
19.2k
19.20k
0.00
57.6k
57.60k
0.00
115.2k
%
Error
SPBRG
value
(decimal)
%
Error
SPBRG
value
(decimal)
Actual
Rate
%
Error
SPBRG
value
(decimal)
Actual
Rate
%
Error
SPBRG
value
(decimal)
300
1200
2400
9600
9600
0.00
119
9615
0.16
103
9600
0.00
71
10417
10378
-0.37
110
10417
0.00
95
10473
0.53
65
19.2k
19.23k
0.16
207
19.20k
0.00
59
19.23k
0.16
51
19.20k
0.00
35
57.6k
57.97k
0.64
68
57.60k
0.00
19
58.82k
2.12
16
57.60k
0.00
11
115.2k
114.29k
-0.79
34
115.2k
0.00
111.1k
-3.55
115.2k
0.00
DS41303B-page 240
Advance Information
PIC18F2XK20/4XK20
TABLE 18-5:
BAUD
RATE
%
Error
SPBRG
value
(decimal)
300
1200
2400
2404
Actual
Rate
%
Error
SPBRG
value
(decimal)
1202
0.16
207
1200
0.00
191
300
1202
0.16
0.16
207
51
0.16
207
2404
0.16
103
2400
0.00
95
2404
0.16
25
Actual
Rate
%
Error
SPBRG
value
(decimal)
Actual
Rate
%
Error
SPBRG
value
(decimal)
9600
9615
0.16
51
9615
0.16
25
9600
0.00
23
10417
10417
0.00
47
10417
0.00
23
10473
0.53
21
10417
0.00
19.2k
19231
0.16
25
19.23k
0.16
12
19.2k
0.00
11
57.6k
55556
-3.55
57.60k
0.00
115.2k
115.2k
0.00
%
Error
SPBRGH
:SPBRG
(decimal)
%
Error
SPBRGH
:SPBRG
(decimal)
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
300
300.0
0.00
13332
300.0
0.00
3839
300.03
0.01
3332
300.0
0.00
2303
1200
1200.1
0.01
3332
1200
0.00
959
1200.5
0.04
832
1200
0.00
575
2400
2399
-0.02
1666
2400
0.00
479
2398
-0.08
416
2400
0.00
287
9600
9592
-0.08
416
9600
0.00
119
9615
0.16
103
9600
0.00
71
10417
10417
0.00
383
10378
-0.37
110
10417
0.00
95
10473
0.53
65
19.2k
19.23k
0.16
207
19.20k
0.00
59
19.23k
0.16
51
19.20k
0.00
35
57.6k
57.97k
0.64
68
57.60k
0.00
19
58.82k
2.12
16
57.60k
0.00
11
115.2k
114.29k
-0.79
34
115.2k
0.00
111.11k
-3.55
115.2k
0.00
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
300
299.9
-0.02
1666
300.1
0.04
832
300.0
0.00
767
300.5
0.16
207
1200
1199
-0.08
416
1202
0.16
207
1200
0.00
191
1202
0.16
51
2400
2404
0.16
207
2404
0.16
103
2400
0.00
95
2404
0.16
25
9600
9615
0.16
51
9615
0.16
25
9600
0.00
23
10417
10417
0.00
47
10417
0.00
23
10473
0.53
21
10417
0.00
19.2k
19.23k
0.16
25
19.23k
0.16
12
19.20k
0.00
11
57.6k
55556
-3.55
57.60k
0.00
115.2k
115.2k
0.00
Advance Information
DS41303B-page 241
PIC18F2XK20/4XK20
TABLE 18-5:
BAUD
RATE
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
300
1200
300
1200
0.00
0.00
53332
13332
300.0
1200
0.00
0.00
15359
3839
300.0
1200.1
0.00
0.01
13332
3332
300.0
1200
0.00
0.00
9215
2303
2400
2400
0.00
6666
2400
0.00
1919
2399.5
-0.02
1666
2400
0.00
1151
9600
9598.1
-0.02
1666
9600
0.00
479
9592
-0.08
416
9600
0.00
287
10417
10417
0.00
1535
10425
0.08
441
10417
0.00
383
10433
0.16
264
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
19.2k
19.21k
0.04
832
19.20k
0.00
239
19.23k
0.16
207
19.20k
0.00
143
57.6k
57.55k
-0.08
277
57.60k
0.00
79
57.97k
0.64
68
57.60k
0.00
47
115.2k
115.11k
-0.08
138
115.2k
0.00
39
114.29k
-0.79
34
115.2k
0.00
23
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
Actual
Rate
%
Error
SPBRGH
:SPBRG
(decimal)
832
300
300.0
0.00
6666
300.0
0.01
3332
300.0
0.00
3071
300.1
0.04
1200
1200
-0.02
1666
1200
0.04
832
1200
0.00
767
1202
0.16
207
2400
2401
0.04
832
2398
0.08
416
2400
0.00
383
2404
0.16
103
25
9600
9615
0.16
207
9615
0.16
103
9600
0.00
95
9615
0.16
10417
10417
0.00
191
10417
0.00
95
10473
0.53
87
10417
0.00
23
19.2k
19.23k
0.16
103
19.23k
0.16
51
19.20k
0.00
47
19.23k
0.16
12
57.6k
57.14k
-0.79
34
58.82k
2.12
16
57.60k
0.00
15
115.2k
117.6k
2.12
16
111.1k
-3.55
115.2k
0.00
DS41303B-page 242
Advance Information
PIC18F2XK20/4XK20
18.3.1
AUTO-BAUD DETECT
TABLE 18-6:
FIGURE 18-6:
BRG16
BRGH
BRG Base
Clock
BRG ABD
Clock
FOSC/64
FOSC/512
FOSC/16
FOSC/128
FOSC/16
FOSC/128
FOSC/4
FOSC/32
1
Note:
BRG Value
RX pin
0000h
001Ch
Start
Edge #1
bit 1
bit 0
Edge #2
bit 3
bit 2
Edge #3
bit 5
bit 4
Edge #4
bit 7
bit 6
Edge #5
Stop bit
BRG Clock
Auto Cleared
Set by User
ABDEN bit
RCIDL
RCIF bit
(Interrupt)
Read
RCREG
SPBRG
XXh
1Ch
SPBRGH
XXh
00h
Note 1:
The ABD sequence requires the EUSART module to be configured in Asynchronous mode.
Advance Information
DS41303B-page 243
PIC18F2XK20/4XK20
18.3.2
AUTO-WAKE-UP ON BREAK
18.3.2.1
Special Considerations
Break Character
To avoid character errors or character fragments during
a wake-up event, the wake-up character must be all
zeros.
When the wake-up is enabled the function works
independent of the low time on the data stream. If the
WUE bit is set and a valid non-zero character is
received, the low time from the Start bit to the first rising
edge will be interpreted as the wake-up event. The
remaining bits in the character will be received as a
fragmented character and subsequent characters can
result in framing or overrun errors.
Therefore, the initial character in the transmission must
be all 0s. This must be 10 or more bit times, 13-bit
times recommended for LIN bus, or any number of bit
times for standard RS-232 devices.
Oscillator Startup Time
Oscillator start-up time must be considered, especially
in applications using oscillators with longer start-up
intervals (i.e., LP, XT or HS/PLL mode). The Sync
Break (or wake-up signal) character must be of
sufficient length, and be followed by a sufficient
interval, to allow enough time for the selected oscillator
to start and provide proper initialization of the EUSART.
WUE Bit
The wake-up event causes a receive interrupt by
setting the RCIF bit. The WUE bit is cleared by
hardware by a rising edge on RX/DT. The interrupt
condition is then cleared by software by reading the
RCREG register and discarding its contents.
To ensure that no actual data is lost, check the RCIDL
bit to verify that a receive operation is not in process
before setting the WUE bit. If a receive operation is not
occurring, the WUE bit may then be set just prior to
entering the Sleep mode.
FIGURE 18-7:
Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4
OSC1
Auto Cleared
Note 1:
DS41303B-page 244
Advance Information
PIC18F2XK20/4XK20
FIGURE 18-8:
Q1
Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1 Q2 Q3 Q4 Q1Q2 Q3 Q4
OSC1
Auto Cleared
Note 1
RCIF
Sleep Command Executed
Note 1:
2:
18.3.3
If the wake-up event requires long oscillator warm-up time, the automatic clearing of the WUE bit can occur while the stposc signal is
still active. This sequence should not depend on the presence of Q clocks.
The EUSART remains in Idle while the WUE bit is set.
18.3.3.1
Sleep Ends
18.3.4
Advance Information
DS41303B-page 245
PIC18F2XK20/4XK20
FIGURE 18-9:
Write to TXREG
BRG Output
(Shift Clock)
TX (pin)
Start bit
bit 0
bit 1
bit 11
Stop bit
Break
TXIF bit
(Transmit
interrupt Flag)
TRMT bit
(Transmit Shift
Reg. Empty Flag)
SENDB Sampled Here
Auto Cleared
SENDB
(send Break
control bit)
DS41303B-page 246
Advance Information
PIC18F2XK20/4XK20
18.4
18.4.1
SYNC = 1
CSRC = 1
SREN = 0 (for transmit); SREN = 1 (for receive)
CREN = 0 (for transmit); CREN = 1 (for receive)
SPEN = 1
18.4.1.2
Clock Polarity
18.4.1.3
18.4.1.4
Data Polarity
18.4.1.1
Master Clock
Advance Information
DS41303B-page 247
PIC18F2XK20/4XK20
18.4.1.5
1.
2.
3.
4.
5.
6.
FIGURE 18-10:
7.
8.
SYNCHRONOUS TRANSMISSION
RX/DT
pin
bit 0
bit 1
Word 1
bit 2
bit 7
bit 0
bit 1
Word 2
bit 7
TX/CK pin
(SCKP = 0)
TX/CK pin
(SCKP = 1)
Write to
TXREG Reg
Write Word 1
Write Word 2
TXIF bit
(Interrupt Flag)
TRMT bit
TXEN bit
Note:
1
Sync Master mode, SPBRG = 0, continuous transmission of two 8-bit words.
FIGURE 18-11:
bit 0
bit 1
bit 2
bit 6
bit 7
TX/CK pin
Write to
TXREG reg
TXIF bit
TRMT bit
TXEN bit
DS41303B-page 248
Advance Information
PIC18F2XK20/4XK20
TABLE 18-7:
Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INTCON
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
(1)
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
RCSTA
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
59
TRISC
TRISC7
TRISC6
TRISC5
TRISC4
TRISC3
TRISC2
TRISC1
TRISC0
60
TXREG
59
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
59
BAUDCTL
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
59
SPBRGH
59
SPBRG
59
TXSTA
Legend: = unimplemented, read as 0. Shaded cells are not used for synchronous master transmission.
Note 1: Reserved in PIC18F2XK20 devices; always maintain these bits clear.
Advance Information
DS41303B-page 249
PIC18F2XK20/4XK20
18.4.1.6
18.4.1.7
18.4.1.9
18.4.1.10
1.
Slave Clock
DS41303B-page 250
18.4.1.8
Advance Information
PIC18F2XK20/4XK20
FIGURE 18-12:
RX/DT
pin
bit 0
bit 1
bit 2
bit 3
bit 4
bit 5
bit 6
bit 7
TX/CK pin
(SCKP = 0)
TX/CK pin
(SCKP = 1)
Write to
bit SREN
SREN bit
CREN bit 0
RCIF bit
(Interrupt)
Read
RXREG
Note:
Timing diagram demonstrates Sync Master mode with bit SREN = 1 and bit BRGH = 0.
TABLE 18-8:
Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INTCON
GIE/GIEH PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
RCSTA
RCREG
TXSTA
BAUDCTL
59
59
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
59
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
59
SPBRGH
59
SPBRG
59
Legend: = unimplemented, read as 0. Shaded cells are not used for synchronous master reception.
Note 1: Reserved in 28-pin devices; always maintain these bits clear.
Advance Information
DS41303B-page 251
PIC18F2XK20/4XK20
18.4.2
SYNC = 1
CSRC = 0
SREN = 0 (for transmit); SREN = 1 (for receive)
CREN = 0 (for transmit); CREN = 1 (for receive)
SPEN = 1
1.
2.
3.
4.
18.4.2.1
5.
18.4.2.2
1.
2.
3.
4.
5.
6.
7.
TABLE 18-9:
Name
INTCON
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF
(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
59
TRISC7
TRISC6
TRISC5
TRISC4
TRISC3
TRISC2
TRISC1
TRISC0
RCSTA
TRISC
TXREG
60
59
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
59
BAUDCTL
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
59
SPBRGH
59
SPBRG
59
TXSTA
Legend: = unimplemented, read as 0. Shaded cells are not used for synchronous master transmission.
Note 1: Reserved in PIC18F2XK20 devices; always maintain these bits clear.
DS41303B-page 252
Advance Information
PIC18F2XK20/4XK20
18.4.2.3
18.4.2.4
1.
Sleep
CREN bit is always set, therefore the receiver is
never Idle
SREN bit, which is a don't care in Slave mode
2.
3.
4.
5.
6.
7.
8.
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
INTCON
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
PSPIE(1)
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
SPEN
RX9
SREN
CREN
ADDEN
FERR
OERR
RX9D
59
RCSTA
RCREG
TXSTA
59
CSRC
TX9
TXEN
SYNC
SENDB
BRGH
TRMT
TX9D
59
BAUDCTL
ABDOVF
RCIDL
DTRXP
CKTXP
BRG16
WUE
ABDEN
59
SPBRGH
59
SPBRG
59
Legend: = unimplemented, read as 0. Shaded cells are not used for synchronous slave reception.
Note 1: Reserved in 28-pin devices; always maintain these bits clear.
Advance Information
DS41303B-page 253
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 254
Advance Information
PIC18F2XK20/4XK20
19.0
ANALOG-TO-DIGITAL
CONVERTER (ADC) MODULE
FIGURE 19-1:
VCFG1 = 0
VCFG1 = 1
AVDD
VCFG0 = 0
VREF+
VCFG0 = 1
AN0
0000
AN1
0001
AN2
0010
AN3
0011
AN4
0100
AN5
0101
AN6
0110
AN7
0111
AN8
1000
AN9
1001
AN10
1010
AN11
1011
AN12
1100
Unused
1101
Unused
1110
ADC
10
GO/DONE
ADFM
0 = Left Justify
1 = Right Justify
ADON
10
VSS
ADRESH
ADRESL
1111
FVR
CHS<3:0>
Advance Information
DS41303B-page 255
PIC18F2XK20/4XK20
19.1
19.1.4
ADC Configuration
Port configuration
Channel selection
ADC voltage reference selection
ADC conversion clock source
Interrupt control
Results formatting
19.1.1
PORT CONFIGURATION
The ANSEL, ANSELH, TRISA, TRISB and TRISE registers all configure the A/D port pins. Any port pin
needed as an analog input should have its corresponding ANSx bit set to disable the digital input buffer and
TRISx bit set to disable the digital output driver. If the
TRISx bit is cleared, the digital output level (VOH or
VOL) will be converted.
The A/D operation is independent of the state of the
ANSx bits and the TRIS bits.
Note 1: When reading the PORT register, all pins
with their corresponding ANSx bit set
read as cleared (a low level). However,
analog conversion of pins configured as
digital inputs (ANSx bit cleared and
TRISx bit set) will be accurately
converted.
19.1.2
19.1.5
19.1.3
2: Analog levels on any pin with the corresponding ANSx bit cleared may cause the
digital input buffer to consume current out
of the devices specification limits.
CHANNEL SELECTION
DS41303B-page 256
CONVERSION CLOCK
The source of the conversion clock is software selectable via the ADCS bits of the ADCON2 register. There
are seven possible clock options:
FOSC/2
FOSC/4
FOSC/8
FOSC/16
FOSC/32
FOSC/64
FRC (dedicated internal oscillator)
Advance Information
PIC18F2XK20/4XK20
19.1.6
INTERRUPTS
TABLE 19-1:
ADCS<2:0>
FOSC/2
64 MHz
000
FOSC/4
100
31.25
ns(2)
62.5
ns(2)
ns(2)
FOSC/8
001
400
FOSC/16
101
250 ns(2)
FOSC/32
010
ns(2)
FOSC/64
110
FRC
Legend:
Note 1:
2:
3:
4:
19.1.7
500
16 MHz
1-4
250
ns(2)
1.0 s
4.0 s(3)
500
ns(2)
2.0 s
8.0 s(3)
1.0 s
4.0 s(3)
16.0 s(3)
2.0 s
s(3)
32.0 s(3)
16.0 s(3)
64.0 s(3)
s(1,4)
1-4 s(1,4)
500
8.0
s(1,4)
1-4
s(1,4)
1-4
ns(2)
1 MHz
125
4.0 s(3)
1.0 s
x11
4 MHz
ns(2)
2.0 s
RESULT FORMATTING
FIGURE 19-2:
(ADFM = 0)
ADRESL
MSB
LSB
bit 7
bit 0
bit 7
bit 0
Unimplemented: Read as 0
MSB
bit 7
LSB
bit 0
bit 7
Unimplemented: Read as 0
Advance Information
bit 0
10-bit A/D Result
DS41303B-page 257
PIC18F2XK20/4XK20
19.2
ADC Operation
19.2.1
STARTING A CONVERSION
FIGURE 19-3:
TCY - TAD TAD1 TAD2 TAD3 TAD4 TAD5 TAD6 TAD7 TAD8 TAD9 TAD10 TAD11 2 TAD
b4
b1
b0
b6
b7
b2
b9
b8
b3
b5
Conversion starts
Discharge
FIGURE 19-4:
TAD Cycles
TACQT Cycles
1
Automatic
Acquisition
Time
b9
b8
b7
b6
6
b5
10
11
b4
b3
b2
b1
b0
Conversion starts
(Holding capacitor is disconnected from analog input)
Set GO bit
(Holding capacitor continues
acquiring input)
DS41303B-page 258
2 TAD
Discharge
Advance Information
PIC18F2XK20/4XK20
19.2.2
COMPLETION OF A CONVERSION
19.2.3
DISCHARGE
19.2.4
TERMINATING A CONVERSION
19.2.5
19.2.7
19.2.8
19.2.6
Advance Information
DS41303B-page 259
PIC18F2XK20/4XK20
19.2.9
2.
3.
4.
5.
6.
7.
8.
Configure Port:
Disable pin output driver (See TRIS register)
Configure pin as analog
Configure the ADC module:
Select ADC conversion clock
Configure voltage reference
Select ADC input channel
Select result format
Select acquisition delay
Turn on ADC module
Configure ADC interrupt (optional):
Clear ADC interrupt flag
Enable ADC interrupt
Enable peripheral interrupt
Enable global interrupt(1)
Wait the required acquisition time(2).
Start conversion by setting the GO/DONE bit.
Wait for ADC conversion to complete by one of
the following:
Polling the GO/DONE bit
Waiting for the ADC interrupt (interrupts
enabled)
Read ADC Result
Clear the ADC interrupt flag (required if interrupt
is enabled).
EXAMPLE 19-1:
A/D CONVERSION
DS41303B-page 260
Advance Information
PIC18F2XK20/4XK20
19.2.10
The following registers are used to control the operation of the ADC.
Note:
REGISTER 19-1:
U-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CHS3
CHS2
CHS1
CHS0
GO/DONE
ADON
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7-6
Unimplemented: Read as 0
bit 5-2
bit 1
bit 0
Note 1:
Advance Information
DS41303B-page 261
PIC18F2XK20/4XK20
REGISTER 19-2:
U-0
U-0
R/W-0
R/W-0
U-0
U-0
U-0
U-0
VCFG1
VCFG0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-6
Unimplemented: Read as 0
bit 5
bit 4
bit 3-0
Unimplemented: Read as 0
DS41303B-page 262
Advance Information
x = Bit is unknown
PIC18F2XK20/4XK20
REGISTER 19-3:
R/W-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
ADFM
ACQT2
ACQT1
ACQT0
ADCS2
ADCS1
ADCS0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
x = Bit is unknown
bit 6
Unimplemented: Read as 0
bit 5-3
ACQT<2:0>: A/D Acquisition time select bits. Acquisition time is the duration that the A/D charge holding capacitor remains connected to A/D channel from the instant the GO/DONE bit is set until conversions begins.
000 = 0(1)
001 = 2 TAD
010 = 4 TAD
011 = 6 TAD
100 = 8 TAD
101 = 12 TAD
110 = 16 TAD
111 = 20 TAD
bit 2-0
Note 1:
When the A/D clock source is selected as FRC then the start of conversion is delayed by one instruction
cycle after the GO/DONE bit is set to allow the SLEEP instruction to be executed.
Advance Information
DS41303B-page 263
PIC18F2XK20/4XK20
REGISTER 19-4:
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
ADRES9
ADRES8
ADRES7
ADRES6
ADRES5
ADRES4
ADRES3
ADRES2
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-0
x = Bit is unknown
REGISTER 19-5:
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
ADRES1
ADRES0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-6
bit 5-0
REGISTER 19-6:
x = Bit is unknown
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
ADRES9
ADRES8
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-2
bit 1-0
REGISTER 19-7:
x = Bit is unknown
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
ADRES7
ADRES6
ADRES5
ADRES4
ADRES3
ADRES2
ADRES1
ADRES0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-0
x = Bit is unknown
DS41303B-page 264
Advance Information
PIC18F2XK20/4XK20
19.3
EQUATION 19-1:
Assumptions:
T ACQ = Amplifier Settling Time + Hold Capacitor Charging Time + Temperature Coefficient
= T AMP + T C + T COFF
= 5s + T C + [ ( Temperature - 25C ) ( 0.05s/C ) ]
The value for TC can be approximated with the following equations:
1
V AP PLIE D 1 ------------ = V CHOLD
2047
TC
----------
RC
V AP P LI ED 1 e = V CHOLD
Tc
---------
1
RC
V AP P LIED 1 e = V A P PLIE D 1 ------------
2047
T C = C HOLD ( R IC + R SS + R S ) ln(1/2047)
= 13.5pF ( 1k + 700 + 10k ) ln(0.0004885)
= 1.20 s
Therefore:
T ACQ = 5 S + 1.20 S + [ ( 50C- 25C ) ( 0.05 S /C ) ]
= 7.45 S
Note 1: The reference voltage (VREF) has no effect on the equation, since it cancels itself out.
2: The charge holding capacitor (CHOLD) is discharged after each conversion.
3: The maximum recommended impedance for analog sources is 10 k. This is required to meet the pin
leakage specification.
Advance Information
DS41303B-page 265
PIC18F2XK20/4XK20
FIGURE 19-5:
Rs
CPIN
5 pF
VA
VT = 0.6V
VT = 0.6V
Sampling
Switch
SS Rss
RIC 1k
CHOLD = 13.5 pF
I LEAKAGE(1)
Legend: CPIN
= Input Capacitance
= Threshold Voltage
VT
I LEAKAGE = Leakage current at the pin due to
various junctions
RIC
= Interconnect Resistance
SS
= Sampling Switch
CHOLD
= Sample/Hold Capacitance
Note 1:
VDD
Discharge
Switch
VSS/VREF-
3.5V
3.0V
2.5V
2.0V
1.5V
.1
1
10
Rss (k)
100
FIGURE 19-6:
Full-Scale Range
3FFh
3FEh
ADC Output Code
3FDh
3FCh
3FBh
Full-Scale
Transition
004h
003h
002h
001h
000h
VSS/VREF-
DS41303B-page 266
Zero-Scale
Transition
VDD/VREF+
Advance Information
PIC18F2XK20/4XK20
TABLE 19-2:
Name
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
INTCON
GIE/GIEH PEIE/GIEL
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR1
PSPIF(1)
ADIF
RCIF
TXIF
SSPIF
CCP1IF
TMR2IF
TMR1IF
60
PIE1
(1)
PSPIE
ADIE
RCIE
TXIE
SSPIE
CCP1IE
TMR2IE
TMR1IE
60
IPR1
PSPIP(1)
ADIP
RCIP
TXIP
SSPIP
CCP1IP
TMR2IP
TMR1IP
60
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
IPR2
ADRESH
59
ADRESL
59
ADCON0
CHS3
CHS2
CHS1
CHS0
GO/DONE
ADON
59
ADCON1
VCFG1
VCFG0
59
ADCON2
ADFM
ACQT2
ACQT1
ACQT0
ADCS2
ADCS1
ADCS0
59
ANS7(1)
ANS6(1)
ANS5(1)
ANS4
ANS3
ANS2
ANS1
ANS0
60
ANS12
ANS11
ANS10
ANS9
ANS8
60
PORTA
RA7(2)
RA6(2)
RA5
RA4
RA3
RA2
RA1
RA0
60
TRISA
TRISA7(2)
TRISA6(2)
PORTB
RB7
RB6
RB1
RB0
60
ANSEL
ANSELH
RB4
RB3
RB2
60
TRISB
60
LATB
60
PORTE(4)
RE3(3)
RE2
RE1
RE0
60
TRISE(4)
IBF
OBF
IBOV
PSPMODE
TRISE2
TRISE1
TRISE0
60
LATE(4)
60
Legend: = unimplemented, read as 0. Shaded cells are not used for A/D conversion.
Note 1: These bits are unimplemented on PIC18F2XK20 devices; always maintain these bits clear.
2: PORTA<7:6> and their direction bits are individually configured as port pins based on various primary
oscillator modes. When disabled, these bits read as 0.
3: RE3 port bit is available only as an input pin when the MCLRE Configuration bit is 0.
4: These registers are not implemented on PIC18F2XK20 devices.
Advance Information
DS41303B-page 267
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 268
Advance Information
PIC18F2XK20/4XK20
20.0
COMPARATOR MODULE
20.1
FIGURE 20-1:
SINGLE COMPARATOR
VIN+
VIN-
Output
VINVIN+
Output
Note:
Comparator Overview
Advance Information
DS41303B-page 269
PIC18F2XK20/4XK20
FIGURE 20-2:
C1CH<1:0>
2
D
Q1
C12IN0-
C12IN1C12IN2-
1
MUX
2
C12IN3-
To
Data Bus
EN
RD_CM1CON0
Set C1IF
D
Q3*RD_CM1CON0
EN
CL
To PWM Logic
Reset
C1ON(1)
C1R
C1OE
C1VIN-
C1IN+
CVREF
0
MUX
1
0
MUX
C1VREF
1
FVR
C1RSEL
Note 1:
2:
3:
4:
FIGURE 20-3:
C1VIN+ C1
+
C1OUT
C1OUT pin(2)
C1SP
C1POL
When C1ON = 0, the C1 comparator will produce a 0 output to the XOR Gate.
Output shown for reference only. See I/O port pin block diagram for more detail.
Q1 and Q3 are phases of the four-phase system clock (FOSC).
Q1 is held high during Sleep mode.
To
Data Bus
EN
RD_CM2CON0
C2CH<1:0>
Set C2IF
2
C12IN0-
C12IN1C12IN2-
1
MUX
2
C12IN3-
C2R
C2IN+
CVREF
FVR
C2RSEL
C2ON(1)
Q3*RD_CM2CON0
EN
CL
NRESET
To PWM Logic
C2OE
C2VINC2VIN+
C2
C2OUT
C2OUT pin(2)
C2SP
C2POL
0
MUX
1
0
MUX
C2VREF
1
Note 1:
2:
3:
4:
DS41303B-page 270
When C2ON = 0, the C2 comparator will produce a 0 output to the XOR Gate.
Output shown for reference only. See I/O port pin block diagram for more detail.
Q1 and Q3 are phases of the four-phase system clock (FOSC).
Q1 is held high during Sleep mode.
Advance Information
PIC18F2XK20/4XK20
20.2
Comparator Control
20.2.5
Enable
Input selection
Reference selection
Output selection
Output polarity
Speed selection
20.2.1
TABLE 20-1:
COMPARATOR ENABLE
COMPARATOR OUTPUT
STATE VS. INPUT
CONDITIONS
Input Condition
CxPOL
CxOUT
20.2.2
20.2.3
COMPARATOR REFERENCE
SELECTION
20.2.4
COMPARATOR OUTPUT
SELECTION
20.2.6
The trade-off between speed or power can be optimized during program execution with the CxSP control
bit. The default state for this bit is 1 which selects the
normal speed mode. Device power consumption can
be optimized at the cost of slower comparator propagation delay by clearing the CxSP bit to 0.
20.3
Advance Information
DS41303B-page 271
PIC18F2XK20/4XK20
20.4
20.4.1
FIGURE 20-4:
COMPARATOR
INTERRUPT TIMING W/O
CMxCON0 READ
Q1
Q3
CxIN+
TRT
CxOUT
Set CxIF (edge)
CxIF
reset by software
FIGURE 20-5:
DS41303B-page 272
COMPARATOR
INTERRUPT TIMING WITH
CMxCON0 READ
Q1
Q3
CxIN+
TRT
CxOUT
CxIF
cleared by CMxCON0 read
reset by software
Advance Information
PIC18F2XK20/4XK20
20.5
20.6
Effects of a Reset
Advance Information
DS41303B-page 273
PIC18F2XK20/4XK20
REGISTER 20-1:
R/W-0
R-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
C1ON
C1OUT
C1OE
C1POL
C1SP
C1R
C1CH1
C1CH0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1-0
Note 1:
x = Bit is unknown
Comparator output requires the following three conditions: C1OE = 1, C1ON = 1 and corresponding port
TRIS bit = 0.
DS41303B-page 274
Advance Information
PIC18F2XK20/4XK20
REGISTER 20-2:
R/W-0
R-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
C2ON
C2OUT
C2OE
C2POL
C2SP
C2R
C2CH1
C2CH0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1-0
Note 1:
x = Bit is unknown
Comparator output requires the following three conditions: C2OE = 1, C2ON = 1 and corresponding port
TRIS bit = 0.
Advance Information
DS41303B-page 275
PIC18F2XK20/4XK20
20.7
FIGURE 20-6:
Rs < 10K
AIN
VA
CPIN
5 pF
VT 0.6V
RIC
ILEAKAGE(1)
Vss
Legend: CPIN
= Input Capacitance
ILEAKAGE = Leakage Current at the pin due to various junctions
RIC
= Interconnect Resistance
= Source Impedance
RS
= Analog Voltage
VA
VT
= Threshold Voltage
Note 1: See Section 26.0 Electrical Characteristics.
DS41303B-page 276
Advance Information
PIC18F2XK20/4XK20
20.8
20.8.2
20.8.1
SIMULTANEOUS COMPARATOR
OUTPUT READ
INTERNAL REFERENCE
SELECTION
REGISTER 20-3:
R-0
R-0
R/W-0
R/W-0
U-0
U-0
U-0
U-0
MC1OUT
MC2OUT
C1RSEL
C2RSEL
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7
bit 6
bit 5
bit 4
bit 3-0
Unimplemented: Read as 0
Advance Information
x = Bit is unknown
DS41303B-page 277
PIC18F2XK20/4XK20
TABLE 20-2:
Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
CM1CON0
C1ON
C1OUT
C1OE
C1POL
C1SP
C1R
C1CH1
C1CH0
60
CM2CON0
C2ON
C2OUT
C2OE
C2POL
C2SP
C2R
C2CH1
C2CH0
60
CM2CON1
MC1OUT
MC2OUT
C1RSEL
C2RSEL
61
CVRCON
CVREN
CVROE
CVRR
CVRSS
CVR3
CVR2
CVR1
CVR0
59
FVREN
FVRST
CVRCON2
INTCON
GIE/GIEH PEIE/GIEL
59
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
RA7(1)
RA6(1)
RA5
RA4
RA3
RA2
RA1
RA0
60
LATA
LATA7(1)
LATA6(1)
TRISA
TRISA7(1)
PORTA
60
60
Legend: = unimplemented, read as 0. Shaded cells are unused by the comparator module.
Note 1: PORTA<7:6> and their direction and latch bits are individually configured as port pins based on various
primary oscillator modes. When disabled, these bits read as 0.
DS41303B-page 278
Advance Information
PIC18F2XK20/4XK20
21.0
VOLTAGE REFERENCES
21.1
INDEPENDENT OPERATION
21.1.2
EQUATION 21-1:
21.1.4
21.1.1
21.1.3
21.1.5
21.1.6
21.1.7
EFFECTS OF A RESET
CV RR = 1 (low range):
CVREF = (VR<3:0>/24) CV RSRC
CV RR = 0 (high range):
CV REF = (CVRSRC/4) + (VR<3:0> CVRSRC/32)
CV RSRC = V DD or [(VREF+) - (VREF-)]
Advance Information
DS41303B-page 279
PIC18F2XK20/4XK20
21.2
21.2.1
FIGURE 21-1:
VREF+
VDD
8R
CVRSS = 0
CVR<3:0>
CVREN
16-to-1 MUX
R
R
16 Steps
CVREF
R
R
R
CVRR
8R
CVRSS = 1
VREF-
CVRSS = 0
FVREN
From HVLD and
BOR circuits
DS41303B-page 280
EN
Advance Information
FVR
FVRST
PIC18F2XK20/4XK20
FIGURE 21-2:
R(1)
Voltage
Reference
Output
Impedance
Note 1:
CVREF
R is dependent upon the voltage reference Configuration bits, CVR<3:0> and CVRR.
REGISTER 21-1:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CVREN
CVROE(1)
CVRR
CVRSS
CVR3
CVR2
CVR1
CVR0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5
bit 4
bit 3-0
Note 1:
Advance Information
DS41303B-page 281
PIC18F2XK20/4XK20
REGISTER 21-2:
R/W-0
R-0
U-0
U-0
U-0
U-0
U-0
U-0
FVREN
FVRST
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
bit 5-0
Unimplemented: Read as 0.
TABLE 21-1:
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
CVRCON
CVREN
CVROE
CVRR
CVRSS
CVR3
CVR2
CVR1
CVR0
60
CVRCON2
FVREN
FVRST
59
CM1CON0
C1ON
C1OUT
C1OE
C1POL
C1SP
C1R
C1CH1
C1CH0
60
CM2CON0
C2ON
C2OUT
C2OE
C2POL
C2SP
C2R
C2CH1
C2CH0
60
61
Name
CM2CON1
TRISA
60
Legend: Shaded cells are not used with the comparator voltage reference.
Note 1: PORTA pins are enabled based on oscillator configuration.
DS41303B-page 282
Advance Information
PIC18F2XK20/4XK20
22.0
HIGH/LOW-VOLTAGE
DETECT (HLVD)
REGISTER 22-1:
R/W-0
VDIRMAG
R-0
IRVST
R/W-0
R/W-0
R/W-1
R/W-0
R/W-1
HLVDEN
HLVDL3(1)
HLVDL2(1)
HLVDL1(1)
HLVDL0(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
U = Unimplemented
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 7
bit 6
Unimplemented: Read as 0
bit 5
bit 4
bit 3-0
Note 1:
Advance Information
DS41303B-page 283
PIC18F2XK20/4XK20
22.1
Operation
FIGURE 22-1:
Externally Generated
Trip Point
VDD
VDD
HLVDCON
Register
HLVDEN
HLVDIN
16-to-1 MUX
HLVDIN
HLVDL<3:0>
VDIRMAG
Set
HLVDIF
HLVDEN
BOREN
DS41303B-page 284
Internal Voltage
Reference
Advance Information
PIC18F2XK20/4XK20
22.2
HLVD Setup
5.
22.3
22.4
Current Consumption
FIGURE 22-2:
CASE 1:
Enable HLVD
TIVRST
IRVST
CASE 2:
VDD
VHLVD
HLVDIF
Enable HLVD
TIVRST
IRVST
Internal Reference is stable
Advance Information
DS41303B-page 285
PIC18F2XK20/4XK20
FIGURE 22-3:
CASE 1:
Enable HLVD
TIVRST
IRVST
HLVDIF
Enable HLVD
TIVRST
IRVST
Internal Reference is stable
DS41303B-page 286
Advance Information
PIC18F2XK20/4XK20
FIGURE 22-4:
Applications
VA
VB
TABLE 22-1:
TYPICAL LOW-VOLTAGE
DETECT APPLICATION
Voltage
22.5
Time
TA
TB
Name
Bit 7
Bit 6
HLVDCON
VDIRMAG
INTCON
GIE/GIEH PEIE/GIEL
Reset
Values
on Page
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
IRVST
HLVDEN
HLVDL3
HLVDL2
HLVDL1
HLVDL0
58
TMR0IE
INT0IE
RBIE
TMR0IF
INT0IF
RBIF
57
PIR2
OSCFIF
C1IF
C2IF
EEIF
BCLIF
HLVDIF
TMR3IF
CCP2IF
60
PIE2
OSCFIE
C1IE
C2IE
EEIE
BCLIE
HLVDIE
TMR3IE
CCP2IE
60
IPR2
OSCFIP
C1IP
C2IP
EEIP
BCLIP
HLVDIP
TMR3IP
CCP2IP
60
Legend: = unimplemented, read as 0. Shaded cells are unused by the HLVD module.
Advance Information
DS41303B-page 287
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 288
Advance Information
PIC18F2XK20/4XK20
23.0
SPECIAL FEATURES OF
THE CPU
Advance Information
DS41303B-page 289
PIC18F2XK20/4XK20
23.1
Configuration Bits
TABLE 23-1:
File Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
300001h
CONFIG1H
IESO
FCMEN
FOSC3
FOSC2
300002h
CONFIG2L
BORV1
BORV0
BOREN1
WDTPS3
WDTPS2
Bit 1
Bit 0
FOSC1
FOSC0
BOREN0 PWRTEN
Default/
Unprogrammed
Value
00-- 0111
---1 1111
WDTPS1 WDTPS0
WDTEN
CCP2MX
1--- 1011
STVREN
10-- -1-1
CP2(1)
CP1
CP0
---- 1111
11-- ----
WRT3(1)
WRT2(1)
WRT1
WRT0
---- 1111
111- ----
300003h
CONFIG2H
300005h
CONFIG3H MCLRE
300006h
CONFIG4L
DEBUG
XINST
LVP
300008h
CONFIG5L
CP3(1)
300009h
CONFIG5H
CPD
CPB
30000Ah
CONFIG6L
30000Bh
CONFIG6H
WRTD
WRTB
WRTC
(1)
(1)
---1 1111
30000Ch
CONFIG7L
EBTR1
EBTR0
---- 1111
30000Dh
CONFIG7H
EBTRB
-1-- ----
DEV2
DEV1
DEV0
REV4
REV3
REV2
REV1
REV0
qqqq qqqq(2)
DEV10
DEV9
DEV8
DEV7
DEV6
DEV5
DEV4
DEV3
0000 1100
3FFFFEh DEVID1(2)
(2)
EBTR3
EBTR2
3FFFFFh
DEVID2
Legend:
Note 1:
2:
DS41303B-page 290
Advance Information
PIC18F2XK20/4XK20
REGISTER 23-1:
R/P-0
R/P-0
U-0
U-0
R/P-0
R/P-1
R/P-1
R/P-1
IESO
FCMEN
FOSC3
FOSC2
FOSC1
FOSC0
bit 7
bit 0
Legend:
R = Readable bit
P = Programmable bit
bit 7
bit 6
bit 5-4
Unimplemented: Read as 0
bit 3-0
Advance Information
DS41303B-page 291
PIC18F2XK20/4XK20
REGISTER 23-2:
U-0
U-0
U-0
R/P-1
R/P-1
R/P-1
R/P-1
R/P-1
BORV1(1)
BORV0(1)
BOREN1(2)
BOREN0(2)
PWRTEN(2)
bit 7
bit 0
Legend:
R = Readable bit
P = Programmable bit
bit 7-5
Unimplemented: Read as 0
bit 4-3
bit 2-1
bit 0
Note
1:
2:
REGISTER 23-3:
U-0
U-0
U-0
R/P-1
R/P-1
R/P-1
R/P-1
R/P-1
WDTPS3
WDTPS2
WDTPS1
WDTPS0
WDTEN
bit 7
bit 0
Legend:
R = Readable bit
P = Programmable bit
bit 7-5
Unimplemented: Read as 0
bit 4-1
bit 0
DS41303B-page 292
Advance Information
PIC18F2XK20/4XK20
REGISTER 23-4:
R/P-1
U-0
U-0
U-0
R/P-1
R/P-0
R/P-1
R/P-1
MCLRE
HFOFST
LPT1OSC
PBADEN
CCP2MX
bit 7
bit 0
Legend:
R = Readable bit
P = Programmable bit
bit 7
bit 6-4
Unimplemented: Read as 0
bit 3
bit 2
bit 1
bit 0
REGISTER 23-5:
R/P-1
R/P-0
U-0
U-0
U-0
R/P-1
U-0
R/P-1
DEBUG
XINST
LVP
STVREN
bit 0
bit 7
Legend:
R = Readable bit
P = Programmable bit
bit 7
bit 6
bit 5-3
Unimplemented: Read as 0
bit 2
bit 1
Unimplemented: Read as 0
bit 0
Advance Information
DS41303B-page 293
PIC18F2XK20/4XK20
REGISTER 23-6:
U-0
U-0
U-0
R/C-1
R/C-1
(1)
(1)
CP3
CP2
R/C-1
R/C-1
CP1
CP0
bit 7
bit 0
Legend:
R = Readable bit
bit 7-4
Unimplemented: Read as 0
bit 3
bit 2
bit 1
bit 0
Note 1:
REGISTER 23-7:
R/C-1
R/C-1
U-0
U-0
U-0
U-0
U-0
U-0
CPD
CPB
bit 7
bit 0
Legend:
R = Readable bit
bit 7
bit 6
bit 5-0
Unimplemented: Read as 0
DS41303B-page 294
Advance Information
PIC18F2XK20/4XK20
REGISTER 23-8:
U-0
U-0
U-0
U-0
R/C-1
R/C-1
R/C-1
R/C-1
WRT3(1)
WRT2(1)
WRT1
WRT0
bit 7
bit 0
Legend:
R = Readable bit
bit 7-4
Unimplemented: Read as 0
bit 3
bit 2
bit 1
bit 0
Note 1:
REGISTER 23-9:
R/C-1
WRTD
R-1
U-0
U-0
U-0
U-0
U-0
WRTB
WRTC(1)
bit 7
bit 0
Legend:
R = Readable bit
bit 7
bit 6
bit 5
bit 4-0
Unimplemented: Read as 0
Note 1:
This bit is read-only in normal execution mode; it can be written only in Program mode.
Advance Information
DS41303B-page 295
PIC18F2XK20/4XK20
REGISTER 23-10: CONFIG7L: CONFIGURATION REGISTER 7 LOW
U-0
U-0
U-0
U-0
R/C-1
R/C-1
R/C-1
R/C-1
EBTR3(1)
EBTR2(1)
EBTR1
EBTR0
bit 7
bit 0
Legend:
R = Readable bit
bit 7-4
Unimplemented: Read as 0
bit 3
bit 2
bit 1
bit 0
Note 1:
R/C-1
U-0
U-0
U-0
U-0
U-0
U-0
EBTRB
bit 7
bit 0
Legend:
R = Readable bit
bit 7
Unimplemented: Read as 0
bit 6
bit 5-0
Unimplemented: Read as 0
DS41303B-page 296
Advance Information
PIC18F2XK20/4XK20
REGISTER 23-12: DEVID1: DEVICE ID REGISTER 1 FOR PIC18F2XK20/4XK20
R
DEV2
DEV1
DEV0
REV4
REV3
REV2
REV1
REV0
bit 7
bit 0
Legend:
R = Readable bit
bit 7-5
bit 4-0
DEV10
DEV9
DEV8
DEV7
DEV6
DEV5
DEV4
DEV3
bit 7
bit 0
Legend:
R = Readable bit
bit 7-0
Note 1:
Advance Information
DS41303B-page 297
PIC18F2XK20/4XK20
23.2
FIGURE 23-1:
SWDTEN
WDTEN
Enable WDT
WDT Counter
LFINTOSC Source
128
Wake-up
from Power
Managed Modes
CLRWDT
Reset
WDT
Reset
Sleep
DS41303B-page 298
Advance Information
PIC18F2XK20/4XK20
23.2.1
CONTROL REGISTER
U-0
U-0
U-0
U-0
U-0
U-0
R/W-0
SWDTEN(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 7-1
Unimplemented: Read as 0
bit 0
x = Bit is unknown
Note 1: This bit has no effect if the Configuration bit, WDTEN, is enabled.
TABLE 23-2:
Name
RCON
WDTCON
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
Reset
Values
on page
IPEN
SBOREN
RI
TO
PD
POR
BOR
56
SWDTEN
58
WDTEN
292
CONFIG2H
Legend: = unimplemented, read as 0. Shaded cells are not used by the Watchdog Timer.
Advance Information
DS41303B-page 299
PIC18F2XK20/4XK20
23.3
FIGURE 23-2:
8 Kbytes
(PIC18FX3K20)
16 Kbytes
(PIC18FX4K20)
32 Kbytes
(PIC18FX5K20)
64 Kbytes
(PIC18FX6K20)
Boot Block
(000h-1FFh)
Boot Block
(000h-7FFh)
Boot Block
(000h-7FFh)
Boot Block
(000h-7FFh)
Block 0
(200h-FFFh)
Block 0
(800h-1FFFh)
Block 0
(800h-1FFFh)
Block 0
(800h-3FFFh)
Block 1
(1000h-1FFFh)
Block 1
(2000h-3FFFh)
Block 1
(2000h-3FFFh)
Block 1
(4000h-7FFFh)
Block 2
(4000h-5FFFh)
Block 2
(8000h-BFFFh)
Block 3
(6000h-7FFFh)
Block 3
(C000h-FFFFh)
Unimplemented
Read 0s
(2000h-1FFFFFh)
Unimplemented
Read 0s
(4000h-1FFFFFh)
Unimplemented
Unimplemented
Read 0s
Read 0s
(8000h-1FFFFFh) (10000h-1FFFFFh)
TABLE 23-3:
(Unimplemented
Memory Space)
File Name
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
CP3(1)
CP2(1)
CP1
CP0
300008h
CONFIG5L
300009h
CONFIG5H
CPD
CPB
30000Ah
CONFIG6L
WRT3(1)
WRT2(1)
WRT1
WRT0
30000Bh
CONFIG6H
WRTD
WRTB
WRTC
30000Ch
CONFIG7L
EBTR3(1)
EBTR2(1)
EBTR1
EBTR0
30000Dh
CONFIG7H
EBTRB
DS41303B-page 300
Advance Information
PIC18F2XK20/4XK20
23.3.1
PROGRAM MEMORY
CODE PROTECTION
FIGURE 23-3:
Register Values
Program Memory
WRTB, EBTRB = 11
TBLPTR = 0008FFh
WRT0, EBTR0 = 01
PC = 001FFEh
TBLWT*
001FFFh
002000h
WRT1, EBTR1 = 11
003FFFh
004000h
PC = 005FFEh
WRT2, EBTR2 = 11
TBLWT*
005FFFh
006000h
WRT3, EBTR3 = 11
007FFFh
Results: All table writes disabled to Blockn whenever WRTn = 0.
Advance Information
DS41303B-page 301
PIC18F2XK20/4XK20
FIGURE 23-4:
Register Values
Program Memory
WRTB, EBTRB = 11
TBLPTR = 0008FFh
WRT0, EBTR0 = 10
001FFFh
002000h
PC = 003FFEh
TBLRD*
WRT1, EBTR1 = 11
003FFFh
004000h
WRT2, EBTR2 = 11
005FFFh
006000h
WRT3, EBTR3 = 11
007FFFh
Results: All table reads from external blocks to Blockn are disabled whenever EBTRn = 0.
TABLAT register returns a value of 0.
FIGURE 23-5:
Register Values
Program Memory
TBLPTR = 0008FFh
PC = 001FFEh
WRT0, EBTR0 = 10
TBLRD*
001FFFh
002000h
WRT1, EBTR1 = 11
003FFFh
004000h
WRT2, EBTR2 = 11
005FFFh
006000h
WRT3, EBTR3 = 11
007FFFh
DS41303B-page 302
Advance Information
PIC18F2XK20/4XK20
23.3.2
DATA EEPROM
CODE PROTECTION
23.3.3
CONFIGURATION REGISTER
PROTECTION
23.4
ID Locations
23.5
23.6
In-Circuit Debugger
TABLE 23-4:
DEBUGGER RESOURCES
I/O pins:
RB6, RB7
Stack:
2 levels
Program Memory:
512 bytes
Data Memory:
10 bytes
To use the In-Circuit Debugger function of the microcontroller, the design must implement In-Circuit Serial
Programming connections to the following pins:
MCLR/VPP/RE3
VDD
VSS
RB7
RB6
23.7
Advance Information
DS41303B-page 303
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 304
Advance Information
PIC18F2XK20/4XK20
24.0
24.1
Byte-oriented operations
Bit-oriented operations
Literal operations
Control operations
Advance Information
DS41303B-page 305
PIC18F2XK20/4XK20
TABLE 24-1:
Field
Description
bbb
BSR
C, DC, Z, OV, N
dest
Destination: either the WREG register or the specified register file location.
8-bit Register file address (00h to FFh) or 2-bit FSR designator (0h to 3h).
fs
12-bit Register file address (000h to FFFh). This is the source address.
fd
12-bit Register file address (000h to FFFh). This is the destination address.
GIE
Literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-bit value).
label
Label name.
mm
The mode of the TBLPTR register for the table read and table write instructions.
Only used with table read and table write instructions:
*+
*-
+*
n
The relative address (2s complement number) for relative branch instructions or the direct address for
CALL/BRANCH and RETURN instructions.
PC
Program Counter.
PCL
PCH
PCLATH
PCLATU
PD
Power-down bit.
PRODH
PRODL
TBLPTR
TABLAT
TO
Time-out bit.
TOS
Top-of-Stack.
Unused or unchanged.
WDT
Watchdog Timer.
WREG
Dont care (0 or 1). The assembler will generate code with x = 0. It is the recommended form of use for
compatibility with all Microchip software tools.
zs
zd
{
Optional argument.
[text]
(text)
[expr]<n>
Assigned to.
< >
italics
DS41303B-page 306
Advance Information
PIC18F2XK20/4XK20
FIGURE 24-1:
10
9 8 7
OPCODE d
a
Example Instruction
0
f (FILE #)
ADDWF MYREG, W, B
0
f (Source FILE #)
12 11
f (Destination FILE #)
1111
12 11
9 8 7
OPCODE b (BIT #) a
0
f (FILE #)
OPCODE
0
k (literal)
MOVLW 7Fh
8 7
OPCODE
15
0
n<7:0> (literal)
12 11
GOTO Label
0
n<19:8> (literal)
1111
8 7
OPCODE
15
0
CALL MYFUNC
n<7:0> (literal)
12 11
0
n<19:8> (literal)
1111
S = Fast bit
15
OPCODE
15
OPCODE
11 10
0
BRA MYFUNC
n<10:0> (literal)
8 7
n<7:0> (literal)
Advance Information
BC MYFUNC
DS41303B-page 307
PIC18F2XK20/4XK20
TABLE 24-2:
Mnemonic,
Operands
Cycles
MSb
LSb
Status
Affected
Notes
BYTE-ORIENTED OPERATIONS
ADDWF
ADDWFC
ANDWF
CLRF
COMF
CPFSEQ
CPFSGT
CPFSLT
DECF
DECFSZ
DCFSNZ
INCF
INCFSZ
INFSNZ
IORWF
MOVF
MOVFF
f, d, a
f, d, a
f, d, a
f, a
f, d, a
f, a
f, a
f, a
f, d, a
f, d, a
f, d, a
f, d, a
f, d, a
f, d, a
f, d, a
f, d, a
fs, fd
MOVWF
MULWF
NEGF
RLCF
RLNCF
RRCF
RRNCF
SETF
SUBFWB
f, a
f, a
f, a
f, d, a
f, d, a
f, d, a
f, d, a
f, a
f, d, a
SUBWF
SUBWFB
f, d, a
f, d, a
SWAPF
TSTFSZ
XORWF
f, d, a
f, a
f, d, a
Note 1:
When a PORT register is modified as a function of itself (e.g., MOVF PORTB, 1, 0), the value used will be that value
present on the pins themselves. For example, if the data latch is 1 for a pin configured as input and is driven low by an
external device, the data will be written back with a 0.
If this instruction is executed on the TMR0 register (and where applicable, d = 1), the prescaler will be cleared if
assigned.
If Program Counter (PC) is modified or a conditional test is true, the instruction requires two cycles. The second cycle is
executed as a NOP.
Some instructions are two-word instructions. The second word of these instructions will be executed as a NOP unless the
first word of the instruction retrieves the information embedded in these 16 bits. This ensures that all program memory
locations have a valid instruction.
2:
3:
4:
DS41303B-page 308
1
1
1
1
1
1 (2 or 3)
1 (2 or 3)
1 (2 or 3)
1
1 (2 or 3)
1 (2 or 3)
1
1 (2 or 3)
1 (2 or 3)
1
1
2
C, DC, Z, OV, N
C, DC, Z, OV, N
Z, N
Z
Z, N
None
None
None
C, DC, Z, OV, N
None
None
C, DC, Z, OV, N
None
None
Z, N
Z, N
None
1, 2
1, 2
1,2
2
1, 2
4
4
1, 2
1, 2, 3, 4
1, 2, 3, 4
1, 2
1, 2, 3, 4
4
1, 2
1, 2
1
1
1
1
1
1
1
1
1
1
0010 01da0
0010 0da
0001 01da
0110 101a
0001 11da
0110 001a
0110 010a
0110 000a
0000 01da
0010 11da
0100 11da
0010 10da
0011 11da
0100 10da
0001 00da
0101 00da
1100 ffff
1111 ffff
0110 111a
0000 001a
0110 110a
0011 01da
0100 01da
0011 00da
0100 00da
0110 100a
0101 01da
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
1
1
0101
0101
11da
10da
ffff
ffff
1, 2
1
1 (2 or 3)
1
0011
0110
0001
10da
011a
10da
ffff
ffff
ffff
ffff None
ffff None
ffff Z, N
4
1, 2
Advance Information
None
None
C, DC, Z, OV, N
C, Z, N
Z, N
C, Z, N
Z, N
None
C, DC, Z, OV, N
1, 2
1, 2
1, 2
PIC18F2XK20/4XK20
TABLE 24-2:
Mnemonic,
Operands
Description
Cycles
MSb
LSb
Status
Affected
Notes
BIT-ORIENTED OPERATIONS
BCF
BSF
BTFSC
BTFSS
BTG
f, b, a
f, b, a
f, b, a
f, b, a
f, d, a
Bit Clear f
Bit Set f
Bit Test f, Skip if Clear
Bit Test f, Skip if Set
Bit Toggle f
1
1
1 (2 or 3)
1 (2 or 3)
1
1001
1000
1011
1010
0111
bbba
bbba
bbba
bbba
bbba
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
ffff
None
None
None
None
None
1 (2)
1 (2)
1 (2)
1 (2)
1 (2)
1 (2)
1 (2)
2
1 (2)
2
0010
0110
0011
0111
0101
0001
0100
0nnn
0000
110s
kkkk
0000
0000
1111
kkkk
0000
xxxx
0000
0000
1nnn
0000
0000
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
kkkk
kkkk
0000
0000
kkkk
kkkk
0000
xxxx
0000
0000
nnnn
1111
0001
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
nnnn
kkkk
kkkk
0100
0111
kkkk
kkkk
0000
xxxx
0110
0101
nnnn
1111
000s
None
None
None
None
None
None
None
None
None
None
1
1
1
1
2
1
2
1110
1110
1110
1110
1110
1110
1110
1101
1110
1110
1111
0000
0000
1110
1111
0000
1111
0000
0000
1101
0000
0000
2
2
1
0000
0000
0000
1100
0000
0000
kkkk
0001
0000
1, 2
1, 2
3, 4
3, 4
1, 2
CONTROL OPERATIONS
BC
BN
BNC
BNN
BNOV
BNZ
BOV
BRA
BZ
CALL
n
n
n
n
n
n
n
n
n
n, s
CLRWDT
DAW
GOTO
NOP
NOP
POP
PUSH
RCALL
RESET
RETFIE
n
s
Branch if Carry
Branch if Negative
Branch if Not Carry
Branch if Not Negative
Branch if Not Overflow
Branch if Not Zero
Branch if Overflow
Branch Unconditionally
Branch if Zero
Call subroutine 1st word
2nd word
Clear Watchdog Timer
Decimal Adjust WREG
Go to address 1st word
2nd word
No Operation
No Operation
Pop top of return stack (TOS)
Push top of return stack (TOS)
Relative Call
Software device Reset
Return from interrupt enable
RETLW
RETURN
SLEEP
k
s
Note 1:
When a PORT register is modified as a function of itself (e.g., MOVF PORTB, 1, 0), the value used will be that value
present on the pins themselves. For example, if the data latch is 1 for a pin configured as input and is driven low by an
external device, the data will be written back with a 0.
If this instruction is executed on the TMR0 register (and where applicable, d = 1), the prescaler will be cleared if
assigned.
If Program Counter (PC) is modified or a conditional test is true, the instruction requires two cycles. The second cycle is
executed as a NOP.
Some instructions are two-word instructions. The second word of these instructions will be executed as a NOP unless the
first word of the instruction retrieves the information embedded in these 16 bits. This ensures that all program memory
locations have a valid instruction.
2:
3:
4:
1
1
2
Advance Information
TO, PD
C
None
None
None
None
None
None
All
GIE/GIEH,
PEIE/GIEL
kkkk None
001s None
0011 TO, PD
DS41303B-page 309
PIC18F2XK20/4XK20
TABLE 24-2:
Mnemonic,
Operands
Description
Cycles
MSb
LSb
Status
Affected
Notes
LITERAL OPERATIONS
ADDLW
ANDLW
IORLW
LFSR
k
k
k
f, k
MOVLB
MOVLW
MULLW
RETLW
SUBLW
XORLW
k
k
k
k
k
k
1
1
1
2
1
1
1
2
1
1
0000
0000
0000
1110
1111
0000
0000
0000
0000
0000
0000
1111
1011
1001
1110
0000
0001
1110
1101
1100
1000
1010
kkkk
kkkk
kkkk
00ff
kkkk
0000
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
kkkk
C, DC, Z, OV, N
Z, N
Z, N
None
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
1000
1001
1010
1011
1100
1101
1110
1111
None
None
None
None
None
None
None
None
None
None
None
None
C, DC, Z, OV, N
Z, N
2:
3:
4:
Table Read
Table Read with post-increment
Table Read with post-decrement
Table Read with pre-increment
Table Write
Table Write with post-increment
Table Write with post-decrement
Table Write with pre-increment
When a PORT register is modified as a function of itself (e.g., MOVF PORTB, 1, 0), the value used will be that value
present on the pins themselves. For example, if the data latch is 1 for a pin configured as input and is driven low by an
external device, the data will be written back with a 0.
If this instruction is executed on the TMR0 register (and where applicable, d = 1), the prescaler will be cleared if
assigned.
If Program Counter (PC) is modified or a conditional test is true, the instruction requires two cycles. The second cycle is
executed as a NOP.
Some instructions are two-word instructions. The second word of these instructions will be executed as a NOP unless the
first word of the instruction retrieves the information embedded in these 16 bits. This ensures that all program memory
locations have a valid instruction.
DS41303B-page 310
Advance Information
PIC18F2XK20/4XK20
24.1.1
ADDLW
ADD literal to W
ADDWF
ADD W to f
Syntax:
ADDLW
Syntax:
ADDWF
Operands:
0 f 255
d [0,1]
a [0,1]
Operation:
Status Affected:
N, OV, C, DC, Z
Operands:
0 k 255
Operation:
(W) + k W
Status Affected:
N, OV, C, DC, Z
Encoding:
0000
1111
kkkk
kkkk
Description:
Words:
Cycles:
Encoding:
0010
Q1
Q2
Q3
Q4
Read
literal k
Process
Data
Write to W
Example:
ADDLW
25h
ffff
Words:
Cycles:
Before Instruction
ffff
15h
W
= 10h
After Instruction
01da
Description:
Q Cycle Activity:
Decode
f {,d {,a}}
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example:
ADDWF
REG, 0, 0
Before Instruction
W
=
REG
=
After Instruction
W
REG
Note:
=
=
17h
0C2h
0D9h
0C2h
All PIC18 instructions may take an optional label argument preceding the instruction mnemonic for use in
symbolic addressing. If a label is used, the instruction format then becomes: {label} instruction argument(s).
Advance Information
DS41303B-page 311
PIC18F2XK20/4XK20
ADDWFC
ANDLW
Syntax:
ADDWFC
Syntax:
ANDLW
Operands:
0 f 255
d [0,1]
a [0,1]
Operands:
0 k 255
Operation:
(W) .AND. k W
Status Affected:
N, Z
f {,d {,a}}
Operation:
Status Affected:
N,OV, C, DC, Z
Encoding:
0010
Description:
00da
Encoding:
ffff
ffff
Add W, the CARRY flag and data memory location f. If d is 0, the result is
placed in W. If d is 1, the result is
placed in data memory location f.
If a is 0, the Access Bank is selected.
If a is 1, the BSR is used to select the
GPR bank (default).
If a is 0 and the extended instruction
set is enabled, this instruction operates
in Indexed Literal Offset Addressing
mode whenever f 95 (5Fh). See
Section 24.2.3 Byte-Oriented and
Bit-Oriented Instructions in Indexed
Literal Offset Mode for details.
Words:
Cycles:
0000
1011
kkkk
kkkk
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read literal
k
Process
Data
Write to W
Example:
ANDLW
05Fh
Before Instruction
W
=
After Instruction
W
A3h
03h
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example:
ADDWFC
Before Instruction
CARRY bit =
REG
=
W
=
After Instruction
CARRY bit =
REG
=
W
=
DS41303B-page 312
REG, 0, 1
1
02h
4Dh
0
02h
50h
Advance Information
PIC18F2XK20/4XK20
ANDWF
AND W with f
BC
Branch if Carry
Syntax:
ANDWF
Syntax:
BC
Operands:
0 f 255
d [0,1]
a [0,1]
Operands:
-128 n 127
Operation:
if CARRY bit is 1
(PC) + 2 + 2n PC
None
f {,d {,a}}
Operation:
Status Affected:
Status Affected:
N, Z
Encoding:
Encoding:
0001
Description:
01da
ffff
ffff
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example:
ANDWF
REG, 0, 0
Before Instruction
W
=
REG
=
After Instruction
W
REG
=
=
17h
C2h
02h
C2h
1110
Description:
0010
nnnn
nnnn
Words:
Cycles:
1(2)
Q Cycle Activity:
If Jump:
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
No
operation
No
operation
If No Jump:
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
No
operation
Example:
HERE
Before Instruction
PC
After Instruction
If CARRY
PC
If CARRY
PC
Advance Information
BC
address (HERE)
=
=
=
=
1;
address (HERE + 12)
0;
address (HERE + 2)
DS41303B-page 313
PIC18F2XK20/4XK20
BCF
Bit Clear f
BN
Branch if Negative
Syntax:
BCF
Syntax:
BN
Operands:
0 f 255
0b7
a [0,1]
Operands:
-128 n 127
Operation:
if NEGATIVE bit is 1
(PC) + 2 + 2n PC
None
f, b {,a}
Operation:
0 f<b>
Status Affected:
Status Affected:
None
Encoding:
Encoding:
1001
Description:
bbba
ffff
ffff
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
register f
Example:
BCF
Before Instruction
FLAG_REG =
After Instruction
FLAG_REG =
DS41303B-page 314
FLAG_REG,
7, 0
1110
Description:
0110
nnnn
nnnn
Words:
Cycles:
1(2)
Q Cycle Activity:
If Jump:
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
No
operation
No
operation
If No Jump:
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
No
operation
C7h
47h
Example:
HERE
Before Instruction
PC
After Instruction
If NEGATIVE
PC
If NEGATIVE
PC
Advance Information
BN
Jump
address (HERE)
=
=
=
=
1;
address (Jump)
0;
address (HERE + 2)
PIC18F2XK20/4XK20
BNC
BNN
Syntax:
BNC
Syntax:
BNN
Operands:
-128 n 127
Operands:
-128 n 127
Operation:
if CARRY bit is 0
(PC) + 2 + 2n PC
Operation:
if NEGATIVE bit is 0
(PC) + 2 + 2n PC
Status Affected:
None
Status Affected:
None
Encoding:
1110
Description:
0011
nnnn
nnnn
Encoding:
1110
Description:
Words:
Words:
Cycles:
1(2)
Cycles:
1(2)
Q Cycle Activity:
If Jump:
0111
nnnn
nnnn
Q Cycle Activity:
If Jump:
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
Write to PC
Decode
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
No
operation
Decode
Read literal
n
Process
Data
No
operation
If No Jump:
Example:
If No Jump:
HERE
Before Instruction
PC
After Instruction
If CARRY
PC
If CARRY
PC
BNC
Example:
Jump
address (HERE)
=
=
=
=
0;
address (Jump)
1;
address (HERE + 2)
HERE
Before Instruction
PC
After Instruction
If NEGATIVE
PC
If NEGATIVE
PC
Advance Information
BNN
Jump
address (HERE)
=
=
=
=
0;
address (Jump)
1;
address (HERE + 2)
DS41303B-page 315
PIC18F2XK20/4XK20
BNOV
BNZ
Syntax:
BNOV
Syntax:
BNZ
Operands:
-128 n 127
Operands:
-128 n 127
Operation:
if OVERFLOW bit is 0
(PC) + 2 + 2n PC
Operation:
if ZERO bit is 0
(PC) + 2 + 2n PC
Status Affected:
None
Status Affected:
None
Encoding:
1110
Description:
0101
nnnn
nnnn
Encoding:
1110
Description:
Words:
Words:
Cycles:
1(2)
Cycles:
1(2)
Q Cycle Activity:
If Jump:
0001
nnnn
nnnn
Q Cycle Activity:
If Jump:
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
Write to PC
Decode
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
No
operation
Decode
Read literal
n
Process
Data
No
operation
If No Jump:
If No Jump:
Example:
HERE
Before Instruction
PC
=
After Instruction
If OVERFLOW =
PC
=
If OVERFLOW =
PC
=
DS41303B-page 316
Example:
BNOV Jump
address (HERE)
0;
address (Jump)
1;
address (HERE + 2)
HERE
Before Instruction
PC
After Instruction
If ZERO
PC
If ZERO
PC
Advance Information
BNZ
Jump
address (HERE)
=
=
=
=
0;
address (Jump)
1;
address (HERE + 2)
PIC18F2XK20/4XK20
BRA
Unconditional Branch
BSF
Syntax:
BRA
Syntax:
BSF
Operands:
-1024 n 1023
Operands:
0 f 255
0b7
a [0,1]
Operation:
(PC) + 2 + 2n PC
Status Affected:
None
Encoding:
1101
Description:
0nnn
nnnn
nnnn
Words:
Cycles:
Bit Set f
Operation:
1 f<b>
Status Affected:
None
Encoding:
1000
Q1
Q2
Q3
Q4
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
No
operation
No
operation
Example:
bbba
ffff
ffff
Description:
Words:
Cycles:
Q Cycle Activity:
Decode
f, b {,a}
Q Cycle Activity:
HERE
Before Instruction
PC
After Instruction
PC
BRA
Jump
address (HERE)
address (Jump)
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
register f
Example:
BSF
Before Instruction
FLAG_REG
After Instruction
FLAG_REG
Advance Information
FLAG_REG, 7, 1
0Ah
8Ah
DS41303B-page 317
PIC18F2XK20/4XK20
BTFSC
BTFSS
Syntax:
BTFSC f, b {,a}
Syntax:
BTFSS f, b {,a}
Operands:
0 f 255
0b7
a [0,1]
Operands:
0 f 255
0b<7
a [0,1]
Operation:
skip if (f<b>) = 0
Operation:
skip if (f<b>) = 1
Status Affected:
None
Status Affected:
None
Encoding:
1011
bbba
ffff
ffff
Encoding:
1010
bbba
ffff
ffff
Description:
Description:
Words:
Words:
Cycles:
1(2)
Note: 3 cycles if skip and followed
by a 2-word instruction.
Cycles:
1(2)
Note:
Q Cycle Activity:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
No
operation
Decode
Read
register f
Process
Data
No
operation
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
If skip:
If skip:
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
Example:
HERE
FALSE
TRUE
Before Instruction
PC
After Instruction
If FLAG<1>
PC
If FLAG<1>
PC
DS41303B-page 318
BTFSC
:
:
FLAG, 1, 0
address (HERE)
=
=
=
=
0;
address (TRUE)
1;
address (FALSE)
Example:
HERE
FALSE
TRUE
Before Instruction
PC
After Instruction
If FLAG<1>
PC
If FLAG<1>
PC
Advance Information
BTFSS
:
:
FLAG, 1, 0
address (HERE)
=
=
=
=
0;
address (FALSE)
1;
address (TRUE)
PIC18F2XK20/4XK20
BTG
Bit Toggle f
BOV
Branch if Overflow
Syntax:
BTG f, b {,a}
Syntax:
BOV
Operands:
0 f 255
0b<7
a [0,1]
Operands:
-128 n 127
Operation:
if OVERFLOW bit is 1
(PC) + 2 + 2n PC
None
Operation:
(f<b>) f<b>
Status Affected:
Status Affected:
None
Encoding:
Encoding:
0111
Description:
Words:
Cycles:
bbba
ffff
ffff
1110
Description:
0100
nnnn
nnnn
Words:
Cycles:
1(2)
Q Cycle Activity:
If Jump:
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
No
operation
No
operation
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
register f
Example:
BTG
PORTC,
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
No
operation
4, 0
Before Instruction:
PORTC =
0111 0101 [75h]
After Instruction:
PORTC =
0110 0101 [65h]
If No Jump:
Example:
HERE
Before Instruction
PC
=
After Instruction
If OVERFLOW =
PC
=
If OVERFLOW =
PC
=
Advance Information
BOV
Jump
address (HERE)
1;
address (Jump)
0;
address (HERE + 2)
DS41303B-page 319
PIC18F2XK20/4XK20
BZ
Branch if Zero
CALL
Subroutine Call
Syntax:
BZ
Syntax:
CALL k {,s}
Operands:
-128 n 127
Operands:
Operation:
if ZERO bit is 1
(PC) + 2 + 2n PC
0 k 1048575
s [0,1]
Operation:
(PC) + 4 TOS,
k PC<20:1>,
if s = 1
(W) WS,
(Status) STATUSS,
(BSR) BSRS
Status Affected:
None
Status Affected:
None
Encoding:
1110
Description:
0000
nnnn
nnnn
Words:
Cycles:
1(2)
Encoding:
1st word (k<7:0>)
2nd word(k<19:8>)
Q1
Q2
Q3
Q4
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
No
operation
No
operation
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
No
operation
If No Jump:
Example:
HERE
Before Instruction
PC
After Instruction
If ZERO
PC
If ZERO
PC
DS41303B-page 320
BZ
k7kkk
kkkk
110s
k19kkk
Words:
Cycles:
Q Cycle Activity:
Q1
Decode
Q2
Q3
Q4
Jump
No
operation
address (HERE)
=
=
=
=
1;
address (Jump)
0;
address (HERE + 2)
kkkk0
kkkk8
Description:
Q Cycle Activity:
If Jump:
Decode
1110
1111
Example:
No
operation
HERE
Before Instruction
PC
=
After Instruction
PC
=
TOS
=
WS
=
BSRS
=
STATUSS =
Advance Information
No
operation
CALL
Read literal
k<19:8>,
Write to PC
No
operation
THERE, 1
address (HERE)
address (THERE)
address (HERE + 4)
W
BSR
Status
PIC18F2XK20/4XK20
CLRF
Clear f
Syntax:
CLRF
Operands:
0 f 255
a [0,1]
Operation:
000h f
1Z
Status Affected:
Encoding:
f {,a}
0110
Description:
101a
ffff
ffff
Words:
Cycles:
1
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
register f
CLRF
Before Instruction
FLAG_REG
After Instruction
FLAG_REG
Syntax:
CLRWDT
Operands:
None
Operation:
000h WDT,
000h WDT postscaler,
1 TO,
1 PD
Status Affected:
TO, PD
Encoding:
0000
FLAG_REG, 1
5Ah
00h
0000
0000
0100
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
No
operation
Process
Data
No
operation
Example:
Q Cycle Activity:
Example:
CLRWDT
CLRWDT
Before Instruction
WDT Counter
After Instruction
WDT Counter
WDT Postscaler
TO
PD
Advance Information
=
=
=
=
00h
0
1
1
DS41303B-page 321
PIC18F2XK20/4XK20
COMF
Complement f
CPFSEQ
Syntax:
COMF
Syntax:
CPFSEQ
Operands:
0 f 255
a [0,1]
Operation:
(f) (W),
skip if (f) = (W)
(unsigned comparison)
Status Affected:
None
f {,d {,a}}
0 f 255
d [0,1]
a [0,1]
Operands:
Operation:
(f) dest
Status Affected:
N, Z
Encoding:
0001
11da
ffff
ffff
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Encoding:
0110
Description:
f {,a}
001a
ffff
ffff
Words:
Cycles:
1(2)
Note: 3 cycles if skip and followed
by a 2-word instruction.
Q Cycle Activity:
Example:
COMF
Before Instruction
REG
=
After Instruction
REG
=
W
=
REG, 0, 0
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
No
operation
13h
If skip:
13h
ECh
Q1
Q2
Q3
No
No
No
operation
operation
operation
If skip and followed by 2-word instruction:
Q1
Q2
Q3
No
No
No
operation
operation
operation
No
No
No
operation
operation
operation
Example:
DS41303B-page 322
HERE
NEQUAL
EQUAL
Q4
No
operation
Q4
No
operation
No
operation
CPFSEQ REG, 0
:
:
Before Instruction
PC Address
W
REG
After Instruction
=
=
=
HERE
?
?
If REG
PC
If REG
PC
=
=
W;
Address (EQUAL)
W;
Address (NEQUAL)
Advance Information
PIC18F2XK20/4XK20
CPFSGT
CPFSLT
Syntax:
CPFSGT
Syntax:
CPFSLT
Operands:
0 f 255
a [0,1]
Operands:
0 f 255
a [0,1]
Operation:
(f) (W),
skip if (f) > (W)
(unsigned comparison)
Operation:
(f) (W),
skip if (f) < (W)
(unsigned comparison)
Status Affected:
None
Status Affected:
None
Encoding:
0110
Description:
Words:
f {,a}
010a
ffff
ffff
Encoding:
1(2)
Note: 3 cycles if skip and followed
by a 2-word instruction.
Q2
Read
register f
Q3
Process
Data
Q4
No
operation
Q1
Q2
Q3
No
No
No
operation
operation
operation
If skip and followed by 2-word instruction:
Q1
Q2
Q3
No
No
No
operation
operation
operation
No
No
No
operation
operation
operation
Q4
No
operation
Example:
HERE
NGREATER
GREATER
CPFSGT REG, 0
:
:
Before Instruction
PC
W
After Instruction
=
=
Address (HERE)
?
If REG
PC
If REG
PC
>
=
W;
Address (GREATER)
W;
Address (NGREATER)
ffff
ffff
Words:
Cycles:
1(2)
Note:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
No
operation
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
If skip:
If skip:
Q4
No
operation
No
operation
000a
Q Cycle Activity:
Q1
Decode
0110
Description:
Cycles:
f {,a}
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
Example:
HERE
NLESS
LESS
CPFSLT REG, 1
:
:
Before Instruction
PC
W
After Instruction
=
=
Address (HERE)
?
If REG
PC
If REG
PC
<
=
W;
Address (LESS)
W;
Address (NLESS)
Advance Information
DS41303B-page 323
PIC18F2XK20/4XK20
DAW
DECF
Decrement f
Syntax:
DAW
Syntax:
Operands:
None
Operands:
Operation:
0 f 255
d [0,1]
a [0,1]
Operation:
(f) 1 dest
Status Affected:
C, DC, N, OV, Z
Encoding:
(W<7:4>) + 6 + DC W<7:4> ;
Description:
Words:
Cycles:
0000
else
(W<7:4>) + DC W<7:4>
Status Affected:
Encoding:
0000
0000
0000
0111
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register W
Process
Data
Write
W
ffff
ffff
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example1:
DAW
01da
Before Instruction
W
=
C
=
DC
=
After Instruction
W
C
DC
Example 2:
=
=
=
A5h
0
0
05h
1
0
Example:
DECF
Before Instruction
CNT
=
Z
=
After Instruction
CNT
=
Z
=
CNT,
1, 0
01h
0
00h
1
Before Instruction
W
=
C
=
DC
=
After Instruction
W
C
DC
=
=
=
DS41303B-page 324
CEh
0
0
34h
1
0
Advance Information
PIC18F2XK20/4XK20
Decrement f, skip if 0
DCFSNZ
Syntax:
Syntax:
DCFSNZ
Operands:
0 f 255
d [0,1]
a [0,1]
Operands:
0 f 255
d [0,1]
a [0,1]
Operation:
(f) 1 dest,
skip if result = 0
Operation:
(f) 1 dest,
skip if result 0
Status Affected:
None
Status Affected:
None
DECFSZ
Encoding:
0010
Description:
11da
ffff
ffff
Words:
Cycles:
1(2)
Note: 3 cycles if skip and followed
by a 2-word instruction.
Encoding:
0100
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
HERE
DECFSZ
GOTO
Example:
CNT, 1, 1
LOOP
Address (HERE)
CNT - 1
0;
Address (CONTINUE)
0;
Address (HERE + 2)
ffff
Words:
Cycles:
1(2)
Note:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
If skip:
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
Example:
CONTINUE
Before Instruction
PC
=
After Instruction
CNT
=
If CNT
=
PC =
If CNT
PC =
ffff
If skip:
11da
Description:
Q Cycle Activity:
Q1
f {,d {,a}}
HERE
ZERO
NZERO
Before Instruction
TEMP
After Instruction
TEMP
If TEMP
PC
If TEMP
PC
Advance Information
DCFSNZ
:
:
TEMP, 1, 0
=
=
=
TEMP 1,
0;
Address (ZERO)
0;
Address (NZERO)
DS41303B-page 325
PIC18F2XK20/4XK20
GOTO
Unconditional Branch
INCF
Syntax:
GOTO k
Syntax:
INCF
Operands:
0 k 1048575
Operands:
Operation:
k PC<20:1>
Status Affected:
None
0 f 255
d [0,1]
a [0,1]
Operation:
(f) + 1 dest
Status Affected:
C, DC, N, OV, Z
Encoding:
1st word (k<7:0>)
2nd word(k<19:8>)
1110
1111
Description:
1111
k19kkk
k7kkk
kkkk
kkkk0
kkkk8
Increment f
Encoding:
0010
Cycles:
2
Q1
Q2
Q3
Q4
Read literal
k<7:0>,
No
operation
Read literal
k<19:8>,
Write to PC
No
operation
No
operation
No
operation
No
operation
ffff
ffff
Words:
Cycles:
Q Cycle Activity:
Decode
10da
Description:
f {,d {,a}}
Q Cycle Activity:
Example:
GOTO THERE
After Instruction
PC =
Address (THERE)
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example:
INCF
Before Instruction
CNT
=
Z
=
C
=
DC
=
After Instruction
CNT
=
Z
=
C
=
DC
=
DS41303B-page 326
Advance Information
CNT, 1, 0
FFh
0
?
?
00h
1
1
1
PIC18F2XK20/4XK20
INCFSZ
Increment f, skip if 0
INFSNZ
Syntax:
INCFSZ
Syntax:
INFSNZ
0 f 255
d [0,1]
a [0,1]
f {,d {,a}}
Operands:
0 f 255
d [0,1]
a [0,1]
Operands:
Operation:
(f) + 1 dest,
skip if result = 0
Operation:
(f) + 1 dest,
skip if result 0
Status Affected:
None
Status Affected:
None
Encoding:
0011
Description:
11da
ffff
ffff
Encoding:
0100
Description:
Words:
Words:
Cycles:
1(2)
Note: 3 cycles if skip and followed
by a 2-word instruction.
Cycles:
1(2)
Note:
Q Cycle Activity:
10da
ffff
ffff
Q Cycle Activity:
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Decode
Read
register f
Process
Data
Write to
destination
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
If skip:
If skip:
Q1
Q2
Q3
Q4
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
Example:
HERE
NZERO
ZERO
Before Instruction
PC
=
After Instruction
CNT
=
If CNT
=
PC
=
If CNT
PC
=
INCFSZ
:
:
Address (HERE)
CNT + 1
0;
Address (ZERO)
0;
Address (NZERO)
CNT, 1, 0
Example:
HERE
ZERO
NZERO
Before Instruction
PC
=
After Instruction
REG
=
If REG
PC
=
If REG
=
PC
=
Advance Information
INFSNZ
REG, 1, 0
Address (HERE)
REG + 1
0;
Address (NZERO)
0;
Address (ZERO)
DS41303B-page 327
PIC18F2XK20/4XK20
IORLW
IORWF
Syntax:
IORLW k
Syntax:
IORWF
Operands:
0 k 255
Operands:
Operation:
(W) .OR. k W
Status Affected:
N, Z
0 f 255
d [0,1]
a [0,1]
Operation:
Status Affected:
N, Z
Encoding:
0000
Description:
1001
kkkk
kkkk
Words:
Cycles:
Inclusive OR W with f
Encoding:
0001
Q1
Q2
Q3
Q4
Read
literal k
Process
Data
Write to W
Example:
IORLW
ffff
Words:
Cycles:
35h
9Ah
BFh
ffff
Before Instruction
W
=
After Instruction
00da
Description:
Q Cycle Activity:
Decode
f {,d {,a}}
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example:
IORWF
Before Instruction
RESULT =
W
=
After Instruction
RESULT =
W
=
DS41303B-page 328
Advance Information
RESULT, 0, 1
13h
91h
13h
93h
PIC18F2XK20/4XK20
LFSR
Load FSR
MOVF
Syntax:
LFSR f, k
Syntax:
MOVF
Operands:
0f2
0 k 4095
Operands:
Operation:
k FSRf
0 f 255
d [0,1]
a [0,1]
Status Affected:
None
Encoding:
1110
1111
1110
0000
00ff
k7kkk
k11kkk
kkkk
Description:
Words:
Cycles:
Move f
Operation:
f dest
Status Affected:
N, Z
Encoding:
0101
Q1
Q2
Q3
Q4
Read literal
k MSB
Process
Data
Write
literal k
MSB to
FSRfH
Decode
Read literal
k LSB
Process
Data
Write literal
k to FSRfL
Example:
After Instruction
FSR2H
FSR2L
03h
ABh
ffff
ffff
Words:
Cycles:
LFSR 2, 3ABh
=
=
00da
Description:
Q Cycle Activity:
Decode
f {,d {,a}}
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write W
Example:
MOVF
Before Instruction
REG
W
After Instruction
REG
W
Advance Information
REG, 0, 0
=
=
22h
FFh
=
=
22h
22h
DS41303B-page 329
PIC18F2XK20/4XK20
MOVFF
Move f to f
MOVLB
Syntax:
MOVFF fs,fd
Syntax:
MOVLW k
Operands:
0 fs 4095
0 fd 4095
Operands:
0 k 255
Operation:
(fs) fd
k BSR
Operation:
Status Affected:
None
Status Affected:
None
Encoding:
Encoding:
1st word (source)
2nd word (destin.)
1100
1111
Description:
ffff
ffff
ffff
ffff
ffffs
ffffd
Words:
Cycles:
2 (3)
0000
0001
kkkk
kkkk
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
literal k
Process
Data
Write literal
k to BSR
MOVLB
Example:
Before Instruction
BSR Register =
After Instruction
BSR Register =
02h
05h
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
(src)
Process
Data
No
operation
Decode
No
operation
No
operation
Write
register f
(dest)
No dummy
read
Example:
MOVFF
Before Instruction
REG1
REG2
After Instruction
REG1
REG2
DS41303B-page 330
REG1, REG2
=
=
33h
11h
=
=
33h
33h
Advance Information
PIC18F2XK20/4XK20
MOVLW
Move literal to W
MOVWF
Syntax:
MOVLW k
Syntax:
MOVWF
Operands:
0 k 255
Operands:
0 f 255
a [0,1]
Operation:
(W) f
Status Affected:
None
Operation:
kW
Status Affected:
None
Encoding:
0000
1110
kkkk
kkkk
Description:
Words:
Cycles:
Move W to f
Encoding:
0110
Q1
Q2
Q3
Q4
Read
literal k
Process
Data
Write to W
Example:
MOVLW
=
ffff
ffff
Words:
Cycles:
5Ah
After Instruction
W
111a
Description:
Q Cycle Activity:
Decode
f {,a}
5Ah
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
register f
Example:
MOVWF
REG, 0
Before Instruction
W
=
REG
=
After Instruction
W
REG
Advance Information
=
=
4Fh
FFh
4Fh
4Fh
DS41303B-page 331
PIC18F2XK20/4XK20
MULLW
MULWF
Syntax:
MULLW
Syntax:
MULWF
Operands:
0 k 255
Operands:
Operation:
(W) x k PRODH:PRODL
0 f 255
a [0,1]
Status Affected:
None
Operation:
Status Affected:
None
Encoding:
0000
Description:
1101
kkkk
kkkk
Words:
Cycles:
Multiply W with f
Encoding:
0000
Q1
Q2
Q3
Q4
Read
literal k
Process
Data
Write
registers
PRODH:
PRODL
Example:
MULLW
W
PRODH
PRODL
E2h
?
?
=
=
=
E2h
ADh
08h
ffff
Words:
Cycles:
0C4h
=
=
=
ffff
Before Instruction
W
PRODH
PRODL
After Instruction
001a
Description:
Q Cycle Activity:
Decode
f {,a}
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
registers
PRODH:
PRODL
Example:
MULWF
REG, 1
Before Instruction
W
REG
PRODH
PRODL
After Instruction
W
REG
PRODH
PRODL
DS41303B-page 332
Advance Information
=
=
=
=
C4h
B5h
?
?
=
=
=
=
C4h
B5h
8Ah
94h
PIC18F2XK20/4XK20
NEGF
Negate f
NOP
No Operation
Syntax:
NEGF
Syntax:
NOP
Operands:
0 f 255
a [0,1]
Operands:
None
Operation:
(f)+1f
Status Affected:
N, OV, C, DC, Z
Encoding:
f {,a}
0110
Description:
Cycles:
No operation
Status Affected:
None
Encoding:
110a
ffff
0000
1111
ffff
Words:
Operation:
0000
xxxx
Description:
No operation.
Words:
Cycles:
0000
xxxx
0000
xxxx
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
No
operation
No
operation
No
operation
Example:
None.
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
register f
Example:
NEGF
Before Instruction
REG
=
After Instruction
REG
=
REG, 1
Advance Information
DS41303B-page 333
PIC18F2XK20/4XK20
POP
PUSH
Syntax:
POP
Syntax:
PUSH
Operands:
None
Operands:
None
Operation:
Operation:
(PC + 2) TOS
Status Affected:
None
Status Affected:
None
Encoding:
0000
0000
0000
0110
Description:
Words:
Cycles:
Encoding:
Q2
Q3
Q4
Decode
No
operation
POP TOS
value
No
operation
POP
GOTO
NEW
Before Instruction
TOS
Stack (1 level down)
DS41303B-page 334
0000
0101
Words:
Cycles:
Q Cycle Activity:
Q1
After Instruction
TOS
PC
0000
Q Cycle Activity:
Example:
0000
Description:
Q1
Q2
Q3
Q4
Decode
PUSH
PC + 2 onto
return stack
No
operation
No
operation
Example:
=
=
=
=
0031A2h
014332h
014332h
NEW
PUSH
Before Instruction
TOS
PC
=
=
345Ah
0124h
After Instruction
PC
TOS
Stack (1 level down)
=
=
=
0126h
0126h
345Ah
Advance Information
PIC18F2XK20/4XK20
RCALL
Relative Call
RESET
Reset
Syntax:
RCALL
Syntax:
RESET
Operands:
-1024 n 1023
Operands:
None
Operation:
(PC) + 2 TOS,
(PC) + 2 + 2n PC
Operation:
Status Affected:
None
Status Affected:
All
Encoding:
1101
Description:
1nnn
nnnn
nnnn
Words:
Cycles:
Encoding:
0000
Q1
Q2
Q3
Q4
Decode
Read literal
n
Process
Data
Write to PC
No
operation
No
operation
1111
1111
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Start
Reset
No
operation
No
operation
Example:
Q Cycle Activity:
0000
After Instruction
Registers =
Flags*
=
RESET
Reset Value
Reset Value
PUSH PC to
stack
No
operation
Example:
No
operation
HERE
RCALL Jump
Before Instruction
PC =
Address (HERE)
After Instruction
PC =
Address (Jump)
TOS =
Address (HERE + 2)
Advance Information
DS41303B-page 335
PIC18F2XK20/4XK20
RETFIE
RETLW
Return literal to W
Syntax:
RETFIE {s}
Syntax:
RETLW k
Operands:
s [0,1]
Operands:
0 k 255
Operation:
(TOS) PC,
1 GIE/GIEH or PEIE/GIEL,
if s = 1
(WS) W,
(STATUSS) Status,
(BSRS) BSR,
PCLATU, PCLATH are unchanged.
Operation:
k W,
(TOS) PC,
PCLATU, PCLATH are unchanged
Status Affected:
None
Status Affected:
0000
0000
0001
Cycles:
Q Cycle Activity:
Q2
Q3
Q4
Decode
No
operation
No
operation
POP PC
from stack
Set GIEH or
GIEL
No
operation
RETFIE
After Interrupt
PC
W
BSR
Status
GIE/GIEH, PEIE/GIEL
DS41303B-page 336
kkkk
kkkk
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
literal k
Process
Data
POP PC
from stack,
Write to W
No
operation
No
operation
No
operation
No
operation
Example:
Q1
Example:
1100
000s
Words:
No
operation
0000
Description:
GIE/GIEH, PEIE/GIEL.
Encoding:
Description:
Encoding:
No
operation
No
operation
1
=
=
=
=
=
TOS
WS
BSRS
STATUSS
1
CALL TABLE ;
;
;
;
:
TABLE
ADDWF PCL ;
RETLW k0
;
RETLW k1
;
:
:
RETLW kn
;
Before Instruction
W
=
After Instruction
W
=
Advance Information
W contains table
offset value
W now has
table value
W = offset
Begin table
End of table
07h
value of kn
PIC18F2XK20/4XK20
RETURN
RLCF
Syntax:
RETURN {s}
Syntax:
RLCF
Operands:
s [0,1]
Operands:
Operation:
(TOS) PC,
if s = 1
(WS) W,
(STATUSS) Status,
(BSRS) BSR,
PCLATU, PCLATH are unchanged
0 f 255
d [0,1]
a [0,1]
Operation:
Status Affected:
C, N, Z
Status Affected:
None
Encoding:
0000
Encoding:
0000
0001
001s
Description:
Words:
Cycles:
0011
Description:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
No
operation
Process
Data
POP PC
from stack
No
operation
No
operation
No
operation
No
operation
f {,d {,a}}
01da
ffff
ffff
C
Words:
Cycles:
Q Cycle Activity:
Example:
RETURN
After Instruction:
PC = TOS
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example:
Before Instruction
REG
=
C
=
After Instruction
REG
=
W
=
C
=
Advance Information
RLCF
REG, 0, 0
1110 0110
0
1110 0110
1100 1100
1
DS41303B-page 337
PIC18F2XK20/4XK20
RLNCF
RRCF
Syntax:
RLNCF
Syntax:
RRCF
Operands:
0 f 255
d [0,1]
a [0,1]
Operands:
0 f 255
d [0,1]
a [0,1]
Operation:
Operation:
Status Affected:
N, Z
Status Affected:
C, N, Z
Encoding:
0100
Description:
f {,d {,a}}
01da
ffff
ffff
Encoding:
0011
Description:
register f
Words:
Cycles:
1
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Before Instruction
REG
=
After Instruction
REG
=
DS41303B-page 338
00da
RLNCF
Words:
Cycles:
0101 0111
ffff
register f
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
RRCF
REG, 0, 0
REG, 1, 0
1010 1011
ffff
Q Cycle Activity:
Example:
f {,d {,a}}
Example:
Before Instruction
REG
=
C
=
After Instruction
REG
=
W
=
C
=
Advance Information
1110 0110
0
1110 0110
0111 0011
0
PIC18F2XK20/4XK20
RRNCF
SETF
Syntax:
RRNCF
Syntax:
SETF
Operands:
0 f 255
d [0,1]
a [0,1]
Operands:
0 f 255
a [0,1]
Operation:
FFh f
Operation:
Status Affected:
None
Status Affected:
f {,d {,a}}
Encoding:
N, Z
Encoding:
0100
Description:
00da
ffff
ffff
Words:
Cycles:
1
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
RRNCF
Before Instruction
REG
=
After Instruction
REG
=
Example 2:
f {,a}
0110
100a
ffff
ffff
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write
register f
Example:
Q Cycle Activity:
Example 1:
Set f
SETF
Before Instruction
REG
After Instruction
REG
REG, 1
5Ah
FFh
REG, 1, 0
1101 0111
1110 1011
RRNCF
REG, 0, 0
Before Instruction
W
=
REG
=
After Instruction
?
1101 0111
=
=
1110 1011
1101 0111
W
REG
Advance Information
DS41303B-page 339
PIC18F2XK20/4XK20
SLEEP
SUBFWB
Syntax:
SLEEP
Syntax:
SUBFWB
Operands:
None
Operands:
Operation:
00h WDT,
0 WDT postscaler,
1 TO,
0 PD
0 f 255
d [0,1]
a [0,1]
Operation:
Status Affected:
N, OV, C, DC, Z
Status Affected:
TO, PD
Encoding:
0000
Encoding:
0000
0000
0011
Description:
Words:
Cycles:
0101
Q1
Q2
Q3
Q4
Decode
No
operation
Process
Data
Go to
Sleep
SLEEP
Before Instruction
TO =
?
PD =
?
After Instruction
1
TO =
0
PD =
If WDT causes wake-up, this bit is cleared.
DS41303B-page 340
f {,d {,a}}
01da
ffff
ffff
Description:
Words:
Cycles:
Q Cycle Activity:
Example:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
SUBFWB
REG, 1, 0
Example 1:
Before Instruction
REG
=
3
W
=
2
C
=
1
After Instruction
REG
=
FF
W
=
2
C
=
0
Z
=
0
N
=
1 ; result is negative
SUBFWB
REG, 0, 0
Example 2:
Before Instruction
REG
=
2
W
=
5
C
=
1
After Instruction
REG
=
2
W
=
3
C
=
1
Z
=
0
N
=
0 ; result is positive
SUBFWB
REG, 1, 0
Example 3:
Before Instruction
REG
=
1
W
=
2
C
=
0
After Instruction
REG
=
0
W
=
2
C
=
1
Z
=
1 ; result is zero
N
=
0
Advance Information
PIC18F2XK20/4XK20
SUBLW
SUBWF
Syntax:
SUBLW k
Syntax:
SUBWF
Operands:
0 k 255
Operands:
Operation:
k (W) W
Status Affected:
N, OV, C, DC, Z
0 f 255
d [0,1]
a [0,1]
Operation:
Status Affected:
N, OV, C, DC, Z
Encoding:
0000
Description
1000
kkkk
kkkk
Words:
Cycles:
Subtract W from f
Encoding:
0101
Q2
Q3
Q4
Decode
Read
literal k
Process
Data
Write to W
Example 1:
Before Instruction
W
=
C
=
After Instruction
W
=
C
=
Z
=
N
=
Example 2:
Before Instruction
W
=
C
=
After Instruction
W
=
C
=
Z
=
N
=
Example 3:
Before Instruction
W
=
C
=
After Instruction
W
=
C
=
Z
=
N
=
SUBLW
Cycles:
02h
Q Cycle Activity:
02h
?
00h
1
; result is zero
1
0
SUBLW
ffff
Words:
01h
?
SUBLW
ffff
02h
01h
1
; result is positive
0
0
11da
Description:
Q Cycle Activity:
Q1
f {,d {,a}}
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
SUBWF
REG, 1, 0
Example 1:
02h
03h
?
FFh ; (2s complement)
0
; result is negative
0
1
Before Instruction
REG
=
W
=
C
=
After Instruction
REG
=
W
=
C
=
Z
=
N
=
Example 2:
Before Instruction
REG
=
W
=
C
=
After Instruction
REG
=
W
=
C
=
Z
=
N
=
Example 3:
Before Instruction
REG
=
W
=
C
=
After Instruction
REG
=
W
=
C
=
Z
=
N
=
Advance Information
3
2
?
1
2
1
0
0
; result is positive
SUBWF
REG, 0, 0
2
2
?
2
0
1
1
0
SUBWF
; result is zero
REG, 1, 0
1
2
?
FFh ;(2s complement)
2
0
; result is negative
0
1
DS41303B-page 341
PIC18F2XK20/4XK20
SUBWFB
SWAPF
Swap f
Syntax:
SUBWFB
Syntax:
Operands:
0 f 255
d [0,1]
a [0,1]
Operands:
0 f 255
d [0,1]
a [0,1]
Operation:
Operation:
Status Affected:
N, OV, C, DC, Z
(f<3:0>) dest<7:4>,
(f<7:4>) dest<3:0>
Status Affected:
None
Encoding:
0101
Description:
f {,d {,a}}
10da
ffff
ffff
Words:
Cycles:
Q Cycle Activity:
Q1
Decode
Q2
Read
register f
Example 1:
SUBWFB
Before Instruction
REG
=
W
=
C
=
After Instruction
REG
=
W
=
C
=
Z
=
N
=
Example 2:
Q4
Write to
destination
0011
(0001 1001)
(0000 1101)
0Ch
0Dh
1
0
0
(0000 1011)
(0000 1101)
ffff
ffff
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
REG, 1, 0
19h
0Dh
1
10da
Example:
SWAPF
Before Instruction
REG
=
After Instruction
REG
=
REG, 1, 0
53h
35h
; result is positive
SUBWFB REG, 0, 0
Before Instruction
REG
=
W
=
C
=
After Instruction
REG
=
W
=
C
=
Z
=
N
=
Example 3:
1Bh
1Ah
0
(0001 1011)
(0001 1010)
1Bh
00h
1
1
0
(0001 1011)
SUBWFB
Before Instruction
REG
=
W
=
C
=
After Instruction
REG
=
W
C
Z
N
Q3
Process
Data
Encoding:
=
=
=
=
DS41303B-page 342
; result is zero
REG, 1, 0
03h
0Eh
1
(0000 0011)
(0000 1101)
F5h
(1111 0100)
; [2s comp]
(0000 1101)
0Eh
0
0
1
; result is negative
Advance Information
PIC18F2XK20/4XK20
TBLRD
Table Read
TBLRD
Syntax:
Example1:
TBLRD
Operands:
None
Operation:
if TBLRD *,
(Prog Mem (TBLPTR)) TABLAT;
TBLPTR No Change;
if TBLRD *+,
(Prog Mem (TBLPTR)) TABLAT;
(TBLPTR) + 1 TBLPTR;
if TBLRD *-,
(Prog Mem (TBLPTR)) TABLAT;
(TBLPTR) 1 TBLPTR;
if TBLRD +*,
(TBLPTR) + 1 TBLPTR;
(Prog Mem (TBLPTR)) TABLAT;
Example2:
0000
0000
0000
TBLRD
=
=
=
55h
00A356h
34h
=
=
34h
00A357h
+* ;
Before Instruction
TABLAT
TBLPTR
MEMORY (01A357h)
MEMORY (01A358h)
After Instruction
TABLAT
TBLPTR
*+ ;
Before Instruction
TABLAT
TBLPTR
MEMORY (00A356h)
After Instruction
TABLAT
TBLPTR
=
=
=
=
AAh
01A357h
12h
34h
=
=
34h
01A358h
10nn
nn=0 *
=1 *+
=2 *=3 +*
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
No
operation
No
operation
No
operation
No
operation
No operation
(Read Program
Memory)
No
operation
No operation
(Write TABLAT)
Advance Information
DS41303B-page 343
PIC18F2XK20/4XK20
TBLWT
Table Write
TBLWT
Syntax:
Example1:
TBLWT *+;
Operands:
None
Operation:
if TBLWT*,
(TABLAT) Holding Register;
TBLPTR No Change;
if TBLWT*+,
(TABLAT) Holding Register;
(TBLPTR) + 1 TBLPTR;
if TBLWT*-,
(TABLAT) Holding Register;
(TBLPTR) 1 TBLPTR;
if TBLWT+*,
(TBLPTR) + 1 TBLPTR;
(TABLAT) Holding Register;
Status Affected:
Before Instruction
TABLAT
=
55h
TBLPTR
=
00A356h
HOLDING REGISTER
(00A356h)
=
FFh
After Instructions (table write completion)
TABLAT
=
55h
TBLPTR
=
00A357h
HOLDING REGISTER
(00A356h)
=
55h
Example 2:
None
Encoding:
0000
0000
0000
11nn
nn=0 *
=1 *+
=2 *=3 +*
Description:
Words:
Cycles:
TBLWT +*;
Before Instruction
TABLAT
=
34h
TBLPTR
=
01389Ah
HOLDING REGISTER
(01389Ah)
=
FFh
HOLDING REGISTER
(01389Bh)
=
FFh
After Instruction (table write completion)
TABLAT
=
34h
TBLPTR
=
01389Bh
HOLDING REGISTER
(01389Ah)
=
FFh
HOLDING REGISTER
(01389Bh)
=
34h
Q Cycle Activity:
Q1
Decode
Q2
Q3
Q4
No
No
No
operation operation operation
No
No
No
No
operation operation operation operation
(Read
(Write to
TABLAT)
Holding
Register )
DS41303B-page 344
Advance Information
PIC18F2XK20/4XK20
TSTFSZ
Test f, skip if 0
XORLW
Syntax:
TSTFSZ f {,a}
Syntax:
XORLW k
Operands:
0 f 255
a [0,1]
Operands:
0 k 255
Operation:
(W) .XOR. k W
Operation:
skip if f = 0
Status Affected:
N, Z
Status Affected:
None
Encoding:
Encoding:
0110
Description:
011a
ffff
ffff
Words:
Cycles:
1(2)
Note: 3 cycles if skip and followed
by a 2-word instruction.
0000
1010
kkkk
kkkk
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
literal k
Process
Data
Write to W
Example:
XORLW
0AFh
Before Instruction
W
=
After Instruction
W
B5h
1Ah
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
No
operation
Q1
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
If skip:
Q2
Q3
Q4
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
No
operation
Example:
HERE
NZERO
ZERO
Before Instruction
PC
After Instruction
If CNT
PC
If CNT
PC
TSTFSZ
:
:
CNT, 1
Address (HERE)
=
=
00h,
Address (ZERO)
00h,
Address (NZERO)
Advance Information
DS41303B-page 345
PIC18F2XK20/4XK20
XORWF
Exclusive OR W with f
Syntax:
XORWF
Operands:
0 f 255
d [0,1]
a [0,1]
Operation:
Status Affected:
N, Z
Encoding:
0001
f {,d {,a}}
10da
ffff
ffff
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
Example:
XORWF
Before Instruction
REG
=
W
=
After Instruction
REG
=
W
=
DS41303B-page 346
REG, 1, 0
AFh
B5h
1Ah
B5h
Advance Information
PIC18F2XK20/4XK20
24.2
A summary of the instructions in the extended instruction set is provided in Table 24-3. Detailed descriptions
are provided in Section 24.2.2 Extended Instruction
Set. The opcode field descriptions in Table 24-1
(page 306) apply to both the standard and extended
PIC18 instruction sets.
Note:
24.2.1
TABLE 24-3:
Note:
Mnemonic,
Operands
ADDFSR
ADDULNK
CALLW
MOVSF
f, k
k
MOVSS
zs, zd
PUSHL
SUBFSR
SUBULNK
f, k
k
zs, fd
Description
Cycles
MSb
1
2
2
2
LSb
Status
Affected
1000
1000
0000
1011
ffff
1011
xxxx
1010
ffkk
11kk
0001
0zzz
ffff
1zzz
xzzz
kkkk
kkkk
kkkk
0100
zzzz
ffff
zzzz
zzzz
kkkk
None
None
None
None
1110
1110
0000
1110
1111
1110
1111
1110
1
2
1110
1110
1001
1001
ffkk
11kk
kkkk
kkkk
None
None
Advance Information
None
None
DS41303B-page 347
PIC18F2XK20/4XK20
24.2.2
ADDFSR
ADDULNK
Syntax:
ADDFSR f, k
Syntax:
ADDULNK k
Operands:
0 k 63
f [ 0, 1, 2 ]
Operands:
0 k 63
Operation:
FSR(f) + k FSR(f)
Status Affected:
None
Encoding:
1110
FSR2 + k FSR2,
Operation:
(TOS) PC
Status Affected:
1000
ffkk
kkkk
Description:
Words:
Cycles:
None
Encoding:
1110
Q1
Q2
Q3
Q4
Read
literal k
Process
Data
Write to
FSR
Example:
ADDFSR 2, 23h
Before Instruction
FSR2
=
03FFh
After Instruction
FSR2
=
0422h
kkkk
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read
literal k
Process
Data
Write to
FSR
No
Operation
No
Operation
No
Operation
No
Operation
Example:
Note:
11kk
Q Cycle Activity:
Decode
1000
Description:
ADDULNK 23h
Before Instruction
FSR2
=
PC
=
03FFh
0100h
After Instruction
FSR2
=
PC
=
0422h
(TOS)
All PIC18 instructions may take an optional label argument preceding the instruction mnemonic for use in
symbolic addressing. If a label is used, the instruction syntax then becomes: {label} instruction argument(s).
DS41303B-page 348
Advance Information
PIC18F2XK20/4XK20
CALLW
MOVSF
Syntax:
CALLW
Syntax:
MOVSF [zs], fd
Operands:
None
Operands:
Operation:
(PC + 2) TOS,
(W) PCL,
(PCLATH) PCH,
(PCLATU) PCU
0 zs 127
0 fd 4095
Operation:
((FSR2) + zs) fd
Status Affected:
None
Status Affected:
None
Encoding:
0000
0000
0001
0100
Description
Words:
Cycles:
Move Indexed to f
Encoding:
1st word (source)
2nd word (destin.)
Q1
Q2
Q3
Q4
Read
WREG
PUSH PC to
stack
No
operation
No
operation
No
operation
No
operation
No
operation
HERE
Before Instruction
PC
=
PCLATH =
PCLATU =
W
=
After Instruction
PC
=
TOS
=
PCLATH =
PCLATU =
W
=
Cycles:
Q Cycle Activity:
Q1
Decode
address (HERE)
10h
00h
06h
zzzzs
ffffd
Words:
CALLW
001006h
address (HERE + 2)
10h
00h
06h
0zzz
ffff
Decode
Example:
1011
ffff
Description:
Q Cycle Activity:
Decode
1110
1111
Q2
Q3
Determine
Determine
source addr source addr
No
operation
No
operation
No dummy
read
Example:
MOVSF
Before Instruction
FSR2
Contents
of 85h
REG2
After Instruction
FSR2
Contents
of 85h
REG2
Advance Information
Q4
Read
source reg
Write
register f
(dest)
[05h], REG2
80h
=
=
33h
11h
80h
=
=
33h
33h
DS41303B-page 349
PIC18F2XK20/4XK20
MOVSS
PUSHL
Syntax:
Syntax:
PUSHL k
Operands:
Operands:
0 k 255
Operation:
Operation:
k (FSR2),
FSR2 1 FSR2
Status Affected:
None
Status Affected:
None
Encoding:
1st word (source)
2nd word (dest.)
1110
1111
Description
1011
xxxx
1zzz
xzzz
zzzzs
zzzzd
Words:
Cycles:
Encoding:
1111
1010
kkkk
kkkk
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read k
Process
data
Write to
destination
Example:
PUSHL 08h
Before Instruction
FSR2H:FSR2L
Memory (01ECh)
=
=
01ECh
00h
After Instruction
FSR2H:FSR2L
Memory (01ECh)
=
=
01EBh
08h
Q Cycle Activity:
Q1
Decode
Decode
Q2
Q3
Determine
Determine
source addr source addr
Determine
dest addr
Example:
Write
to dest reg
Before Instruction
FSR2
Contents
of 85h
Contents
of 86h
After Instruction
FSR2
Contents
of 85h
Contents
of 86h
DS41303B-page 350
Determine
dest addr
Q4
Read
source reg
80h
33h
11h
80h
33h
33h
Advance Information
PIC18F2XK20/4XK20
SUBFSR
SUBULNK
Syntax:
SUBFSR f, k
Syntax:
SUBULNK k
Operands:
0 k 63
Operands:
0 k 63
f [ 0, 1, 2 ]
Operation:
Operation:
FSR(f) k FSRf
Status Affected:
None
Encoding:
1110
FSR2 k FSR2
(TOS) PC
ffkk
kkkk
Description:
Words:
Cycles:
Encoding:
1110
Q1
Q2
Q3
Q4
Decode
Read
register f
Process
Data
Write to
destination
SUBFSR 2, 23h
1001
11kk
kkkk
Description:
Words:
Cycles:
Q Cycle Activity:
Example:
Q Cycle Activity:
Before Instruction
FSR2
=
Q1
Q2
Q3
Q4
03FFh
Decode
After Instruction
FSR2
=
Read
register f
Process
Data
Write to
destination
03DCh
No
Operation
No
Operation
No
Operation
No
Operation
Example:
SUBULNK 23h
Before Instruction
FSR2
=
PC
=
03FFh
0100h
After Instruction
FSR2
=
PC
=
03DCh
(TOS)
Advance Information
DS41303B-page 351
PIC18F2XK20/4XK20
24.2.3
Note:
BYTE-ORIENTED AND
BIT-ORIENTED INSTRUCTIONS IN
INDEXED LITERAL OFFSET MODE
Enabling the PIC18 instruction set
extension may cause legacy applications
to behave erratically or fail entirely.
24.2.3.1
24.2.4
CONSIDERATIONS WHEN
ENABLING THE EXTENDED
INSTRUCTION SET
It is important to note that the extensions to the instruction set may not be beneficial to all users. In particular,
users who are not writing code that uses a software
stack may not benefit from using the extensions to the
instruction set.
DS41303B-page 352
Advance Information
PIC18F2XK20/4XK20
ADDWF
ADD W to Indexed
(Indexed Literal Offset mode)
BSF
Syntax:
ADDWF
Syntax:
BSF [k], b
Operands:
0 k 95
d [0,1]
Operands:
0 f 95
0b7
Operation:
Operation:
1 ((FSR2) + k)<b>
Status Affected:
N, OV, C, DC, Z
Status Affected:
None
Encoding:
[k] {,d}
0010
Description:
01d0
kkkk
kkkk
Encoding:
1000
bbb0
kkkk
kkkk
Description:
Words:
Cycles:
Q Cycle Activity:
Words:
Q1
Q2
Q3
Q4
Cycles:
Decode
Read
register f
Process
Data
Write to
destination
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read k
Process
Data
Write to
destination
Example:
ADDWF
[OFST] , 0
Before Instruction
W
OFST
FSR2
Contents
of 0A2Ch
After Instruction
W
Contents
of 0A2Ch
=
=
=
17h
2Ch
0A00h
20h
37h
20h
Example:
BSF
Before Instruction
FLAG_OFST
FSR2
Contents
of 0A0Ah
After Instruction
Contents
of 0A0Ah
[FLAG_OFST], 7
=
=
0Ah
0A00h
55h
D5h
Set Indexed
(Indexed Literal Offset mode)
SETF
Syntax:
SETF [k]
Operands:
0 k 95
Operation:
FFh ((FSR2) + k)
Status Affected:
None
Encoding:
0110
1000
kkkk
kkkk
Description:
Words:
Cycles:
Q Cycle Activity:
Q1
Q2
Q3
Q4
Decode
Read k
Process
Data
Write
register
Example:
SETF
Before Instruction
OFST
FSR2
Contents
of 0A2Ch
After Instruction
Contents
of 0A2Ch
Advance Information
[OFST]
=
=
2Ch
0A00h
00h
FFh
DS41303B-page 353
PIC18F2XK20/4XK20
24.2.5
DS41303B-page 354
Advance Information
PIC18F2XK20/4XK20
25.0
DEVELOPMENT SUPPORT
25.1
Advance Information
DS41303B-page 355
PIC18F2XK20/4XK20
25.2
MPASM Assembler
25.5
25.6
25.3
25.4
DS41303B-page 356
Advance Information
PIC18F2XK20/4XK20
25.7
25.9
25.8
Advance Information
DS41303B-page 357
PIC18F2XK20/4XK20
25.11 PICSTART Plus Development
Programmer
DS41303B-page 358
Advance Information
PIC18F2XK20/4XK20
26.0
ELECTRICAL CHARACTERISTICS
Advance Information
DS41303B-page 359
PIC18F2XK20/4XK20
FIGURE 26-1:
3.5V
3.0V
Voltage
2.7V
2.2V
1.8V
10
20
30 32
40
50
60
64
Frequency (MHz)
DS41303B-page 360
Advance Information
PIC18F2XK20/4XK20
26.1
PIC18F2XK20/4XK20
Param
Symbol
No.
Characteristic
Typ
Max
Units
D001
VDD
Supply Voltage
1.8
3.6
D002
VDR
1.5
D003
VPOR
0.7
D004
SVDD
0.05
D005
VBOR
BORV<1:0> = 11
1.8
BORV<1:0> = 10
2.3
BORV<1:0> = 01
2.8
BORV<1:0> = 00
3.1
This is the limit to which VDD can be lowered in Sleep mode, or during a device Reset, without losing RAM
data.
Note 1:
26.2
Conditions
PIC18F2XK20/4XK20
Param
No.
D006
Device Characteristics
Typ
0.1
D007
Note 1:
nA
Conditions
-40C
0.5
+25C
+85C
10
+125C
0.1
-40C
0.5
+25C
+85C
10
+125C
The power-down current in Sleep mode does not depend on the oscillator type. Power-down current is
measured with the part in Sleep mode, with all I/O pins in high-impedance state and tied to VDD or VSS and
all features that add delta current disabled (such as WDT, Timer1 Oscillator, BOR, etc.).
Advance Information
DS41303B-page 361
PIC18F2XK20/4XK20
26.3
PIC18F2XK20/4XK20
Param
No.
Device Characteristics
Supply Current (IDD)(1, 2)
D008
D008A
D009
D009A
D010
D010A
Note 1:
2:
Max Units
Conditions
-40C
+25C
12
+85C
32
+125C
14
-40C
15
+25C
21
+85C
48
+125C
0.5
mA
-40C TO +85C
0.5
mA
-40C to +125C
0.8
mA
-40C TO +85C
0.8
mA
-40C to +125C
2.7
mA
-40C TO +85C
2.7
mA
-40C to +125C
5.2
mA
-40C TO +85C
5.2
mA
-40C to +125C
VDD = 1.8V
FOSC = 31 kHz
(RC_RUN mode,
LFINTOSC source)
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
FOSC = 1 MHz
(RC_RUN mode,
HF-INTOSC source)
FOSC = 16 MHz
(RC_RUN mode,
HF-INTOSC source)
The supply current is mainly a function of operating voltage, frequency and mode. Other factors, such as
I/O pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption.
The test conditions for all IDD measurements in active operation mode are:
OSC1 = external square wave, from rail-to-rail; all I/O pins tri-stated, pulled to VDD;
MCLR = VDD; WDT enabled/disabled as specified.
For RC oscillator configurations, current through REXT is not included. The current through the resistor can
be estimated by the formula Ir = VDD/2REXT (mA) with REXT in k.
DS41303B-page 362
Advance Information
PIC18F2XK20/4XK20
26.4
PIC18F2XK20/4XK20
Param
No.
D011
Device Characteristics
Typ
3.0
-40C
4.5
+25C
9.0
+85C
TBD
+125C
3.0
-40C
4.5
+25C
9.0
+85C
TBD
+125C
250
-40C to +85C
250
-40C to +125C
400
-40C to +85C
400
-40C to +125C
1.0
mA
-40C to +85C
1.0
mA
-40C to +125C
2.0
mA
-40C to +85C
2.0
mA
-40C to +125C
D011A
D012
D012A
D013
D013A
Max Units
Conditions
VDD = 1.8V
FOSC = 31 kHz
(RC_IDLE mode,
LFINTOSC source)
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
FOSC = 1 MHz
(RC_IDLE mode,
HF-INTOSC source)
FOSC = 16 MHz
(RC_IDLE mode,
HF-INTOSC source)
Advance Information
DS41303B-page 363
PIC18F2XK20/4XK20
26.5
PIC18F2XK20/4XK20
Param
No.
D012
Device Characteristics
Typ
0.5
mA
-40C to +85C
0.5
mA
-40C to +125C
0.8
mA
-40C to +85C
0.8
mA
-40C to +125C
3.0
mA
-40C to +85C
3.0
mA
-40C to +125C
6.5
mA
-40C to +85C
6.5
mA
-40C to +125C
14
mA
-40C to +85C
14
mA
-40C to +125C
18
mA
-40C to +85C
18
mA
-40C to +125C
2.7
mA
-40C to +85C
2.7
mA
-40C to +125C
5.2
mA
-40C to +85C
5.2
mA
-40C to +125C
14
mA
-40C to +85C
14
mA
-40C to +125C
18
mA
-40C to +85C
18
mA
-40C to +125C
D012A
D013
D013A
D014
D014A
D015
D015A
D016
D016A
Note 1:
2:
Conditions
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
FOSC = 1 MHz
(PRI_RUN,
EC oscillator)
FOSC = 20 MHz
(PRI_RUN,
EC oscillator)
FOSC = 64 MHz
(PRI_RUN,
EC oscillator)
FOSC = 4 MHz
16 MHz Internal
(PRI_RUN HS+PLL)
FOSC = 16 MHz
64 MHz Internal
(PRI_RUN HS+PLL)
The supply current is mainly a function of operating voltage, frequency and mode. Other factors, such as
I/O pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption.
The test conditions for all IDD measurements in active operation mode are:
OSC1 = external square wave, from rail-to-rail; all I/O pins tri-stated, pulled to VDD;
MCLR = VDD; WDT enabled/disabled as specified.
For RC oscillator configurations, current through REXT is not included. The current through the resistor can
be estimated by the formula Ir = VDD/2REXT (mA) with REXT in k.
DS41303B-page 364
Advance Information
PIC18F2XK20/4XK20
26.6
PIC18F2XK20/4XK20
Param
No.
D017
Device Characteristics
Typ
250
-40C to +85C
250
-40C to +125C
400
-40C to +85C
400
-40C to +125C
1.0
mA
-40C to +85C
1.0
mA
-40C to +125C
2.0
mA
-40C to +85C
2.0
mA
-40C to +125C
TBD
mA
-40C to +85C
TBD
mA
-40C to +125C
TBD
mA
-40C to +85C
TBD
mA
-40C to +125C
D017A
D018
D018A
D019
D019A
Max Units
Conditions
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
VDD = 1.8V
VDD = 3.0V
FOSC = 1 MHz
(PRI_IDLE mode,
EC oscillator)
FOSC = 20 MHz
(PRI_IDLEmode,
EC oscillator)
FOSC = 64 MHz
(PRI_IDLEmode,
EC oscillator)
Advance Information
DS41303B-page 365
PIC18F2XK20/4XK20
26.7
PIC18F2XK20/4XK20
Param
No.
Device Characteristics
Supply Current (IDD)(1, 2)
D020
D020A
D021
D021A
Note 1:
2:
3:
Max Units
Conditions
-40C
+25C
14
+85C
14
-40C
15
+25C
24
+85C
1.0
-40C
1.5
+25C
3.5
+85C
1.0
-40C
1.5
+25C
3.5
+85C
VDD = 1.8V
FOSC = 32 kHz(3)
(SEC_RUN mode,
Timer1 as clock)
VDD = 3.0V
VDD = 1.8V
FOSC = 32 kHz(3)
(SEC_IDLE mode,
Timer1 as clock)
VDD = 3.0V
The supply current is mainly a function of operating voltage, frequency and mode. Other factors, such as
I/O pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption.
The test conditions for all IDD measurements in active operation mode are:
OSC1 = external square wave, from rail-to-rail; all I/O pins tri-stated, pulled to VDD;
MCLR = VDD; WDT enabled/disabled as specified.
For RC oscillator configurations, current through REXT is not included. The current through the resistor can
be estimated by the formula Ir = VDD/2REXT (mA) with REXT in k.
Low-Power mode on T1 osc.
DS41303B-page 366
Advance Information
PIC18F2XK20/4XK20
26.8
PIC18F2XK20/4XK20
Param
No.
Device Characteristics
Typ
Max Units
Conditions
D022A
(IBOR)
Watchdog Timer
Brown-out Reset(2)
D022B
(IHLVD)
High/Low-Voltage Detect(2)
D025
(IOSCB)
Timer1 Oscillator
D025A
(IOSCB)
Timer1 Oscillator
1.5
-40C
1.5
+25C
2.0
+85C
2.0
-40C
3.0
+25C
3.0
+85C
40
-40C to +85C
VDD = 2.7V
45
-40C to +85C
VDD = 3.3V
-40C to +85C
VDD = 3.3V
TBD
-40C to +85C
VDD = 1.8V
TBD
-40C to +85C
VDD = 3.3V
-40C
+25C
+85C
-40C
+25C
+85C
-40C
+25C
+85C
-40C
+25C
+85C
TBD
-40C to +85C
VDD = 1.8V
TBD
-40C to +85C
VDD = 3.0V
VDD = 1.8V
D026
(IAD)
A/D Converter
D027
Comparators
TBD
-40C to +85C
CVREF
TBD
-40C to +85C
D028
Sleep mode,
BOREN<1:0> = 10
VDD = 1.8V
32 kHz on Timer1(1)
VDD = 3.0V
32 kHz on Timer1(1)
VDD = 1.8V
32 kHz on Timer1(3)
VDD = 3.0V
32 kHz on Timer1(3)
VDD = 1.8V
VDD = 3.0V
(ICVREF)
3:
VDD = 3.0V
VDD = 3.0V
(ICOMP)
Legend:
Note 1:
2:
VDD = 1.8V
TBD = To Be Determined.
Low-Power mode on T1 osc.
BOR and HLVD enable internal band gap reference. With both modules enabled, current consumption will be
less than the sum of both specifications.
High-Power mode in T1 osc.
Advance Information
DS41303B-page 367
PIC18F2XK20/4XK20
26.9
DC CHARACTERISTICS
Param
Symbol
No.
VIL
Characteristic
Min
Max
Units
Conditions
D030
VSS
0.15 VDD
D031
VSS
VSS
0.2 VDD
0.3 VDD
V
V
MCLR
VSS
0.2 VDD
D033
OSC1
VSS
0.3 VDD
D033A
D033B
D034
OSC1
OSC1
T13CKI
VSS
VSS
VSS
0.2 VDD
0.3 VDD
0.3 VDD
V
V
V
RC, EC modes(1)
XT, LP modes
VDD
0.8 VDD
0.7 VDD
VDD
VDD
V
V
MCLR
0.8 VDD
VDD
D043
OSC1
0.7
VDD
VDD
D043A
D043B
D043C
D044
OSC1
OSC1
OSC1
T13CKI
0.8 VDD
0.9 VDD
1.6
1.6
VDD
VDD
VDD
VDD
V
V
V
V
EC mode
RC mode(1)
XT, LP modes
D032
VIH
D040
D041
D042
IIL
D060
I/O ports
D061
MCLR
OSC1
50
400
D063
D070
Note 1:
2:
3:
4:
IPU
IPURB
In RC oscillator configuration, the OSC1/CLKIN pin is a Schmitt Trigger input. It is not recommended that
the PIC device be driven with an external clock while in RC mode.
The leakage current on the MCLR pin is strongly dependent on the applied voltage level. The specified
levels represent normal operating conditions. Higher leakage current may be measured at different input
voltages.
Negative current is defined as current sourced by the pin.
Parameter is characterized but not tested.
DS41303B-page 368
Advance Information
PIC18F2XK20/4XK20
26.9
DC CHARACTERISTICS
Param
Symbol
No.
VOL
Characteristic
Min
Max
Units
Conditions
D080
I/O ports
0.6
D083
OSC2/CLKOUT
(RC, RCIO, EC, ECIO modes)
0.6
VOH
D090
I/O ports
VDD 0.7
D092
OSC2/CLKOUT
(RC, RCIO, EC, ECIO modes)
VDD 0.7
OSC2 pin
15
pF
D101
CIO
50
pF
D102
CB
SCL, SDA
400
pF
I2C Specification
Note 1:
2:
3:
4:
In RC oscillator configuration, the OSC1/CLKIN pin is a Schmitt Trigger input. It is not recommended that
the PIC device be driven with an external clock while in RC mode.
The leakage current on the MCLR pin is strongly dependent on the applied voltage level. The specified
levels represent normal operating conditions. Higher leakage current may be measured at different input
voltages.
Negative current is defined as current sourced by the pin.
Parameter is characterized but not tested.
Advance Information
DS41303B-page 369
PIC18F2XK20/4XK20
26.10 Memory Programming Requirements
Standard Operating Conditions (unless otherwise stated)
Operating temperature -40C TA +125C
DC CHARACTERISTICS
Param
No.
Sym
Characteristic
Min
Typ
Max
Units
9.00
13.25
10
mA
E/W
Conditions
VPP
D113
IDDP
D120
ED
Byte Endurance
100K
(Note 3)
D121
VDRW
1.8
3.6
D122
TDEW
ms
D123
40
D124
TREF
1M
10M
E/W
-40C to +125C
-40C to +125C
EP
Cell Endurance
10K
E/W
D131
VPR
1.8
3.6
D132
VIW
1.8
3.6
D133
TIW
ms
D134
40
Data in Typ column is at 3.0V, 25C unless otherwise stated. These parameters are for design guidance
only and are not tested.
Note 1: These specifications are for programming the on-chip program memory through the use of table write
instructions.
2: Refer to Section 7.8 Using the Data EEPROM for a more detailed discussion on data EEPROM
endurance.
3: Required only if single-supply programming is disabled.
DS41303B-page 370
Advance Information
PIC18F2XK20/4XK20
TABLE 26-1:
COMPARATOR SPECIFICATIONS
Operating Conditions: 1.8V < VDD < 3.6V, -40C < TA < +125C (unless otherwise stated).
Param
No.
Sym
Characteristics
Min
Typ
Max
Units
CM01
VIOFF
7.5
15
mV
CM02
VICM
VDD
V
dB
CM03
CMRR
55
CM04
TRESP
Response Time
150
400
ns
CM05
TMC2OV
10
*
Note 1:
Comments
Note 1
TABLE 26-2:
Operating Conditions: 1.8V < VDD < 3.6V, -40C < TA < +125C (unless otherwise stated).
Param
No.
Sym
Characteristics
Min
Typ
Max
Units
Comments
CV01*
CLSB
Step Size(2)
VDD/24
VDD/32
V
V
CV02*
CACC
Absolute Accuracy
1/4
1/2
LSb
LSb
CV03*
CR
2k
CV04*
CST
Settling Time(1)
10
*
Note 1:
TABLE 26-3:
Operating Conditions: 1.8V < VDD < 3.6V, -40C < TA < +125C (unless otherwise stated).
Standard Operating Conditions (unless otherwise stated)
Operating temperature
-40C TA +125C
Sym
Characteristics
Min
Typ
Max
Units
TBD
1.2
TBD
VR01
VROUT
VR voltage output
VR02
TCVOUT
TBD
TBD
ppm/C
VR03
VROUT/
VDD
TBD
V/V
VR04
TSTABLE
Settling Time
TBD
TBD
Comments
Advance Information
DS41303B-page 371
PIC18F2XK20/4XK20
FIGURE 26-2:
VHLVD
(HLVDIF set by hardware)
HLVDIF
TABLE 26-4:
Characteristic
Min
Typ
Max
Units
1.8
1.9
LVV = 0010
2.0
LVV = 0011
2.1
LVV = 0100
2.2
LVV = 0101
2.3
LVV = 0110
2.4
LVV = 0111
2.5
LVV = 1000
2.6
LVV = 1001
2.7
LVV = 1010
2.8
LVV = 1011
2.9
LVV = 1100
3.0
LVV = 1101
3.3
LVV = 1110
3.5
Conditions
Production tested at TAMB = 25C. Specifications over temperature limits ensured by characterization.
DS41303B-page 372
Advance Information
PIC18F2XK20/4XK20
26.11 AC (Timing) Characteristics
26.11.1
3. TCC:ST
4. Ts
Time
osc
rd
rw
sc
ss
t0
t1
wr
OSC1
RD
RD or WR
SCK
SS
T0CKI
T13CKI
WR
P
R
V
Z
Period
Rise
Valid
High-impedance
High
Low
High
Low
SU
Setup
STO
Stop condition
Advance Information
DS41303B-page 373
PIC18F2XK20/4XK20
26.11.2
TIMING CONDITIONS
TABLE 26-5:
AC CHARACTERISTICS
FIGURE 26-3:
Load Condition 2
VDD/2
RL
CL
Pin
VSS
CL
Pin
RL = 464
VSS
DS41303B-page 374
CL = 50 pF
Advance Information
PIC18F2XK20/4XK20
26.11.3
FIGURE 26-4:
Q1
Q2
Q3
Q4
Q1
OSC1
1
CLKOUT
TABLE 26-6:
Param.
No.
1A
Symbol
FOSC
TOSC
Characteristic
Min
Max
Units
External CLKIN
Frequency(1)
DC
64
MHz
Oscillator Frequency(1)
DC
MHz
RC Oscillator mode
0.1
MHz
XT Oscillator mode
Period(1)
Time(1)
TCY
Instruction Cycle
TOSL,
TOSH
Note 1:
TOSR,
TOSF
Conditions
EC, ECIO Oscillator mode
25
MHz
HS Oscillator mode
16
MHz
33
kHz
LP Oscillator mode
15.6
ns
250
ns
RC Oscillator mode
250
10,000
ns
XT Oscillator mode
40
62.5
250
250
ns
ns
HS Oscillator mode
HS + PLL Oscillator mode
30
LP Oscillator mode
100
ns
TCY = 4/FOSC
30
ns
XT Oscillator mode
2.5
LP Oscillator mode
10
ns
HS Oscillator mode
20
ns
XT Oscillator mode
50
ns
LP Oscillator mode
7.5
ns
HS Oscillator mode
Instruction cycle period (TCY) equals four times the input oscillator time base period for all configurations
except PLL. All specified values are based on characterization data for that particular oscillator type under
standard operating conditions with the device executing code. Exceeding these specified limits may result
in an unstable oscillator operation and/or higher than expected current consumption. All devices are tested
to operate at min. values with an external clock applied to the OSC1/CLKIN pin. When an external clock
input is used, the max. cycle time limit is DC (no clock) for all devices.
Advance Information
DS41303B-page 375
PIC18F2XK20/4XK20
TABLE 26-7:
Param
No.
Sym
Characteristic
Min
Typ
Max
Units
Conditions
F10
64
F11
FSYS
32
64
F12
trc
ms
CLK
-2
+2
F13
Data in Typ column is at 5V, 25C unless otherwise stated. These parameters are for design guidance
only and are not tested.
TABLE 26-8:
PIC18F2XK20/4XK20
Param
No.
Min
Typ
Max
Units
Conditions
HFINTOSC Accuracy @ Freq = 16 MHz, 8 MHz, 4 MHz, 2 MHz, 1 MHz, 500 kHz, 250 kHz(1)
1
+25C
VDD = 1.8-3.6V
-2
+2
0C to +85C
VDD = 1.8-3.6V
-5
+5
-40C to +125C
VDD = 1.8-3.6V
35.938
kHz
-40C to +125C
VDD = 1.8-3.6V
FIGURE 26-5:
Q4
Q2
Q3
OSC1
11
10
CLKOUT
13
14
19
12
18
16
I/O pin
(Input)
17
I/O pin
(Output)
Note:
15
New Value
Old Value
20, 21
Refer to Figure 26-3 for load conditions.
DS41303B-page 376
Advance Information
PIC18F2XK20/4XK20
TABLE 26-9:
Param
No.
Symbol
Characteristic
Min
Typ
Max
Units Conditions
10
75
200
ns
(Note 1)
11
75
200
ns
(Note 1)
12
TckR
35
100
ns
(Note 1)
13
TckF
35
100
ns
(Note 1)
0.5 TCY + 20
ns
(Note 1)
0.25 TCY + 25
ns
(Note 1)
(Note 1)
14
TckL2ioV
15
16
TckH2ioI
17
18
TosH2ioI
ns
50
150
ns
100
ns
19
ns
20
TioR
10
25
ns
21
TioF
10
25
ns
22
TINP
TCY
ns
23
TRBP
TCY
24
TRCP
20
ns
ns
These parameters are asynchronous events not related to any internal clock edges.
Note 1: Measurements are taken in RC mode, where CLKOUT output is 4 x TOSC.
FIGURE 26-6:
VDD
MCLR
30
Internal
POR
PWRT
Time-out
33
32
OSC
Time-out
Internal
Reset
Watchdog
Timer
Reset
31
34
34
I/O pins
Note:
Advance Information
DS41303B-page 377
PIC18F2XK20/4XK20
FIGURE 26-7:
VDD
35
VBGAP = 1.2V
VIRVST
Enable Internal
Reference Voltage
Internal Reference
Voltage Stable
36
TABLE 26-10: RESET, WATCHDOG TIMER, OSCILLATOR START-UP TIMER, POWER-UP TIMER
AND BROWN-OUT RESET REQUIREMENTS
Param.
Symbol
No.
30
31
TmcL
TWDT
32
33
TOST
TPWRT
34
TIOZ
Characteristic
MCLR Pulse Width (low)
Watchdog Timer Time-out Period
(no postscaler)
Oscillation Start-up Timer Period
Power-up Timer Period
FIGURE 26-8:
Min
Typ
Max
Units
4.00
TBD
s
ms
65.5
TBD
ms
Conditions
200
20
50
s
s
200
5
10
s
s
ms
VDD VHLVD
41
40
42
T1OSO/T13CKI
46
45
47
48
TMR0 or
TMR1
Note:
DS41303B-page 378
Advance Information
PIC18F2XK20/4XK20
TABLE 26-11: TIMER0 AND TIMER1 EXTERNAL CLOCK REQUIREMENTS
Param
No.
Symbol
Characteristic
40
Tt0H
41
Tt0L
42
Tt0P
T0CKI Period
No prescaler
Min
Max
0.5 TCY + 20
ns
With prescaler
No prescaler
10
ns
0.5 TCY + 20
ns
With prescaler
45
46
Tt1H
Tt1L
T13CKI
High Time
10
ns
TCY + 10
ns
Greater of:
20 ns or
(TCY + 40)/N
ns
No prescaler
With prescaler
Synchronous, no prescaler
0.5 TCY + 20
ns
Synchronous,
with prescaler
10
ns
Asynchronous
30
ns
0.5 TCY + 5
ns
10
ns
Asynchronous
47
Tt1P
T13CKI
Synchronous
Input Period
30
ns
Greater of:
20 ns or
(TCY + 40)/N
ns
60
ns
DC
50
kHz
2 TOSC
7 TOSC
Asynchronous
Ft1
48
FIGURE 26-9:
Units Conditions
N = prescale
value
(1, 2, 4,..., 256)
N = prescale
value (1, 2, 4, 8)
50
51
52
CCPx
(Compare or PWM Mode)
53
Note:
54
Advance Information
DS41303B-page 379
PIC18F2XK20/4XK20
TABLE 26-12: CAPTURE/COMPARE/PWM REQUIREMENTS (ALL CCP MODULES)
Param
Symbol
No.
50
TccL
51
TccH
Characteristic
Min
Max
Units
0.5 TCY + 20
ns
10
ns
CCPx Input
High Time
0.5 TCY + 20
ns
10
ns
3 TCY + 40
N
ns
No prescaler
With
prescaler
52
TccP
53
TccR
25
ns
54
TccF
25
ns
FIGURE 26-10:
Conditions
N = prescale
value (1, 4 or 16)
RE2/CS
RE0/RD
RE1/WR
65
RD7:RD0
62
64
63
Note:
Symbol
Characteristic
Min
Max
Units
62
TdtV2wrH
20
ns
63
TwrH2dtI
20
ns
64
TrdL2dtV
80
ns
65
TrdH2dtI
RD or CS to DataOut Invalid
10
30
ns
66
TibfINH
3 TCY
DS41303B-page 380
Advance Information
Conditions
PIC18F2XK20/4XK20
FIGURE 26-11:
SS
70
SCK
(CKP = 0)
71
72
78
79
79
78
SCK
(CKP = 1)
80
bit 6 - - - - - -1
MSb
SDO
LSb
75, 76
SDI
MSb In
bit 6 - - - -1
LSb In
74
73
Note:
Symbol
Characteristic
70
TssL2scH,
TssL2scL
71
TscH
71A
72
TscL
72A
Min
Max Units
TCY
ns
Continuous
1.25 TCY + 30
ns
Single Byte
40
ns
Continuous
1.25 TCY + 30
ns
Single Byte
40
ns
100
ns
1.5 TCY + 40
ns
100
ns
73
TdiV2scH,
TdiV2scL
73A
Tb2b
74
TscH2diL,
TscL2diL
75
TdoR
25
ns
76
TdoF
25
ns
78
TscR
25
ns
79
TscF
25
ns
80
TscH2doV,
TscL2doV
50
ns
Note 1:
2:
Conditions
(Note 1)
(Note 1)
(Note 2)
Advance Information
DS41303B-page 381
PIC18F2XK20/4XK20
FIGURE 26-12:
SS
81
SCK
(CKP = 0)
71
72
79
73
SCK
(CKP = 1)
80
78
MSb
SDO
bit 6 - - - - - -1
LSb
bit 6 - - - -1
LSb In
75, 76
SDI
MSb In
74
Note:
Symbol
Characteristic
TscH
TscL
73
TdiV2scH,
TdiV2scL
73A
Tb2b
74
TscH2diL,
TscL2diL
75
TdoR
71A
72
72A
Continuous
Min
Max Units
1.25 TCY + 30
ns
Single Byte
40
ns
Continuous
1.25 TCY + 30
ns
Single Byte
40
ns
100
ns
1.5 TCY + 40
ns
100
ns
25
ns
76
TdoF
25
ns
78
TscR
25
ns
79
TscF
25
ns
80
TscH2doV,
TscL2doV
50
ns
81
TdoV2scH,
TdoV2scL
TCY
ns
Note 1:
2:
Conditions
(Note 1)
(Note 1)
(Note 2)
DS41303B-page 382
Advance Information
PIC18F2XK20/4XK20
FIGURE 26-13:
SS
70
SCK
(CKP = 0)
83
71
72
78
79
79
78
SCK
(CKP = 1)
80
MSb
SDO
bit 6 - - - - - -1
LSb
77
75, 76
MSb In
SDI
73
Note:
bit 6 - - - -1
LSb In
74
TABLE 26-16: EXAMPLE SPI MODE REQUIREMENTS (SLAVE MODE TIMING, CKE = 0)
Param
No.
Symbol
Characteristic
70
71
TscH
71A
72
TscL
72A
Min
TCY
ns
Continuous
1.25 TCY + 30
ns
Single Byte
40
ns
Continuous
1.25 TCY + 30
ns
Single Byte
40
ns
100
ns
73
73A
Tb2b
74
Last Clock Edge of Byte 1 to the First Clock Edge of Byte 2 1.5 TCY + 40
ns
100
ns
75
TdoR
25
ns
76
TdoF
25
ns
77
10
50
ns
78
TscR
25
ns
79
TscF
80
83
Note 1:
2:
25
ns
50
ns
1.5 TCY + 40
ns
(Note 1)
(Note 1)
(Note 2)
Advance Information
DS41303B-page 383
PIC18F2XK20/4XK20
FIGURE 26-14:
SS
SCK
(CKP = 0)
70
83
71
72
SCK
(CKP = 1)
80
MSb
SDO
bit 6 - - - - - -1
LSb
75, 76
SDI
MSb In
Note:
77
bit 6 - - - -1
LSb In
74
Refer to Figure 26-3 for load conditions.
Symbol
Characteristic
Min
70
71
TscH
TscL
73A
Tb2b
Last Clock Edge of Byte 1 to the First Clock Edge of Byte 2 1.5 TCY + 40
74
75
TdoR
76
TdoF
25
ns
77
10
50
ns
78
TscR
25
ns
79
TscF
25
ns
80
50
ns
82
50
ns
83
1.5 TCY + 40
ns
71A
72
72A
Note 1:
2:
Continuous
TCY
ns
1.25 TCY + 30
ns
Single Byte
40
ns
Continuous
1.25 TCY + 30
ns
Single Byte
40
(Note 1)
ns
(Note 1)
ns
(Note 2)
100
ns
25
ns
DS41303B-page 384
Advance Information
PIC18F2XK20/4XK20
I2C BUS START/STOP BITS TIMING
FIGURE 26-15:
SCL
91
93
90
92
SDA
Stop
Condition
Start
Condition
Note:
Characteristic
90
TSU:STA
Start Condition
91
THD:STA
92
TSU:STO
93
Max
Units
Conditions
4700
ns
ns
Setup Time
600
Start Condition
4000
Hold Time
600
Stop Condition
4700
Setup Time
Hold Time
FIGURE 26-16:
Min
600
4000
600
ns
ns
102
100
101
SCL
90
106
107
91
92
SDA
In
110
109
109
SDA
Out
Note:
Advance Information
DS41303B-page 385
PIC18F2XK20/4XK20
TABLE 26-19: I2C BUS DATA REQUIREMENTS (SLAVE MODE)
Param.
Symbol
No.
100
THIGH
Characteristic
Clock High Time
Min
Max
Units
4.0
0.6
1.5 TCY
4.7
1.3
SSP Module
101
TLOW
SSP Module
102
91
106
107
92
109
2:
300
ns
300
ns
20 + 0.1 CB
300
ns
CB is specified to be from
10 to 400 pF
4.7
0.6
4.0
0.6
ns
0.9
250
ns
100
ns
4.7
0.6
CB
Note 1:
20 + 0.1 CB
TBUF
D102
ns
TAA
110
1000
TF
90
TR
103
1.5 TCY
Conditions
3500
ns
ns
4.7
1.3
400
pF
CB is specified to be from
10 to 400 pF
(Note 2)
(Note 1)
Time the bus must be free
before a new transmission
can start
As a transmitter, the device must provide this internal minimum delay time to bridge the undefined region
(min. 300 ns) of the falling edge of SCL to avoid unintended generation of Start or Stop conditions.
A fast mode I2C bus device can be used in a standard mode I2C bus system but the requirement,
TSU:DAT 250 ns, must then be met. This will automatically be the case if the device does not stretch the
LOW period of the SCL signal. If such a device does stretch the LOW period of the SCL signal, it must
output the next data bit to the SDA line, TR max. + TSU:DAT = 1000 + 250 = 1250 ns (according to the
standard mode I2C bus specification), before the SCL line is released.
DS41303B-page 386
Advance Information
PIC18F2XK20/4XK20
FIGURE 26-17:
SCL
93
91
90
92
SDA
Stop
Condition
Start
Condition
Note:
TSU:STA
Characteristic
Start Condition
92
93
Max
Units
2(TOSC)(BRG + 1)
ns
2(TOSC)(BRG + 1)
1 MHz mode(1)
2(TOSC)(BRG + 1)
ns
Setup Time
91
Min
2(TOSC)(BRG + 1)
2(TOSC)(BRG + 1)
1 MHz mode(1)
2(TOSC)(BRG + 1)
2(TOSC)(BRG + 1)
2(TOSC)(BRG + 1)
1 MHz mode(1)
2(TOSC)(BRG + 1)
2(TOSC)(BRG + 1)
2(TOSC)(BRG + 1)
1 MHz mode(1)
2(TOSC)(BRG + 1)
Conditions
ns
ns
FIGURE 26-18:
102
100
101
SCL
90
106
91
107
92
SDA
In
109
109
110
SDA
Out
Note:
Advance Information
DS41303B-page 387
PIC18F2XK20/4XK20
TABLE 26-21: MASTER SSP I2C BUS DATA REQUIREMENTS
Param.
Symbol
No.
100
THIGH
Characteristic
Min
Max
Units
2(TOSC)(BRG + 1)
ms
2(TOSC)(BRG
+ 1)
ms
mode(1)
2(TOSC)(BRG + 1)
ms
2(TOSC)(BRG + 1)
ms
2(TOSC)(BRG + 1)
ms
mode(1)
ms
101
TLOW
1 MHz
102
103
90
91
TR
TF
TSU:STA
Start Condition
Setup Time
2(TOSC)(BRG + 1)
1000
ns
20 + 0.1 CB
300
ns
1 MHz mode(1)
300
ns
300
ns
20 + 0.1 CB
300
ns
1 MHz mode(1)
100
ns
2(TOSC)(BRG + 1)
ms
2(TOSC)(BRG
+ 1)
ms
1 MHz mode(1)
2(TOSC)(BRG + 1)
ms
2(TOSC)(BRG + 1)
ms
2(TOSC)(BRG + 1)
ms
1 MHz mode(1)
2(TOSC)(BRG + 1)
ms
ns
106
0.9
ms
107
TSU:DAT
250
ns
92
109
TAA
Data Input
Setup Time
Output Valid
from Clock
100
ns
2(TOSC)(BRG + 1)
ms
2(TOSC)(BRG + 1)
ms
1 MHz mode(1)
2(TOSC)(BRG + 1)
ms
3500
ns
ns
1000
mode(1)
ns
4.7
ms
1.3
ms
400
pF
1 MHz
110
D102
Note 1:
2:
TBUF
CB
Conditions
CB is specified to be from
10 to 400 pF
CB is specified to be from
10 to 400 pF
(Note 2)
I2C
DS41303B-page 388
Advance Information
PIC18F2XK20/4XK20
FIGURE 26-19:
RC6/TX/CK
pin
121
121
RC7/RX/DT
pin
120
Note:
122
Symbol
Characteristic
Min
Max
Units
120
40
ns
121
Tckrf
20
ns
122
Tdtrf
20
ns
FIGURE 26-20:
Conditions
RC6/TX/CK
pin
125
RC7/RX/DT
pin
126
Note:
Symbol
Characteristic
Min
Max
Units
125
TdtV2ckl
10
ns
126
TckL2dtl
15
ns
Advance Information
Conditions
DS41303B-page 389
PIC18F2XK20/4XK20
TABLE 26-24: A/D CONVERTER CHARACTERISTICS: PIC18F2XK20/4XK20
Param
Symbol
No.
Characteristic
Min
Typ
Max
Units
10
bit
Conditions
VREF 3.0V
A01
NR
Resolution
A03
EIL
<1
A04
EDL
<1
A06
EOFF
Offset Error
<3
A07
EGN
Gain Error
<3
A10
Monotonicity
A20
VREF
A21
VREFH
A22
1.8
3
V
V
VDD/2
VDD + 0.6
VREFL
VSS 0.3V
VDD 3.0V
A25
VAIN
VREFL
VREFH
A30
ZAIN
Recommended Impedance of
Analog Voltage Source
2.5
A50
IREF
5
150
A
A
Note 1:
2:
Guaranteed(1)
The A/D conversion result never decreases with an increase in the input voltage and has no missing
codes.
VREFH current is from RA3/AN3/VREF+ pin or VDD, whichever is selected as the VREFH source.
VREFL current is from RA2/AN2/VREF-/CVREF pin or VSS, whichever is selected as the VREFL source.
DS41303B-page 390
Advance Information
PIC18F2XK20/4XK20
FIGURE 26-21:
BSF ADCON0, GO
(Note 2)
131
Q4
130
132
A/D CLK
A/D DATA
.. .
...
NEW_DATA
OLD_DATA
ADRES
TCY
ADIF
GO
DONE
SAMPLING STOPPED
SAMPLE
Note
1:
If the A/D clock source is selected as RC, a time of TCY is added before the A/D clock starts.
This allows the SLEEP instruction to be executed.
2:
This is a minimal RC delay (typically 100 ns), which also disconnects the holding capacitor from the analog input.
TAD
Characteristic
A/D Clock Period
Min
Max
Units
0.7
25.0(1)
TBD
A/D RC mode
11
12
TAD
1.4
TBD
s
s
131
TCNV
Conversion Time
(not including acquisition time) (Note 2)
132
TACQ
135
TSWC
(Note 4)
TBD
TDIS
Discharge Time
0.2
Legend:
Note 1:
2:
3:
4:
Conditions
-40C to +85C
0C to +85C
TBD = To Be Determined
The time of the A/D clock period is dependent on the device frequency and the TAD clock divider.
ADRES register may be read on the following TCY cycle.
The time for the holding capacitor to acquire the New input voltage when the voltage changes full scale
after the conversion (VDD to VSS or VSS to VDD). The source impedance (RS) on the input channels is 50
.
On the following cycle of the device clock.
Advance Information
DS41303B-page 391
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 392
Advance Information
PIC18F2XK20/4XK20
27.0
DC AND AC
CHARACTERISTICS GRAPHS
AND TABLES
Advance Information
DS41303B-page 393
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 394
Advance Information
PIC18F2XK20/4XK20
28.0
PACKAGING INFORMATION
28.1
Example
PIC18F25K20-E/SP
XXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX
YYWWNNN
e3
0610017
Example
PIC18F25K20-E/SO e3
0610017
XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
YYWWNNN
40-Lead PDIP
Example
PIC18F45K20-E/P e3
0610017
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
YYWWNNN
Legend: XX...X
Y
YY
WW
NNN
e3
Note:
Customer-specific information
Year code (last digit of calendar year)
Year code (last 2 digits of calendar year)
Week code (week of January 1 is week 01)
Alphanumeric traceability code
Pb-free JEDEC designator for Matte Tin (Sn)
This package is Pb-free. The Pb-free JEDEC designator ( e3 )
can be found on the outer packaging for this package.
In the event the full Microchip part number cannot be marked on one line, it will
be carried over to the next line, thus limiting the number of available
characters for customer-specific information.
Advance Information
DS41303B-page 395
PIC18F2XK20/4XK20
Package Marking Information (Continued)
28-Lead SSOP
Example
XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXX
YYWWNNN
PIC18F25K20-E/SS
e3
0610017
28-Lead QFN
Example
XXXXXXXX
XXXXXXXX
YYWWNNN
18F24K20
-E/ML e3
0610017
44-Lead QFN
Example
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
YYWWNNN
PIC18F45K20
-E/ML e3
0610017
44-Lead TQFP
Example
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
YYWWNNN
DS41303B-page 396
PIC18F44K20
-E/PT e3
0610017
Advance Information
PIC18F2XK20/4XK20
28.2
Package Details
28-Lead Skinny Plastic Dual In-Line (SP or PJ) 300 mil Body [SPDIP]
Note:
For the most current package drawings, please see the Microchip Packaging Specification located at
http://www.microchip.com/packaging
N
NOTE 1
E1
3
D
E
A2
b1
A1
eB
Units
Dimension Limits
Number of Pins
INCHES
MIN
NOM
MAX
28
Pitch
.200
A2
.120
.135
.150
A1
.015
.290
.310
.335
E1
.240
.285
.295
Overall Length
1.345
1.365
1.400
.110
.130
.150
Lead Thickness
.008
.010
.015
b1
.040
.050
.070
.014
.018
.022
eB
.100 BSC
.430
Notes:
1. Pin 1 visual index feature may vary, but must be located within the hatched area.
2. Significant Characteristic.
3. Dimensions D and E1 do not include mold flash or protrusions. Mold flash or protrusions shall not exceed .010" per side.
4. Dimensioning and tolerancing per ASME Y14.5M.
BSC: Basic Dimension. Theoretically exact value shown without tolerances.
Microchip Technology Drawing C04-070B
Advance Information
DS41303B-page 397
PIC18F2XK20/4XK20
28-Lead Plastic Small Outline (SO or OI) Wide, 7.50 mm Body [SOIC]
Note:
For the most current package drawings, please see the Microchip Packaging Specification located at
http://www.microchip.com/packaging
D
N
E
E1
NOTE 1
1 2 3
b
A2
h
c
L
A1
L1
Units
Dimension Limits
Number of Pins
MILLIMETERS
MIN
NOM
MAX
28
Pitch
Overall Height
1.27 BSC
A2
2.05
Standoff
A1
0.10
0.30
Overall Width
E1
7.50 BSC
Overall Length
17.90 BSC
2.65
10.30 BSC
Chamfer (optional)
0.25
0.75
Foot Length
0.40
1.27
Footprint
L1
1.40 REF
Lead Thickness
0.18
0.33
Lead Width
0.31
0.51
15
15
Notes:
1. Pin 1 visual index feature may vary, but must be located within the hatched area.
2. Significant Characteristic.
3. Dimensions D and E1 do not include mold flash or protrusions. Mold flash or protrusions shall not exceed 0.15 mm per side.
4. Dimensioning and tolerancing per ASME Y14.5M.
BSC: Basic Dimension. Theoretically exact value shown without tolerances.
REF: Reference Dimension, usually without tolerance, for information purposes only.
Microchip Technology Drawing C04-052B
DS41303B-page 398
Advance Information
PIC18F2XK20/4XK20
40-Lead Plastic Dual In-Line (P or PL) 600 mil Body [PDIP]
Note:
For the most current package drawings, please see the Microchip Packaging Specification located at
http://www.microchip.com/packaging
N
NOTE 1
E1
1 2 3
D
E
A2
b1
A1
eB
Units
Dimension Limits
Number of Pins
INCHES
MIN
NOM
MAX
40
Pitch
.250
A2
.125
.195
A1
.015
.590
.625
E1
.485
.580
Overall Length
1.980
2.095
.115
.200
Lead Thickness
.008
.015
b1
.030
.070
.014
.023
eB
.100 BSC
.700
Notes:
1. Pin 1 visual index feature may vary, but must be located within the hatched area.
2. Significant Characteristic.
3. Dimensions D and E1 do not include mold flash or protrusions. Mold flash or protrusions shall not exceed .010" per side.
4. Dimensioning and tolerancing per ASME Y14.5M.
BSC: Basic Dimension. Theoretically exact value shown without tolerances.
Microchip Technology Drawing C04-016B
Advance Information
DS41303B-page 399
PIC18F2XK20/4XK20
28-Lead Plastic Quad Flat, No Lead Package (ML) 6x6 mm Body [QFN]
with 0.55 mm Contact Length
Note:
For the most current package drawings, please see the Microchip Packaging Specification located at
http://www.microchip.com/packaging
D
D2
EXPOSED
PAD
e
E
E2
2
K
N
NOTE 1
L
BOTTOM VIEW
TOP VIEW
A3
A1
Units
Dimension Limits
Number of Pins
MILLIMETERS
MIN
NOM
MAX
28
Pitch
Overall Height
0.80
0.65 BSC
0.90
1.00
Standoff
A1
0.00
0.02
0.05
Contact Thickness
A3
0.20 REF
Overall Width
E2
Overall Length
D2
3.65
3.70
4.20
0.23
0.30
0.35
Contact Length
0.50
0.55
0.70
Contact-to-Exposed Pad
0.20
Contact Width
6.00 BSC
3.65
3.70
4.20
6.00 BSC
Notes:
1. Pin 1 visual index feature may vary, but must be located within the hatched area.
2. Package is saw singulated.
3. Dimensioning and tolerancing per ASME Y14.5M.
BSC: Basic Dimension. Theoretically exact value shown without tolerances.
REF: Reference Dimension, usually without tolerance, for information purposes only.
Microchip Technology Drawing C04-105B
DS41303B-page 400
Advance Information
PIC18F2XK20/4XK20
44-Lead Plastic Quad Flat, No Lead Package (ML) 8x8 mm Body [QFN]
Note:
For the most current package drawings, please see the Microchip Packaging Specification located at
http://www.microchip.com/packaging
D
D2
EXPOSED
PAD
e
E
E2
b
2
2
1
N
1
N
NOTE 1
TOP VIEW
L
BOTTOM VIEW
A
A3
A1
Units
Dimension Limits
Number of Pins
MILLIMETERS
MIN
NOM
MAX
44
Pitch
Overall Height
0.80
0.65 BSC
0.90
1.00
Standoff
A1
0.00
0.02
0.05
Contact Thickness
A3
0.20 REF
Overall Width
E2
Overall Length
D2
6.30
6.45
6.80
0.25
0.30
0.38
Contact Length
0.30
0.40
0.50
Contact-to-Exposed Pad
0.20
Contact Width
8.00 BSC
6.30
6.45
6.80
8.00 BSC
Notes:
1. Pin 1 visual index feature may vary, but must be located within the hatched area.
2. Package is saw singulated.
3. Dimensioning and tolerancing per ASME Y14.5M.
BSC: Basic Dimension. Theoretically exact value shown without tolerances.
REF: Reference Dimension, usually without tolerance, for information purposes only.
Microchip Technology Drawing C04-103B
Advance Information
DS41303B-page 401
PIC18F2XK20/4XK20
44-Lead Plastic Thin Quad Flatpack (PT) 10x10x1 mm Body, 2.00 mm Footprint [TQFP]
Note:
For the most current package drawings, please see the Microchip Packaging Specification located at
http://www.microchip.com/packaging
D
D1
E
e
E1
NOTE 1
1 2 3
NOTE 2
A1
Units
Dimension Limits
Number of Leads
A2
L1
MILLIMETERS
MIN
NOM
MAX
44
Lead Pitch
Overall Height
0.80 BSC
A2
0.95
1.00
1.05
Standoff
A1
0.05
0.15
Foot Length
0.45
0.60
0.75
Footprint
L1
1.20
1.00 REF
Foot Angle
Overall Width
12.00 BSC
Overall Length
12.00 BSC
E1
10.00 BSC
D1
10.00 BSC
3.5
Lead Thickness
0.09
0.20
Lead Width
0.30
0.37
0.45
11
12
13
11
12
13
Notes:
1. Pin 1 visual index feature may vary, but must be located within the hatched area.
2. Chamfers at corners are optional; size may vary.
3. Dimensions D1 and E1 do not include mold flash or protrusions. Mold flash or protrusions shall not exceed 0.25 mm per side.
4. Dimensioning and tolerancing per ASME Y14.5M.
BSC: Basic Dimension. Theoretically exact value shown without tolerances.
REF: Reference Dimension, usually without tolerance, for information purposes only.
Microchip Technology Drawing C04-076B
DS41303B-page 402
Advance Information
PIC18F2XK20/4XK20
APPENDIX A:
REVISION HISTORY
APPENDIX B:
DEVICE
DIFFERENCES
Revision B (03/2007)
Added
part
numbers
PIC18F26K20
and
PIC18F46K20; Replaced Development Support
Section; Replaced Package Drawings.
TABLE B-1:
DEVICE DIFFERENCES
Features
Program Memory
(Bytes)
8192
16384
32768
65536
8192
16384
32768
65536
Program Memory
(Instructions)
4096
8192
16384
32768
4096
8192
16384
32768
19
19
19
19
20
20
20
20
Interrupt Sources
I/O Ports
Capture/Compare/PWM
Modules
Enhanced
Capture/Compare/PWM
Modules
Parallel
Communications (PSP)
No
No
No
No
Yes
Yes
Yes
Yes
10-bit Analog-to-Digital
Module
11 input
channels
11 input
channels
11 input
channels
11 input
channels
14 input
channels
14 input
channels
14 input
channels
14 input
channels
28-pin PDIP
28-pin SOIC
28-pin SSOP
28-pin QFN
28-pin PDIP
28-pin SOIC
28-pin SSOP
28-pin QFN
28-pin PDIP
28-pin SOIC
28-pin SSOP
28-pin QFN
28-pin PDIP
28-pin SOIC
28-pin SSOP
28-pin QFN
40-pin PDIP
44-pin TQFP
44-pin QFN
40-pin PDIP
44-pin TQFP
44-pin QFN
40-pin PDIP
44-pin TQFP
44-pin QFN
40-pin PDIP
44-pin TQFP
44-pin QFN
Packages
Advance Information
DS41303B-page 403
PIC18F2XK20/4XK20
NOTES:
DS41303B-page 404
Advance Information
PIC18F2XK20/4XK20
INDEX
A
A/D
Advance Information
DS41303B-page 405
PIC18F2XK20/4XK20
Detecting .................................................................... 52
Disabling in Sleep Mode ............................................ 52
Software Enabled ....................................................... 52
BSF .................................................................................. 311
BTFSC ............................................................................. 312
BTFSS .............................................................................. 312
BTG .................................................................................. 313
BZ ..................................................................................... 314
C
C Compilers
MPLAB C18 ............................................................. 350
MPLAB C30 ............................................................. 350
CALL ................................................................................ 314
CALLW ............................................................................. 343
Capture (CCP Module) ..................................................... 153
Associated Registers ............................................... 156
CCP Pin Configuration ............................................. 153
CCPRxH:CCPRxL Registers ................................... 153
Prescaler .................................................................. 153
Software Interrupt .................................................... 153
Timer1/Timer3 Mode Selection ................................ 153
Capture (ECCP Module) .................................................. 164
Capture/Compare/PWM (CCP) ........................................ 151
Capture Mode. See Capture.
CCP Mode and Timer Resources ............................ 152
CCPRxH Register .................................................... 152
CCPRxL Register ..................................................... 152
Compare Mode. See Compare.
Interaction of Two CCP Modules ............................. 152
Module Configuration ............................................... 152
PWM Mode .............................................................. 157
Duty Cycle ........................................................ 158
Effects of Reset ................................................ 160
Example PWM Frequencies & Resolutions
Fosc=20 MHZ .......................................... 159
Fosc=40 MHZ .......................................... 159
Fosc=8 MHZ ............................................ 159
Operation in Sleep Mode ................................. 160
Setup for Operation .......................................... 160
System Clock Frequency Changes .................. 160
PWM Period ............................................................. 158
Setup for PWM Operation ........................................ 160
CCP1CON Register ......................................................... 163
CCP2CON Register ......................................................... 151
Clock Accuracy with Asynchronous Operation ................ 232
Clock Sources
Associated registers ................................................... 39
External Modes .......................................................... 28
EC ...................................................................... 28
HS ...................................................................... 29
LP ....................................................................... 29
OST .................................................................... 28
RC ...................................................................... 30
XT ...................................................................... 29
Internal Modes ........................................................... 30
Frequency Selection .......................................... 32
HFINTOSC ......................................................... 30
INTOSC ............................................................. 30
INTOSCIO .......................................................... 30
LFINTOSC ......................................................... 32
Selecting the 31 kHz Source ...................................... 26
Selection Using OSCCON Register ........................... 26
Clock Switching .................................................................. 35
CLRF ................................................................................ 315
CLRWDT .......................................................................... 315
DS41303B-page 406
Advance Information
PIC18F2XK20/4XK20
CONFIG7H Register ........................................................ 290
CONFIG7L Register ......................................................... 290
Configuration Bits ............................................................. 284
Configuration Register Protection .................................... 297
Context Saving During Interrupts ..................................... 113
CPFSEQ .......................................................................... 316
CPFSGT .......................................................................... 317
CPFSLT ........................................................................... 317
Customer Change Notification Service ............................ 409
Customer Notification Service .......................................... 409
Customer Support ............................................................ 409
CVREF Voltage Reference Specifications ........................ 365
D
Data Addressing Modes ..................................................... 80
Comparing Addressing Modes with the
Extended Instruction Set Enabled ..................... 83
Direct .......................................................................... 80
Indexed Literal Offset ................................................. 82
Instructions Affected .......................................... 82
Indirect ....................................................................... 80
Inherent and Literal .................................................... 80
Data EEPROM
Code Protection ....................................................... 297
Data EEPROM Memory ..................................................... 95
Associated Registers ................................................. 98
EEADR Register ........................................................ 95
EECON1 and EECON2 Registers ............................. 95
Operation During Code-Protect ................................. 98
Protection Against Spurious Write ............................. 98
Reading ...................................................................... 97
Using .......................................................................... 98
Write Verify ................................................................ 97
Writing ........................................................................ 97
Data Memory ..................................................................... 69
Access Bank .............................................................. 74
and the Extended Instruction Set ............................... 82
Bank Select Register (BSR) ....................................... 69
General Purpose Registers ........................................ 74
Map for PIC18F23K20/43K20 .................................... 70
Map for PIC18F24K20/44K20 .................................... 71
Map for PIC18F25K20/45K20 .................................... 72
Special Function Registers ........................................ 74
DAW ................................................................................. 318
DC and AC Characteristics
Graphs and Tables .................................................. 387
DC Characteristics
Input/Output ............................................................. 362
Peripheral Supply Current ........................................ 361
Power-Down Current ............................................... 355
Primary Idle Supply Current ..................................... 359
Primary Run Supply Current .................................... 358
RC Idle Supply Current ............................................ 357
RC Run Supply Current ........................................... 356
Secondary Oscillator Supply Current ....................... 360
Supply Voltage ......................................................... 355
DCFSNZ .......................................................................... 319
DECF ............................................................................... 318
DECFSZ ........................................................................... 319
Development Support ...................................................... 349
Device Differences ........................................................... 397
Device Overview .................................................................. 9
Details on Individual Family Members ....................... 10
Features (table) .......................................................... 11
New Core Features ...................................................... 9
Other Special Features .............................................. 10
E
ECCPAS Register ............................................................ 173
EECON1 Register ........................................................ 87, 96
Effect on Standard PIC Instructions ................................. 346
Effects of Power Managed Modes on Various
Clock Sources ........................................................... 34
Effects of Reset
PWM mode .............................................................. 160
Electrical Characteristics ................................................. 353
Enhanced Capture/Compare/PWM (ECCP) .................... 163
Associated Registers ............................................... 181
Capture and Compare Modes ................................. 164
Capture Mode. See Capture (ECCP Module).
Enhanced PWM Mode ............................................. 165
Auto-Restart .................................................... 174
Auto-shutdown ................................................ 173
Direction Change in Full-Bridge Output Mode . 171
Full-Bridge Application ..................................... 169
Full-Bridge Mode ............................................. 169
Half-Bridge Application .................................... 168
Half-Bridge Application Examples ................... 175
Half-Bridge Mode ............................................. 168
Output Relationships (Active-High and
Active-Low) .............................................. 166
Output Relationships Diagram ......................... 167
Programmable Dead Band Delay .................... 175
Shoot-through Current ..................................... 175
Start-up Considerations ................................... 172
Outputs and Configuration ....................................... 164
Standard PWM Mode .............................................. 164
Timer Resources ..................................................... 164
Enhanced Universal Synchronous Asynchronous
Receiver Transmitter (EUSART) ............................. 223
Errata ................................................................................... 8
EUSART .......................................................................... 223
Asynchronous Mode ................................................ 225
12-bit Break Transmit and Receive ................. 241
Associated Registers, Receive ........................ 231
Associated Registers, Transmit ....................... 227
Auto-Wake-up on Break .................................. 240
Baud Rate Generator (BRG) ........................... 235
Clock Accuracy ................................................ 232
Receiver .......................................................... 228
Setting up 9-bit Mode with Address Detect ..... 230
Transmitter ...................................................... 225
Baud Rate Generator (BRG)
Associated Registers ....................................... 235
Auto Baud Rate Detect .................................... 239
Baud Rate Error, Calculating ........................... 235
Baud Rates, Asynchronous Modes ................. 236
Formulas .......................................................... 235
High Baud Rate Select (BRGH Bit) ................. 235
Clock polarity
Synchronous Mode .......................................... 243
Data polarity
Asychronous Receive ...................................... 228
Asychronous Transmit ..................................... 225
Advance Information
DS41303B-page 407
PIC18F2XK20/4XK20
Synchronous Mode .......................................... 243
Interrupts
Asychronous Receive ...................................... 229
Asychronous Transmit ..................................... 225
Synchronous Master Mode .............................. 243, 247
Associated Registers, Receive ........................ 246
Associated Registers, Transmit ............... 244, 247
Reception ......................................................... 245
Transmission .................................................... 243
Synchronous Slave Mode
Associated Registers, Receive ........................ 248
Reception ......................................................... 248
Transmission .................................................... 247
Extended Instruction Set
ADDFSR .................................................................. 342
ADDULNK ................................................................ 342
and Using MPLAB Tools .......................................... 348
CALLW ..................................................................... 343
Considerations for Use ............................................ 346
MOVSF .................................................................... 343
MOVSS .................................................................... 344
PUSHL ..................................................................... 344
SUBFSR .................................................................. 345
SUBULNK ................................................................ 345
Syntax ...................................................................... 341
F
Fail-Safe Clock Monitor .............................................. 38, 283
Fail-Safe Condition Clearing ...................................... 38
Fail-Safe Detection .................................................... 38
Fail-Safe Operation .................................................... 38
Reset or Wake-up from Sleep .................................... 38
Fast Register Stack ............................................................ 66
Firmware Instructions ....................................................... 299
Flash Program Memory ...................................................... 85
Associated Registers ................................................. 93
Control Registers ....................................................... 86
EECON1 and EECON2 ..................................... 86
TABLAT (Table Latch) Register ......................... 88
TBLPTR (Table Pointer) Register ...................... 88
Erase Sequence ........................................................ 90
Erasing ....................................................................... 90
Operation During Code-Protect ................................. 93
Reading ...................................................................... 89
Table Pointer
Boundaries Based on Operation ........................ 88
Table Pointer Boundaries .......................................... 88
Table Reads and Table Writes .................................. 85
Write Sequence ......................................................... 91
Writing To ................................................................... 91
Protection Against Spurious Writes ................... 93
Unexpected Termination .................................... 93
Write Verify ........................................................ 93
G
General Call Address Support ......................................... 207
GOTO ............................................................................... 320
H
Hardware Multiplier ............................................................ 99
Introduction ................................................................ 99
Operation ................................................................... 99
Performance Comparison .......................................... 99
High/Low-Voltage Detect ................................................. 277
Applications .............................................................. 281
Associated Registers ............................................... 281
DS41303B-page 408
I
I/O Ports ........................................................................... 115
I2C Mode (MSSP)
Acknowledge Sequence Timing .............................. 217
Baud Rate Generator .............................................. 210
Bus Collision
During a Repeated Start Condition .................. 221
During a Stop Condition .................................. 222
Clock Arbitration ...................................................... 211
Clock Stretching ....................................................... 203
10-Bit Slave Receive Mode (SEN = 1) ............ 203
10-Bit Slave Transmit Mode ............................ 203
7-Bit Slave Receive Mode (SEN = 1) .............. 203
7-Bit Slave Transmit Mode .............................. 203
Clock Synchronization and the CKP bit (SEN = 1) .. 204
Effects of a Reset .................................................... 218
General Call Address Support ................................. 207
I2C Clock Rate w/BRG ............................................. 210
Master Mode ............................................................ 208
Operation ......................................................... 209
Reception ........................................................ 214
Repeated Start Condition Timing .................... 213
Start Condition Timing ..................................... 212
Transmission ................................................... 214
Multi-Master Communication, Bus Collision
and Arbitration ................................................. 218
Multi-Master Mode ................................................... 218
Operation ................................................................. 196
Read/Write Bit Information (R/W Bit) ............... 196, 197
Registers ................................................................. 192
Serial Clock (RC3/SCK/SCL) ................................... 197
Slave Mode .............................................................. 196
Addressing ....................................................... 196
Reception ........................................................ 197
Transmission ................................................... 197
Sleep Operation ....................................................... 218
Stop Condition Timing ............................................. 217
ID Locations ............................................................. 283, 297
INCF ................................................................................ 320
INCFSZ ............................................................................ 321
In-Circuit Debugger .......................................................... 297
In-Circuit Serial Programming (ICSP) ...................... 283, 297
Indexed Literal Offset Addressing
and Standard PIC18 Instructions ............................. 346
Indexed Literal Offset Mode ............................................. 346
Indirect Addressing ............................................................ 81
INFSNZ ............................................................................ 321
Initialization Conditions for all Registers ...................... 5760
Instruction Cycle ................................................................ 67
Clocking Scheme ....................................................... 67
Instruction Flow/Pipelining ................................................. 67
Instruction Set .................................................................. 299
ADDLW .................................................................... 305
ADDWF .................................................................... 305
ADDWF (Indexed Literal Offset Mode) .................... 347
Advance Information
PIC18F2XK20/4XK20
ADDWFC ................................................................. 306
ANDLW .................................................................... 306
ANDWF .................................................................... 307
BC ............................................................................ 307
BCF .......................................................................... 308
BN ............................................................................ 308
BNC ......................................................................... 309
BNN ......................................................................... 309
BNOV ....................................................................... 310
BNZ .......................................................................... 310
BOV ......................................................................... 313
BRA .......................................................................... 311
BSF .......................................................................... 311
BSF (Indexed Literal Offset Mode) .......................... 347
BTFSC ..................................................................... 312
BTFSS ..................................................................... 312
BTG .......................................................................... 313
BZ ............................................................................ 314
CALL ........................................................................ 314
CLRF ........................................................................ 315
CLRWDT .................................................................. 315
COMF ...................................................................... 316
CPFSEQ .................................................................. 316
CPFSGT .................................................................. 317
CPFSLT ................................................................... 317
DAW ......................................................................... 318
DCFSNZ .................................................................. 319
DECF ....................................................................... 318
DECFSZ ................................................................... 319
Extended Instruction Set .......................................... 341
General Format ........................................................ 301
GOTO ...................................................................... 320
INCF ......................................................................... 320
INCFSZ .................................................................... 321
INFSNZ .................................................................... 321
IORLW ..................................................................... 322
IORWF ..................................................................... 322
LFSR ........................................................................ 323
MOVF ....................................................................... 323
MOVFF .................................................................... 324
MOVLB .................................................................... 324
MOVLW ................................................................... 325
MOVWF ................................................................... 325
MULLW .................................................................... 326
MULWF .................................................................... 326
NEGF ....................................................................... 327
NOP ......................................................................... 327
Opcode Field Descriptions ....................................... 300
POP ......................................................................... 328
PUSH ....................................................................... 328
RCALL ..................................................................... 329
RESET ..................................................................... 329
RETFIE .................................................................... 330
RETLW .................................................................... 330
RETURN .................................................................. 331
RLCF ........................................................................ 331
RLNCF ..................................................................... 332
RRCF ....................................................................... 332
RRNCF .................................................................... 333
SETF ........................................................................ 333
SETF (Indexed Literal Offset Mode) ........................ 347
SLEEP ..................................................................... 334
SUBFWB .................................................................. 334
SUBLW .................................................................... 335
SUBWF .................................................................... 335
L
LFSR ............................................................................... 323
Low-Voltage ICSP Programming. See Single-Supply
ICSP Programming
M
Master Clear (MCLR) ......................................................... 51
Master Synchronous Serial Port (MSSP). See MSSP.
Memory Organization ........................................................ 63
Data Memory ............................................................. 69
Program Memory ....................................................... 63
Microchip Internet Web Site ............................................. 409
MOVF .............................................................................. 323
MOVFF ............................................................................ 324
MOVLB ............................................................................ 324
MOVLW ........................................................................... 325
MOVSF ............................................................................ 343
MOVSS ............................................................................ 344
MOVWF ........................................................................... 325
MPLAB ASM30 Assembler, Linker, Librarian .................. 350
MPLAB ICD 2 In-Circuit Debugger .................................. 351
MPLAB ICE 2000 High-Performance Universal
In-Circuit Emulator ................................................... 351
MPLAB ICE 4000 High-Performance Universal
In-Circuit Emulator ................................................... 351
MPLAB Integrated Development Environment Software . 349
MPLAB PM3 Device Programmer ................................... 351
MPLINK Object Linker/MPLIB Object Librarian ............... 350
MSSP
Advance Information
DS41303B-page 409
PIC18F2XK20/4XK20
ACK Pulse ........................................................ 196, 197
Control Registers (general) ...................................... 183
I2C Mode. See I2C Mode.
Module Overview ..................................................... 183
SPI Master/Slave Connection .................................. 187
SPI Mode. See SPI Mode.
SSPBUF Register .................................................... 188
SSPSR Register ...................................................... 188
MULLW ............................................................................ 326
MULWF ............................................................................ 326
N
NEGF ............................................................................... 327
NOP ................................................................................. 327
O
OSCCON Register ............................................................. 27
Oscillator Configuration
EC .............................................................................. 25
ECIO .......................................................................... 25
HS .............................................................................. 25
HSPLL ........................................................................ 25
INTOSC ..................................................................... 25
INTOSCIO .................................................................. 25
LP ............................................................................... 25
RC .............................................................................. 25
RCIO .......................................................................... 25
XT .............................................................................. 25
Oscillator Module ............................................................... 25
HFINTOSC ................................................................. 25
LFINTOSC ................................................................. 25
Oscillator Selection .......................................................... 283
Oscillator Start-up Timer (OST) ................................... 34, 53
Oscillator Switching
Fail-Safe Clock Monitor .............................................. 38
Two-Speed Clock Start-up ......................................... 35
Oscillator Transitions .......................................................... 26
Oscillator, Timer1 ..................................................... 139, 149
Oscillator, Timer3 ............................................................. 147
OSCTUNE Register ........................................................... 31
P
P1A/P1B/P1C/P1D.See Enhanced
Capture/Compare/PWM (ECCP) ............................. 165
Packaging Information ..................................................... 389
Marking .................................................................... 389
Parallel Slave Port (PSP) ......................................... 124, 133
Associated Registers ............................................... 134
CS (Chip Select) ...................................................... 133
PORTD .................................................................... 133
RD (Read Input) ....................................................... 133
Select (PSPMODE Bit) .................................... 124, 133
WR (Write Input) ...................................................... 133
PICSTART Plus Development Programmer .................... 352
PIE Registers ................................................................... 108
PIE1 Register ................................................................... 108
PIE2 Register ................................................................... 109
Pin Functions
MCLR/VPP/RE3 .................................................... 14, 18
OSC1/CLKI/RA7 .................................................. 14, 18
OSC2/CLKO/RA6 ................................................ 14, 18
RA0/AN0/C12IN0- ................................................ 15, 19
RA1/AN1/C12IN0- ...................................................... 19
RA1/AN1/C12IN1- ...................................................... 15
RA2/AN2/VREF-/CVREF/C2IN+ ............................. 15, 19
RA3/AN3/VREF+/C1IN+ ........................................ 15, 19
DS41303B-page 410
Advance Information
PIC18F2XK20/4XK20
TRISD Register ........................................................ 124
PORTE
Associated Registers ............................................... 129
LATE Register .......................................................... 127
PORTE Register ...................................................... 127
PSP Mode Select (PSPMODE Bit) .......................... 124
TRISE Register ........................................................ 127
Power Managed Modes ..................................................... 41
and A/D Operation ................................................... 253
and PWM Operation ................................................ 180
and SPI Operation ................................................... 191
Clock Transitions and Status Indicators ..................... 42
Effects on Clock Sources ........................................... 34
Entering ...................................................................... 41
Exiting Idle and Sleep Modes .................................... 46
by Interrupt ......................................................... 46
by Reset ............................................................. 46
by WDT Time-out ............................................... 46
Without a Start-up Delay .................................... 47
Idle Modes ................................................................. 43
PRI_IDLE ........................................................... 45
RC_IDLE ............................................................ 46
SEC_IDLE ......................................................... 45
Multiple Sleep Functions ............................................ 42
Run Modes ................................................................. 42
PRI_RUN ........................................................... 42
RC_RUN ............................................................ 43
SEC_RUN .......................................................... 42
Selecting .................................................................... 41
Sleep Mode ................................................................ 43
Summary (table) ........................................................ 41
Power-on Reset (POR) ...................................................... 51
Power-up Timer (PWRT) ........................................... 53
Time-out Sequence .................................................... 53
Power-up Delays ................................................................ 34
Power-up Timer (PWRT) ................................................... 34
Prescaler, Timer0 ............................................................. 137
PRI_IDLE Mode ................................................................. 45
PRI_RUN Mode ................................................................. 42
Program Counter ............................................................... 64
PCL, PCH and PCU Registers ................................... 64
PCLATH and PCLATU Registers .............................. 64
Program Memory
and Extended Instruction Set ..................................... 84
Code Protection ....................................................... 295
Instructions ................................................................. 68
Two-Word .......................................................... 68
Interrupt Vector .......................................................... 63
Look-up Tables .......................................................... 66
Map and Stack (diagram) ........................................... 63
Reset Vector .............................................................. 63
Program Verification and Code Protection ....................... 294
Associated Registers ............................................... 294
Programming, Device Instructions ................................... 299
PSP. See Parallel Slave Port.
PSTRCON Register ......................................................... 177
Pulse Steering .................................................................. 177
PUSH ............................................................................... 328
PUSH and POP Instructions .............................................. 65
PUSHL ............................................................................. 344
PWM (CCP Module)
Associated Registers ............................................... 161
PWM (ECCP Module)
Effects of a Reset ..................................................... 180
Operation in Power Managed Modes ...................... 180
R
RAM. See Data Memory.
RC_IDLE Mode .................................................................. 46
RC_RUN Mode .................................................................. 43
RCALL ............................................................................. 329
RCON Register .......................................................... 50, 112
Bit Status During Initialization .................................... 56
RCREG ............................................................................ 230
RCSTA Register .............................................................. 233
Reader Response ............................................................ 410
Register
RCREG Register ..................................................... 239
Register File ....................................................................... 74
Register File Summary ................................................ 7678
Registers
ADCON0 (ADC Control 0) ....................................... 255
ADCON1 (ADC Control 1) ............................... 256, 257
ADRESH (ADC Result High) with ADFM = 0) ......... 258
ADRESH (ADC Result High) with ADFM = 1) ......... 258
ADRESL (ADC Result Low) with ADFM = 0) ........... 258
ADRESL (ADC Result Low) with ADFM = 1) ........... 258
ANSEL (Analog Select 1) ........................................ 130
ANSEL (PORT Analog Control) ............................... 130
ANSELH (Analog Select 2) ...................................... 131
ANSELH (PORT Analog Control) ............................ 131
BAUDCTL (Baud Rate Control) ............................... 234
BAUDCTL (EUSART Baud Rate Control) ............... 234
CCP1CON (Enhanced Capture/Compare/PWM
Control) ............................................................ 163
CCP2CON (Standard Capture/Compare/
PWM Control) .................................................. 151
CM1CON0 (C1 Control) .......................................... 268
CM2CON0 (C2 Control) .......................................... 269
CM2CON1 (C2 Control) .......................................... 271
CONFIG1H (Configuration 1 High) .......................... 285
CONFIG2H (Configuration 2 High) .......................... 286
CONFIG2L (Configuration 2 Low) ........................... 286
CONFIG3H (Configuration 3 High) .......................... 287
CONFIG4L (Configuration 4 Low) ........................... 287
CONFIG5H (Configuration 5 High) .......................... 288
CONFIG5L (Configuration 5 Low) ........................... 288
CONFIG6H (Configuration 6 High) .......................... 289
CONFIG6L (Configuration 6 Low) ........................... 289
CONFIG7H (Configuration 7 High) .......................... 290
CONFIG7L (Configuration 7 Low) ........................... 290
CVRCON (Comparator Voltage Reference Control)
CVRCON Register ........................................... 275
CVRCON2 (Comparator Voltage Reference Control 2)
CVRCON2 Register ......................................... 276
DEVID1 (Device ID 1) .............................................. 291
DEVID2 (Device ID 2) .............................................. 291
ECCPAS (Enhanced CCP Auto-shutdown Control) 173
EECON1 (Data EEPROM Control 1) ................... 87, 96
HLVDCON (High/Low-Voltage Detect Control) ....... 277
INTCON (Interrupt Control) ..................................... 103
INTCON2 (Interrupt Control 2) ................................ 104
INTCON3 (Interrupt Control 3) ................................ 105
IPR1 (Peripheral Interrupt Priority 1) ....................... 110
IPR2 (Peripheral Interrupt Priority 2) ....................... 111
OSCCON (Oscillator Control) .................................... 27
Advance Information
DS41303B-page 411
PIC18F2XK20/4XK20
OSCTUNE (Oscillator Tuning) ................................... 31
PIE1 (Peripheral Interrupt Enable 1) ........................ 108
PIE2 (Peripheral Interrupt Enable 2) ........................ 109
PIR1 (Peripheral Interrupt Request 1) ..................... 106
PIR2 (Peripheral Interrupt Request 2) ..................... 107
PSTRCON (Pulse Steering Control) ........................ 177
PWM1CON (Enhanced PWM Control) .................... 176
RCON (Reset Control) ....................................... 50, 112
RCON (Reset control) .............................................. 112
RCSTA (Receive Status and Control) ...................... 233
SLRCON (PORT Slew Rate Control) ....................... 132
SSPCON1 (MSSP Control 1, I2C Mode) ................. 194
SSPCON1 (MSSP Control 1, SPI Mode) ................. 185
SSPCON2 (MSSP Control 2, I2C Mode) ................. 195
SSPMSK (SSP Mask) .............................................. 202
SSPSTAT (MSSP Status, SPI Mode) .............. 184, 193
STATUS ..................................................................... 79
STKPTR (Stack Pointer) ............................................ 65
T0CON (Timer0 Control) .......................................... 135
T1CON (Timer1 Control) .......................................... 139
T2CON (Timer2 Control) .......................................... 145
T3CON (Timer3 Control) .......................................... 147
TRISE (PORTE/PSP Control) .................................. 128
TXSTA (Transmit Status and Control) ..................... 232
WDTCON (Watchdog Timer Control) ....................... 293
RESET ............................................................................. 329
Reset State of Registers .................................................... 56
Resets ........................................................................ 49, 283
Brown-out Reset (BOR) ........................................... 283
Oscillator Start-up Timer (OST) ............................... 283
Power-on Reset (POR) ............................................ 283
Power-up Timer (PWRT) ......................................... 283
RETFIE ............................................................................ 330
RETLW ............................................................................. 330
RETURN .......................................................................... 331
Return Address Stack ........................................................ 64
Return Stack Pointer (STKPTR) ........................................ 65
Revision History ............................................................... 397
RLCF ................................................................................ 331
RLNCF ............................................................................. 332
RRCF ............................................................................... 332
RRNCF ............................................................................. 333
DS41303B-page 412
Advance Information
PIC18F2XK20/4XK20
Timer1 .............................................................................. 139
16-Bit Read/Write Mode ........................................... 141
Associated Registers ............................................... 144
Interrupt .................................................................... 142
Operation ................................................................. 140
Oscillator .......................................................... 139, 141
Oscillator Layout Considerations ............................. 142
Overflow Interrupt .................................................... 139
Resetting, Using the CCP Special Event Trigger ..... 142
Special Event Trigger (ECCP) ................................. 164
TMR1H Register ...................................................... 139
TMR1L Register ....................................................... 139
Use as a Real-Time Clock ....................................... 143
Timer2 .............................................................................. 145
Associated Registers ............................................... 146
Interrupt .................................................................... 146
Operation ................................................................. 145
Output ...................................................................... 146
Timer3 .............................................................................. 147
16-Bit Read/Write Mode ........................................... 149
Associated Registers ............................................... 150
Operation ................................................................. 148
Oscillator .......................................................... 147, 149
Overflow Interrupt ............................................ 147, 149
Special Event Trigger (CCP) .................................... 150
TMR3H Register ...................................................... 147
TMR3L Register ....................................................... 147
Timing Diagrams
A/D Conversion ........................................................ 385
Acknowledge Sequence .......................................... 217
Asynchronous Reception ......................................... 230
Asynchronous Transmission .................................... 226
Asynchronous Transmission (Back to Back) ........... 227
Auto Wake-up Bit (WUE) During Normal Operation 240
Auto Wake-up Bit (WUE) During Sleep ................... 241
Automatic Baud Rate Calculator .............................. 239
Baud Rate Generator with Clock Arbitration ............ 211
BRG Reset Due to SDA Arbitration During
Start Condition ................................................. 220
Brown-out Reset (BOR) ........................................... 372
Bus Collision During a Repeated Start
Condition (Case 1) ........................................... 221
Bus Collision During a Repeated Start
Condition (Case 2) ........................................... 221
Bus Collision During a Start Condition (SCL = 0) .... 220
Bus Collision During a Stop Condition (Case 1) ...... 222
Bus Collision During a Stop Condition (Case 2) ...... 222
Bus Collision During Start Condition (SDA only) ..... 219
Bus Collision for Transmit and Acknowledge ........... 218
Capture/Compare/PWM (CCP) ................................ 373
CLKO and I/O .......................................................... 370
Clock Synchronization ............................................. 204
Clock/Instruction Cycle .............................................. 67
Comparator Output .................................................. 263
Example SPI Master Mode (CKE = 0) ..................... 375
Example SPI Master Mode (CKE = 1) ..................... 376
Example SPI Slave Mode (CKE = 0) ....................... 377
Example SPI Slave Mode (CKE = 1) ....................... 378
External Clock (All Modes except PLL) .................... 369
Fail-Safe Clock Monitor (FSCM) ................................ 39
First Start Bit Timing ................................................ 212
Full-Bridge PWM Output .......................................... 170
Half-Bridge PWM Output ................................. 168, 175
High/Low-Voltage Detect Characteristics ................ 366
Advance Information
DS41303B-page 413
PIC18F2XK20/4XK20
(Slave Mode, CKE = 0) .................................... 377
(Slave Mode, CKE = 1) .................................... 378
External Clock Requirements .................................. 369
I2C Bus Data Requirements (Slave Mode) .............. 380
I2C Bus Start/Stop Bits Requirements (Slave Mode) .....
379
Master SSP I2C Bus Data Requirements ................ 382
Master SSP I2C Bus Start/Stop Bits Requirements . 381
Parallel Slave Port Requirements (PIC18F4X20) .... 374
PLL Clock ................................................................. 370
Reset, Watchdog Timer, Oscillator Start-up Timer,
Power-up Timer and Brown-out Reset
Requirements ................................................... 372
Timer0 and Timer1 External Clock Requirements ... 373
USART Synchronous Receive Requirements ......... 383
USART Synchronous Transmission Requirements . 383
Top-of-Stack Access .......................................................... 64
TRISE Register ................................................................ 128
PSPMODE Bit .......................................................... 124
TSTFSZ ............................................................................ 339
Two-Speed Clock Start-up Mode ....................................... 35
Two-Speed Start-up ......................................................... 283
Two-Word Instructions
Example Cases .......................................................... 68
TXREG ............................................................................. 225
TXSTA Register ............................................................... 232
BRGH Bit ................................................................. 235
W
Wake-up on Break ........................................................... 240
Watchdog Timer (WDT) ........................................... 283, 292
Associated Registers ............................................... 293
Control Register ....................................................... 293
Programming Considerations .................................. 292
WCOL ...................................................... 212, 213, 214, 217
WCOL Status Flag ................................... 212, 213, 214, 217
WDTCON Register .......................................................... 293
WWW Address ................................................................ 409
WWW, On-Line Support ...................................................... 8
X
XORLW ............................................................................ 339
XORWF ........................................................................... 340
V
Voltage Reference. See Comparator Voltage
Reference (CVREF)
Voltage References
Fixed Voltage Reference (FVR) ............................... 274
VR Stabilization ........................................................ 274
VREF. SEE ADC Reference Voltage
DS41303B-page 414
Advance Information
PIC18F2XK20/4XK20
THE MICROCHIP WEB SITE
CUSTOMER SUPPORT
Distributor or Representative
Local Sales Office
Field Application Engineer (FAE)
Technical Support
Development Systems Information Line
Customers
should
contact
their
distributor,
representative or field application engineer (FAE) for
support. Local sales offices are also available to help
customers. A listing of sales offices and locations is
included in the back of this document.
Technical support is available through the web site
at: http://support.microchip.com
Advance Information
DS41303B-page 415
PIC18F2XK20/4XK20
READER RESPONSE
It is our intention to provide you with the best documentation possible to ensure successful use of your Microchip product. If you wish to provide your comments on organization, clarity, subject matter, and ways in which our documentation
can better serve you, please FAX your comments to the Technical Publications Manager at (480) 792-4150.
Please list the following information, and use this outline to provide us with your comments about this document.
To:
RE:
Reader Response
From: Name
Company
Address
City / State / ZIP / Country
Telephone: (_______) _________ - _________
Application (optional):
Would you like a reply?
Device: PIC18F2XK20/4XK20
N
Literature Number: DS41303B
Questions:
1. What are the best features of this document?
2. How does this document meet your hardware and software development needs?
3. Do you find the organization of this document easy to follow? If not, why?
4. What additions to the document do you think would enhance the structure and subject?
5. What deletions from the document could be made without affecting the overall usefulness?
DS41303B-page 416
Advance Information
PIC18F2XK20/4XK20
PRODUCT IDENTIFICATION SYSTEM
To order or obtain information, e.g., on pricing or delivery, refer to the factory or the listed sales office.
PART NO.
/XX
XXX
Device
Temperature
Range
Package
Pattern
Examples:
a)
b)
Device:
Temperature
Range:
Package:
ML
P
PT
SO
SP
SS
Pattern:
= -40C to +125C
=
=
=
=
=
=
c)
(Extended)
QFN
PDIP
TQFP (Thin Quad Flatpack)
SOIC
Skinny Plastic DIP
SSOP
Note 1:
Advance Information
DS41303B-page 417
ASIA/PACIFIC
ASIA/PACIFIC
EUROPE
Corporate Office
2355 West Chandler Blvd.
Chandler, AZ 85224-6199
Tel: 480-792-7200
Fax: 480-792-7277
Technical Support:
http://support.microchip.com
Web Address:
www.microchip.com
India - Bangalore
Tel: 91-80-4182-8400
Fax: 91-80-4182-8422
India - New Delhi
Tel: 91-11-4160-8631
Fax: 91-11-4160-8632
Austria - Wels
Tel: 43-7242-2244-39
Fax: 43-7242-2244-393
Denmark - Copenhagen
Tel: 45-4450-2828
Fax: 45-4485-2829
India - Pune
Tel: 91-20-2566-1512
Fax: 91-20-2566-1513
France - Paris
Tel: 33-1-69-53-63-20
Fax: 33-1-69-30-90-79
Japan - Yokohama
Tel: 81-45-471- 6166
Fax: 81-45-471-6122
Germany - Munich
Tel: 49-89-627-144-0
Fax: 49-89-627-144-44
Atlanta
Duluth, GA
Tel: 678-957-9614
Fax: 678-957-1455
Boston
Westborough, MA
Tel: 774-760-0087
Fax: 774-760-0088
Chicago
Itasca, IL
Tel: 630-285-0071
Fax: 630-285-0075
Dallas
Addison, TX
Tel: 972-818-7423
Fax: 972-818-2924
Detroit
Farmington Hills, MI
Tel: 248-538-2250
Fax: 248-538-2260
Kokomo
Kokomo, IN
Tel: 765-864-8360
Fax: 765-864-8387
Los Angeles
Mission Viejo, CA
Tel: 949-462-9523
Fax: 949-462-9608
Santa Clara
Santa Clara, CA
Tel: 408-961-6444
Fax: 408-961-6445
Toronto
Mississauga, Ontario,
Canada
Tel: 905-673-0699
Fax: 905-673-6509
Australia - Sydney
Tel: 61-2-9868-6733
Fax: 61-2-9868-6755
China - Beijing
Tel: 86-10-8528-2100
Fax: 86-10-8528-2104
China - Chengdu
Tel: 86-28-8665-5511
Fax: 86-28-8665-7889
Korea - Gumi
Tel: 82-54-473-4301
Fax: 82-54-473-4302
China - Fuzhou
Tel: 86-591-8750-3506
Fax: 86-591-8750-3521
Korea - Seoul
Tel: 82-2-554-7200
Fax: 82-2-558-5932 or
82-2-558-5934
Malaysia - Penang
Tel: 60-4-646-8870
Fax: 60-4-646-5086
China - Qingdao
Tel: 86-532-8502-7355
Fax: 86-532-8502-7205
Philippines - Manila
Tel: 63-2-634-9065
Fax: 63-2-634-9069
China - Shanghai
Tel: 86-21-5407-5533
Fax: 86-21-5407-5066
Singapore
Tel: 65-6334-8870
Fax: 65-6334-8850
China - Shenyang
Tel: 86-24-2334-2829
Fax: 86-24-2334-2393
China - Shenzhen
Tel: 86-755-8203-2660
Fax: 86-755-8203-1760
Taiwan - Kaohsiung
Tel: 886-7-536-4818
Fax: 886-7-536-4803
China - Shunde
Tel: 86-757-2839-5507
Fax: 86-757-2839-5571
Taiwan - Taipei
Tel: 886-2-2500-6610
Fax: 886-2-2508-0102
China - Wuhan
Tel: 86-27-5980-5300
Fax: 86-27-5980-5118
Thailand - Bangkok
Tel: 66-2-694-1351
Fax: 66-2-694-1350
Italy - Milan
Tel: 39-0331-742611
Fax: 39-0331-466781
Netherlands - Drunen
Tel: 31-416-690399
Fax: 31-416-690340
Spain - Madrid
Tel: 34-91-708-08-90
Fax: 34-91-708-08-91
UK - Wokingham
Tel: 44-118-921-5869
Fax: 44-118-921-5820
China - Xian
Tel: 86-29-8833-7250
Fax: 86-29-8833-7256
12/08/06
DS41303B-page 418
Advance Information