6.08 X 8 Hardware Multiplier...........................................................................................................................................................61
14.0 Master Synchronous Serial Port (MSSP) Module ....................................................................................................................117
17.0 Low Voltage Detect ..................................................................................................................................................................175
18.0 Special Features of the CPU.................................................................................................................................................... 181
19.0 Instruction Set Summary..........................................................................................................................................................191
20.0 Development Support. .............................................................................................................................................................. 235
22.0 DC and AC Characteristics Graphs and Tables.......................................................................................................................273
Appendix D:Migration from Baseline to Enhanced Devices................................................... ....... .... .. .... .. ....................................284
Appendix E:Migration from Midrange to Enhanced Devices........................................................... .. .. .... .. .................................... 285
Appendix F:Migration from High-end to Enhanced Devices ......................................................................................................... 285
PIC18CXX2 Product Identification System ........................................................................................................................................ 295
To Our Valued Customers
Most Current Data Sheet
To obtain the most up-to-date version of this data sheet, please register at our Worldwide Web site at:
http://www.microchip.com
You can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page.
The last character of the literature number is the version number. e.g., DS30000A is version A of document DS30000.
New Customer Notification System
Register on our web site (www.microchip.com/cn) to receive the most current information on our products.
Errata
An errata sheet may exist for current devices, describing minor operational differences (from the data sheet) and recommended
workarounds. As device/documentation issues become known to us, w e will pub lish an errata sheet. The errata will specify the re vision 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:
• Microchip’s Worldwide Web site; http://www.microchip.com
• Your local Microchip sales office (see last page)
• The Microchip Corporate Literature Center; U.S. FAX: (602) 786-7277
When contacting a sales office or the literature center, please specify which device, revision of silicon and data sheet (include liter-
ature number) you are using.
Corrections to this Data Sheet
We constantly strive to improve the quality of all our products and documentation. We have spent a great deal of time to ensure
that this document is correct. However , w e realize that we ma y ha v e missed a f ew things . If y ou find any inf ormation that is missi n g
or appears in error, please:
• Fill out and mail in the reader response form in the back of this data sheet.
• E-mail us at webmaster@microchip.com.
We appreciate your assistance in making this a better document.
DS39026B-page 4Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
1.0DEVICE OVERVIEW
This document contains device-specific information for
the following four devices:
1.PIC18C242
2.PIC18C252
3.PIC18C442
4.PIC18C452
These devices come in 28 and 40-pin packages. The
28-pin devices do not have a Parallel Slave Port (PSP)
implemented and the numb er of Analog-to-Digital (A/D)
converter input channels is reduced to 5. An overview
of features is shown in Table 1-1.
The following two figures are device block diagrams
sorted by pin count; 28-pin f or Figur e 1-1 and 40-pin f or
Figure 1-2. The 28-pin and 40-pin pinouts are listed in
Table 1-2 and Table 1-3 respectively.
Note1: Optional multiplexing of CCP2 input/output with RB3 is enabled by selection of configuration bit.
2: The high order bits of the Direc t Address f or the RAM are from the BSR registe r (e xce pt f or the M O VF F
instruction).
3: Many of the general purpose I/O pins are multiplexed with one or more peripheral module functions.
The multiplexing combinations are device dependent.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 7
PIC18CXX2
TABLE 1-2:PIC18C2X2 PINOUT I/O DESCRIPTIONS
Pin Name
MCLR
/VPP
MCLR
VPP
NC————These pins should be left unconnected.
OSC1/CLKI
OSC1
CLKI
OSC2/CLKO/RA6
OSC2
CLKO
RA6
RA0/AN0
RA0
AN0
RA1/AN1
RA1
AN1
RA2/AN2/V
RA3/AN3/V
RA4/T0CKI
RA5/AN4/SS
RA6See the OSC2/CLKO/RA6 pin.
Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output
REF-
RA2
AN2
REF-
V
REF+
RA3
AN3
REF+
V
RA4
T0CKI
/LVDIN
RA5
AN4
SS
LVDIN
ST = Schmitt Trigger input with CMOS levels
I = Input O = Output
P = Power OD = Open Drain (no P diode to V
Pin Number
DIPSOICDescription
11
99
1010
22
33
44
55
66
77
Pin
Type
Buffer
Type
I
P
I
I
O
O
I/O
I/O
I
I/O
I
I/O
I
I
I/O
I
I
I/OIST/OD
I/O
I
I
I
STMaster clear (reset) input. This pin is an active low reset
ST
CMOS
—
—
TTL
TTL
Analog
TTL
Analog
TTL
Analog
Analog
TTL
Analog
Analog
ST
TTL
Analog
ST
Analog
to the device.
Programming voltage input.
Oscillator crystal input or external clock source input.
ST buffer when configured in RC mode. CMOS otherwise.
External clock source input. Always associated with
pin function OSC1. (See related OSC1/CLKIN,
OSC2/CLKOUT pins).
Oscillator crystal output. Connects to crystal or
resonator in crystal oscillator mode.
In RC mode, OSC2 pin outputs CLKOUT which has 1/4
the frequency of OSC1, and denotes the instruction
cycle rate.
General Purpose I/O pin.
PORTA is a bi-directional I/O port.
Digital I/O.
Analog input 0.
Digital I/O.
Analog input 1.
Digital I/O.
Analog input 2.
A/D Reference Voltage (Low) input.
Digital I/O.
Analog input 3.
A/D Reference Voltage (High) input.
Digital I/O. Open drain when configured as output.
Timer0 external cloc k input .
Digital I/O.
Analog input 4.
SPI Slave Select input.
Low Voltage Detect Input.
Digital I/O.
Capture1 input/Compare1 output/PWM1 output.
Digital I/O.
Synchronous serial clock input/output for SPI mode.
Synchronous serial clock input/output for I
Digital I/O.
SPI Data In.
2
C Data I/O.
I
Digital I/O.
SPI Data Out.
Digital I/O.
USART Asynchronous Transmit.
USART Synchronous Clock.
(See related RX/DT)
Digital I/O.
USART Asynchronous Receive.
USART Synchronous Data.
(See related TX/CK)
DD)
2
C mode
DS39026B-page 10Preliminary
7/99 Microchip Technology Inc.
TABLE 1-3:PIC18C4X2 PINOUT I/O DESCRIPTIONS
PIC18CXX2
Pin Name
MCLR
/VPP
MCLR
VPP
NC———These pins should be left unconnected.
OSC1/CLKI
OSC1
CLKI
OSC2/CLKO/RA6
OSC2
CLKO
RA6
RA0/AN0
RA0
AN0
RA1/AN1
RA1
AN1
RA2/AN2/V
RA3/AN3/V
RA4/T0CKI
RA5/AN4/SS
RA6See the OSC2/CLKO/RA6 pin.
Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output
REF-
RA2
AN2
REF-
V
REF+
RA3
AN3
REF+
V
RA4
T0CKI
/LVDIN
RA5
AN4
SS
LVDIN
ST = Schmitt Trigger input with CMOS levels
I = Input O = Output
P = Power OD = Open Drain (no P diode to V
Pin Number
DIPPLCC TQFPDescription
1218
131430
141531
2319
3420
4521
5622
6723
7824
Pin
Type
I/O
I/O
I/O
I/O
I/O
I/OIST/OD
I/O
Buffer
Type
I
P
I
I
O
O
I
I
I
I
I
I
I
I
I
STMaster clear (reset) input. This pin is an active
ST
CMOS
—
—
TTL
TTL
Analog
TTL
Analog
TTL
Analog
Analog
TTL
Analog
Analog
ST
TTL
Analog
ST
Analog
low reset to the device.
Programming voltage input.
Oscillator crystal input or external clock
source input. ST buffer when configured in
RC mode. CMOS otherwise.
External clock source input. Always
associated with pin function OSC1. (See
related OSC1/CLKIN, OSC2/CLKOUT pins).
Oscillator crystal output. Connects to crystal
or resonator in crystal oscillator mode.
In RC mode, OSC2 pin outputs CLKOUT,
which has 1/4 the frequency of OSC1 and
denotes the instruction cycle rate.
General Purpose I/O pin.
PORTA is a bi-directional I/O port.
Digital I/O.
Analog input 0.
Digital I/O.
Analog input 1.
Digital I/O.
Analog input 2.
A/D Reference Voltage (Low) input.
Digital I/O.
Analog input 3.
A/D Reference Voltage (High) input.
Digital I/O . Open dr ain when config ured as outpu t.
Timer0 external clock input.
Digital I/O.
Analog input 4.
SPI Slave Select input.
Low Voltage Detect Input.
SS12, 31 13, 34 6, 29P—Ground reference for logic and I/O pins.
ST = Schmitt Trigger input with CMOS levels
I = Input O = Output
P = Power OD = Open Drain (no P diode to V
Pin Number
DIPPLCC TQFPDescription
8925I/O
91026I/O
101127I/O
Pin
Type
Buffer
Type
TTL
TTL
TTL
TTL
TTL
TTL
TTL
TTL
ST
TTL
Analog
ST
TTL
Analog
ST
TTL
Analog
PORTD is a bi-directional I/O port.
Parallel Slave Po rt (PSP) for interfacing to a microprocessor port. These pins have TTL input buffers when
PSP module is enabled.
Digital I/O.
Parallel Slave Port Data.
Digital I/O.
Parallel Slave Port Data.
Digital I/O.
Parallel Slave Port Data.
Digital I/O.
Parallel Slave Port Data.
Digital I/O.
Parallel Slave Port Data.
Digital I/O.
Parallel Slave Port Data.
Digital I/O.
Parallel Slave Port Data.
Digital I/O.
Parallel Slave Port Data.
PORTE is a bi-directional I/O port.
Digital I/O.
Read control for parallel slave port.
(See also WR
Analog input 5.
Digital I/O.
Write control for parallel slave port.
(See CS
Analog input 6.
Digital I/O.
Chip Select control for parallel slave port.
(See related RD
Analog input 7.
and CS pins)
and RD pins)
and WR)
DD)
DS39026B-page 14Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
2.0OSCILLATOR
CONFIGURATIONS
2.1Oscillator Types
The PIC18CXX2 can be operated in eight different
oscillator modes. The user can program three configuration bits (FOSC 2, FOSC 1, an d FOSC0 ) to s elect one
of these eight modes:
1.LPLow Power Crystal
2.XTCrystal/Resonator
3.HSHigh Speed Crystal/Resonator
4.HS + PLL High Speed Crystal/Resonator with
PLL enabled
5.RCExternal Resistor/Capacitor
6.RCIOExternal Resistor/Capacitor with
I/O pin enabled
7.ECExternal Clock
8.ECIOExternal Clock with I/O pin enabled
2.2Crystal Oscillator/Ceramic
Resonators
In XT, LP, HS or HS-PLL oscillator modes, a crystal or
ceramic resonator is connected to the OSC1 and
OSC2 pins to establish oscillation. Figure 2-1 shows
the pin connec tions. An ex ternal clock source ma y als o
be connected to the OSC1 pin in these modes, as
shown in Figure2-2.
The PIC18CXX2 oscillat or desi gn requ ires th e use o f a
parallel cut crystal.
Note:Use of a series cut crystal may give a fre-
quency out of the crystal manufacturers
specifications.
FIGURE 2-1:CRYSTAL/CERAMIC
RESONATOR OPERATION
(HS, XT OR LP
OSC CONFIGURATION)
(1)
C1
(1)
C2
Note 1: See Table 2-1 and Table 2-2 for recom-
Note 2: A series resistor (RS) may be required
Note 3: R
OSC1
XTAL
(2)
RS
OSC2
mended values of C1 and C2.
for AT strip cut crystals.
F varies with the crystal chosen.
RF
(3)
SLEEP
PIC18CXXX
To
internal
logic
FIGURE 2-2:EXTERNAL CLOCK INPUT
OPERATION (HS, XT OR LP
OSC CONFIGURATION)
Clock from
ext. system
Open
OSC1
PIC18CXXX
OSC2
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 15
PIC18CXX2
TABLE 2-1:CERAMIC RESONATORS
Ranges Tested:
ModeFreqOSC1OSC2
XT455 kHz
2.0 MHz
4.0 MHz
HS8.0 MHz
16.0 MHz
These values are for design guidance only. See
notes at bottom of page.
68 - 100 pF
15 - 68 pF
15 - 68 pF
10 - 68 pF
10 - 22 pF
68 - 100 pF
15 - 68 pF
15 - 68 pF
10 - 68 pF
10 - 22 pF
Resonators Used:
455 kHzPanasonic EFO-A455K04B
2.0 MHzMurata Erie CSA2.00MG
4.0 MHzMurata Erie CSA4.00MG
8.0 MHzMurata Erie CSA8.00MT
16.0 MHzMurata Erie CSA16.00MX
All resonators used did not have built-in capacitors.
0.3%
±
0.5%
±
0.5%
±
0.5%
±
0.5%
±
TABLE 2-2:CAPACITOR SELECTION FOR
CRYSTAL OSCILLATOR
Osc Type
LP32.0 kHz33 pF33 pF
XT200 kHz47-68 pF47-68 pF
HS4.0 MHz15 pF15 pF
32.0 kHzEpson C-001R32.768K-A± 20 PPM
200 kHzSTD XTL 200.000KHz± 20 PPM
1.0 MHzECS ECS-10-13-1± 50 PPM
4.0 MHzECS ECS-40-20-1± 50 PPM
8.0 MHzEPSON CA-301 8.000M-C± 30 PPM
20.0 MHzE PS ON CA-301 20.000M-C± 30 PPM
Crystal
Freq
200 kHz15 pF15 pF
1.0 MHz15 pF15 pF
4.0 MHz15 pF15 pF
8.0 MHz15-33 pF15-33 pF
20.0 MHz15-33 pF15-33 pF
25.0 MHzTBDTBD
These values are for design guidance only. See
notes at bottom of page.
Cap. Range
C1
Crystals Used
Cap.
Range
C2
Note1: Recommended values of C1 and C2 are
identical to the ranges tested (Table 2-1).
2: Higher capacitance increases the stability
of the oscillator, but also increases the st artup time.
3: Since each r esonator/crystal has its own
characteristics, the user should consult the
resonator/crystal manufacturer for app ropri ate values of external components.
4: Rs may be required in HS mode, as well as
XT mode, to avoid overdriving crystals with
low drive level specification.
2.3RC Oscillator
For timing insensitive applications, the “RC” and
"RCIO" device options offer additional cost savings.
The RC oscillator frequency is a function of the supply
voltage, the resistor (R
EXT) and capacitor ( CEXT) val-
ues and the operating temperature. In addition to this,
the oscillator frequency will vary from unit to unit due
to normal process parameter variation. Furthermore,
the difference in lead frame capacitance between
package types will also affect the oscillation frequency,
especially for low C
EXT values. The user also needs to
take into account variation due to tolerance of external
R and C components used. Figure 2-3 shows how the
R/C combination is connected.
In the RC oscillator mode, the oscillator frequency
divided by 4 is available on the OSC2 pin. This signal
may be used for test purposes or to synchronize other
logic.
FIGURE 2-3:RC OSCILLATOR MODE
VDD
REXT
OSC1
CEXT
VSS
F
Recommended values: 3 kΩ ≤ REXT ≤ 100 k
OSC/4
OSC2/CLKO
EXT > 20pF
C
Internal
clock
PIC18CXXX
Ω
The RCIO oscillator mode functions like the RC mode,
except that the OSC2 pin becomes an additional general purpose I/O pin. The I/O pin becomes bit 6 of
PORTA (RA6).
2.4External Clock Input
The EC and ECIO oscillator mode s require an e xternal
clock source to be connected to the OSC1 pi n. The
feedback device between OSC1 and OSC2 is turned
off in these modes to sa v e current. There is no osc illator startup time required after a Power-On-Reset or
after a recovery from SLEEP mode.
In the EC oscillator mode, the oscillator frequency
divided by 4 is available on the OSC2 pin. This signal
may be used for test purposes or to synchronize other
logic. Figure 2-4 shows the pin conne cti ons for the EC
oscillator mode.
DS39026B-page 16Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
FIGURE 2-4:E XTERN AL CLOCK INPUT
OPERATION
(EC OSC CONFIGURATION)
Clock from
ext. system
OSC/4
F
The ECIO oscillator mode functions like the EC mode,
except that the OSC 2 pin be comes a n addit ional general purpose I/O pin. The I/O pin becomes Bit 6 of
PORTA (RA6). Figure 2-5 shows the pin connections
for the ECIO oscillator mode.
OSC1
PIC18CXXX
OSC2
FIGURE 2-5:E XTERN AL CLOCK INPUT
OPERATION
(ECIO CONFIGURATION)
Clock from
ext. system
RA6
OSC1
PIC18CXXX
I/O (OSC2)
2.5HS/PLL
A Phase Locked Loop circuit is provided as a programmable option for users that want to multiply the frequency of the incoming crystal oscillator signal by 4.
For an input clock frequency of 10 MHz, the internal
clock frequency will be multiplied to 40 MHz. This is
useful for customers who are concerned with EMI due
to high frequency crystals.
The PLL can only be enabled when the oscillator configuration bits are programmed for HS mode. If they
are programmed for any other mode, the PLL is not
enabled and the system clock will come directly from
OSC1.
The PLL is one of the modes of the FOSC<2:0> configuration bits. The oscillator mode is specified during
device programming.
A PLL lock timer is used to ensure that the PLL has
locked before device execution starts. The PLL lock
timer has a time-out that is called T
PLL.
FIGURE 2-6:PLL BLOCK DIAGRAM
(from configuration
HS Osc
bit register)
PLL Enable
OSC2
Phase
Comparator
IN
F
Crystal
Osc
FOUT
OSC1
Loop
Filter
Divide by 4
VCO
SYSCLK
MUX
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 17
PIC18CXX2
2.6Oscillator Switching Feature
The PIC18CXX2 devices include a feature that allows
the system clock source to be switched from the main
oscillator to an alternate low frequency clock source.
For the PIC18CXX2 devices, this alternate clock
source is the Timer1 oscilla tor . If a low-freque ncy crystal (32 KHz, for example) has been attached to the
Timer1 oscillator pins and the Timer1 oscillator has
FIGURE 2-7:DEVICE CLOCK SOURCES
PIC18CXXX
OSC2
OSC1
T1OSO
T1OSI
Main Oscillator
Sleep
Timer1 Oscillator
T1OSCEN
Enable
Oscillator
been enabled, the device can switch to a low power
execution mode. Figure2-7 shows a block diagram of
the system clock sources. The clock switching feature
is enabled by programming the Oscillator Switching
Enable (OSCSEN
) bit in Configuration R eg ist er1 H to a
’0’. Clock switching is disabled in an erased device.
See Section 9 f or further details of the T imer1 oscillator .
See Section 18.0 for Configuration Register details.
4 x PLL
TOSC
TT1P
Clock Source option
for other modules
Tosc/4
MUX
Clock
Source
TSCLK
2.6.1SYSTEM CLOCK SWITCH BIT
The system clock source switching is performed under
software control. The system clock switch bit, SCS
(OSCCON<0>) controls the cloc k s wi tching . Whe n the
SCS bit is ’0’, the system clock source comes from the
main oscillator that is sel ect ed by the FOSC configur ation bits in Configuration Register1H. When the SCS
bit is set, th e system clock sour ce will come from the
Timer1 oscillator. The SCS bit is cleared on all forms
of reset.
Register 2-1:OSCCON Register
U-0U-0U-0U-0U-0U-0U-0R/W-1
———————
bit 7bit 0
bit 7-1Unim plemented: Read as '0'
bit 0SCS: System Clock Switch bit
OSCSEN configuration bit = ’0’ and T1OSCEN bit is set:
when
1 = Switch to Timer1 Oscillator/Clock pin
0 = Use primary Oscillator/Clock input pin
OSCSEN and T1OSCEN are in other states:
when
bit is forced clear
Note:The Timer1 oscillator must be enabled to
switch the system clock source. The
Timer1 osci llator i s enabled by setting t he
T1OSCEN bit in the Time r1 control register
(T1CON). If the Timer1 oscillator is not
enabled, then any write to the SCS bit will
be ignored (SCS bit forced cleared) and
the main oscillator will continue to be the
system clock source.
SCS
Legend
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
DS39026B-page 18Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
2.6.2OSCILLATOR TRANSITIONS
A timing diagram indicating the transition from the
main oscillator to the Timer1 oscillator is shown in
The PIC18CXX2 devices contain circuitry to prevent
"glitches" when switching between oscillator sources.
Essential ly, the circuitry waits for eigh t ri sing ed ges of
the clock s ource t hat the pro cessor is s wi tching to . This
ensures that the new clock source is stable and that its
pulse width will not be less than the shortest pulse
Figure 2-8. The Timer1 oscillator is assumed to be
running all the time. After the SCS bit is set, the processor is frozen at the next occurring Q1 cycle. After
eight synchronization cycles are counted from the
Timer1 oscillator, operation resumes. No additional
delays are required after the synchronization cycles.
width of the two clock sources.
FIGURE 2-8:TIMING DIAGRAM FOR TRANSITION FROM OSC1 TO TIMER1 OSCILLATOR
Q1
T1OSI
OSC1
Internal
System
Clock
SCS
(OSCCON<0>)
Program
Counter
TOSC
Q1
TDLY
TT1P
21345678
Tscs
PC + 2PC
Note1: Delay on internal system clock is eight oscillator cycles for synchronization.
Q3Q2Q1Q4Q3Q2
Q4Q1
Q2Q3Q4Q1
PC + 4
The sequence of events that takes place when switching from the Timer1 oscillator to the main oscillator will
depend on the mode of the main oscillator. In addition
to eight clock cycles of the main oscillator, additional
delays may take place.
If the main oscillator is configured for an external crystal (HS, XT, LP), then the transition will take place after
an oscillator startup time (T
OST) has occurred. A timing
diagram indicating the transition from the Timer1 oscillator to the main oscillator for HS, XT and LP modes is
shown in Figure 2-9.
FIGURE 2-9:TIMING FOR TRANSITION BETWEEN TIMER1 AND OSC1 (HS,XT,LP)
Q1 Q2 Q3 Q4 Q1 Q2
T1OSI
OSC1
OSC2
Internal System
Clock
SCS
(OSCCON<0>)
Program Counter
Q3Q4
PCPC + 2
Q1
TOST
Note1: TOST = 1024TOSC (drawing not to scale).
TT1P
12345678
TSCS
TOSC
Q3
PC + 6
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 19
PIC18CXX2
If the main oscillator is configured f or HS-PLL mode , an
oscillator startup time (T
timeout (T
PLL) will occur . The PL L timeout is typic ally 2
OST) plus an additional PLL
quency. A timing diagram indicating the tran sitio n from
the Timer1 oscillator to the main oscillator for HS-PLL
mode is shown in Figure 2 -10.
ms and allows the PLL to lock to the main o scillato r fre-
FIGURE 2-10: TIMING FOR TRANSITION BETWEEN TIMER1 AND OSC1 (HS WITH PLL)
Q4Q1
T1OSI
OSC1
OSC2
PLL Clock
Input
Internal System
Program Counter
Clock
SCS
(OSCCON<0>)
TOST
PCPC + 2
TPLL
Note 1: TOST = 1024TOSC (drawing not to scale).
TT1P
TOSC
1 234 5678
TSCS
Q1 Q2 Q3 Q4 Q1 Q2
PC + 4
Q3
Q4
If the main os ci lla tor is c on fig ured in the RC, RCIO, EC
or ECIO modes, there is no oscillator startup timeout.
Operation will resume after eight cycles of the main
ing the transition from the Timer1 oscillator to the main
oscillator for R C, RCIO , EC and EC IO modes is shown
in Figure 2-11.
oscillator ha ve been count ed. A timing diagram indicat-
FIGURE 2-11: TIMING FOR TRANSITION BETWEEN TIMER1 AND OSC1 (RC, EC)
Q3Q4
T1OSI
OSC1
OSC2
Internal System
Clock
SCS
(OSCCON<0>)
Program Counter
PCPC + 2
Note 1: RC oscillator mode assumed.
Q1
TOSC
1
TT1P
23
45678
TSCS
Q1 Q2 Q3 Q4 Q1 Q2 Q3
Q4
PC + 4
DS39026B-page 20Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
2.7Effects of Sleep Mode on the On-chip
Oscillator
When the device e xec utes a SLEEP instruction, the onchip clocks and oscillator are turned off and the device
is held at the beginning of an instruction cycle (Q1
state). With the oscill ato r off, the OSC1 and OSC2 si gnals will stop oscillating. Since all the transistor switch-
ing currents have been rem o ved, sleep mode achiev e s
the lowest current consumption of the device (only
leakage currents). Enabling any on-chip feature that
will operate during sleep will increase the current consumed during sleep. The user can wake from SLEEP
through external reset, Watchdog Timer Reset or
through an interrupt.
TABLE 2-3:OSC1 AND OSC2 PIN STATES IN SLEEP MODE
OSC ModeOSC1 PinOSC2 Pin
RCFloating, external resistor should pull
high
RCIOFloating, external resistor should pull
high
ECIOFloatingConfigured as Port A, bit 6
ECFloatingAt logic low
LP, XT, and HSFeedback inverter disabled, at quies-
cent voltage level
See Table 3-1, in the “Reset” section, for time-outs due to Sleep and MCLR
2.8Power-up Delays
Pow er up dela ys are control led by two timers, so that n o
external reset circuitry is required f or most applic ations.
The delays ensure that the device is kept in RESET
until the device power supply and clock are stable. For
additional information on RESET operation, see the
“Reset” section.
The first timer is the Power-up Timer (PWRT), which
optionally provides a fixed delay of 72 ms (nominal) on
power-up only (POR and BOR). The second timer is
the Oscillator Start-up Timer OS T, intended to keep th e
chip in RESET until the crystal oscillator is stable.
With the PLL enabled (HS/PLL oscillator mode), the
time-out sequence following a power-on reset is different from other oscilla tor modes. Th e time-out seque nce
is as follows : First the PWR T time-o ut is inv ok ed after a
POR time delay has expired. Then the Oscillator Startup Timer (OST) is invoked. However, this is still not a
sufficient amount o f time to al low the PL L to loc k at high
frequencies. The PWRT timer is used to provide an
additional fix ed 2ms (nominal) ti me-out to a llow the PLL
ample time to lock to the incoming clock frequency.
At logic low
Configured as P o rt A, bit 6
Feedback inverter disabled, at quiescent voltage level
reset.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 21
PIC18CXX2
NOTES:
DS39026B-page 22Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
3.0RESET
The PIC18CXXX differentiates between various kinds
of reset:
e)Programmable Brown-out Reset (BOR)
f)Reset Instruction
g)Stack Full rese t
h)Stack Underflow reset
Most registers are unaffected by a reset. Their status is
unknown on POR and unchanged by all other resets.
The other registers are forced to a “reset state” on
reset during normal operation
reset during SLEEP
operation)
Power-on Reset, MCLR
CLR reset during SLEEP and by the RESET instruc-
M
tion.
Most registers are not affected by a WDT wake-up,
since this is viewed as the resumption of normal operation. Stat us bit s from th e RCON r egiste r, RI
and BOR, are set or clea red diff er ently in diff ere nt
POR
reset situations, as indicated in Table 3-2. These bits
are used in software to determine the nature of the
reset. See Table 3-3 for a full descripti on of the rese t
states of all registers.
A simplified bl ock diag ram of the on-ch ip res et circui t is
shown in Figure 3-1.
The Enhanced MCU devices have a MCLR
in the MCLR
reset path. The filter wi ll detect a nd ignore
small pulses.
, WDT reset, Brown-out Reset,
A WDT reset does not drive MCLR pin low.
FIGURE 3-1:SIMPLIFIED BLOCK DIAGRAM OF ON-CHIP RESET CIRCUIT
RESET
Instruction
Stack
Pointer
Stack Full/Underflow Reset
External Reset
, TO, PD,
noise filter
MCLR
SLEEP
WDT
Time-out
Reset
Power-on Reset
BOREN
OST
10-bit Ripple counter
PWRT
10-bit Ripple counter
Enable PWRT
Enable OST
(2)
VDD
OSC1
Module
V
Brown-out
OST/PWRT
On-chip
RC OSC
WDT
DD rise
detect
Reset
(1)
Note1: This is a separate oscillator from the RC oscillator of the CLKIN pin.
2: See Table 3-1 for time-out situations.
S
Chip_Reset
R
Q
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 23
PIC18CXX2
3.1Power-On Reset (POR)
A Power-on Reset pulse is generated on-chip when
V
DD rise is detected. To take advantag e of the P OR cir-
cuitry , ju st tie the MC LR
tor) to V
DD. This will elimi nate e xternal R C compon ents
pin directly (or th rough a resi s-
usually needed to create a Power-on Reset delay. A
maximum rise time for VDD is specified (parameter
D004). For a slow rise time, see Figure 3-2.
When the device starts normal operation (exits the
reset condition), d evice operating p arameters (vol tage,
frequency , temperature ,...) must be m et to ensure operation. If these conditions are not met, the device must
be held in reset until the operating conditions are met.
Brown-out Reset may be used to meet the voltage
start-up condition.
FIGURE 3-2:EXTERN AL POWER-ON
RESET CIRCUIT (FOR SLOW
DD POWER-UP)
V
V
DD
D
R
R1
MCLR
C
Note1: External Po wer-on Reset cir cuit is required
only if the V
DD power-up s lop e i s too s low .
The diode D hel ps d isch arge th e ca pacito r
quickly when V
DD powers down.
2: R < 40 kΩ is recommended to make sure
that the voltage drop across R does not
violate the device’s electrical specification.
3: R1 = 100Ω to 1 kΩ will limit any current
flowing into MCLR
C in the event of MCLR/
down due to Electrostatic Discharge
(ESD) or Electrical Overstress (EOS).
PIC18CXXX
from external capacitor
VPP pin break-
3.2Power-up Timer (PWRT)
The Power-up Timer provides a fixed nominal time-out
(parameter #33) only on power-up from the POR. The
Power-up Timer operates on an internal RC oscillator.
The chip is ke pt in reset a s lon g as the PW R T i s act iv e .
The PWRT’s time delay allows VDD to rise to an acceptable level. A configuration bit is provided to enable/disable the PWRT.
The power-up ti me dela y will v ary from chip-to-chi p due
DD, temperature and process variation. See DC
to V
parameter #33 for details.
3.3Oscillator Start-up Timer (OST)
The Oscillator Start-up Timer (OST) provides 1024
oscillator cycle (from OSC1 input) delay after the
PWRT dela y is ov er (parameter #3 2). This ensures th at
the crystal oscillator or reso nator has started and stabilized.
The OST time-out is invoked only for XT, LP and HS
modes and only on Power-on Reset or wake-up from
SLEEP.
3.4PLL Lock Timeout
With the PLL enabled, the timeout sequence following
a power-on reset is different from other oscillator
modes. A portion of the P ow er-up Timer is use d to provide a fixed timeout that is sufficient for the PLL to lock
to the main oscillator fre que nc y. This PLL lock timeout
PLL) is typically 2 ms and follows the oscillator startup
(T
timeout (OST).
3.5Brown-Out Reset (BOR)
A configuration bit, BOREN, can disable (if clear/programmed) or enable (if set) the Brown-out Reset circuitry. If VDD falls below parameter D005 for greater
than parameter #35, the brown-out situation will reset
the chip. A reset may not occur if V
parameter D005 for less than parameter #35. The chip
will remain in Brown-out Reset until VDD rises above
DD. The Po wer-up Tim er will then be in vok ed and will
BV
keep the chip in RESET an additional time delay
(parameter #33). If VDD drops below BVDD while the
Power-up Timer is running, the chip will go back into a
Brown-out Reset and the Power-up Timer will be initialized. Once V
DD rises abo v e BV DD, the Power-up Timer
will execute the additional time delay.
DD falls below
DS39026B-page 24Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
3.6Time-out Sequence
On power-up , the time-out sequence is as f ollows: First,
PWRT time-out is invoked after the POR time delay has
expired. Then, OST is activated. The total time-out will
vary based on oscillat or config urati on and the s tatu s of
the PWRT. For example, in RC mode with the PWRT
disabled, there will be no time-out at all. Figure 3-3,
Figure 3-4, Figure 3-5, Figure 3-6 and Figure 3-7
depict time-out sequences on power-up.
Since the time-outs oc cur from the POR p ulse, if MC LR
is kept low long enough, the time-outs will expire.
Bringing MCLR
(Figure 3-5). This is useful for testing purposes or to
synchronize more than one PIC18CXXX device ope rating in parallel.
Table 3-2 shows the re set condi tio ns for som e Spe cial
Function Registers, while Table 3-3 shows the reset
conditions for all the registers.
high will begin execution immediately
TABLE 3-1:TIME-OUT IN VARIOUS SITUATIONS
Oscillator
Configuration
HS with PLL enabled
HS, XT, LP72 ms + 1024Tosc1024Tosc72 ms + 1024Tosc1024Tosc
EC72 ms—72 ms—
External RC72 ms—72 ms—
Note1: 2 ms = Nominal time required for the 4x PLL to lock.
2: 72 ms is the nominal power-up timer delay
Power-up
(1)
72 ms + 1024Tosc + 2ms 1024Tosc + 2 ms 72 ms + 1024To sc + 2ms 1024Tosc + 2 ms
(2)
Brown-out
(2)
Wake-up from
SLEEP or
Oscillator SwitchPWRTE = 0PWRTE = 1
Register 3-1:RCON Register Bits and Positions
R/W-0R/W-0U-0R/W-1R/W-1R/W-1R/W-1R/W-1
IPENLWRT
bit 7bit 0
—RITOPDPORBOR
TABLE 3-2:STATUS BITS, THEIR SIGNIFICANCE AND THE INITIALIZATION CONDITION FOR
RCON REGISTER
Program
Condition
Power-on Reset0000h00-1 110011100uu
Reset during normal
MCLR
operation
Software Reset during normal
operation
Stack Full Reset during normal
operation
Stack Underflow Reset during
normal operation
MCLR Reset during SLEEP0000h00-u 10uuu10uuuu
WDT Reset0000h0u-u 01uu101uuuu
WDT Wake-upPC + 2uu-u 00uuu00uuuu
Brown-out Reset0000h0u-1 11u011110uu
Interrupt wake-up from SLEEP
Legend: u = unchanged, x = unknown, — = unimplemented bit read as '0'.
Note1: 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 (0x000008h or 0x000018h).
Counter
0000h00-u uuuuuuuuuuu
0000h0u-0 uuuu0uuuuuu
0000h0u-u uu11uuuuuu1
0000h0u-u uu11uuuuu1u
PC + 2
RCON
RegisterRITOPDPORBORSTKFULSTKUNF
(1)
uu-u 00uuu10uuuu
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 25
PIC18CXX2
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS
POSTINC0242442252452N/AN/AN/A
POSTDEC0 242442252452N/AN/AN/A
PREINC0242442252452N/AN/AN/A
PLUSW0242442252452N/AN/AN/A
FSR0H242442252452---- 0000---- 0000---- uuuu
FSR0L242442252452xxxx xxxxuuuu uuuuuuuu uuuu
WREG242442252452xxxx xxxxuuuu uuuuuuuu uuuu
INDF1242442252452N/AN/AN/A
POSTINC1242442252452N/AN/AN/A
POSTDEC1 242442252452N/AN/AN/A
PREINC1242442252452N/AN/AN/A
PLUSW1242442252452N/AN/AN/A
Legend: u = unchanged, x = unknown, - = unimplemented bit, read as ’0’, q = value depends on condition
Note1: One or more bits in the INTCONx or PIRx registers will be affected (to cause wake-up).
2: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the PC is loaded with the interrupt
vector (0008h or 0018h).
3: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the TOSU, TO SH and TOSL are
updated with the current value of the PC. The STKPTR is modified to point to the next location in the hardware stack.
4: See Table 3-2 for reset value for specific condition.
5: Bit 6 of PORTA, LATA, and TRISA are enabled in ECIO and RCIO oscillator modes only. In all other
oscillator modes, they are disabled and read ’0’.
6: The long write enable is only reset on a POR or MCLR
7: Bit 6 of PORTA, LATA and TRISA are not available on all devices. When unimplemented, they are read ’0’.
Reset Instruction
Stack Resets
reset.
Wake-up via WDT
or Interrupt
---0 uuuu
uuuu uuuu
uuuu uuuu
uu-u uuuu
PC + 2
uuuu uuuu
uuuu -u-u
uu-u u-uu
(3)
(3)
(3)
(3)
(2)
(1)
(1)
(1)
DS39026B-page 26Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS (Cont.’d)
Note1: One or more bits in the INTCONx or PIRx registers will be affected (to cause wake-up).
2: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the PC is loaded with the interrupt
3: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the TOSU, TO SH and TOSL are
4: See Table 3-2 for reset value for specific condition.
5: Bit 6 of PORTA, LATA, and TRISA are enabled in ECIO and RCIO oscillator modes only. In all other
6: The long write enable is only reset on a POR or MCLR
7: Bit 6 of PORTA, LATA and TRISA are not available on all devices. When unimplemented, they are read ’0’.
24244225245200-1 11q000-1 qquuuu-u qquu
vector (0008h or 0018h).
updated with the current value of the PC. The STKPTR is modified to point to the next location in the hard-
ware stack.
oscillator modes, they are disabled and read ’0’.
Brown-out Reset
Reset Instruction
Stack Resets
reset.
Wake-up via WDT
or Interrupt
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 27
PIC18CXX2
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS (Cont.’d)
Legend: u = unchanged, x = unknown, - = unimplemented bit, read as ’0’, q = value depends on condition
Note1: One or more bits in the INTCONx or PIRx registers will be affected (to cause wake-up).
2: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the PC is loaded with the interrupt
3: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the TOSU, TO SH and TOSL are
4: See Table 3-2 for reset value for specific condition.
5: Bit 6 of PORTA, LATA, and TRISA are enabled in ECIO and RCIO oscillator modes only. In all other
6: The long write enable is only reset on a POR or MCLR
7: Bit 6 of PORTA, LATA and TRISA are not available on all devices. When unimplemented, they are read ’0’.
Legend: u = unchanged, x = unknown, - = unimplemented bit, read as ’0’, q = value depends on condition
Note1: One or more bits in the INTCONx or PIRx registers will be affected (to cause wake-up).
2: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the PC is loaded with the interrupt
vector (0008h or 0018h).
3: When the wake-up is due to an interrupt and the GIEL or GIEH bit is set, the TOSU, TO SH and TOSL are
updated with the current value of the PC. The STKPTR is modified to point to the next location in the hardware stack.
4: See Table 3-2 for reset value for specific condition.
5: Bit 6 of PORTA, LATA, and TRISA are enabled in ECIO and RCIO oscillator modes only. In all other
oscillator modes, they are disabled and read ’0’.
6: The long write enable is only reset on a POR or MCLR
7: Bit 6 of PORTA, LATA and TRISA are not available on all devices. When unimplemented, they are read ’0’.
Reset Instruction
Stack Resets
-111 1111
-uuu uuuu
-u0u 0000
reset.
(5)
(5)
(5)
Wake-up via WDT
or Interrupt
-uuu uuuu
-uuu uuuu
-uuu uuuu
(5)
(5)
(5)
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 29
PIC18CXX2
FIGURE 3-3:TIME-OUT SEQUENCE ON POWER-UP (MCLR TIED TO VDD)
VDD
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
OST TIME-OUT
INTERNAL RESET
TOST
FIGURE 3-4:TIME-OUT SEQUENCE ON POWER-UP (MCLR
VDD
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
OST TIME-OUT
INTERNAL RESET
NOT TIED TO VDD ): CASE 1
TOST
FIGURE 3-5:TIME-OUT SEQUENCE ON POWER-UP (MCLR
VDD
MCLR
INTERNAL POR
TPWRT
PWRT TIME-OUT
OST TIME-OUT
INTERNAL RESET
DS39026B-page 30Preliminary
NOT TIED TO VDD ): CASE 2
TOST
7/99 Microchip Technology Inc.
FIGURE 3-6:SLOW RISE TIME (MCLR TIED TO VDD)
VDD
MCLR
INTERNAL POR
0V
PWRT
T
1V
PIC18CXX2
5V
PWRT TIME-OUT
OST TIME-OUT
INTERNAL RES ET
TOST
FIGURE 3-7:TIME-OUT SEQUENCE ON POR W/ PLL ENABLED (MCLR
VDD
MCLR
IINTERNAL POR
TPWRT
PWRT TIME-OUT
OST TIME-OUT
PLL TIME-OUT
TOST
TPLL
TIED TO VDD)
INTERNAL RESET
TOST = 1024 clock cycles.
PLL≈ 2 ms max. First three stages of the PWRT timer.
T
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 31
PIC18CXX2
NOTES:
DS39026B-page 32Preliminary
7/99 Microchip Technology Inc.
4.0MEMORY ORGANIZATION
There are two memory blocks in Enhanced MCU
devices. These memory blocks are:
• Program Memory
• Data Memory
Each block has its own bus so that concurrent access
can occur .
4.1Program Memory Organization
A 21-bit program counter is capable of addressing the
2-Mbyte program memory space. Ac ce ss in g a location
between the physically implemented memory and the
2-Mbyte address will cause a read of all ’0’s (a NOP
instructi on).
PIC18C252 and PIC18C452 have 32-KBytes of
EPROM, while PIC18C242 and PIC18C442 have
16-KBytes of EPROM. This means that PIC18CX52
devices can store up to 16K of sin gle word instructions, and PIC18CX42 devices can store up to 8K of
single word inst r u ct ions.
The reset vector address is at 0000h and the interrupt
vector addresses are at 0008h and 0018h.
Figure 4-1 shows the Program Memory Map for
PIC18C242/442 devices and Figure 4-2 shows the
Program Memory Map for PIC18C252/452 devices.
PIC18CXX2
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 33
PIC18CXX2
FIGURE 4-1:PROGRAM MEMORY MAP
AND STACK FOR PIC18C442/
242
PC<20:0>
CALL,BSUB,RETURN
RETFIE,RETLW
Stack Level 1
Stack Level 31
Reset Vector
High Priority Interrupt Vector
Low Priority Interrupt Vector
On-chip
Program Memory
Read ’0’
21
•
•
•
0000h
0008h
0018h
3FFFh
4000h
User Memory Space
FIGURE 4-2:PROGRAM MEMORY MAP
AND STACK FOR PIC18C452/
252
PC<20:0>
CALL,BSUB,RETURN
RETFIE,RETLW
Stack Level 1
Stack Level 31
Reset Vector
High Priority Interrupt Vector
Low Priority Interrupt Vector
On-chip
Program Memory
21
•
•
•
0000h
0008h
0018h
7FFFh
8000h
User Memory Space
1FFFFFh
200000h
Read ’0’
1FFFFFh
200000h
DS39026B-page 34Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
4.2Return Address Stack
The return address stack allows any combination of u p
to 31 program calls and interrupts to occur. The PC
(Program Counter) is pushed onto the stack when a
CALL or RCALL instruction is e xecute d or an interrupt i s
acknowledged. The PC value is pulled off the stack on
a RETURN, RETLW or a RETFIE instr uction. PCLATU
and PCLATH are not affected by any of the return
instructions.
The stack operates as a 31 word by 21-bit RAM and a
5-bit stack pointer, with the stack pointer initialized to
00000b after all resets. There is no RAM associated
with stack pointer 00000b. This is only a reset value.
During a CALL type instruction causing a push onto the
stack, the stack pointer is first incremented and the
RAM location pointed to by the stack pointer is written
with the contents of the PC. During a RETURN type
instruction causing a pop from the stack, the contents
of the RAM location pointed to by th e STKPTR is tr ansferred to the PC and then th e stack pointer is decremented.
The stack space is not part of either program or data
space. The stack pointer is readable and writable, and
the address on the t op of the stac k is read able and writable through SFR registers. Data can also be pushed
to or popped from the stack using the top-of-stack
SFRs. Status bits indicate if the stack pointer is at or
beyond the 31 levels provided.
After the PC is push ed on to the stac k 31 tim es (wi tho ut
popping any values off the stack), the STKFUL bit is
set. The STKFUL bit can o nly be cle ared in softw are or
by a POR.
The action that takes place when the stack becomes
full depends on the state of the STVREN (stack overflow reset enab l e) con figur ation bit. R ef er to Sec tion 1 8
for a description of the device configuration bits. If
STVREN is set (defau lt) the 31st push will pu sh the (PC
+ 2) value onto th e stack , set the STKFUL b it, and reset
the device. The STKFUL bit will remain set and the
stack pointer will be set to 0.
If STVREN is cleared, the STKFUL bit will be set on the
31st push and the stack pointer will increment to 31.
The 32nd push will o verwrite th e 31st push (a nd so on),
while STKPTR remains at 31.
When the stack has been popped enough times to
unload the stac k, the next pop will re turn a valu e of ze ro
to the PC and sets the STKUNF bit, while the stack
pointer remains at 0. The STKUNF bit will remain set
until cleared in software or a POR occurs.
Note:Return ing a value of zero t o t he PC o n an
underflow has the effect of vectoring the
program to the reset vector, where the
stack condit ions can be v erified and appropriate actions can be taken.
4.2.1TOP-OF-STACK ACCESS
The top of the stack is readable and writable. Three
register locations, TOSU, TOSH and TOSL hold the
contents of the stack location pointed to by the
STKPTR register. This allows users to implement a
software stack if necessary. After a CALL, RCALL or
interrupt, the software can read the pushed value by
reading the TOSU, TOSH and TOSL registers. These
values ca n be p laced on a user d efined softw ar e stac k .
At return time, the software can replace the TOSU,
TOSH and TOSL and do a return.
The user must disable the global interrupt enable bits
during this time to pre vent i nadvertent stack operations .
4.2.2RETURN STACK POINTER (STKPTR)
The STKPTR register contains the stack pointer value,
the STKFUL (stack full) status bit, and the STKUNF
(stack underflow) status bits. Register 4-1 shows the
STKPTR register . The va lue of th e stac k poi nter can b e
0 through 31. The stack pointer increments when values are pushed onto the stack and decrements when
values are popped off the stack. At reset, the stack
pointer va lue will be 0. Th e user m a y read a nd write th e
stack point er v alu e. T his f e ature c an be u sed b y a Rea l
Time Operating System for return stack maintenance.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 35
PIC18CXX2
Register 4-1:STKPTR - Stack Pointer Register
R/C-0R/C-0U-0R/W-0R/W-0R/W-0R/W -0R/W-0
STKFUL STKUNF
bit7654321bit0
(1)
bit 7
: STKFUL: Stack Full Flag bit
1 = Stack became full or overflowed
0 = Stack has not become full or overflowed
(1)
: STKUNF: Stack Underflow Flag bit
bit 6
1 = Stack underflow occurred
0 = Stack underflow did not occur
bit 5:Unimplemented: Read as ’0’
bit 4-0: SP4:SP0: Stack Pointer Location bits
Note 1: Bit 7 and Bit 6 can only be cleared in user software or by a POR.
-SP4SP3SP2SP1SP0R = Readable bit
W = Writeable bit
C = Clearable bit
U = Unimplemented bit,
Read as ‘0’
- n = Value at POR reset
FIGURE 4-3:RETURN ADDRESS STACK AND ASSOCIATED REGISTERS
Return Address Stack
11111
11110
TOSLTOSHTOSU
0x340x1A0x00
Top of Stack
0x001A34
0x000D58
11101
00011
00010
00001
00000
STKPTR<4:0>
00010
4.2.3PUSH AND POP INSTRUCTIONS
Since the Top-of-Stack (TOS) is readable and writable,
the ability to p ush values onto the st ac k and pu ll v a lues
off the stack without disturbing normal program execution is a desirable option. To push the current PC value
onto the stack, a PUSH instruction can be executed.
This will i ncrem ent th e sta ck pointe r and load t he cu rrent PC value onto the stack. TOSU, TOSH and TOSL
can then be modified to place a return address on the
stack.
The ability to pull the TOS value off of the stack and
replace it with the value that was previously pushed
onto the stack, wit hout di sturbin g norma l execution, is
achiev ed by u sing the POP instructio n. The POP instruction discards the current TOS by decrementing the
stack pointer. The previous value pushed onto the
stack then becomes the TOS value.
4.2.4STACK FULL/UNDERFLOW RESETS
These resets are enabled by programming the
STVREN configuration bit. When the STVREN bit is
disabled, a full or unde rflow cond ition will set the app ropriate STKFUL or STKUNF bit, but not cause a device
reset. When the STVREN bit i s enabled, a full or underflow will set the appropriate STKFUL or STKUNF bit
and then cause a device reset. The STKFUL or
STKUNF bit s are on ly cle ared by the us er soft ware or
a POR reset.
DS39026B-page 36Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
4.3Fast Register Stack
A "fast interrupt return" optio n is av ailable for interrupts .
A Fast Register Stack is provided for the STATUS,
WREG and BSR registers and are only one in depth.
The stack is n ot reada b le or w ritab le and is lo aded wi th
the current value of the corresponding register when
the processor vectors for an inte rrupt. The value s in the
registers are then loaded back into the working registers if the fast return instruction is used to return from
the interrupt.
A low or high priority interrupt source will push values
into the stack registers. If both low and high priority
interrupts are enabled, the stack registers cannot be
used reliably for low priority interrupts. If a high priority
interrupt occurs while servicing a low priority interrupt,
the stack register v alv es st ored by the low priority int errupt will be overwritten.
If high priority interrupts are n ot disab led du ring lo w priority interrupts, users must save the key registers in
software during a low priority interrupt.
If no inter r up t s a r e used, the fast r eg is t er s t ack ca n be
used to restore the STATUS, WREG and BSR registers
at the end of a s ubr outin e ca ll. To use the fast regi ste r
stack for a subroutine call, a fast call instruction must be
ex ecuted.
Example 4-1 shows a sour ce code example tha t uses
the fast register stack.
EXAMPLE 4-1:FAST REGISTER STACK
CODE EXAMPLE
CALL SUB1, FAST;STATUS, WREG, BSR
;SAVED IN FAST REGISTER
•
•
SUB1
•
•
•
RETURN FAST;RESTORE VALUES SAVED
;STACK
;IN FAST REGISTER STACK
4.4PCL
, PCLATH and PCLATU
The program counter (PC) speci fies the address of the
instruction to fetch for execution. The PC is 21-bits
wide. The low byte is called the PCL register. This register is readable and writable. The high byte is called
the PCH register. This register contains the PC<15:8>
bits and is not directly readable or writable. Updates to
the PCH register may be performed through the
PCLATH register. The upper byte is called PCU. This
register contains the PC<20:16> bit s and is n ot direc tly
readable or writable. Updates to the PCU register may
be performed through the PCLATU register.
The PC addresses bytes in the program memory. To
prevent the PC from becoming misaligned with word
instructions, the LSB of PCL is fixed to a value of ’0’.
The PC increments by 2 to addres s se que nti al ins tructions in the program memory.
The CALL, RCALL, GOTO and program branch
instructions write to the program counter directly. For
these instructions, the contents of PCLATH and
PCLATU are not transferred to the program counter.
The contents of PCLATH and PCLATU will be transferred to the program counter by an operation that
writes PCL. Similarly, the upper two bytes of the program counter will be transferred to PCLATH and
PCLATU by an operation that reads PCL. This is usefu l
for computed offsets to the PC. (See Section 4.8.1)
4.5Clocking Scheme/Instruction Cycle
The clock input (from OSC1) is internally divided by
four to generate four non-overlapping quadrature
clocks namely Q1, Q2, Q3 and Q4. Internally, the program counter (PC) is incremented every Q1, the
instruction is fetched from the program memory and
latched into the instruction register in Q4. The instruction is decoded and executed during the following Q1
through Q4. The clocks and instruction execution flow
is shown in Figure4-4.
FIGURE 4-4:CLOCK/INSTRUCTION CYCLE
Q2Q3Q4
Q1
OSC1
Q1
Q2
Q3
Q4
PC
OSC2/CLKOUT
(RC mode)
7/99 Microchip Technology Inc.
PCPC+2PC+4
Fetch INST (PC)
Execute INST (PC-2)Fetch INST (PC+2)
Q1
PreliminaryDS39026B-page 37
Q2Q3Q4
Execute INST (PC)Fetch INST (PC+4)
Q2Q3Q4
Q1
Execute INST (PC+2)
Internal
phase
clock
PIC18CXX2
4.6Instruction Flow/Pipelining
A fetch cycle begins with the program counter (PC)
incrementing in Q1.
An “Instruction Cycle” consists of four Q cycles (Q1,
Q2, Q3 and Q4). The instruction fetch and execute are
pipelined such that fetch takes one instruction cycle
while decode and execute takes another instruction
cycle. However, due to the pipelining, each instruction
effectively executes in one cycle. If an instruction
causes the program counter to change (e.g. GOTO)
In the ex ecu tion cy cle, t he f etched i nstruction i s latche d
into the “Instruction Register" (IR) in cycle Q1. This
instruction is then decoded and executed during the
Q2, Q3, and Q4 cycles . Data memory is read during Q2
(operand read) and written during Q4 (destination
write).
then two cycles ar e required to complete the instruction
(Example 4-2).
EXAMPLE 4-2:INSTRUCTION PIPELINE FLOW
Tcy0Tcy1Tcy2Tcy3Tcy4Tcy5
1. MOVLW 55h
2. MOVWF PORTB
3. BRA SUB_1
4. BSF PORTA, BIT3 (Forced NOP)
5. Instruction @ address 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.
Fetch 1Execute 1
Fetch 2Execute 2
Fetch 3Execute 3
Fetch 4Flush
Fetch SUB_1 Execute SUB_1
4.7Instructions in Program Memory
boundaries, the data contained in the instruction is a
word address. The word address is written to
The program memory is addressed in bytes. Instructions are stored as two bytes or four bytes in program
memory. The least significant byte of an instruction
word is always stored in a program memory location
with an even address (LSB = ’0’). Figure 4-5 shows an
example of ho w in struction w ords a re sto red in th e program memory. To maintain alignment with instruction
boundaries, the PC increments in steps of 2 and the
LSB will always read ’0’. (See Section 4.4)
The CALL and GOTO instructions have an absolute
PC<20:1>, which accesses the desired byte address
in program memory. Instruction #2 in Figure 4-5
shows how the instruction "GOTO 000006h’ is
encoded in the program memory. Program branch
instructions which encode a relative address offset
operate in the same manner. The offset value stored
in a branch instruction represents the number of single word instructions that the PC will be offset by.
Section 19.0 provides further details of the instruction
set.
program memory address embedded in to the instruction. Since instructions are always stored on word
4.7.1TWO-WORD INSTRUCTIONS
The PIC18CXX2 de vic es ha v e 4 tw o-w ord in structions :
MOVFF, CALL, GOTO and LFSR. The second word of
these instructions has the 4 MSB’s set to 1’s and is a
special kind o f NOP in structi on. The lower 12 bits o f th e
second word con tain data to be used b y the instructio n.
If the first word of the instruction is executed, the data
in the second word is accessed. If the second word of
the instruction is executed by itself (first word was
skipped), it will execute as a NOP. This action is neces-
sary when the two word instruction is preceded by a
conditional instructio n that changes the PC. A progr am
example that demonstrates this concept is shown in
Example 4-3. Refer to Sect ion 19.0 f or further details of
the instruction set.
Look-up tables are implemented two ways. These are:
• Computed GOTO
•Table Reads
4.8.1COMPUTED GOTO
A computed GOTO is accomplished by adding an offset
to the program counter (ADDWF PCL).
A lookup table can be formed with an ADDWF PCL
instruction and a group of RETLW 0xnn instructions.
WREG is loaded with an offset into the table before
executing a call to that table. The first instruction of the
called routine is the ADDWF PCL instruction. The next
instruction executed will be one of the RETLW 0xnn
instructions that returns the value 0xnn to the calling
function.
The offset va lue (v alue in WR EG) specifie s the n umber
of bytes that the program counter should advance.
In this method, only one data byte may be stored in
each instruction location and room on the return
address stack is required.
A better method of storing data in program memory
allows 2 bytes of data to be stored in each instruction
location.
Lookup table data may be stored 2 bytes per program
word by u si ng ta ble reads and writes. The tab le pointer
(TBLPTR) specifies the byte address and the table
latch (TABLAT) contains the data that is read from or
written to program memory. Data is transferred to/from
program memory one byte at a time.
A description of the Table Read/Table Write operation
is shown in Section 5.0.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 39
PIC18CXX2
4.9Data Memory Organization
The data memory is implemented as static RAM. Each
register in the data memory has a 12-bit address,
allowing up to 4096 bytes of data memory. Figure 4-6
and Figure 4-7 show the data memory organization for
the PIC18CXX2 devices.
Banking is required to allo w more than 256 bytes to be
accessed. T he data memory map is divided into as
many as 16 banks that contain 256 bytes each. The
lower 4 bits of the Bank Select Register (BSR<3:0>)
select which bank will be accessed. The upper 4 bits
for the BSR are not implemented.
The data memory contains Special Function Registers
(SFR) and General Purpose Registers (GPR). The
SFRs are used for control and status of the controller
and peripheral functio ns , whi le GPRs are us ed f o r data
storage and scratch pad operations in the user’s application. The SFRs start at the last location of Bank 15
(OxFFF) and grow downwards. Any remaining space
beyond the SFRs in th e Bank may be im plem ent ed as
GPRs. GPRs start at the first location of Bank 0 and
grow upw ards. An y read o f an unimpl emented l ocation
will read as ’0’s.
The entire data memory may be accessed directly or
indirectly. Direct addressing may require the use of the
BSR register. Indirect addressing requires the use of
the File Select Register (FSR). Each FSR holds a 12bit address value that can be used to access any location in the Data Memory map without banking.
The instruction set and architecture allow operations
across all banks . This ma y be accomp lished b y indirect
addressing or by the use of the MOVFF instruction.
The MOVF F instructi on is a tw o word/tw o cycle instruction that moves a value from one register to another.
To ensure that commonly used registers (SFRs and
select GPRs) can be accesse d in a singl e cycle regardless of the current BSR values, an Access Bank is
implemented. A segment of Bank 0 and a segment of
Bank 15 comprise the Acc ess RAM . Section 4.10 provides a detailed description of the Access RAM.
4.9.2SPECIAL FUNCTION REGISTERS
The Special Function Registers (SFRs) are registers
used by the C PU and P eripheral M odules f or controlling
the desired oper ation of the de vic e. These re gisters are
implemented as static RAM. A list of these registers is
given in Table 4-1 and Table 4-2.
The SFRs can be classified into two sets; those associated with the “core” function and those related to the
peripheral functions. Those registers related to the
“core” are described in this s ection , while thos e relate d
to the operation of the peripheral features are
described in the section of that peripheral feature.
The SFRs are typically distrib uted amon g the peripherals whose functions they control.
The unused SFR locations will be unimplemented and
read as '0's. See Table 4-1 for addresses f or the SFRs .
4.9.1GENERAL PURPOSE REGISTER FILE
The register file can be ac cess ed eith er direct ly or indi-
rectly. Indirect addressing operates through the File
Select Registers (FSR). The operation of indirect
addressing is shown in Section 4.12.
Enhanced MCU devices may have banked memory in
the GPR area. GPRs are not initialized by a Power-on
Reset and are unchanged on all other resets.
Data RAM is available for use as GPR registers by all
instructi ons. T he top ha lf of ban k 15 (0xF 80 to 0xFF F)
contains SFRs. All other banks of data memory contain
GPR registers starting with bank 0.
DS39026B-page 40Preliminary
7/99 Microchip Technology Inc.
FIGURE 4-6:DATA MEMORY MAP FOR PIC18C242/442
PIC18CXX2
BSR<3:0>
= 0000b
= 0001b
= 0010b
= 1110b
= 1111b
When a = 1,
the BSR is used to specify
the RAM location that the
instruction uses.
Bank 0
Bank 1
Bank 2
to
Bank 14
Bank 15
Data Memory Map
00h
Access RAM
FFh
00h
FFh
Unused
Read ’00h’
00h
FFh
Unused
GPR
GPR
SFR
000h
07Fh
080h
0FFh
100h
1FFh
200h
EFFh
F00h
F7Fh
F80h
FFFh
Access Bank
Access RAM low
Access RAM high
(SFR’s)
When a = 0,
the BSR is ignored and the
Access Bank is used.
The first 128 bytes are
General Purpose RAM
(from Bank 0).
The second 128 bytes are
Special Function Registers
(from Bank 15).
00h
7Fh
80h
FFh
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 41
PIC18CXX2
FIGURE 4-7:DATA MEMORY MAP FOR PIC18C252/452
BSR<3:0>
= 0000b
= 0001b
= 0010b
= 0011b
= 0100b
= 0101b
= 0110b
= 1110b
= 1111b
When a = 1,
the BSR is used to specify
the RAM location that the
instruction uses.
Bank 0
Bank 1
Bank 2
Bank 3
Bank 4
Bank 5
Bank 6
to
Bank 14
Bank 15
Data Memory Map
00h
Access RAM
FFh
00h
FFh
00h
FFh
00h
FFh
00h
FFh
00h
FFh
GPR
GPR
GPR
GPR
GPR
GPR
Unused
Read ’00h’
Unused
SFR
000h
07Fh
080h
0FFh
100h
1FFh
200h
2FFh
300h
3FFh
400h
4FFh
500h
5FFh
600h
EFFh
F00h
F7Fh
F80h
FFFh
Access Bank
Access RAM low
Access RAM high
(SFR’s)
When a = 0,
the BSR is ignored and the
Access Bank is used.
The first 128 bytes are
General Purpose RAM
(from Bank 0).
The second 128 bytes are
Special Function Registers
(from Bank 15).
TBLPTRHProgram Memory Table Pointer High Byte (TBLPTR<15:8>)0000 00000000 0000
TBLPTRLProgram Memory Table Pointer Low Byte (TBLPTR<7:0>)0000 00000000 0000
TABLATProgram Memory Table Latch0000 00000000 0000
PRODHProduct Register High Bytexxxx xxxxuuuu uuuu
PRODLProduct Register Low Bytexxxx xxxxuuuu uuuu
INTCONGIE/GIEHPEIE/GIELTMR0IEINT0IERBIETMR0IFINT0IFRBIF0000 000x0000 000u
INTCON2RBPUINTEDG0INTEDG1INTEDG2
INTCON3INT 2IPINT1IP
INDF0Uses contents of FSR0 to address data memory - value of FSR0 not changed (not a physical register)n/an/a
POSTINC0Uses contents of FSR0 to address data memory - value of FSR0 post-incremented (not a physical register)n/an/a
POSTDEC0Uses contents of FSR0 to address data memory - value of FSR0 post-decremented (not a physical register)n/an/a
PREINC0Uses contents of FSR0 to address data memory - value of FSR0 pre-incremented (not a physical register)n/an/a
PLUSW0Uses contents of FSR0 to address data memory - value of FSR0 pre-incremented (not a physical register) -
FSR0H
FSR0LIndirect Data Memory Address Pointer 0 Low Bytexxxx xxxxuuuu uuuu
WREGWorking Registerxxxx xxxxuuuu uuuu
INDF1Uses contents of FSR1 to address data memory - value of FSR1 not changed (not a physical register)n/an/a
POSTINC1Uses contents of FSR1 to address data memory - value of FSR1 post-incremented (not a physical register)n/an/a
POSTDEC1Uses contents of FSR1 to address data memory - value of FSR1 post-decremented (not a physical register)n/an/a
PREINC1Uses contents of FSR1 to address data memory - value of FSR1 pre-incremented (not a physical register)n/an/a
PLUSW1Uses contents of FSR1 to address data memory - value of FSR1 pre-incremented (not a physical register) -
FSR1H
FSR1LIndirect Data Memory Address Pointer 1 Low Bytexxxx xxxxuuuu uuuu
BSR
INDF2Uses contents of FSR2 to address data memory - value of FSR2 not changed (not a physical register)n/an/a
POSTINC2Uses contents of FSR2 to address data memory - value of FSR2 post-incremented (not a physical register)n/an/a
POSTDEC2Uses contents of FSR2 to address data memory - value of FSR2 post-decremented (not a physical register)n/an/a
PREINC2Uses contents of FSR2 to address data memory - value of FSR2 pre-incremented (not a physical register)n/an/a
PLUSW2Uses contents of FSR2 to address data memory - value of FSR2 pre-incremented (not a physical register) -
FSR2H
FSR2LIndirect Data Memory Address Pointer 2 Low Bytexxxx xxxxuuuu uuuu
STATUS
TMR0HTimer0 register high byte0000 00000000 0000
TMR0LTimer0 register low bytexxxx xxxxuuuu uuuu
T0CONTMR0ONT08BITT0CST0SEPSAT0PS2T0PS1T0PS01111 11111111 1111
———Holdi ng Regi st er for PC<20:16 >---0 0000---0 0000
——
value of FSR0 offset by value in WREG
————Indirect Data Memory Address Pointer 0 High Byte---- 0000---- 0000
value of FSR1 offset by value in WREG
————Indirect Data Memory Address Pointer 1 High Byte---- 0000---- 0000
————Bank Select Register---- 0000---- 0000
value of FSR2 offset by value in WREG
————Indirect Data Memory Address Pointer 2 High Byte---- 0000---- 0000
———NOVZDC C---x xxxx---u uuuu
—Return Stack Pointer00-0 000000-0 0000
(2)
bit21
Program Memory Table Pointe r Upper Byte (TBLPTR<20:16>)---0 0000---0 0000
—TMR0IP —RBIP 1111 -1-11111 -1-1
—INT2IEINT1IE—INT2IFINT1IF11-0 0-0011-0 0-00
n/an/a
n/an/a
n/an/a
Value on
all other
resets
(note 3)
Legend: x = unknown, u = unchanged, - = unimplemented, q = value depends on condition
Note1: RA6 and associated bits are configured as port pins in RCIO and ECIO oscillator mode only and read ’0’ in all
other oscillator modes.
2: Bit 21 of the TBLP TRU allows access to the device configuration bits.
3: Other (non-power-up) resets include external reset through MCLR
C Slave Mode. SSP Baud Rate Reload Register in I2C Master Mode.
PSR/WUABF0000 00000000 0000
—ADON0000 00-00000 00-0
——PCFG3PCFG2PCFG1PCFG000-- 000000-- 0000
—BRGHTRMTTX9D0000 -0100000 -010
0000 00000000 0000
Val ue on
all other
resets
(note 3)
Legend: x = unknown, u = unchanged, - = unimplemented, q = value depends on condition
Note1: RA6 and associated bits are configured as port pins in RCIO and ECIO oscillator mode only and read ’0’ in all
other oscillator modes.
2: Bit 21 of the TBLP TRU allows access to the device configuration bits.
3: Other (non-power-up) resets include external reset through MCLR
TRISDData Direction Control Register for PORTD1111 11111111 1111
TRISCData Direction Control Register for PORTC1111 11111111 1111
TRISBData Direction Control Register for PORTB1111 11111111 1111
TRISA
LATE
LATDRead PORTD Data Latch, Write PORTD Data Latchxxxx xxxxuuuu uuuu
LATCRead PORTC Data Latch, Write PORTC Data Latchxxxx xxxxuuuu uuuu
LATBRead PORTB Data Latch, Write PORTB Data Latchxxxx xxxxuuuu uuuu
LATA
PORTERead PORTE pins, Write PORTE Data Latch---- -000---- -000
PORTDRead PORTD pins, Write PORTD Data Latchxxxx xxxxuuuu uuuu
PORTCRead PORTC pins, Write PORTC Data Latchxxxx xxxxuuuu uuuu
PORTBRead PORTB pins, Write PORTB Data Latchxxxx xxxxuuuu uuuu
PORTA
Data Direction Control Regi st er for PORTA-111 1111-111 1111
(1)
Read PORTA Data Latch, Write PORTA Data Latch
Read PORTA pins, Write PORTA Data Latch
—Data Direction bits for PORTE0000 -1110000 -111
PORTE Data Latch
(1)
(1)
POR,
BOR
---- -xxx---- -uuu
-xxx xxxx-uuu uuuu
-x0x 0000-u0u 0000
Value on
all other
resets
(note 3)
Legend: x = unknown, u = unchanged, - = unimplemented, q = value depends on condition
Note1: RA6 and associated bits are configured as port pins in RCIO and ECIO oscillator mode only and read ’0’ in all
other oscillator modes.
2: Bit 21 of the TBLP TRU allows access to the device configuration bits.
3: Other (non-power-up) resets include external reset through MCLR
and Watchdog Timer Reset.
4.10Access Bank
A bit in the instruction word specifies if the operation is
to occur in the bank specified by the BSR register or in
The Access Bank is an architectural enhancement
which is very useful for C compiler code optimization.
The techniques used by the C compiler may also be
useful for programs written in assembly.
This data memory region can be used for:
• Intermediate computational values
• Local variables of subrou tines
• Faster context saving/switching of variables
• Common variab le s
the Access Bank. This bit is denoted by the ’a’ bit (for
access bit).
When forced in the Access Bank (a = ’0’), the last
address in Access RAM Low is followed by the first
address in Access RAM High. Access RAM High maps
the Special Function registers so that these registers
can be accessed without any software overhead. This
is useful for testing status flags and modifying control
bits.
• Faster evaluation/control of SFRs (no banking)
The Access Bank is comprised of the upper 128 bytes
in Bank 15 (SFRs) and the lower 128 bytes in Bank 0.
These two sections will be referred to as Access RAM
High and Access RAM Low, respectively. Figure 4-6
and Figure 4-7 indicate the Access RAM areas.
DS39026B-page 46Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
4.11Bank Select Regist er (BS R)
The need for a large general purpose memory space
dictates a RAM banking scheme. The data memory is
partitioned into sixteen banks. When using direct
addressing, the BSR should be configured for the
desired bank.
BSR<3:0> holds the upper 4 bits of the 12-bit RAM
address. The BSR<7:4> bits will always read ’0’s, and
writes will have no effect.
MOVLB instruction has bee n provided in the instruc-
A
tion set to assist in selecting banks.
FIGURE 4-8:DIRECT ADDRESSING
Direct Addressing
from opcode
(3)
bank select
BSR<3:0>7
(2)
location select
(3)
If the currently selected bank is not implemented, any
read will return all '0's and all writes are ignored. The
STAT U S regis ter bi ts will be set/cleared as appropriate
for the instruction performed.
Each Bank extends up to FFh (256 bytes). All data
memory is implement ed as static RAM.
A MOVFF i nstruction ignores the BSR , si nc e th e 1 2-b it
addresses are embedded into the instruction word.
Section 4.12 provides a d escription of ind irect address ing, whic h allows lin ear add ressing o f the ent ire RAM
space.
0
00h01h0Eh0Fh
000h
100h
E00h
F00h
Data
Memory
(1)
0FFh
1FFh
EFFh
Bank 0Bank 1Bank 14Bank 15
Note 1: For register file map detail, see Table 4-1.
2: The access 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.
3: The MOVFF instruction embeds the entire 12-bit address in the instruction.
FFFh
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 47
PIC18CXX2
4.12Indirect Addressing, INDF and FSR
Registers
Indirect addressin g is a mode of addr essing data mem ory, where the data memory address in the instruction
is not fixed. An SF R re gister is used as a pointer to th e
data memory location that is to be read or written.
Since this pointer is in RAM, the contents can be modified by the program. This can be useful for data tables
in the data memory and for so ftware stac ks . Figu re 4-9
shows the operati on of indirect addressing . This sho ws
the moving of the value to the data memory address
specified by the value of the FSR register.
Indirect addressing is possible by using one of the
INDF registers. Any instruction using the INDF register
actually accesses the register pointed to by the File
Select Register, FSR. Reading the INDF register itself
indirectly (FSR = ’0’) will read 00h. Writing to the INDF
register indirectly results in a no-operation. The FSR
register contains a 12-bit address, which is shown in
Figure 4-10.
The INDFn register is not a physical register. Addressing INDFn actually addresses the register whose
address is contained in the FSRn register (FSRn is a
pointer). T his is indirect addressing.
Example 4-4 shows a simple use of indirect addressing
to clear the RAM in Bank1 (locations 100h-1FFh) in a
minimum number of instructions.
EXAMPLE 4-4:HOW TO CLEAR RAM
(BANK1) USING INDIRECT
ADDRESSING
LFSR 0x100, FSR0 ;
NEXT CLRF POSTINC0 ; Clear INDF register
; & inc pointer
BTFSS FSR0H, 1 ; All done w/ Bank1?
GOTO NEXT ; NO, clear next
CONTINUE ;
: ; YES, continue
There are three indirect addressing registers. To
address the entire data memory space (4096 bytes),
these registers are 12-bit wide. To store the 12-bits of
addressing information, two 8-bit registers are
required. These indirect addres si ng regi ste r s are:
1.FSR0: composed of FSR0H:FSR0L
2.FSR1: composed of FSR1H:FSR1L
3.FSR2: composed of FSR2H:FSR2L
In addition, there are registers INDF0, INDF1 and
INDF2, which are not physically implemented. Reading
or writing to these registers activates indirect addressing, with the value in the corresponding FSR register
being the address of the data.
If an instruction writes a value to INDF0, the value will
be written to the add res s poi nte d t o b y F S R0H:FSR 0 L.
A read from INDF1 reads the data from the address
pointed t o by FSR1H:FSR1L. INDFn can be us ed in
code anywhere an operand can be used.
If INDF0, I NDF1 or INDF2 are re ad indirectly via an
FSR, all ’0’s are read (zero bit is set). Similarly, if
INDF0, INDF1 or INDF2 are written to indirectly, the
operation will be equiv alent to a NOP instruction and the
STATUS bits are not affected.
4.12.1INDIRECT ADDRESSING OPERATION
Each FSR register has an INDF register associated
with it, plus four additional register addresses. Performing an operation on one of these five registers
determines how the FSR will be modified during indirect addres sing.
When data access is done to one of the five INDFn
locations, the address selected will configure the FSRn
register to:
• Do nothing to FSRn after an indirect access (no
change) - INDFn
• Auto-decrement FSRn after an indirect access
(post-decrement) - POSTDECn
• Auto-increment FSRn after an indirect access
(post-increment) - POSTINCn
• Auto-increment FSRn before an indirect access
(pre-increment) - PREINCn
• Use the value in the WREG register as an offset
to FSRn. Do not modify the v alue of the W REG or
the FSRn register after an indirect access (no
change) - PLUSWn
When using the auto-increment or auto-decrement
features, the effect on the FSR is no t reflecte d in the
STATUS register. For example, if the indir ect a ddress
causes the FSR to equal '0' , the Z bit will not be set.
Incrementing or decrementing an FSR affects all 12
bits. That is, when FSRnL overflow s from an increment,
FSRnH will be incremented automatically.
Adding these fe atures allo ws the FSR n to be used as a
stack pointer in addition to its uses for table operations
in data memory.
Each FSR has an address associated with it that performs an inde xed indirect access. When a data acces s
to this INDFn location (PLUSWn) occurs, the FSRn is
configured to add the si gne d value in the WREG register and the value in FSR to form the add ress before an
indirect access. The FSR value is not changed.
If an FSR register contains a v alue tha t points to one of
the INDFn , an indirect re ad will read 00h (zero bit is
set), while an indirect write will be equivalent to a NOP
(STATUS bits are not affected).
If an indirect addressing operation is done where the
target address is an FSRnH or FSRnL register, the
write operation will dominate over the pre- or postincrement /decrement functions.
DS39026B-page 48Preliminary
7/99 Microchip Technology Inc.
FIGURE 4-9:INDIRECT ADDRESSING OPERATION
Instruction
Executed
OpcodeAddress
12
File Address = access of an indirect addressing register
RAM
PIC18CXX2
0h
FFFh
BSR<3:0>
Instruction
Fetched
Opcode
4
FIGURE 4-10: INDIRECT ADDRESSING
Indirect Addressing
FSR register11
location select
Data
Memory
(1)
12
8
File
12
FSR
0
0000h
Note 1: For register file map detail, see Table 4-1.
7/99 Microchip Technology Inc.
0FFFh
PreliminaryDS39026B-page 49
PIC18CXX2
4.13STATUS Register
The STATUS register, shown in Register4-2, contains
the arithmetic status of the ALU. The STATUS register
can be the destination for any instruction, as with any
other register. If the STATUS register is the destination
for an instruction that affects the Z, DC, C, OV or N bits,
then the write to these five bits is disabled. These bits
are set or cleared a cc ord ing to t he device log ic. The r efore, th e r esult of an instructi on with th e STA TUS re gi ster as destination may be different than intended.
Register 4-2:STATUS Register
U-0U-0U-0R/W-xR/W-xR/W-xR/W-xR/W-x
——— NOVZDCC
bit 7bit 0
bit 7:5Unimplemented: Read as ’0’
bit 4N: Negative bit
This bit is used for sign ed arithmatic ( 2’ s complement). It indicates whether th e result was neg-
ative, (ALU MSB = 1)
1 = Result was negative
0 = Result was positive
bit 3OV: Overflow bit
This bit is used f or signed arithmetic (2’s co mplement). It indi cates an ov erflow of the 7-bit magnitude, which causes the sign bit (bit7) to change state.
1 = Overflow occurred for signed arithmatic (in this arithmetic operation)
0 = No overflow occurre d
bit2Z: 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 1DC: Digit carry/borrow
For ADDWF, ADDLW, SUBLW, and SUB WF instructions
1 = A carry-out from the 4th low order bit of the result occurred
0 = No carry-out from the 4th low order bit of the result
Note:For borrow
complement of the second operand. For rotate (RRF, RLF) instructions, this bit is
loaded with either the bit 4 or bit 3 of the source register.
bit 0C: Carry/borrow bit
For ADDWF, ADDLW, SUBLW, and SUBWF instructions
1 = A carry-out from the most significant bit of the result occurred
0 = No carry-out from the most significant bit of the result occurred
Note:For borrow
complement of the second operand. For rotate (RRF, RLF) instructions, this bit is
loaded with either the high or low order bit of the source register.
bit
, the polarity is reversed. A subtraction is executed by adding the two’s
, the polarity is reversed. A subtraction is executed by adding the two’s
For example, CLRF STATUS will clear th e upp er -t h ree
bits and set th e Z bi t. T his l ea v es the STATUS register
as 000u u1uu (where u = unchanged).
It is recommended, therefore, that only BCF, BSF,SWAPF, MOVFF and MOVWF instr uctions are used to
alter the STATUS register, because these instructions do not affect the Z, C, DC, OV or N bits from
the STATUS register. For other instructions not
affecting any status bits, see Table 19-2.
Note:The C and DC b its operat e as a bo rrow and
digit borrow
bit respectively, in subtraction.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
DS39026B-page 50Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
4.13.1RCON REGISTER
The Reset Control (RCON) register contains flag bits,
that allow differentiation between the sources of a
device reset. T hese flags include t he TO, PD, POR,
and RI bits. This regis ter is readab le and writab le .
BOR
Register 4-3:RCON Register
R/W-0R/W-0U-0R/W-1R/W-1R/W-1R/W-0R/W-0
IPENLWRT
bit 7bit 0
bit 7IPEN: Interrupt Priority Enable bit
1 =Enable priority levels on interrupts
0 =Disable priority levels on interrupts (16CXXX compatibility mode)
bit 6LWRT: Long Write Enable bit
1 =Enable TBLWT to internal program memoryOnce this bit is set, it can only be cleared by a POR or MCLR
0 = Disable TBLWT to internal program memory; TBLWT only to external program memory
bit 5Unimplemented: Read as ’0’
bit 4RI
bit 3TO
bit 2PD
bit 1POR
bit 0BOR
: Reset Instruction Flag bit
1 =The Reset instruction was not executed
0 =The Reset instruction was executed causing a device reset
(must be set in software after a Brown-out Reset occurs)
: Watchdog Time-out Flag bit
1 =After power-up, CLRWDT instruction, or SLEEP instruction
0 =A WDT time-out occurred
: Power-down Detection Flag bit
1 =After power-up or by the CLRWDT instruction
0 =By execution of the SLEEP inst ruction
: Power-on Reset Status bit
1 =A Power-on Reset has not occurred
0 =A Power-on Reset occurred
(must be set in software after a Power-on Reset occurs)
: Brown-out Reset Status bit
1 =A Brown-out Reset has not occurred
0 =A Brown-out Reset occurred
(must be set in software after a Brown-out Reset occurs)
—RITOPDPORBOR
Note 1: If the BOREN conf iguration b it is set, BOR
is ’1’ on Power-on Reset. If the BOREN
configuration b it i s clea r, BOR
on Power-on Reset.
The BOR
not necessarily predictable if the brownout circuit is disabled (the BOREN configuration bit is clear). BO R
by the user and checked on subsequent
resets to see if it is clear, indicating a
brown-out has occurred.
2: It is recommended that the POR
after a Power-on Reset has been
detected, so that subsequent Power-on
Resets may be detected.
reset.
status bit is a "don 't care " and i s
is unknown
must then be set
bit be set
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 51
PIC18CXX2
NOTES:
DS39026B-page 52Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
5.0TABLE READS/TABLE WRITES
Enhanced devices have two memory spaces: the program memory space and the data memory space. The
program memory space is 16 bits wide, while the data
memory spa ce is 8 bits wide. Table Reads and Table
Writes have been provided to move data between
these two memory spaces through an 8 bit register
(TABLAT).
The operations that allow the processor to move data
between the data and program memory spaces are:
•Table Read (TBLRD)
• Table Write (TBLWT)
FIGURE 5-1:TABLE READ OPERATION
TABLE POINTER
TBLPTRU
TBLPTRHTBLPTRL
(1)
PROGRAM MEMOR Y
Table Read operations retrieve data from program
memory and place it into the Data memory space.
Figure 5-1 shows the operation of a Table Read with
program and data memory.
Table Write operations store data from the data memory space into program memory. Figure 5-2 shows the
operation of a Table Write with program a nd data m emory .
Table operations work with byte entities. A table block
containing data is not req uired to be wo rd align ed, so a
table block can st art and end at any byte a ddr ess. I f a
table write is being used to write an e xecutab le program
to program memory, program instructions will need to
be word aligned.
TABLE LATCH (8-bit)
TABLAT
Program Memory
Instruction: TBLRD*
(TBLPTR)
Note 1: Table Pointer points to a byte in
program memory
FIGURE 5-2:TABLE WRITE OPERATION
TABLE POINTER
TBLPTRU
Instruction: TBLWT*
TBLPTRHTBLPTRL
Note 1: Table Pointer points to a byte in
program memory
(1)
PROGRAM MEMOR Y
Program Memory
(TBLPTR)
TABLE LATCH (8-bit)
TABLAT
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 53
PIC18CXX2
5.1Control Registers
Several control registers are used in conjunction with
the TBLRD and TBLWT instructions. These include the:
• TBLPTR registers
• TABLAT register
• RCON register
Register 5-1:RCON Register (Address: 08h)
R/W-0R/W-0U-0R/W-1R/W-1R/W-1R/W-0R/W-0
IPENLWRT
bit 7bit 0
bit 7IPEN: Interrupt Priority Enable
1 = Enable priority levels on interrupts
0 = Disable priority levels on interrupts (16CXXX compatibility mode)
bit 6LWRT: Long Write Enable
1 = Enable TBLWT to internal program memory
0 = Disable TBLWT to internal program memory.
Note 1: Only cleared on a POR or MCLR reset.
This bit has no effect on TBLWTs to external program memory.
bit 5Unimplemented: Read as ’0’
bit 4RI
bit 3TO
bit 2PD
bit 1POR
bit 0BOR
: Reset Instr uction Flag bit
1 = No Reset inst ruction occurred
0 = A Reset instruction occurred
: Time-out bit
1 = After power-up, CLRWDT instruction, or SLEEP instruction
0 = A WDT time-out occurred
: Power-down bit
1 = After power-up or by the CLRWDT instruction
0 = By execution of the SLEEP instruction
: Power-on Reset Status bit
1 = No Power-on Reset occurred
0 = A Power-on Reset occurred (must be set in software after a Power-on Reset occurs)
: Brown-out Reset Status bit
1 = No Brown-out Reset nor POR reset occurred
0 = A Brown-out Reset nor POR reset occurred
(must be set in software after a Brown-out Reset occurs)
—RITOPDPORBOR
5.1.1RCON REGISTER
The L WR T bit spe cifies the oper ation of Table Writes to
internal memory when the V
pin. When the LWRT bit is set, the controller
MCLR
continues to execute user code, but long table writes
are allowed (for programming internal program memory) from user mode. The L WR T bit can be cleared only
by performing either a POR or MCLR reset.
PP voltage i s ap plied to th e
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’ 0’ = Bit is clearedx = Bit is unknown
DS39026B-page 54Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
5.1.2TABLAT - TABLE LATCH REGISTER
The Table Latch (TABLAT) is an 8-bit register mapped
into the SFR space. The Table Latch is used to hold
8-bit data during data transfers between program
memory and data memory.
5.1.3TBLPTR - TABLE POINTER REGISTER
The Table Pointer (TBLPTR) addresses a byte within
the program memory. The TBLPTR is comprised of
three SFR registers (Table Pointer Upper byte, High
byte and Low byte). These three registers (TBLPTRU:TBLPTRH:TBLPTRL) join to form a 22-bit wide
pointer. The low order 21-bits allow the device to
address up to 2M by tes of progr am memory space. Th e
22nd bit allows access to the Device ID, the User ID
and the Configuration bits.
The table pointer TBL PT R is use d by the TB LR D an d
TBLWT instructions. These instructions can update
the TBLPTR in one of four ways based on the table
operation. These operations are shown in Table 5- 1.
These operations on the T BLPTR only affect the low
order 21-bits.
TABLE 5-1:TABLE POINTER OPERATIONS WITH TBLRD AND TBLWT INSTRUCTIONS
ExampleOperation on Table Pointer
TBLRD*
TBLWT*
TBLRD*+
TBLWT*+
TBLRD*TBLWT*-
TBLRD+*
TBLWT+*
TBLPTR is not modified
TBLPTR is incremented after the read/write
TBLPTR is decremented after the read/write
TBLPTR is incremented before the read/write
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 55
PIC18CXX2
5.2Internal Program Memory Read/
Writes
5.2.1TABLE READ OVERVIEW (TBLRD)
The TBLRD in structions are use d to read data fr om pro-
gram memory to data memory.
TBLPTR points to a byte address in program space.
Executing TBLRD places the byte pointed to into TAB-
LAT. In addition, TBLPTR can be modified automatically for the next Table Read operation.
Table Reads from program memo ry are perf o rmed one
byte at a time. The inst ruction will load TABLA T with the
one byte from p rogr am memory point ed to b y TBLPTR.
5.2.2INTERNAL PROGRAM MEMORY WRITE
BLOCK SIZE
The internal program memory of PIC18CXXX devices
is written in blocks. For PIC18CXX2 devices, the write
block size is 2 bytes. Consequently, Table Write operations to internal program memory are performed in
pairs, one byte at a time.
When a Table Write occurs to an even program memory address (TBLPTR<0> = 0), t he conte nts of TABLAT
are transferred to an internal holding register. This is
performed as a short write and the program memory
block is not actually p rogramm ed at this time . The holding register is not accessible by the user.
When a Table Write occurs to an odd pro gr am memo ry
address (TBLPTR,)>=1), a long write is started. During
the long write, th e contents of TABLA T are written to the
high byte of the program memory block and the contents of the holding register are transferred to the low
byte of the program memory block.
Figure 5-3 shows the holding register and the program
memory write blocks.
If a single byte is to be prog rammed, the lo w (ev en) byte
of the destination program word should be read using
TBLRD*, modified or changed, if required, and written
back to the same address using TBLWT*+. The high
(odd) byte should be read using TBLRD*, modified or
changed if required, and written back to the same
address using TBLWT. The write to an odd address will
cause a long write to begin. This process ensures that
existing data in either byte will not be changed unless
desired.
FIGURE 5-3:HOLDING REGISTER AND THE WRITE BLOCK
Program Memory (x 2-bits)
Block n
Block n + 1
Block n + 2
Write Bl ock
MSB
The write to the MSB of the Write Block
causes the en tire block to be written to pr ogram memory. The program memory block
that is written depends on the address that is
written to in the MSB of the Write Block.
Holding Register
DS39026B-page 56Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
5.2.2.1OPERATION
The long write is what actua ll y programs words of da ta
into the internal memory. When a TBLWT to the MSB of
the write block occurs, instruction execution is halted.
During this time, programming voltage and the data
stored in internal latches is applied to program memory.
For a long write to occur:
1.MCLR
2.LWRT bit must be set
3.TBLWT to the address of the MSB of the write
If the LWRT bit is clear, a short write will occur and program memory will not be changed. If the TBLWT is not
to the MSB of the write block, then the programming
phase is not initiated.
Setting the LWRT bit enables long writes when the
MCLR
is set, it can be cleared only by performing a POR or
MCLR reset.
To ensure that the memory location has been well programmed, a minimum programming time is required.
The long write can be terminated after the programming time has expired by a reset or an interrupt. Having
only one interrupt so urce e nab led to terminate the lon g
write ensures that no unintended interrupts will prematurely terminate the long write.
/VPP pin must be at the prog ramming v olt-
age
block
pin is taken to VPP voltage. Once the LWRT bit
5.2.2.2SEQUENCE OF EVENTS
The sequence of events for programming an internal
program memory location should be:
1.Enable the interrupt that terminates the long
write. Disable all other interrupts.
2.Clear the source interrupt flag.
3.If Interrupt Service Routine execution is desired
when the device wakes, enable global interrupts.
4.Set LWRT bit in the RCON register.
5.Raise MCLR
age, V
6.Clear the WDT (if enabled).
7.Set the interrupt source to interrupt at the
required time.
8.Execute the table write for the lower (e v en) b yte.
This will be a short write.
9.Execute the table write for the upper (odd) byte.
This will be a long w rite. The controlle r will go to
sleep while programming. The interrupt wakes
the controller.
10. If GIE was set, service the interrupt request.
11. Lower MCLR
12. Verify the memory location (table read).
/VPP pin to the pr ogramming volt-
PP.
/VPP pin to VDD.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 57
PIC18CXX2
5.2.3INTERRUPTS
The long write must be terminated by a reset or any
interrupt.
The interrupt source must have its interrupt enable bit
set. When the source sets its interrupt flag, programming will ter minate. This will oc cur regardless of the
settings of interrupt priority bits , the GIE/GIEH bit or th e
PIE/GIEL bit.
Depending on the states of interrupt priority bits, the
GIE/GIEH bit or the PIE/GIEL bit, program execution
can either be vectored to the high or low priority Interrupt Service Routine (ISR) or continue execution from
where programming commenced.
In either case, the interrupt flag will not be cleared
when programming is terminated and will need to be
cleared by the software.
TABLE 5-2:SLEEP MODE, INTERRUPT ENABLE BITS AND INTERRUPT RESULTS
GIE/
GIEH
XX X0
XXX10Long write continues, will wake when
0
(default)0(default)
0
(default)
PIE/
GIEL
11
Priority
X11Terminates long write,
high priority
(default)
Interrupt
Enable
(default)
11Terminates long write,
Interrupt
Flag
XLong write continues even if interrupt
flag becomes set during sleep.
the interrupt flag is set.
executes next instruction. Interrupt flag
not cleared.
executes next instruction. Interrupt flag
not cleared.
Action
10
(default)
0
(default)
10
10
(default)1high priority
0
low
low
(default)
11Terminates long write, executes next
instruction. Interrupt flag not cleared.
11Terminates long write, branches to low
priority interrupt vector.
Interrupt flag can be cleared by ISR.
11Terminates long write, branches to high
priority interrupt vector.
Interrupt flag can be cleared by ISR.
DS39026B-page 58Preliminary
7/99 Microchip Technology Inc.
5.2.4UNEXPECTED TERMINATION OF WRITE
OPERATIONS
If a write is terminated by an unplanned event such as
loss of powe r, an unexpected reset, or an in terrup t th at
was not disabled, the memory location just programmed should be verified and reprogrammed if
needed.
PIC18CXX2
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 59
PIC18CXX2
NOTES:
DS39026B-page 60Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
6.08 X 8 HARDWARE MULTIPLIER
6.1Introduction
An 8 x 8 hardware multiplier is included in the ALU of
the PIC18CXX2 devices. By making the multiply a
hardware oper at ion , it c om pl ete s in a single instruction
cycle. This is an unsigned multiply that gives a 16-bit
result. The result is store d into th e 16-bit produ ct regi ster pair (PRODH:PRODL). The multiplier does not
affect any flags in the ALUSTA register.
Making the 8 x 8 multiplier execute in a single cycle
gives the following advantages:
• Higher computational throughput
• Reduces code si z e req uirements for multiply algorithms
The performance i ncrease all ows the de vice to be used
in applications previously reserved for Digital Signal
Processors.
Table 6-1 shows a performance comparison between
enhanced de vices using the s ingle cycle hardw are multiply, and performing the same function without the
hardware multiply.
TABLE 6-1:PERFORMANCE COMPARISON
RoutineMultiply MethodProgram
Memory
(Words)
8 x 8 unsignedWithout hardware multiply13696.9 µs27.6 µs69 µs
Hardware multiply11100 ns400 ns1 µs
8 x 8 signedWithout hardware multiply33919.1 µs36.4 µs91 µs
Hardware multiply66600 ns2.4 µs6 µs
16 x 16 unsignedWithout hardware multiply2124224.2 µs96.8 µs242 µs
Cycles
(Max)
Time
@ 40 MHz@ 10 MHz@ 4 MHz
Hardware multiply24242.4 µs9.6 µs24 µs
16 x 16 signedWithout hardware multiply5225425.4 µs102.6 µs254 µs
Hardware multiply36363.6 µs14.4 µs36 µs
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 61
PIC18CXX2
6.2Operation
Example 6-1 shows the sequence to do an 8 x 8
unsigned multiply. Only one instruction is required
when one argumen t of the multiply i s alre ady l oad ed in
the WREG register.
Example 6-2 shows th e sequence t o do an 8 x 8 s igned
multiply. To account for the sign bits of the arguments,
each argument’s most significant bit (MSb) is tested
and the appropriate subtractions are done.
MOVFF ARG1, WREG
MULWF ARG2 ; ARG1 * ARG2 ->
; PRODH:PRODL
BTFSC ARG2, SB ; Test Sign Bit
SUBWF PRODH, F ; PRODH = PRODH
; - ARG1
MOVFF ARG2, WREG
BTFSC ARG1, SB ; Test Sign Bit
SUBWF PRODH, F ; PRODH = PRODH
; - ARG2
Example 6-3 shows the sequence to do a 16 x 16
unsigned multiply. Equation 6-1 shows the algorithm
that is used. The 32-bit result is stored in 4 registers
RES3:RES0.
Example 6-4 shows the sequence to do an 16 x 16
signed multiply. Equation 6-2 shows the algorithm
used. The 32-bit result is stored in four registers
RES3:RES0. To account for the sign bits of the arguments, each argument pairs most significant bit (MSb)
is tested and the appropriate subtractions are done.
The PIC18CXX2 devices have multiple interrupt
sources and an interrupt priority feature that allows
each interrupt source to be assigned a high priority
level or a low priority level. The high priority interrupt
vector is at 0000 08h and the low priority interrupt vector
is at 000018h. High priority interrupt events will override any low priority interrupts that may be in progress.
There are ten registers which are used to control interrupt operation. These registers are:
• RCON
•INTCON
• INTCON2
• INTCON3
• PIR1, PIR2
• PIE1, PIE2
• IPR1, IPR2
It is recommended that the Microchip header files supplied with MPLAB be used for the symbolic bit names
in these registers. This allows the assembler/compiler
to automatically tak e care of the placement of these bit s
within the specified register.
Each interrupt source has three bits to control its operation. The functions of these bits are:
• Flag bit to indicate that an interrupt event
occurred
• Enable bit that allows pr ogram execution to
branch to the interrupt ve ctor address when
the flag bit is set
• Priority bit to select high priority or lo w priority
The interr upt pri ority featu re is enabled by setting the
IPEN bit (RCON<7>). When interrupt priority is
enabled, the re are tw o bits wh ich e nab le in terrupts globally. Setting the GIEH bit (INTCON<7>) enables all
interrupts that hav e the priority bit set. Setting the GIEL
bit (INTCON<6>) enables all interrupts that have the
priority bit cleared. When the interrupt flag, enable bit
and appropriate global interrupt enable it are set, the
interrupt will v ecto r imme diatel y to a ddress 00000 8h or
000018h depending on the priority level. Individual
interrupts can be disabled through their corresponding
enable bits.
When the IPEN bit is cleared (default state), the interrupt priority feature is disabled and interrupts are compatible with PICmicro mid-range devices. In
compatibility mode, the interrupt priority bits for each
source have no effect. INTCON<6> is the PEIE bit,
which enable s/disab les all peripher al inte rrupt sources .
INTCON<7> is the GIE bit, which enables/disables all
interrupt sources. All interrupts branch to address
000008h in compatibility mode.
When an interrupt is res po nde d to, the Global Interrupt
Enable bit is c le ared t o dis able further interrupts. If th e
IPEN bit is cleared, this is the GIE bit. If interrupt priority levels are used, this will be either the GIEH or GIEL
bit. High priority interrupt sources can interrupt a low
priority interrupt.
The return address is pushed onto the stack and the
PC is loaded with the interrupt vector address
(000008h or 000018h). Once in the interrupt service
routine, the source(s) of the interrupt can be determined by polling the interrupt flag bits. The interrupt
flag bits must b e cle ared i n sof tware bef ore re-ena b ling
interrupts to avoid recursive interrupts.
The "return from interrupt" instruction, RETFIE, exits
the interrupt routine and se ts the GIE bit (GI EH or GIEL
if priority levels are used), which re-enables interrupts.
For external interrupt events, such as the INT pins or
the PORTB input c hange interrupt, th e interrupt latency
will be three to four instruction cycles. The exact
latency is the same for one or two cycle instr uctions.
Individual interrupt flag bits are set regardless of the
status of their corresponding enable bit or the GIE bit.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 65
PIC18CXX2
FIGURE 7-1:INTE RRUPT LOGIC
Peripheral Interrupt Flag bit
Peripheral Interrupt Enable bit
Peripheral Interrupt Priority bit
TMR1IF
TMR1IE
TMR1IP
XXXXIF
XXXXIE
XXXXIP
High Priority Interrupt Generation
Low Priority Interrupt Generation
Peripheral Interrupt Flag bit
Peripheral Interrupt Enable bit
Peripheral Interrupt Priority bit
TMR1IF
TMR1IE
TMR1IP
XXXXIF
XXXXIE
XXXXIP
Additional Peripheral Interrupts
Additional Peripheral Interrupts
T0IF
T0IE
T0IP
RBIF
RBIE
RBIP
INT0F
INT0E
INT1F
INT1E
INT1P
INT2F
INT2E
INT2P
IPE
IPE
GIEL/PEIE
T0IF
T0IE
T0IP
RBIF
RBIE
RBIP
INT0F
INT0E
INT1F
INT1E
INT1P
INT2F
INT2E
INT2P
IPE
Wake-up if in SLEEP mode
Interrupt to CPU
Vector to location
0008h
GIEH/GIE
Interrupt to CPU
Vector to Location
0018h
GIEL\PEIE
DS39026B-page 66Preliminary
7/99 Microchip Technology Inc.
7.0.1INTCON REGISTERS
The INTCON Registers are readable and writable
registers, which contains various enable, priority and
flag bits.
1 = Enables all un-masked interrupts
0 = Disables all interrupts
When IPEN = 1:
1 = Enables all interrupts
0 = Disables all interrupts
bit 6PEIE/GEIL: Peripheral Interrupt Enable bit
IPEN = 0:
When
1 = Enables all un-masked peripheral interrupts
0 = Disables all peripheral interrupts
When IPEN = 1:
1 = Enables all low priority peripheral interrupts
0 = Disables all priority peripheral interrupts
bit 5TMR0IE: TMR0 Overflow Interrupt Enable bit
1 = Enables the TMR0 overflow interrupt
0 = Disables the TMR0 overflow interrupt
bit 4INT0IE: INT0 External Interrupt Enable bit
1 = Enables the INT0 external interrupt
0 = Disables the INT0 external interrupt
bit 3RBIE: RB Port Change Interrupt Enable bit
1 = Enables the RB port change interrupt
0 = Disables the RB port change interrupt
bit 2TMR0IF: TMR0 Overflow Interrupt Flag bit
1 = TMR0 register has overflowed (must be cleared in software)
0 = TMR0 register did not overflow
bit 1INT0IF: INT0 External Interrupt Flag bit
1 = The INT0 external interrupt occurred (must be cleared in software)
0 = The INT0 external interrupt did not occur
bit 0RBIF: RB Port Change Interrupt Flag bit
1 = At least one of the RB7:RB4 pins changed state (must be cleared in software)
0 = None of the RB7:RB4 pins have changed state
PIC18CXX2
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
Note:Interrupt flag bits get set when an interrupt condition occurs, regardless of the state
of its corresponding e nable bit or the glob al enable bit. User s oftware should ensure
the appropriate interrupt fl ag bi ts are clear prior to e nab ling an in terrupt. This feature
allows for software polling.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 67
PIC18CXX2
Register 7-2:INTCON2 Register
R/W-1R/W-1R/W-1R/W-1U-0R/W-1U-0R/W-1
RBPU
bit 7bit 0
bit 7RBPU: PORTB Pull-up Enable bit
1 = All PORTB pull-ups are disabled
0 = PORTB pull-ups are enabled by individual port latch values
bit 6INTEDG0:External Interrupt0 Edge Select bit
1 = Interrupt on rising edge
0 = Interrupt on falling edge
bit 5INTEDG1: External Interrupt1 Edge Select bit
1 = Interrupt on rising edge
0 = Interrupt on falling edge
bit 4INTEDG2: External Interrupt2 Edge Select bit
1 = Interrupt on rising edge
0 = Interrupt on falling edge
bit 3Unimplemented: Read as '0'
bit 2TMR0IP: TMR0 Overflow Interrupt Priority bit
1 = High priority
0 = Low priority
bit 1Unimplemented: Read as '0'
bit 0RBIP: RB Port Change Interrupt Priority bit
1 = High priority
0 = Low priority
INTEDG0INTEDG1INTEDG2
—
TMR0IP
—
RBIP
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
Note:Interrupt flag bits get set when an interrupt condition occurs, regardless of the state
of its corresponding e nable bit or the glob al enable bit. User s oftware should ensure
the appropriate interrupt f lag bi ts are clear prior t o enab l ing an interrupt. This feature
allows for software polling.
DS39026B-page 68Preliminary
7/99 Microchip Technology Inc.
Register 7-3:INTCON3 Register
R/W-1R/W-1U-0R/W-0R/W-0U-0R/W-0R/W-0
PIC18CXX2
INT2IPINT1IP
bit 7bit 0
bit 7INT2IP: INT2 External Interrupt Priority bit
1 =High priority
0 =Low priority
bit 6INT1IP: INT1 External Interrupt Priority bit
1 =High priority
0 =Low priority
bit 5Unimplemented: Read as '0'
bit 4INT2IE: INT2 External Interrupt Enable bit
1 =Enables the INT2 external interrupt
0 =Disables the INT2 external interrupt
bit 3INT1IE: INT1 External Interrupt Enable bit
1 =Enables the INT1 external interrupt
0 =Disables the INT1 external interrupt
bit 2Unimplemented: Read as '0'
bit 1INT2IF: INT2 External Interrupt Flag bit
1 =The INT2 external interrupt occurred
(must be cleared in software)
0 =The INT2 external interrupt did not occur
bit 0INT1IF: INT1 External Interrupt Flag bit
1 =The INT1 external interrupt occurred
(must be cleared in software)
0 =The INT1 external interrupt did not occur
—
INT2IEINT1IE
—
INT2IFINT1IF
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
Note:Interrupt flag bits get set when an interrupt condition occurs, regardless of the state
of its corresponding e nable bit or the glob al enable bit. User s oftware should ensure
the appropriate interrupt fl ag bi ts are clear prior to e nab ling an in terrupt. This feature
allows for software polling.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 69
PIC18CXX2
7.0.2PIR REGISTERS
The PIR registers contain th e individu al flag bi ts f o r the
peripheral interrupts. Due to he number of peripheral
interrupt sources, there are two Peripheral Interrupt
Flag Registers (PIR1, PIR2).
Note 1: Interrupt flag bits get set when an interrupt
condition occurs , regardless of the state of
its corresponding enable bit or the global
enable bit, GIE (INTCON<7>).
Note 2: User softw ar e should e nsure th e approp ri-
ate interrupt flag bits are cleared prior to
enabling an interrupt, and after servicing
that interrupt.
Register 7-4:RCON Register
R/W-0R/W-0U-0R/W-1R/W-1R/W-1R/W-0R/W-0
IPENLWRT
—RITOPDPORBOR
7.0.3PIE REGISTERS
The PIE registers contain the individual enable bits for
the peripheral interrupts. Due to the number of peripheral interrupt sources, there are two Per ipheral Interrupt Enable Registers (PIE1, PIE2). When IPEN = 0,
the PEIE bit must be s et to e na b l e an y o f th es e p eripheral interrupts.
7.0.4IPR REGISTERS
The IPR registers contain the individual priority bits for
the peripheral interrupts. Due to on the number of
peripheral interrupt sources, there are two Peripheral
Interrupt Priority Registers (IPR1, IPR2). The opera tion
of the priority bits requires that the Interrupt Priority
Enable (IPEN) bit be set.
7.0.5RCON REGISTER
The RCON register contains the bit which is used to
enable prioritized interrupts (IPEN).
bit 7bit 0
bit 7IPEN: Interrupt Priority Enable bit
1 =Enable priority levels on interrupts
0 =Disable priority levels on interrupts (16CXXX compatibility mode)
bit 6LWRT: Long Write Enable
For details of bit operation see Register 4-1
bit 5Unimplemented: Read as ’0’
bit 4RI
bit 3TO
bit 2PD
bit 1POR
bit 0BOR
: Reset Instru ction Flag bit
For details of bit operation see Register 4-1
: Watchdog Time-out Flag bit
For details of bit operation see Register 4-1
: Power-down Detection Flag bit
For details of bit operation see Register 4-1
: Power-on Reset Status bit
For details of bit operation see Register 4-1
: Brown-out Reset Status bit
For details of bit operation see Register 4-1
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
IPR1 bit 7PSPIP: Parallel Slave Port Read/Write Interrupt Priority bit
1 = High priority
0 = Low priority
bit 6ADIP: A/D Converter Interrupt Priority bit
1 = High priority
0 = Low priority
bit 5RCIP: USART Receive Interrupt Priority bit
1 = High priority
0 = Low priority
bit 4TXIP: USART Transmit Interrupt Priority bit
1 = High priority
0 = Low priority
bit 3SSPIP: Master Synchronous Serial Port Interrupt Priority bit
1 = High priority
0 = Low priority
bit 2CCP1IP: CCP1 Interrupt Priority bit
1 = High priority
0 = Low priority
bit 1TMR2IP: TMR2 to PR2 Match Interrupt Priority bit
1 = High priority
0 = Low priority
bit 0TMR1IP: TMR1 Overflow Interrupt Priority bit
1 = High priority
0 = Low priority
IPR2 bit 7-4Unimplemented: Read as '0'
bit 3BCLIP: Bus Collision Interrupt Priority bit
1 = High priority
0 = Low priority
bit 2LVDIP: Low-voltage Detect Interrupt Priority bit
1 = High priority
0 = Low priority
bit 1TMR3IP: TMR3 Overflow Interrupt Priority bit
1 = High priority
0 = Low priority
bit 0CCP2IP: CCP2 Interrupt Priority bit
1 = High priority
0 = Low priority
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
DS39026B-page 74Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
7.0.6INT0 INTERRUPT
External interrupts on the RB0/INT0, RB1/INT1 and
RB2/INT2 pins are edge triggered: either rising if the
corresponding INTEDGx b it is set in the INTCON2 re gister, or falling, if the INTEDGx bit is clear. When a valid
edge appears on the RBx/INTx pin, the corresponding
flag bit INTxF is set. This interrupt can be disabled by
clearing the cor respondi ng enable bit INT xE. Fla g bit
INTxF must be cleared in software in the interrupt service routine before re-enabling the interrupt. All external interrupts (INT0, INT1 and INT2) can wake-up the
processor from SLEEP, if bit INTxE was set prior to
going into SLEEP. If the global in terrupt enable bit G IE
set, the processor will br anch to the inter rupt v ecto r f ollowing wake-up.
Interrupt priority for INT1 and INT2 is determined b y the
value contained in the interrupt priority bits INT1IP
(INTCON3<6>) and INT2IP (INTCON3<7>). There is
no priority bit associated with INT0. It is always a high
priority interrupt source.
7.0.7TMR0 INTERRUPT
In 8-bit mode (which is the d efa ult), an ov erflo w (FFh →
00h) in the TMR0 register will set flag bit TMR0IF. In
16-bit mode, an overflow (FFFFh → 0000h) in the
TMR0H:TMR0L registers will set flag bit TMR0IF. The
interrupt can be enabled/disabled by setting/clearing
enable bit T0IE (INTCON<5>). Interrupt priority for
Timer0 is determined by the value contained in the
interrupt priority bit TMR0IP (INTCON2<2>). See Section 8.0 for further details on the Timer0 module.
7.0.8PORTB INTERRUPT ON CHANGE
An input change on PORTB<7:4> sets flag bit RBIF
(INTCON<0>). The interrupt can be enabled/disabled
by setting/clearing enable bit RBIE (INTCON<3>).
Interrupt priority for PORTB Interrupt on change is
determined by the value contained in the interrupt priority bit RBIP (INTCON2<0>).
7.1Context Saving During Inte rr upts
During an interrupt, the return PC v alue i s sa ve d on the
stack. Additionally, the WREG, STATUS and BSR registers are sav ed on the f ast return stac k. If a f ast return
from interrupt is not used (See Section 4.3), the user
may need t o s ave the WR EG, STATUS a nd BSR regis-
ters in software. Depending on the user’s application,
other registers ma y also nee d to be sav ed. Example 61 saves and restores the WREG, STATUS and BSR
registers during an i nterrupt service routine.
EXAMPLE 7-1:SAVING STATUS, WREG AND BSR REGISTERS IN RAM
MOVWFW_TEMP; W_TEMP is in virtual bank
MOVFFSTATUS, STATUS_TEMP; STATUS_TEMP located anywhere
MOVFFBSR, BSR_TEMP; BSR located anywhere
;
; USER ISR CODE
;
MOVFFBSR_TEMP, BSR; Restore BSR
MOVFW_TEMP, W; Restore WREG
MOVFFSTATUS_TEMP, STATUS; Restore STATUS
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 75
PIC18CXX2
NOTES:
DS39026B-page 76Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
8.0I/O PORTS
Depending on the de vic e selec ted, there are either five
ports or three ports available. Some pins of the I/O
ports are multiplexed with an alternate function from
the peripheral features on the device. In general, when
a peripheral is enabled, that pin may not be used as a
general purpose I/ O pin.
Each port has three registers for its operation. These
registers are:
• TRIS register (Data Direction register)
• PORT registe r (rea ds the levels on th e pi ns of th e
device)
• LAT register (output latch)
The data latch (LAT register) is useful for read-modifywrite operations on the value that the I/O pins are driving.
8.1PORTA, TRISA and LATA Registers
PORTA is a 6-bit wide bi-directional port. The corresponding data direction register is TRISA. Setting a
TRISA bit (=1) will m ake the correspondi ng PORTA pin
an input, (i.e., put the corresponding output driver in a
hi-impedance mode). Clearing a TRISA bit (=0) will
make the corre sponding POR TA pin an output, (i .e., put
the contents of the output latch on the selected pin).
Note:On a Power-on Reset, these pins are con-
figured as inputs and read as '0'.
Reading the PORTA register reads the status of the
pins, whereas writing to it will write to the port latch.
The Data Latch register (LATA) is also memory
mapped. Read-modify-write operations on the LATA
register reads and writes the latched output value for
PORTA.
The RA4 pin is multiplexed with the Timer0 module
clock input to become the RA4/T0CKI pin. The RA4/
T0CKI pin is a Schmitt Trigger input and an open drain
output. All other RA p ort pins hav e TTL input le v els and
full CMOS output drivers.
The other PORTA pins are multiplexed with analog
inputs and the analog V
operation of each pin is se lected by clea ring/settin g the
control bits in the ADCON1 register (A/D Control
Register1).
Note:On a Power-on Reset, these pins are con-
figured as analog inputs and read as '0'.
The TRISA register controls the direction of the RA
pins, even when they are being used as analog inputs.
The user must ensure the bi ts in the TRISA registe r are
maintained set when using them as analog inputs.
REF+ and VREF- inputs. The
EXAMPLE 8-1:INITIALIZING PORTA
CLRF PORTA; Initialize PORTA by
; clearing output
CLRF LATA; Alternate method
MOVLW 0x07; Configure A/D
MOVWF ADCON1; for digital inputs
MOVLW 0xCF
MOVWF TRISA; Set RA<3:0> as inputs
; data latches
; to clear output
; data latches
Value used to
;
; initialize data
; direction
; RA<5:4> as outputs
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 77
PIC18CXX2
FIGURE 8-1:BLOCK DIAGRAM OF
RA3:RA0 AND RA5 PINS
RD LATA
Data
Bus
WR LATA
or
PORTA
WR TRISA
RD PORTA
Q
CK
Data Latch
Q
CK
TRIS Latch
QD
QD
RD TRISA
QD
Analog
input
mode
EN
VDD
P
N
V
SS
I/O pin
TTL
input
buffer
FIGURE 8-3:BLOCK DIAGRAM OF RA6
ECRA6 or
RCRA6 enable
Data
Bus
RD LATA
QD
WR LATA
or
(1)
PORTA
WR
TRISA
CK
Data Latch
CK
TRIS Latch
Q
QD
Q
ECRA6 or
RCRA6
enable
Data Bus
RD TRISA
Data Bus
VDD
P
N
SS
V
QD
I/O pin
TTL
input
buffer
(1)
SS input (RA5 only)
To A/D Converter and LVD Modules
Note 1: I/O pins have protection diodes to VDD and VSS.
FIGURE 8-2:BLOCK DIAGRAM OF RA4/
T0CKI PIN
Data
Bus
WR LATA
or
PORTA
WR TRISA
RD LATA
QD
Q
CK
Data Latch
QD
Q
CK
TRIS Latch
RD TRISA
N
SS
V
Schmitt
Trigger
input
buffer
QD
I/O pin
EN
RD PORTA
Note 1: I/O pins have protection diodes to VDD and VSS.
(1)
EN
EN
RD PORTA
TMR0 Cock Input
Note 1: I/O pin has protection diodes to VSS only.
DS39026B-page 78Preliminary
7/99 Microchip Technology Inc.
TABLE 8-1:PORTA FUNCTIONS
NameBit#Buffer Function
RA0/AN0bit0TTLInput/output or analog input
RA1/AN1bit1TTLInput/output or analog input
PIC18CXX2
RA2/AN2/V
RA3/AN3/VREF+bit3TTLInput/output or analog input or VREF+
RA4/T0CKIbit4STInput/output or external clock input for Timer0
RA5/SS/
OSC2/CLKO/RA6bit6OSC2 or clock output or I/O pin
Legend: TTL = TTL input, ST = Schmitt Trigger input
TABLE 8-2:SUMMARY OF REGISTERS ASSOCIATED WITH PORTA
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
PORT A—RA6RA5RA4RA3RA2RA1RA0--0x 0000--0u 0000
LATA
TRISA
ADCON1ADFMADCS2
Legend: x = unknown, u = un chang ed, - = u nimpl emen ted loc ations re ad a s ’ 0’ . Shad ed ce lls are not used b y PORTA.
REF-bit2TTLInput/output or analog input or VREF-
Output is open drain type
AN4/LVDINbit5TTLInput/output or slave select input for synchronous serial port or analog
input, or low voltage detect input
Value on
POR,
BOR
—Latch A Data Output Register--xx xxxx--uu uuuu
—PORTA Data Direction Register--11 1111--11 1111
——PCFG3PCFG2PCFG1PCFG0--0- 0000--0- 0000
Value on all
other
resets
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 79
PIC18CXX2
8.2PORTB, TRISB and LATB Registers
PORTB is an 8-bit wide bi-directional port. The corresponding data direction register is TRISB. Setting a
TRISB bit (=1) will mak e the correspon ding POR TB pin
an input, (i.e., put the corresponding output driver in a
hi-impedance mode). Clearing a TRISB bit (=0) will
make the corresponding PORTB pin an output, ( i.e. 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 reads and writes the latched output value for
PORTB.
EXAMPLE 8-2:INITIALIZING PORTB
CLRF PORTB; Initialize PORTB by
CLRF LATB; Alternate method
MOVLW 0xCF; Value used to
MOVWF TRISB; Set RB<3:0> as inputs
Each of the PORTB pins has a weak internal pull-up. A
single control bit ca n turn on all the pull-ups . This is performed by clearing bit RBPU
weak pull-up i s autom atically tur ned off when the po rt
pin is configured as an output. The pull-ups are disabled on a Power-on Reset.
Four of PORTB’s pins, RB7:RB4, have an interrupt on
change feature. Only pins configured as inputs can
cause this interrupt to oc cur (i.e . any RB7:RB4 pin configured as an output is excluded from the interrupt on
change comparison). The input pins (of RB7:RB4) are
compared with th e o ld value latche d o n the la st read of
PORTB. The “mismatch” outputs of RB7:RB4 are
OR’ed together to generate the RB Port Change Interrupt with flag bit RBIF (INTCON<0>).
This interrupt can wake the device from SLEEP. The
user, i n the interrupt service routine , can clear the interrupt in the following manner:
a)Any read or write of PORTB (except with the
MOVFF instruction). This will end the mismatch
condition.
b)Clear flag bit RBIF.
A mismatch condition will continue to set flag bit RBIF.
Reading PORTB will end the mismatch condition and
allow flag bit RBIF to be cleared.
The interrupt on change feature is recommended for
wake-up on key depression operation and operations
where PORTB is only used for the interrupt on change
feature. Polling of PORTB is not recommended while
using the interrupt on change feature.
; clearing output
; data latches
; to clear output
; data latches
; initialize data
; direction
; RB<5:4> as outputs
; RB<7:6> as inputs
(INTCON2<7>). The
FIGURE 8-4:BLOCK DIAGRAM OF
RB7:RB4 PINS
TTL
Input
Buffer
EN
EN
DD and VSS.
V
RD PORTB
(2)
RBPU
Data Bus
WR LATB
or
PORTB
WR TRISB
Set RBIF
From other
RB7:RB4 pins
RBx/INTx
Note 1: I/O pins have diode protection to V
2: To enable weak pull-ups, set the appropriate TRIS
Data Latch
QD
CK
TRIS Latch
QD
CK
RD TRISB
RD LATB
RD PORTB
bit(s) and clear the RBPU
Latch
QD
QD
bit (INTCON2<7>).
FIGURE 8-5:BLOCK DIAGRAM OF
RB2:RB0 PINS
(2)
RBPU
Data Bus
WR Port
WR TRIS
RB0/INT
Note 1: I/O pins have diode protection to V
2: To enable weak pull-ups, set the appropriate TRIS
bit(s) and clear the RBPU
Data Latch
CK
TRIS Latch
CK
RD TRIS
RD Port
Schmitt Trigger
Buffer
QD
QD
QD
EN
DD and VSS.
bit (OPTION_REG<7>).
TTL
Input
Buffer
DD
P
weak
pull-up
I/O
pin
Buffer
Q1
V
DD
weak
P
pull-up
RD Port
Q3
(1)
ST
I/O
pin
(1)
DS39026B-page 80Preliminary
7/99 Microchip Technology Inc.
FIGURE 8-6:BLOCK DIAGRAM OF RB3
(2)
RBPU
CCP2MX
CCP Output
(3)
(3)
Enable
CCP Output
Data Bus
WR LATB or
WR PORTB
WR TRISB
1
Data Latch
QD
CK
TRIS Latch
D
CK
Q
RD TRISB
RD LATB
0
Q
PIC18CXX2
V
DD
weak
P
pull-up
V
DD
P
I/O
(1)
N
VSS
TTL
Input
Buffer
D
Pin
RD PORTB
RD PORTB
CCP2 input
Note 1: I/O pin has diode protection to VDD and VSS.
2: To enable weak pull-ups, set the appropriate DDR bit(s) and clear the RBPU
3: The CCP2 input/output is multiplexed with RB3 if the CCP2MX bit is enabled (=’0’) in the configuration register.
(3)
Schmitt Trigger
Buffer
CCP2MX = 0
EN
bit (INTCON2<7>).
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 81
PIC18CXX2
TABLE 8-3:PORTB FUNCTIONS
NameBit#Buffer Function
(1)
RB0/INT0bit0
RB1/INT1bit1
RB2/INT2bit2
(3)
RB3/CCP2
bit3
TTL/ST
TTL/ST
TTL/ST
TTL/ST
RB4bit4TTLInput/output pin (with interrupt on change). Internal software programma-
RB5bit5TTLInput/output pin (with interrupt on change). Internal software programma-
RB6bit6
RB7bit7
TTL/ST
TTL/ST
Legend: TTL = TTL input, ST = Schmitt Trigger input
Note1: This buffer is a Schmitt Trigger input when configured as the external interrupt.
2: This buffer is a Schmitt Trigger input when used in serial programming mode.
3: A device configuration bit selects which I/O pin the CCP2 pin is multiplexed on.
4: This buffer is a Schmitt Trigger input when configured as the CCP2 input.
make a pin an input. Th e user shoul d ref er to the corresponding peripheral se ction for the corre ct TRIS bit set-
PORTC is an 8 bit wide bi-directional port. The corresponding Data Direction Register is TRISC. Setting a
TRISC bit (=1) will mak e the corres ponding POR TC pin
an input, (i.e., put the corresponding output driver in a
hi-impedance mode). Clearing a TRISC bit (=0) will
make the corresponding PORTC pin an output, (i.e.,
put the contents of the output latch on the selected pin).
The Data Latch register (LATC) is also memory
mapped. Read-modify-write operations on the LATC
register reads and writes the latched output value for
PORTC.
PORTC is mul tiple x ed with se v eral peripheral fun ctions
(Table 8-5). PORTC pins have Schmitt Trigger input
buffers.
When enabling peripheral functions, care should be
taken in defining TRIS bits for each PORTC pin. Some
periphe rals override the TRIS bit to make a pin an out-
tings.
The pin override value is not loaded into the TRIS reg-
ister. Th is allows re ad-modify-write of the TRIS register ,
without concern due to peripheral overrides.
EXAMPLE 8-3:INITIALIZING PORTC
CLRF PORTC; Initialize PORTC by
; clearing output
; data latches
CLRF LATC; Alternate method
; to clear output
; data latches
MOVLW 0xCF; Value used to
; initialize data
; direction
MOVWF TRISC; Set RC<3:0> as inputs
; RC<5:4> as outputs
; RC<7:6> as inputs
put, while other peripherals override the TRIS bit to
RC0/T1OSO/ T1CKIbit0STInput/output port pin o r Timer1 oscillator output/Timer1 clock input
RC1/T1OSI/CCP2bit1STInput/output port pin, Timer1 oscillator input, or Capture2 input/
Compare2 output/PWM output when CCP2MX configuration bit is
disabled.
RC2/CCP1bit2STInput/output port pin or Cap ture1 input/Compare1 output/PWM1
output
RC3/SCK/SCLbit3ST
RC3 can also be the synchronous serial clock for both SPI and I
modes.
2
C
RC4/SDI/SDAbit4ST
RC5/SDObit5STInput/output port pin or Synchronous Serial Port data output
RC6/TX/CKbit6STInput/output port pin, Addressable USAR T Async hronous Transmit, or
RC7/RX/DTbit7STInput/output port pin, Addressable USART Asynchronous Receive, or
Legend: ST = Schmitt Trigger input
TABLE 8-6:SUMMARY OF REGISTERS ASSOCIATED WITH PORTC
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
PORTCRC7RC6RC5RC4RC3RC2RC1RC0xxxx xxxxuuuu uuuu
LATCLATC Data Output Registerxxxx xxxxuuuu uuuu
TRISCPORTC Data Direction Register1111 11111111 1111
Legend: x = unknown, u = unchanged.
RC4 can also be the SPI Data In (SPI mode) or data I/O (I2C mode).
Addressable USART Synchronous Clock
Addressable USART Synchro nous Data
Value on
POR,
BOR
Value on all
other resets
DS39026B-page 84Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
8.4PORTD, TRISD and LATD Registers
This section is applicable to only the PIC18C4X2
devices.
PORTD is an 8 bit wide bi-directional port. The corresponding Data Direction Register is TRISD. Setting a
TRISD bit (=1) will mak e the corres ponding POR TD pin
an input, (i.e., put the corresponding output driver in a
hi-impedance mode). Clearing a TRISD bit (=0) will
make the corresponding PORTD pin an output, (i.e.,
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 reads and writes the latched output value for
PORTD.
PORTD is an 8-bit port with Schmitt Trigger input buffers. Each pin is individually configurable as an input or
output.
PORTD can be configured as an 8-bit wide microprocessor por t (parallel slave port) by sett ing control bit
PSPMODE (TRISE<4>). In this mode, the input buff ers
are TTL. See Section 8.6 for additional information on
the Parallel Slave Port (PSP).
EXAMPLE 8-4:INITIALIZING PORTD
CLRF PORTD ; Initialize PORTD by
CLRF LATD; Alternate method
MOVLW 0xCF; Value used to
MOVWF TRISD; Set RD<3:0> as inputs
; clearing output
; data latches
; to clear output
; data latches
; initialize data
; direction
; RD<5:4> as outputs
; RD<7:6> as inputs
FIGURE 8-8:PORTD BLOCK DIAGRAM
IN I/O PORT MODE
Data
Bus
WR LATD
or
PORTD
WR TRISD
RD PORTD
Note 1: I/O pins have protection diodes to VDD and VSS.
Legend: ST = Schmitt Trigger input, TTL = TTL input
Note1: Input buffers are Schmitt Triggers when in I/O mode and TTL buffer when in Parallel Slave Port Mode.
Input/output port pin or parallel slave port bit0
Input/output port pin or parallel slave port bit1
Input/output port pin or parallel slave port bit2
Input/output port pin or parallel slave port bit3
Input/output port pin or parallel slave port bit4
Input/output port pin or parallel slave port bit5
Input/output port pin or parallel slave port bit6
Input/output port pin or parallel slave port bit7
TABLE 8-8:SUMMARY OF REGISTERS ASSOCIATED WITH PORTD
Value on
POR,
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
PORTDRD7RD6RD5RD4RD3RD2RD1RD0xxxx xxxxuuuu uuuu
LATDLATD Data Output Registerxxxx xxxxuuuu uuuu
TRISDPORTD Data Direction Register1111 11111111 1111
TRISE
IBFOBFIBOVPSPMODE—PORTE Data Direction Bits0000 -1110000 -111
BOR
Val ue on all
other resets
Legend: x = unknown, u = unchanged, - = unimplemented read as ’0’. Shaded cells are not used by PORTD.
DS39026B-page 86Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
8.5PORTE, TRISE and LATE Registers
This section is only applicable to the PIC18C4X2
devices.
PORTE is an 3 bit wide bi-directional port. The corresponding Data Direction Register is TRISE. Setting a
TRISE bit (=1) will make the correspon ding POR TE pin
an input, (i.e., put the corresponding output driver in a
hi-impedance mode). Clearing a TRISE bit (=0) will
make the corresponding PORTE pin an output, (i.e.,
put the contents of the output latch on the selected pin).
The Data Latch Register (LATE) is also memory
mapped. Read-modify-write operations on the LATE
register reads and writes the latched output value for
PORTE.
PORTE has three pins RE0/RD
and RE2/CS
/AN7, which are individually configurable
as inputs or outputs. These pins have Schmitt Trigger
input buffers.
Figure 8-1 shows the TRISE register, which also controls the parallel slave port operation. Capture2 input/
Compare2 output/PWM output when CCP2MX configuration bit is enabled.
PORTE pins are multiplexed with analog inputs. When
selected as an an alog input, the se pins will r ead as ’ 0’ s.
TRISE controls the direction of th e RE pins, e v en when
they are being used as analog inputs. The user must
make sure to keep the pins configured as inputs when
using them as analog inputs.
/AN5, RE1/WR/AN6
FIGURE 8-9:PORTE BLOCK DIAGRAM
IN I/O PORT MODE
Data
Bus
WR LATE
or
PORTE
WR TRISE
RD PORTE
Note 1: I/O pins have protection diodes to VDD and VSS.
RD LATE
QD
CK
Data Latch
QD
CK
TRIS Latch
RD TRISE
To Analog Converter
Schmitt
Trig ger
input
buffer
QD
EN
EN
I/O pin
(1)
Note:On a Power-on Reset, these pins are con-
figured as analog inputs.
EXAMPLE 8-5:INITIALIZING PORTE
CLRF PORTE; Initialize PORTE by
; clearing output
CLRF LATE; Alternate method
MOVLW 0x07; Configure A/D
MOVWF ADCON1 ; for digital inputs
MOVLW 0x03; Value used to
MOVWF TRISC; Set RE<0> as inputs
; data latches
; to clear output
; data latches
; initialize data
; direction
; RE<1> as outputs
; RE<2> as inputs
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 87
PIC18CXX2
Register 8-1:TRISE Register
R-0R-0R/W-0R/W-0U-0R/W-1R/W-1R/W-1
IBFOBFIBOVPSPMODE
bit 7bit 0
bit 7IBF: Input Buffer Full Status bit
1 = A word has been received and waiting to be read by the CPU
0 = No word has been received
bit 6OBF: Output Buffer Full Status bit
1 = The output buffer still holds a previously written word
0 = The output buffer has been read
bit 5IBOV: Input Buffer Overflow Detect bit (in microprocessor mode)
1 = A write occurred when a previously input word has not been read
(must be cleared in software)
0 = No overflow occurred
bit 4PSPMODE: Parallel Slave Port Mode Sel ect bit
1 = Parallel slave port mode
0 = General purpose I/O mode
bit 3Unimplemented: Read as ’0’
bit 2TRISE2: RE2 direction control bit
1 = Input
0 = Output
bit 1TRISE1: RE1 direction control bit
1 = Input
0 = Output
bit 0TRISE0: RE0 direction control bit
1 = Input
0 = Output
—TRISE2TRISE1TRISE0
Legend:
R = Readable bitW = Writable bit
U = Unimplemented bit, read as ‘0’- n = Value at POR reset
DS39026B-page 88Preliminary
7/99 Microchip Technology Inc.
TABLE 8-9: PORTE FUNCTIONS
NameBit#Buffer TypeFunction
PIC18CXX2
RE0/RD
RE1/WR
RE2/CS
Legend: ST = Schmitt Trigger input, TTL = TTL input
Note1: Input buffers are Schmitt Triggers when in I/O mode and TTL buffers when in Parallel Slave Port Mode.
/AN5bit0
/AN6bit1
/AN7bit2
ST/TTL
ST/TTL
ST/TTL
TABLE 8-10:SUMMARY OF REGISTERS ASSOCIATED WITH PORTE
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
PORTE—————RE2RE1RE0---- -000---- -000
LATE
TRISEIBFOBFIBOVPSPMODE
ADCON1
Legend: x = unknown, u = unchanged, - = unimplemented read as ’0’. Shaded cells are not used by PORTE.
Input/output port pin or read control i nput in parallel slave port mode
or analog input:
RD
1 = Not a read operation
0 = Read operation. Reads PORTD register (if chip selected)
Input/output port pin or write control input in parallel slave port mode
or analog input:
WR
1 = Not a write operation
0 = Write operation. Writes PORTD register (if chip selected)
Input/output port pin or chip select control input in parallel slave port
mode or analog input:
S
C
1 = Device is not selected
0 = Device is selected
Value on
POR,
BOR
—PORTE Data Direction Bits0000 -1110000 -111
Valu e on all
other
resets
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 89
PIC18CXX2
8.6Parallel Slave Port
The Parallel Slave Port is impleme nted on the 40 -pin
devices only (PIC18C4X2).
PORTD operates as an 8-bit wide Parallel Slave Por t,
or microprocessor port when control bit PSPMODE
(TRISE<4>) i s set. In slave mode it is asynchr onous ly
readable and writab le by the external world thro ugh RD
control input pin RE0/RD and WR control input pin
RE1/WR
It can directly interface to an 8-bit microprocessor data
bus. The e x ternal micro processo r can read or write the
PORTD latch as an 8-bit latch. Setting bit PSPMODE
enables port pin RE0/RD
to be the WR input and RE2/CS to be the CS (chip
select) input. For this functionality, the corresponding
data direction bits of the TRISE register (TRISE<2:0>)
must be configured as inputs (set). The A/D port configuration bits PCFG2:PCFG0 (ADCON1<2:0>) must
be set, which will configure pins RE2:RE0 as digital I/O.
A write to the PSP occurs when both the CS
lines are first detec ted lo w . A read from t he PSP occu rs
when both the CS
The PORTE I/O pins become control inputs for the
microprocessor port when bit PSPMODE (TRISE<4>)
is set. In this mode, the user must make sure that the
TRISE<2:0> bits are set (pins are configured as digital
inputs), and the ADCON1 is confi gured for digit al I/O. In
this mode, the input buffers are TTL.
.
to be the RD input, RE1/WR
and WR
and RD lines are first detected low.
FIGURE 8-10: PORTD AND PORTE BLOCK
DIAGRAM
(PARALLEL SLAVE PORT)
Data Bus
WR LATD
or
PORTD
RD PORTD
RD LATD
One bit of PORTD
Set Interrupt Flag
PSPIF (PIR1<7>)
QD
CK
Data Latch
QD
EN
EN
TTL
Read
Chip Select
TTL
TTL
RDx
Pin
RD
CS
Note: I/O pin has protection diodes to VDD and VSS.
FIGURE 8-11: PARALLEL SLAVE PORT WRITE WAVEFORMS
Q1Q2Q3Q4CSQ1Q2Q3Q4Q1Q2Q3Q4
WR
RD
PORTD<7:0>
IBF
OBF
PSPIF
Write
TTL
WR
DS39026B-page 90Preliminary
7/99 Microchip Technology Inc.
FIGURE 8-12: PARALLEL SLAVE PORT READ WAVEFORMS
PIC18CXX2
Q1Q2Q3Q4
CS
WR
RD
PORTD<7:0>
IBF
OBF
PSPIF
TABLE 8-11:REGISTERS ASSOCIATED WITH PARALLEL SLAVE PORT
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
PORTDPort data latch when written; port pins when readxxxx xxxxuuuu uuuu
LATDLATD Data Output Bitsxxxx xxxxuuuu uuuu
TRISDPORTD Data Direction Bits1111 11111111 1111
PORTE
LATE
TRISEIBFOBFIBOVPSPMODE
INTCONGIE/
PIR1PSPIF
PIE1PSPIE
IPR1PSPIP
ADCON1
—————RE2RE1RE0---- -000---- -000
—————LATE Data Output Bits---- -xxx---- -uuu
Legend: x = unknown, u = unchanged, - = unimplemented read as ’0’.
Shaded cells are not used by the Parallel Slave Port.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 91
PIC18CXX2
NOTES:
DS39026B-page 92Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
9.0TIMER0 MODUL E
The Timer0 module has the following features:
• Software selectable as an 8-bit or 16-bit timer/
counter
• Readable and writable
• Dedicated 8-bit software programmable prescaler
• Clock source selectable to be external or internal
• Interrupt on overflow from FFh to 00h in 8-bit
mode and FFFFh to 0000h in 16-bit mode
• Edge select for external clock
Figure 9-1 shows a simplified block diagram of the
Timer0 module in 8-bit mode and Figure 9-1 shows a
simplified bl ock diagram of the Timer0 module in 1 6-bit
mode.
The T0CON register i s a re adab le and w ritab le reg ister
that controls all the aspects of Timer0, including the
prescale selection.
Register 9-1:T0CON: Timer0 Control Register
R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1
TMR0ONT08BITT0CST0SEPSAT0PS2T0PS1T0PS0
bit 7bit 0
bit 7TMR0ON: Timer0 On/Off Control bit
1 = Enables Timer0
0 = Stops Timer0
bit 6T08BIT: Timer0 8-bit/16-bit Control bit
1 = Timer0 is configured as an 8-bit timer/counter
0 = Timer0 is configured as a 16-bit timer/counter
1 = Increment on high-t o-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin
bit 3PSA: Timer0 Prescaler Assignment bit
1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler
0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output
bit 2:0T0PS2:T0PS0: Timer0 Prescaler Select bits
111 = 1:256 prescale value
110 = 1:128 prescale value
101 = 1:64 prescale value
100 = 1:32 prescale value
011 = 1:16 prescale value
010 = 1:8 prescale value
001 = 1:4 prescale value
000 = 1:2 prescale value
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 93
PIC18CXX2
FIGURE 9-1:TIMER0 BLOCK DIAGRAM IN 8-BIT MODE
FOSC/4
RA4/T0CKI
Pin
T0SE
Note:Upon reset, Timer0 is enabled in 8-bit mode with clock input from T0CKI max. prescale.
FIGURE 9-2:TIMER0 BLOCK DIAGRAM IN 16-BIT MODE
0
1
T0PS2, T0PS1, T0PS0
T0CS
Programmable
Prescaler
3
0
1
PSA
Sync with
Internal
clocks
(2 Tcy delay)
Data Bus
8
TMR0
Set Interrupt
Flag bit TMR0IF
on overflow
FOSC/4
T0CKI pin
T0SE
0
1
Programmable
Prescaler
T0PS2, T0PS1, T0PS0
T0CS
0
1
3
PSA
Sync with
Internal
Clocks
(2 Tcy delay)
TMR0L
8
Note:Upon reset, Timer0 is enabled in 8-bit mode with clock input from T0CKI max. prescale.
TMR0
High Byte
8
TMR0H
8
8
Read TMR0L
Write TMR0L
Data Bus<7:0>
Set Interrupt
Flag bit TMR0IF
on overflow
DS39026B-page 94Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
9.1Timer0 Operation
Timer0 can operate as a timer or as a counter.
Timer mode is selected by clearing the T0CS bit. In
timer mode, the Timer0 module will increment every
instruction cycl e (with out prescaler). If the TMR0 register is written, the increment i s inhibite d f o r the f oll owin g
two instruction cycles. The user can work around this
by writing an adjusted value to the TMR0 register.
Counter mode is selected by setting the T0CS bit. In
counter mode, Timer0 will increment either on every
rising or fal ling e dge o f pin RA4/T0 CKI. The in creme nting edge is determined by the Timer0 Source Edge
Select bit (T0SE). Clearing the T0SE bit selec ts the rising edge. Restrictions on the external clock input are
discussed be low.
When an ex ternal clock i nput is used f or Timer0 , it must
meet certain requirements. The requirements ensure
the external c lock can be synchron ized w ith the int ernal
phase clock (T
incrementing of Timer0 after synchronization.
OSC). Also, there is a delay in the actual
9.2Prescaler
An 8-bit counter is available as a prescaler for the
Timer0 modul e. The presca ler is not readable or wr itable.
The PSA and T0PS2:T0PS0 bits determine the prescaler assignment and prescale ratio.
Clearing bit PSA will assign the prescaler to the Timer0
module. When the prescaler is assigned to the Timer0
module, prescale values of 1:2, 1:4, ..., 1:256 are
selectable.
When assigned to the Ti mer0 mo dul e, all in str u cti on s
writing to the TMR0 register (e.g. CLRF TMR0,
MOVWF TMR0, BSF TMR0, x....etc.) will clear the
prescaler count.
9.2.1SWITCHING PRESCALER ASSIGNMENT
The prescaler assignment is fully under software con-
trol, (i.e., it can be change d “on- the-fly ” during progr am
ex ecutio n).
9.3Timer0 Interrupt
The TMR0 interrupt is generated when the TMR0 register overflo w s from FFh to 00h in 8-bit mod e or FFFFh
to 0000h in 16-bit mode. This ov erflow s ets the T MR0IF
bit. The interrupt can be masked by clearing the
TMR0IE bit. The TMR0IE bit must be cleared in software by the Timer0 module interrupt service routine
before re-enabling this interrupt. The TMR0 interrupt
cannot awaken the processor from SLEEP, since the
timer is shut off during SLEEP.
9.416-Bit Mode Timer Reads and Writes
TMR0H is not the high byte of the timer/counter in 16bit mode, but is actually a buffered version of the high
byte of Timer0 (refer to Figure 9-1). The high byte of
the Timer0 counter/timer is not directly readable nor
writable. TMR0H is updated with the contents of the
high byte of T imer0 during a read of TMR0L. This p r ovides the ability to read all 16-bits of Timer0 without
having to veri fy that t he read of the high and l ow byte
were valid due to a rollover between successive reads
of the high and low byte.
A write to the high byte of Timer0 must also take place
through the TMR0H b uff er register. Timer0 high byte is
updated with the contents of TMR0H when a write
occurs to TMR0L. This allows all 16 bits of Timer0 to
be updated at once.
Note:Writing to TMR0 when the prescaler is
assigned to Timer0 will clear the prescaler
count, but will not change the prescaler
assignment.
TABLE 9-1:REGISTERS ASSOCIATED WITH TIMER0
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TMR0LTimer0 Module’s Low Byte Register
TMR0HTimer0 Module’s High Byte Register
INTCONGIE/GIEH PEIE/GIEL TMR0IE INT0IERBIETMR0IF INT0IFRBIF
T0CONTMR0ONT08BITT0CST0SEPSAT0PS2T0PS1 T0PS0
TRISA——PORTA Data Direction Register
Legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'.
The Timer1 module timer/co unter has th e fol lowing f eatures:
• 16-bit timer/counter
(Two 8-bit registers; TMR1H and TMR1L)
• Readable and writable (Both registers)
• Internal or external clock select
• Interrupt on overflow from FFFFh to 0000h
• Reset from CCP module special event trigger
Figure 10-1 is a simplified block diagram of the Timer1
module.
Register 10-1 shows the Timer1 contro l register. This
register controls the operating mode of the Timer1
module as wel l as contai ns the Time r1 oscillat or enabl e
bit (T1OSCEN). Timer1 can be enabled/disabled by
setting/clearing control bit TMR1ON (T1CON<0>).
Register 10-1: T1CON: Timer1 Control Register
R/W-0U-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0
RD16
bit 7bit 0
bit 7RD16: 16-bit Read/Write Mode Enable bit
1 = Enables register Read/Write of TImer1 in one 16-bit operation
0 = Enables register Read/Write of Timer1 in two 8-bit operations
bit 6Unimplemented: Read as '0'
bit 5:4T1CKPS1:T1CKPS0: Timer1 Input Clock Prescale Select bits
11 = 1:8 Prescale value
10 = 1:4 Prescale value
01 = 1:2 Prescale value
00 = 1:1 Prescale value
bit 3T1OSCEN: Timer1 Oscillator Enable bit
1 = Timer1 Oscillator is enabled
0 = Timer1 Oscillator is shut off.
The oscillator inverter and feedback resistor are turned off to eliminate power drain
bit 2T1SYNC
When TMR1CS = 1:
1 = Do not synchronize external clock input
0 = Synchronize external clock input
When TMR1CS = 0:
This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.
bit 1TMR1CS: Timer1 Clock Source Select bit
1 = External clock from pin RC0/T1OSO/T13CKI (on the rising edge)
0 = Internal clock (F
bit 0TMR1ON: Timer1 On bit
1 = Enables Timer1
0 = Stops Timer1
—T1CKPS1T1CKPS0T1OSCENT1SYNCTMR1CSTMR1ON
: Timer1 External Clock Input Synchronization Select bit
OSC/4)
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR reset’1’ = Bit is set’0’ = Bit is clearedx = Bit is unknown
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 97
PIC18CXX2
10.1Timer1 Operation
Timer1 can operate in one of these modes:
•As a timer
• As a synchronous counter
• As an asynchronous counter
The operating mode is determined by the clock select
bit, TMR1CS (T1CON<1>).
FIGURE 10-1: TIMER1 BLOCK DIAGRAM
TMR1IF
Overflow
Interrupt
Flag Bit
T1CKI/T1OSO
T1OSI
TMR1H
T1OSC
TMR1
TMR1L
T1OSCEN
Enable
Oscillator
CLR
(1)
CCP Special Event Trigger
TMR1ON
on/off
FOSC/4
Internal
Clock
When TMR1CS = 0, Timer1 increments every instruction cycle. When TMR1CS = 1, Timer1 increments on
every rising edge of the external clock input or the
Timer1 oscillator, if enabled.
When the Timer1 oscillator is enabled (T1OSCEN is
set), the RC1/T1OSI and RC0/T1OSO/T1CKI pins
become inputs. That is, the TRISC<1:0> value is
ignored.
Timer1 also has an in ternal “reset input ”. This reset can
be generated by the CCP module (Section 13.0).
Synchronized
Clock Input
Synchronize
det
SLEEP input
1
0
T1CKPS1:T1CKPS0
TMR1CS
0
1
T1SYNC
Prescaler
1, 2, 4, 8
2
Note 1: When enable bit T1OSCEN is cleared, the inverter and feedback resistor are turned off. This
Note1: When enable bit T1OSCEN is cleared, the inverter and feedback resistor are turned off. This eliminates
power drain.
8
TMR1L
T1OSCEN
Enable
Oscillator
0
1
TMR1ON
on/off
1
Fosc/4
(1)
Internal
Clock
0
TMR1CS
T1SYNC
Prescaler
1, 2, 4, 8
2
T1CKPS1:T1CKPS0
Synchronized
clock input
Synchronize
det
SLEEP input
DS39026B-page 98Preliminary
7/99 Microchip Technology Inc.
PIC18CXX2
10.2Timer1 Oscillator
A crystal oscillator circuit is bu ilt-in between pins T1OSI
(input) and T1OSO (amplifier output). It is enabled by
setting control bit T1OSCEN (T 1CON<3>). The oscill ator is a low power oscillator rated up to 200 kHz. It will
continue to run during SLEEP. It is primarily intended
for a 32 kHz crystal. Table 10-1 shows the capacitor
selection for the Timer1 oscillator.
The user must provide a software time delay to ensure
proper start-up of the Timer1 oscill ator.
TABLE 10-1: CAPACITOR SELECTION FOR
THE ALTERNATE OSCILLATOR
Osc TypeFreqC1C2
(1)
LP32 kHzTBD
Crystal to be Tested:
32.768 kHz Epson C-001R32.768K-A± 20
Note1: Microchip suggests 33 pF as a starting
point in validating the oscillator circuit.
2: Higher capacitance increases the stability
of the oscillator, but also increases the
start-up time.
3: Since each resonator/crystal has its own
characteristics, the user should consult the
resonator/crystal manufacturer for appropriate values of external components.
4: Capacitor values are for design guidance
only.
TBD
PPM
(1)
10.3Timer1 Interrupt
The TMR1 Register pair (TMR1H:TMR1L) increments
from 0000h to FFFFh and rolls over to 0000h. The
TMR1 Interrupt, if enabled, is generated on overflow,
which is latched in interrupt flag bit TMR1IF (PI R1<0>).
This interrupt can be enab led/d isab led by se tting/cle aring TMR1 interrupt enable bit TMR1IE (PIE1<0>).
10.4Resetting Timer1 using a CCP Trigger
Output
If the CCP module is configured in compare mode to
generate a “special event trigger" (CCP1M3:CCP1M0
= 1011), this signal will reset Timer1 and start an A/D
conversion (if the A/D module is enabled).
Note:The special event triggers from the CCP1
module will not set interrupt flag bit
TMR1IF (PIR1<0>).
Timer1 must be configured for either timer or synchronized counter mode to tak e adv antage of this fea ture . If
Timer1 is running in asynchronous counter mode, this
reset operation may not work.
In the ev ent that a write to Timer1 coinc ides with a sp ecial event trigger from CCP1, the write will take precedence.
In this mode of operati on, the CC PR1H:CCPR 1L regis ters pair effectively becomes the period register for
Timer1.
10.5Timer1 16-Bit Read/Write Mode
Timer1 can be configured for 16-bit reads and writes
(see Figure 10-2). When the RD16 control bit
(T1CON<7>) is set, the address for TMR1H is mapped
to a buffer register for the high byte of Timer1. A read
from TMR1L w ill load the cont ents of th e high byte o f
Timer1 into the Timer1 high byte buffer. This provides
the user with the ability to accurately read all 16-bits of
Timer1 without having to determine whether a read of
the high byte followed by a read of the low byte is valid
due to a rollover between reads.
A write to the high byte of Timer1 must also take place
through the TMR1H b uff er register. Timer1 high byte is
updated with the contents of TMR1H when a write
occurs to TMR1L. This allo ws a u ser to write all 16 bits
to both the high and low bytes of Timer1 at once.
The high byte of Ti mer1 is not directly readable or writable in this mode . All rea ds an d writes m ust tak e pl ace
through the Timer1 high byte buffer register. Writes to
TMR1H do not clear the Timer1 prescaler. The prescaler is only cleared on writes to TMR1L.
7/99 Microchip Technology Inc.
PreliminaryDS39026B-page 99
PIC18CXX2
TABLE 10-2:REGISTERS ASSOCIATED WITH TIMER1 AS A TIMER/COUNTER
Value on
POR,
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
INTCONGIE/
GIEH
PIR1
PIE1
IPR1
TMR1LHolding register for the Least Significant Byte of the 16-bit TMR1 registerxxxx xxxx uuuu uuuu
TMR1HHolding register for the Most Significant Byte of the 16-bit TMR1 registerxxxx xxxxuuuu uuuu
T1CON