Note the following details of the code protection feature on Microchip devices:
•Microchip products meet the specification contained in their particular Microchip Data Sheet.
•Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
•There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
•Microchip is willing to work with the customer who is concerned about the integrity of their code.
•Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digit al Millennium Copyright Act. If suc h a c t s
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device
applications and the like is provided only for your convenience
and may be superseded by updates. It is your responsibility to
ensure that your application meets with your specifications.
MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED,
WRITTEN OR ORAL, STATUTORY OR OTHERWISE,
RELATED TO THE INFORMATION, INCLUDING BUT NOT
LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE,
MERCHANTABILITY OR FITNESS FOR PURPOSE.
Microchip disclaims all liability arising from this information and
its use. Use of M icrochip’s prod ucts as critical components in
life support systems is not authorized except with express
written approval by Microchip. No licenses are conveyed,
implicitly or otherwise, under any Microchip intellectual property
rights.
Trademarks
The Microchip name and logo, the Microchip logo, Accuron,
dsPIC, K
EELOQ, microID, MPLAB, PIC, PICmicro, PICSTART,
PRO MATE, PowerSmart, rfPIC, and SmartShunt are
registered trademarks of Microchip Technology Incorporated
in the U.S.A. and other countries.
AmpLab, FilterLab, MXDEV, MXLAB, PICMASTER, SEEVAL,
SmartSensor and The Embedded Control Solutions Company
are registered trademarks of Microchip Technology
Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, dsPICDEM,
dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR,
FanSense, FlexROM, fuzzyLAB, In-Circuit Serial
Programming, ICSP, ICEPIC, Migratable Memory, MPASM,
MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net,
PICLAB, PICtail, PowerCal, PowerInfo, PowerMate,
PowerTool, rfLAB, rfPICDEM, Select Mode, Sm art Serial,
SmartTel and Total Endurance are trademarks of Microchip
Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
Microchip received ISO/TS-16949:2002 quality system certification for
its worldwide headquarters, design and wafer fabrication facilities in
Chandler and Tempe, Arizona and Mountain View, California in
October 2003. The Company’s quality system processes and
procedures are for its PICmicro
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
®
8-bit MCUs, KEELOQ
®
code hopping
DS41159D-page ii 2004 Microchip Technology Inc.
PIC18FXX8
28/40-Pin High-Performance, Enhanced Flash
Microcontrollers with CAN
High-Performance RISC CPU:
• Linear program memory addressing up to
2Mbytes
• Linear data memory addressing to 4 Kbytes
• Up to 10 MIPS operation
• DC – 40 MHz clock input
• 4 MHz-10 MHz oscillator/clock inp ut with
PLL active
• 16-bit wide instructions, 8-bit wide data path
• Priority levels for interrupts
• 8 x 8 Single-Cycle Hardware Multiplier
Peripheral Features:
• High current sink/source 25 mA/25 mA
• Three external interrupt pins
•Timer0 module: 8-bit/16-bit timer/counter with
8-bit programmable prescaler
•Timer1 module: 16-bit timer/counter
•Timer2 module: 8-bit timer/counter with 8-bit
period register (time base for PWM)
•Timer3 module: 16-bit timer/counter
• Secondary osc il lat or c loc k option – Timer1/T i me r3
• Capture/Compare/PWM (CCP) modules;
CCP pins can be configured as:
- Capture input: 16-bit, max resolution 6.25 ns
- Compare: 16-bit, max resolution 100 ns (T
- PWM output: PWM resolution is 1 to 10-bit
Max. PWM freq. @:8-bit resolution = 156 kHz
10-bit resolution = 39 kHz
• Enhanced CCP modu le which has al l the features
of the standard CCP module, but also has the
following features for advanced motor control:
- 1, 2 or 4 PWM outputs
- Selectable PWM polar ity
- Programmable PWM dead time
• Master Synchronous Serial Port (MSSP) with two
modes of operation:
- 3-wire SPI™ (Supports all 4 SPI modes)
2
-I
C™ Master and Slave mode
• Addressable USART module:
- Supports interrupt-on-address bit
CY)
Advanced Analog Features:
• 10-bit, up to 8-channel Analog-to-Digital Converter
module (A/D) with:
17.0 Master Synchronous Serial Port (MSSP) Module .................................................................................................................... 143
19.0 CAN Module.............................................................................................................................................................................199
24.0 Special Features of the CPU.............. ..................... ..................... ............................................................................................ 265
25.0 Instruction Set Summary.......................................................................................................................................................... 281
26.0 Development Support...............................................................................................................................................................323
28.0 DC and AC Characteristics Graphs and Tables.......................................................................................................................361
29.0 Packaging Informa tio n..... ..................... .......................................... ..................... ..................................................................... 377
Appendix A: Data Sheet Revision History.......................................................................................................................................... 385
Index .................................................................................................................................................................................................. 387
Systems Information and Upgrade Hot Line...................................................................................................................................... 397
It is our intention to provide our valued customers with the best documentation possible to ensure successful use of your Microchip
products. To this end, we will continue to improve our publications to better suit your needs. Our publications will be refined and
enhanced as new volumes and updates are introduced.
If you have any questions or c omm ents regarding t his publication, p lease c ontact the M arket ing Co mmunications Department via
E-mail at docerrors@microchip.com or fax the Reader Response Form in the back of this data sheet to (480) 792-4150. We
welcome your feedback.
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).
Errata
An errata sheet, describing minor operational differences from the data sheet and recommended workarounds, may exist for current
devices. As device/documentation issues become known to us, we will publish an errata sheet. The errata will specify the revision
of silicon and revision of document to which it applies.
To determine if an errata sheet exists for a particular device, please check with one of the following:
• Microchip’s Worldwide Web site; http://www.microchip.com
• Your local Microchip sales office (see last page)
When contacting a sales office, please specify which device, revision of silicon and data sheet (include literature number) you are
using.
Customer Notification System
Register on our web site at www.microchip.com to receive the most current information on all of our products.
2004 Microchip Technology Inc.DS41159D-page 5
PIC18FXX8
NOTES:
DS41159D-page 6 2004 Microchip Technology Inc.
PIC18FXX8
1.0DEVICE OVERVIEW
This documen t conta i ns dev ic e spec if i c in for m at i on fo r
the following devices:
• PIC18F248
• PIC18F258
•PIC18F448
•PIC18F458
These devices are available in 28-pin, 40-pin and
44-pin packages. They are differentiated from each
other in four ways:
1.PIC18FX58 devices have twice the Flash
program memory and data RAM of PIC18FX48
devices (32 Kbytes and 1536 bytes vs.
16 Kbytes and 768 bytes, respectively).
2.PIC18F2X8 devices imple me nt 5 A/D channels,
as opposed to 8 for PIC18F4X8 devices.
4.Only PIC18F4X8 devices implement the
Enhanced CCP module, analog comparators
and the Parallel Slave Port.
All other features for devices in the PIC18FXX8 family,
including the serial communications modules, are
identical. These are summarized in Table 1-1.
Block diagrams of the PIC18F2X8 and PIC18F4X8
devices are provided in Figure 1-1 and Figure 1-2,
respectively. The pinouts for these device families are
listed in Table 1-2.
Data Memory (Bytes)76815367681536
Data EEPROM Memory (Bytes)256256256256
Interrupt Sources17172121
I/O PortsPorts A, B, CPorts A, B, CPorts A, B, C, D, E Ports A, B, C, D, E
Timers4444
Capture/Compare/PWM Modules1111
Enhanced Capture/Compare/
PWM Modules
Serial Communic ationsMSSP, CAN,
Parallel Communications (PSP)NoNoYesYes
10-bit Analog-to-Digital Converter5 input channels5 input channels8 input channels8 input channels
Analog ComparatorsNoNo22
Analog Comparators V
Resets (and Delays)POR, BOR,
Programmable Low-Voltage DetectYesYesYesYes
Programmable Brown-out ResetYesYesYesYes
CAN ModuleYesYesYesYes
In-Circuit Serial Programming™
ST = Schmitt Trigger input with CMOS levels Analog = Analog input
I= Input O= Output
P= Power OD= Open-Drain (no P diode to V
11182
I
P
1, 17,
33, 34
9133014
10143115
28, 40
——These pins should be left
IICMOS/ST
O
O
I/O
ST
—
CMOS
—
—
TTL
Master Clear (input) or
programming voltage (output).
Master Clear (Reset) input.
This pin is an active low Reset
to the device.
Programming voltage inpu t.
unconnected.
Oscillator crystal or external clock
input.
Oscillator crystal input or
external clock sou rce inpu t. ST
buffer when configured in RC
mode; otherwise, CMOS.
External clock source input.
Always associated with pin
function OSC1 (see OSC1/
CLKI, OSC2/CLKO pins).
Oscillator crystal or clock output.
Oscillator crystal output.
Connects to crystal or
resonator in Crystal Oscillator
mode.
In RC mode, OSC2 pin output s
CLKO, which has 1/4 the
frequency of OSC1 and
denotes the instruction cycle
rate.
General purpose I/O pin.
ST = Schmitt Trigger input with CMOS levels Analog = Analog input
I= Input O= Output
P= Power OD= Open-Drain (no P diode to V
—102711
I/O
I
I
O
ST
Analog
TTL
Analog
Digital I/O.
Analog input 7.
Chip select control for Parallel
Slave Port (see RD
pins).
Comparator 2 output.
I/O pins.
pins.
and WR
DD)
2004 Microchip Technology Inc.DS41159D-page 15
PIC18FXX8
NOTES:
DS41159D-page 16 2004 Microchip Technology Inc.
PIC18FXX8
2.0OSCILLATOR
CONFIGURATIONS
2.1Oscillator Types
The PIC18FXX8 can be operated in one of eight oscillator modes, programmable by three configuration bits
(FOSC2, FOSC1 and FOSC0).
1.LPLow-Power Crystal
2.XTCrystal/Resonator
3.HSHigh-Speed Crystal/Resonator
4.HS4High-Speed Crystal/Resonator with
PLL enabled
5.RCExternal Resistor/Capacito r
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 HS4 (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 connections . An ext ernal clock source m ay also
be connected to the OSC1 pin, as shown in Figure 2-3
and Figure 2-4.
The PIC18FXX8 oscilla tor d esign requires the use of a
parallel cut crystal.
Note:Use of a series cut crystal may give a fre-
quency out of the crystal manufacturer’s
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 recommended
values of C1 and C2.
2: A series resistor (R
strip cut crystals.
3: R
OSC1
XTAL
(2)
RS
OSC2
F varies with the crystal chosen.
(3)
RF
PIC18FXX8
S) may be required for AT
To
Internal
Logic
Sleep
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 following Table 2-2.
Resonators Used:
455 kHzPanasonic EFO-A455K04B±0.3%
2.0 MHzMurata Erie CSA2.00MG±0.5%
4.0 MHzMurata Erie CSA4.00MG±0.5%
8.0 MHzMurata Erie CSA8.00MT±0.5%
16.0 MHzMurata Erie CSA16.00MX±0.5%
All resonators used did not have built-in capacitors.
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
2004 Microchip Technology Inc.DS41159D-page 17
PIC18FXX8
TABLE 2-2:CAPACITOR SELECTION FOR
CRYSTAL OSCILLATOR
Osc Type
Crystal
Freq
LP32.0 kHz33 pF33 pF
200 kHz15 pF15 pF
XT200 kHz47-68 pF47-68 pF
1.0 MHz15 pF15 pF
4.0 MHz15 pF15 pF
HS4.0 MHz15 pF15 pF
8.0 MHz15-33 pF15-33 pF
20.0 MHz15-33 pF15-33 pF
25.0 MHz15-33 pF15-33 pF
These values are for design guidance only.
See notes on this page.
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: Rs may be required in HS mode, as well
as XT mode, to avoid overdrivi ng crys t als
with low drive level specification.
Cap. Range C1Cap. Range
Crystals Used
C2
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
operating temperature. In addition to this, the oscillator
frequency will vary from unit to unit due to normal
process parameter variation. Furthermore, the differ-
ence in lead frame capacit ance between package types
will also affect the oscillation frequency, especially for
EXT values. The user also needs to take into
low C
account variation due to tolerance of external R and C
components used. Figure 2-2 shows how the RC
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 f or t e st pu r pos es or t o sy nc hr o n iz e ot he r
logic.
Note:If the oscillator frequency divided by 4
FIGURE 2-2:RC OSCILLATOR MODE
REXT
CEXT
VSS
Recommended values: 3 kΩ ≤ REXT ≤ 100 kΩ
The RCIO Oscillato r mode f unc tions like t he RC m ode,
except that the OSC2 pin becomes an additional
general purpose I/O pin. The I/O pin becomes bit 6 of
PORTA (RA6).
EXT) and capacitor (CEXT) values and the
signal is not required in the application, it
is recommended to use RCIO mode to
save current.
VDD
PIC18FXX8
OSC1
F
OSC/4
OSC2/CLKO
C
EXT > 20 pF
Internal
Clock
DS41159D-page 18 2004 Microchip Technology Inc.
PIC18FXX8
2.4External Clock Input
The EC and ECIO Oscillator mode s require an externa l
clock source to be connected to the OSC1 pin. The
feedback device between OSC1 and OSC2 is turned
off in these modes to save current. There is no oscillator start-up 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 f or t e st pu r pos es or t o sy nc hr o n iz e ot he r
logic. Figure 2-3 shows the pin connections for the EC
Oscillator mode.
FIGURE 2-3:EXTERNAL CLOCK INPUT
OPERATION (EC OSC
CONFIGURATION)
PIC18FXX8
Clock from
Ext. System
OSC/4
F
The ECIO Oscillator mode func ti ons li ke t he EC m od e,
except that the OSC2 pin becomes an additional
general purpose I/O pin. Figure 2-4 shows the pin
connections for the ECIO Osci ll ator mode.
OSC1
OSC2
FIGURE 2-4:EXTERNAL CLOCK INPUT
OPERATION (ECIO
CONFIGURATION)
PIC18FXX8
Clock from
Ext. System
OSC1
I/O (OSC2)
2.5HS4 (PLL)
A Phase Locked Loop circuit is pro vided as a programmable option for users that want to multiply the
frequency of the incoming cry sta l oscil lator sig nal 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 bi ts are pro grammed for HS mod e. If the y
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 FOSC2:FOSC0
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 referred to as T
PLL.
FIGURE 2-5:PLL BLOCK DIAGRAM
OSC2
Crystal
Osc
OSC1
Phase
Comparator
F
IN
FOUT
FOSC2:FOSC0 = 110
Loop
Filter
Divide by 4
VCO
SYSCLK
MUX
2004 Microchip Technology Inc.DS41159D-page 19
PIC18FXX8
2.6Oscillator Switching Feature
The PIC18FXX8 devices include a featu re that allows
the system clock source to be switc hed from the mai n
oscillator to an alternate low-frequency clock source.
For the PIC18FXX8 devices, this alternate clock source
is the Timer1 oscillator. If a low-frequency crystal
(32 kHz, for example) has been attached to the Timer1
oscillator pins and the Timer1 oscillator has been
enabled, the device can switch to a Low-Power Execution mode. Figure 2-6 shows a block diagram of the
system clock sources. The clock switching feature is
enabled by programming the Oscillator Switching
Enable (OSCSEN
CONFIG1H, to a ‘0’. Clock switching is disabled in an
erased device. See Section 1 2.2 “Timer 1 Oscillat or”
for further details of the Timer1 oscillator and
Section 24.1 “Configuration Bits” for Configuration
register details.
FIGURE 2-6:DEVICE CLOCK SOURCES
) bit in Configuration register,
PIC18FXX8
OSC2
OSC1
T1OSO
T1OSI
Main Oscillator
Sleep
Timer 1 Oscillator
T1OSCEN
Enable
Oscillator
2.6.1SYSTEM CLOCK SWITCH BIT
The system clock source switching is performed under
software control. The system clock switch bit, SCS
(OSCCON register), controls the clock switching. When
the SCS bit is ‘0’, the system clock source comes from
the main oscillator selected by the FOSC2:FOSC0
configuration bits. When the SCS bit is set, the system
clock source comes from the Timer1 oscilla tor . The SCS
bit is cleared on all forms of Reset.
Note:The Timer1 oscillator must be enabled to
switch the system clock source. The
Timer1 oscillator is enabled by setting the
T1OSCEN bit in the Timer1 Control register (T1CON). If the Timer1 oscillator is not
enabled, any write to the SCS bit will be
ignored (SCS bit forced cleared) and the
main oscillator con t in ues to be t he s ys tem
clock source.
4 x PLL
TOSC
TT1P
Clock Source Option
for Other Modules
TOSC/4
MUX
Clock
Source
TSCLK
Note:I/O pins have diode protection to VDD and VSS.
REGISTER 2-1:OSCCON: OSCILLATOR CONTROL REGISTER
U-0U-0U-0U-0U-0U-0U-0R/W-1
———————SCS
bit 7bit 0
bit 7-1Unimplemented: Read as ‘0’
bit 0SCS: System Clock Switch bit
When
OSCSEN configuration bit = 0 and T1OSCEN bit is set:
1 = Switch to Timer1 oscillator/clock pin
0 = Use primary oscillator/clock input pin
OSCSEN is clear or T1OSCEN is clear:
When
Bit is force d clear.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
DS41159D-page 20 2004 Microchip Technology Inc.
PIC18FXX8
2.6.2OSCILLATOR TRANSITIONS
The PIC18FXX8 devices contain circuitry to prevent
“glitches” when switching between oscillator sources.
Essentially, the circuitry waits for eight rising edges of
the clock source that the processor is swit ching to. This
ensures that the n ew c lo ck s ourc e is s t able and that its
pulse width will not be less than the shortest pulse
width of the two clock sources.
Figure 2-7 shows a timing diagram indicating the transition from the main oscillator to the Timer1 oscillator.
The Timer1 oscillator is assumed to be running all the
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), the transition will take place after
an oscillator st art-up 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-8.
time. After the SCS bit is set, th e proce ssor is frozen at
the next occurring Q1 cycle . After eight synchroniz ation
cycles are counted from the Timer1 oscillator,
operation resumes. No additional delays are required
after the synchronization cy cle s.
FIGURE 2-7:TIMING DIAGRAM FOR TRANSITION FROM OSC1 TO TIMER1 OSCILLATOR
Q1
T1OSI
OSC1
Internal
System
Clock
SCS
(OSCCON<0>)
Program
Counter
TOSC
Q4
Q3Q2
Q1
TDLY
TT1P
21345678
Tscs
PC + 2PC
Q3Q2Q1
Q4Q1
Q2Q3Q4 Q1
PC + 4
Note 1: Delay on internal system clock is eight oscillator cycles for synchronization.
FIGURE 2-8:TIMING DIAGRAM FOR TRANSITION BETWEEN TIMER1 AND OSC1 (HS, XT , LP)
T1OSI
OSC1
OSC2
Internal System
Clock
(OSCCON<0>)
Note 1: T
SCS
Program
Counter
OST = 1024 TOSC (drawing not to scale).
Q3Q4
PCPC + 2
Q1
TOST
TOSC
TT1P
12345678
TSCS
Q1 Q2 Q3 Q4 Q1 Q2
Q3
PC + 4
2004 Microchip Technology Inc.DS41159D-page 21
PIC18FXX8
If the main oscillator is configured for HS4 (PLL) mode,
an oscillator start-up time (T
time-out (T
PLL) will occur. The PLL time-out is typically
OST) plus an additional PLL
2 ms and allows the PLL to lock to the main oscillator
frequency. A timing diagram indicating the transition
from the Timer 1 oscilla tor to the mai n oscillator f or HS4
mode is shown in Figure 2-9.
If the main oscillato r is co nfigure d in th e RC, R CIO, EC
or ECIO modes, the re is no oscillator start-u p t im e-ou t.
Operation will resume after eight cycles of the main
oscillator have been counted. A ti ming diag ram indicating the transition from the Timer1 oscillator to the main
oscillator for RC, RC IO, EC and EC IO mo des is sho wn
in Figure 2-10.
FIGURE 2-9:TIMING FOR TRANSITION BETWEEN TIMER1 AND OSC1 (HS WITH PLL)
Q3
PC + 4
T1OSI
OSC1
OSC2
PLL Clock
Input
Internal System
Note 1: T
Clock
(OSCCON<0>)
SCS
Program
Counter
OST = 1024 TOSC (drawing not to scale).
Q4Q1
TOST
PCPC + 2
TPLL
TOSC
TT1P
TSCS
123456
Q1 Q2 Q3 Q4 Q1 Q2
8
7
Q4
FIGURE 2-10:TIMING FOR TRANSITION BETWEEN TIMER1 AND OSC1 (RC, EC)
Q3Q4
T1OSI
OSC1
OSC2
Internal System
Note 1: RC Oscillator mode assumed.
Clock
(OSCCON<0>)
SCS
Program
Counter
Q1
PCPC + 2
TT1P
TOSC
12345 678
TSCS
Q1 Q2 Q3 Q4 Q1 Q2 Q3
Q4
PC + 4
DS41159D-page 22 2004 Microchip Technology Inc.
PIC18FXX8
2.7Effects of Sleep Mode on the
On-Chip Oscillator
When the device executes a SLEEP instruction, the
on-chip clocks and oscillator are turned off and the
device is held at the beginning of an instruction cycle
(Q1 state). With the os ci lla tor o f f, the OSC1 and OSC2
signals will stop oscillating. Since all the transistor
switching currents have been removed, Sleep mode
achieves 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.
2.8Power-up Delays
Power-up delays are con trolled by two time rs so that no
external Reset circuitry is required for most applications. 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 Section 3.0 “Reset”.
The first timer is the Power-up Timer (PWRT), which
optionally provides a fixed delay of T
#D033) on power-up only (POR and BOR). The second
timer is the Oscillator S t art-up T imer (OST), inten ded to
keep the chip in Reset until the crystal oscillator is
stable.
With the PLL enabled ( HS4 Osc ill ato r mo de), the timeout sequence following a Power-on Reset is different
from other oscillator modes. The time-out sequence is
as follows: the PWRT time-out is invoked after a POR
time delay has expired, then the Oscillator Start-up
Timer (OST) is invoked. However, this is still not a
sufficient a mount of ti me to allow the PLL to l ock at hig h
frequencies. The PWRT timer is used to provide an
additional fixed 2ms (nominal) to allow the PLL ample
time to lock to the incoming clock frequency.
TABLE 2-3:OSC1 AND OSC2 PIN STATES IN SLEEP MODE
OSC ModeOSC1 PinOSC2 Pin
RCFloating, external resistor should pull high At logic low
RCIOFloating, external resistor should pull high Configured as PORTA, bit 6
ECIOFloatingConfigured as PORTA, bit 6
ECFloatingAt logic low
LP, XT and HSFeedback inverter disabled at quiescent
voltage level
Note:See Table 3-1 in Section 3.0 “Reset” for time-outs due to Sleep and MCLR
Feedback inverter disabled at quiescent
voltage level
Reset.
PWRT (parameter
2004 Microchip Technology Inc.DS41159D-page 23
PIC18FXX8
NOTES:
DS41159D-page 24 2004 Microchip Technology Inc.
PIC18FXX8
3.0RESET
The PIC18FXX8 differentiates between various kinds
of RESET:
a) Power-on Reset (POR)
b) MCLR
c)MCLR Rese t during Sleep
d) Watchdog Timer (WDT) Reset during normal
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”
Reset during normal operation
operation
state on Power-on Reset, MCLR
out Reset, MCLR
Reset during Sleep and by the
RESET instruction.
Most registers are not affected by a WDT wake-up,
since this is viewed as the resumption of normal operation. Status bits from the RCON register, RI
POR
and BOR are set or cleared differently in different
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 description of the Reset
states of all registers.
A simplified block di agram of the On-Chip Reset Circu it
is shown in Figure 3-1.
The Enhanced MCU devices have a MCLR
in the MCLR
Reset path. The filter will detect and
ignore small pulses.
A WDT Reset does not drive MCLR
, WDT Reset, Brown-
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
pin low.
MCLR
WDT
Module
V
DD Rise
Detect
VDD
OSC1
Note 1: This is a separate oscillator from the RC oscillator of the CLKI pin.
Brown-out
Reset
OST/PWRT
OST
On-chip
RC OSC
2: See Table 3-1 for time-out situations.
PWRT
(1)
Sleep
WDT
Time-out
Reset
Power-on Reset
BOREN
10-bit Ripple Counter
10-bit Ripple Counter
S
R
Enable PWRT
Enable OST
Chip_Reset
Q
(2)
2004 Microchip Technology Inc.DS41159D-page 25
PIC18FXX8
.
t
l
3.1Power-on Reset (POR)
A Power-on Reset pulse is generated on-chip when a
DD rise is detected. To take advantage of the POR
V
circuitry, connect the MCLR
resistor) to V
DD. This eliminates external RC compo-
pin directly (or through a
nents usually needed to create a Power-on Reset
delay. A minimum rise rate for V
DD is specified (refer to
parameter D004). For a slow rise time, see Figure 3-2.
When the device starts normal operation (exits the
Reset condition), device operating parameters
(voltage, frequency, temperature, etc.) must be met 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.
3.2MCLR
PIC18FXX8 devices have a noise filter in the MCLR
Reset path. The filter will detect and ignore small
pulses.
It should be noted that a WDT Reset does not drive
pin low.
MCLR
The behavior of the ESD protection on the MCLR
differs from previous devices of this family. Voltages
applied to the pin that exceed its specification can
result in both Resets and current draws outside of
device specification during the Reset event. For this
reason, Microchip recommends that the MCLR
longer be tied directly to V
DD. The use of an RC
network, as shown in Figure 3-2, is suggested.
pin
pin no
3.3Power-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 kept in R eset as long a s the PWRT is active.
The PWRT’s ti me delay allows V
able level. A configuration bit (PWRTEN
DD to rise to an accept-
in CONFIG2L
register) is provided to enable/disable the PWRT.
The power-up time dela y will vary f rom chip to chip due
DD, temperature and process variation. See DC
to V
parameter #33 for details.
3.4Oscillator Start-up Timer (OST)
The Oscillator Start-up Timer (OST) provides a 1024
oscillator cycle (from OSC1 input) delay after the
PWRT delay is over (parameter #32). This additional
delay ensures that the crystal oscillator or resonator
has started and stabilized.
The OST time-out is invoked only for XT, LP, HS and
HS4 modes and only on Power-on Reset or wake-up
from Sleep.
3.5PLL Lock Time-out
With the PLL enabled, the time-ou t sequen ce foll owin g
a Power-on Reset is different from other oscillator
modes. A portion of the Po wer-up Timer is used to provide a fixed time-out th at is suff icient for the PLL to lock
to the main oscillator fre quenc y. This PLL lock time-out
PLL) is typically 2 ms and follows the oscillator
(T
start-up time-out (OST).
FIGURE 3-2:EXTERNAL POWER-ON
RESET CIRCUIT (FOR
SLOW V
V
DD
D
R
C
Note 1: External Power-on Reset circuit is required
only if the V
The diode D helps discharge the capacitor
quickly when V
2: R < 40 kΩ is recommended to make sure tha
the voltage drop across R does not violate
the device’s electrical specification.
3: R1 = 100Ω to 1 kΩ will limit any current flow-
ing into MCLR
the event of MCLR/
Electrostatic Discharge (ESD) or Electrica
Overstress (EOS).
DD power-up slope is too slow
DD POWER-UP)
R1
MCLR
PIC18FXXX
DD powers down.
from external capacitor C, in
VPP pin breakdown due to
3.6Brown-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 resets the
chip. A Reset may not occur if VDD falls below parameter D005 for less than parameter #35. The chip will
remain in Brown-out Reset unt il V
The Power-up T im er wil l th en be invoked and will keep
the chip in Reset an additional time delay (parameter
#33). If V
DD drops below BVDD while the Power-up
Timer is ru nni ng, the chip will go back in to a Bro wn-out
Reset and the Power-up Timer will be initialized. Once
DD rises above BVDD, the Power-up Timer will
V
execute the additional time delay.
DD rises above BVDD.
DS41159D-page 26 2004 Microchip Technology Inc.
PIC18FXX8
3.7Time-out Sequence
On power-up, the time-out sequence is as follows:
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 oscillator configuration and
the status o f th e PW RT . Fo r e xa mple, 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 occur from the POR pulse, 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 PIC18FXX8 device
operating in parallel.
Table 3-2 shows the Reset condi tions f or some Spec ial
Function Registers, while Table 3-3 shows the Reset
conditions for all registers.
high will begin execution immediately
TABLE 3-1:TIME-OUT IN VARIOUS SITUATIONS
Oscillator
Configuration
HS with PLL enabled
HS, XT, LP72 ms + 1024 TOSC1024 TOSC72 ms + 1024 TOSC1024 TOSC
EC72 ms—72 ms—
External RC72 ms—72 ms—
Note 1:2 ms = Nominal time required for the 4x PLL to lock.
2: 72 ms is the nominal Power-up Timer delay.
(1)
PWRTEN
72 ms + 1024 TOSC + 2 ms 1024 TOSC + 2 ms 72 ms + 10 24 TOSC + 2 m s 1024 TOSC + 2 ms
Power-up
= 0PWRTEN = 1
(2)
Brown-out
(2)
REGISTER 3-1:RCON REGISTER BITS AND POSITIONS
R/W-0U-0U-0R/W-1R/W-1R/W-1R/W-0R/W-1
IPEN
bit 7bit 0
——RITOPDPORBOR
Wake-up from
Sleep or
Oscillator Switch
TABLE 3-2:STATUS BITS, THEIR SIGNIFICANCE AND THE INITIALIZATION CONDITION FOR
RCON REGISTER
Condition
Power-on Reset0000h0--1 110q11100uu
MCLR Reset during normal
Legend: u = unchanged, x = unknown,- = unimplemented bit, read as ‘0’
Note 1: When the wake-up is due to an interrupt and the GIEH or GIEL bits are set, the PC is loaded with the
interrupt vector (000008h or 0000 18h ).
Program
Counter
0000h0--0 011quuuuuuu
0000h0--0 011q0uuuuuu
0000h0--0 011quuu11u1
0000h0--0 011quuu111u
(1)
RCON
Register
0--1 101qu10uuuu
TO PDPORBORSTKFULSTKUNF
RI
2004 Microchip Technology Inc.DS41159D-page 27
PIC18FXX8
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
PWRT TI ME-OUT
OST TIME-OUT
INTERNAL RESET
TPWRT
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
DS41159D-page 28 2004 Microchip Technology Inc.
NOT TIED TO VDD): CASE 2
TOST
FIGURE 3-6:SLOW RISE TIME (MCLR TIED TO VDD)
5V
VDD
MCLR
INTERNAL POR
0V
PWRT
T
1V
PIC18FXX8
PWRT TIME-OUT
OST TIME-OUT
INTERNAL RES ET
TOST
FIGURE 3-7:TIME-OUT SEQUENCE ON POR W/PLL ENABLED (MCLR TIED TO VDD)
VDD
MCLR
IINTERNAL POR
TPWRT
PWRT TIME-OUT
OST TIME-OUT
PLL TIME-OUT
TOST
TPLL
INTERNAL RESET
Note:TOST = 1024 clock cycles.
T
PLL≈ 2 ms max. First three stages of the PWRT timer.
2004 Microchip Technology Inc.DS41159D-page 29
PIC18FXX8
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS
Shaded cells indicate condi tions do not apply for the designated device.
Note 1: 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 G IEL o r GIEH b it is set, the TOSU, TOSH 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: Values for CANSTAT also apply to its other instances (CANSTATRO1 through CANSTATRO4).
WDT Reset
RESET Instruction
Stack Resets
Wake-up via WDT
or Interrupt
(3)
(3)
(3)
(3)
(2)
(1)
(1)
(1)
DS41159D-page 30 2004 Microchip Technology Inc.
PIC18FXX8
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS (CONTINUED)
Shaded cells indicate condi tions do not apply for the designated device.
Note 1: 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 G IEL o r GIEH b it is set, the TOSU, TOSH 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: Values for CANSTAT also apply to its other instances (CANSTATRO1 through CANSTATRO4).
Shaded cells indicate condi tions do not apply for the designated device.
Note 1: 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 G IEL o r GIEH b it is set, the TOSU, TOSH 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: Values for CANSTAT also apply to its other instances (CANSTATRO1 through CANSTATRO4).
WDT Reset
RESET Instruction
Stack Resets
-111 1111
-uuu uuuu
-u0u 0000
(5)
(5)
(5)
Wake-up via WDT
or Interrupt
-uuu uuuu
-uuu uuuu
-uuu uuuu
(5)
(5)
(5)
2004 Microchip Technology Inc.DS41159D-page 33
PIC18FXX8
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS (CONTINUED)
Shaded cells indicate condi tions do not apply for the designated device.
Note 1: 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 G IEL o r GIEH b it is set, the TOSU, TOSH 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: Values for CANSTAT also apply to its other instances (CANSTATRO1 through CANSTATRO4).
Power-on Reset,
Brown-out Reset
WDT Reset
RESET Instruction
Stack Resets
Wake-up via WDT
or Interrupt
DS41159D-page 34 2004 Microchip Technology Inc.
PIC18FXX8
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS (CONTINUED)
Shaded cells indicate condi tions do not apply for the designated device.
Note 1: 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 G IEL o r GIEH b it is set, the TOSU, TOSH 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: Values for CANSTAT also apply to its other instances (CANSTATRO1 through CANSTATRO4).
Power-on Reset,
Brown-out Reset
WDT Reset
RESET Instruction
Stack Resets
Wake-up via WDT
or Interrupt
2004 Microchip Technology Inc.DS41159D-page 35
PIC18FXX8
TABLE 3-3:INITIALIZATION CONDITIONS FOR ALL REGISTERS (CONTINUED)
Shaded cells indicate condi tions do not apply for the designated device.
Note 1: 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 G IEL o r GIEH b it is set, the TOSU, TOSH 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: Values for CANSTAT also apply to its other instances (CANSTATRO1 through CANSTATRO4).
Power-on Reset,
Brown-out Reset
WDT Reset
RESET Instruction
Stack Resets
Wake-up via WDT
or Interrupt
DS41159D-page 36 2004 Microchip Technology Inc.
PIC18FXX8
4.0MEMORY ORGANIZATION
There are three memory blocks in Enhanced MCU
devices. These memory blocks are:
• Enhanced Flash Program Memory
• Data Memory
• EEPROM Data Memory
Data and program memory use separate busses,
which allows concurrent access of these blocks.
Additional detailed information on data EEPROM and
Flash program memory is provided in Section 5.0
“Data EEPROM Memory” and Section 6.0 “Flash
Program Memory”, respectively.
4.1Program Memory Organization
The PIC18F258/458 devices have a 21-bit program
counter that is capable of addressing a 2-Mbyte
program memory space.
The Reset vector add res s is at 00 00h and the interrupt
vector addresses are at 0008h and 0018h.
FIGURE 4-1:PROGRAM MEMORY MAP
AND STACK FOR
PIC18F248/448
PC<20:0>
CALL,RCALL,RETURN
RETFIE,RETLW
Stack Level 1
Stack Level 31
Reset Vector
High Priority Interrupt Vector
Low Priority Interrupt Vector
21
•
•
•
0000h
0008h
0018h
Figure 4-1 shows the diagram for program memory
map and stack for the PIC18F248 and PIC18F448.
Figure 4-2 shows the diag ram for the pr ogram mem ory
map and stack for the PIC18F258 and PIC18F458.
4.1.1INTERNAL PROGRAM MEMORY
OPERATION
The PIC18F258 and the PIC18F458 have 32 Kbytes of
internal Enhanced Flash program memory. This means
that the PIC18F258 and the PIC18F4 58 can store up to
16K of single-word instructions. The PIC18F248 and
PIC18F448 have 16Kbytes of Enhanced Flash
program memory . This translates into 8192 sing le-word
instructions, which can be stored in the program
memory. Accessing a location between the physically
implemented memory and the 2-Mbyte address will
cause a read of all ‘0’s (a NOP instruction).
FIGURE 4-2:PROGRAM MEMORY MAP
AND STACK FOR
PIC18F258/458
PC<20:0>
CALL,RCALL,RETURN
RETFIE,RETLW
Stack Level 1
Stack Level 31
Reset Vector
High Priority Interrupt Vector
Low Priority Interrupt Vector
21
•
•
•
0000h
0008h
0018h
On-Chip
Program Memory
3FFFh
4000h
Read ‘0’
1FFFFFh
200000h
2004 Microchip Technology Inc.DS41159D-page 37
User Memory Space
On-Chip
Program Memory
Read ‘0’
7FFFh
8000h
1FFFFFh
200000h
User Memory Space
PIC18FXX8
4.2Return Address Stack
The return address s tack allows any combinatio n of up
to 31 program calls and interrupts to occur. The PC
(Program Counter) is pushed onto the stack when a
PUSH, CALL or RCALL instruction is ex ecuted, or an
interrupt is Acknowledged. The PC value is pulled off
the stack on a RETURN, RETLW or a RETFIE instruc-
tion. PCLATU and PCLATH are not affected by any of
the RETURN instructions.
The stack operates as a 31-word by 21-bit stack
memory and a 5-bit Stack Pointer register, 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 indicated b y the STKPTR are transferred to the
PC and then the Stac k Poin ter is dec rem en ted .
The stack space is not part of either program or data
space. The Stack Pointer is readable and writable and
the data on the top of the st ack is readable and writable
through SFR registers. Status bits indicate if the stack
pointer is at or beyond the 31 levels provided.
4.2.1TOP-OF-STACK ACCESS
The top of the stack is readable and writable. Three
register locations, TOSU, TOSH and TOSL allow
access to the contents of the stack l ocation indic ated by
the STKPTR regist er. This allows users to implemen t a
software stac k, 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 can be placed on a us er defined s oftware st ack.
At return time, the software can replace the TOSU,
TOSH and TOSL and do a return.
The user should d isabl e t he glo bal i nterrupt enabl e bit s
during this time to prevent inadvertent stack
operations.
4.2.2RETURN STACK POINTER
(STKPTR)
The STKPTR register contai ns the S t ack Poi nter valu e,
the STKFUL (Stack Full) status bit and the STKUNF
(Stack Underflow) status bits. Register 4-1 shows the
STKPTR register . T he value of the St ack Pointer ca n be
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 value will be ‘0’. The user may read and write
the Stack Pointer value. This feature can be used by a
Real-Time Operating System for return stack
maintenance.
After the PC is pus hed on to the st ack 31 tim es (wi thout
popping any values off the stack), the STKFUL bit is
set. The STKFUL bit can on ly be cleared in so ftware or
by a POR.
The action that takes place when the stack becomes
full depends on the state of the STVREN (Stack Overflow Reset Enable) configuration bit. Refer to
Section 21.0 “Comparator Module” for a description
of the device configuration bits. If STVREN is set
(default), the 31st push will push the (PC + 2) value
onto the stack, set the STKFUL bit 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 overwrite th e 31st push (and so on),
while STKPTR remains at 31.
When the stack has been popped enough times to
unload the stac k, the next pop will ret urn a value of zero
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:Returning a value of zero to the PC on an
underflow has the effect of vectoring the
program to the Reset vector, where the
stack conditions can be verified and
appropriate actions can be taken.
DS41159D-page 38 2004 Microchip Technology Inc.
REGISTER 4-1:STKPTR: STACK POINTER REGISTER
R/C-0R/C-0U-0R/W-0R/W-0R/W-0R/W-0R/W-0
STKFULSTKUNF—SP4SP3SP2SP1SP0
bit 7bit 0
bit 7STKFUL: Stack Full Flag bit
1 = Stack became full or overflowed
0 = Stack has not become full or overflowed
bit 6STKUNF: Stack Underflow Flag bit
1 = Stack underflow occurred
0 = Stack underflow did not occur
bit 5Unimplemented: Read as ‘0’
bit 4-0 SP4:SP0: Stack Pointer Locati on bits
Note:Bit 7 and bit 6 need to be cleared following a stack underflow or a stack overflow.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedC = Clearable bit
PIC18FXX8
FIGURE 4-3:RETURN ADDRESS STACK AND ASSOCIATED REGISTERS
Return Address Stack
11111
11110
TOSLTOSHTOSU
34h1Ah00h
Top-of-Stack
Note 1: No RAM associated with this address; always maintained ‘0’s.
001A34h
000D58h
000000h
11101
00011
00010
00001
00000
STKPTR<4:0>
00010
(1)
2004 Microchip Technology Inc.DS41159D-page 39
PIC18FXX8
4.2.3PUSH AND POP INSTRUCTIONS
Since the Top-of-Stack (TOS) is readable and writ abl e,
the ability to push valu es onto the stack and pull va lues
off the sta ck, withou t disturbi ng normal program ex ecution, is a desirable optio n. To push the current PC valu e
onto the stack, a PUSH instruction can be executed.
This will increment the Stack Pointer and load the
current PC value onto the stack. TOSU, TOSH and
TOSL can then be modified to place a return address
on the stack.
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 underf low condition will set the appropriate STKFUL or STKUNF bit, but not cause a device
Reset. When the STVREN bit is enabled, a full or
underflow condition wi ll set the appro priate STK FUL or
STKUNF bit and then cause a device Reset. The
STKFUL or STKUNF bits are only cleared by the user
software or a POR.
4.3Fast Register Stack
A “fast return” option is available for interrupts and
calls. A fast register stack is provided for the Status,
WREG and BSR registers and is only one layer in
depth. The stack is not readable or writable and is
loaded with the current value of the corresponding
register when the processor vectors for an interrupt.
The values in the fast register stack 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 values stored by the low priority
interrupt will be overwritten.
If high priority interrupts are not disabled during low
priority inte rr up ts, us e rs mu st save th e key r eg ist er s in
software during a low priority interrupt.
If no interrupts are used, the fast register stack can be
used to restore the S tatus, WR EG and BSR registers at
the end of a subroutine call. To use the fast register
stack for a subroutine call, a FAST CALL instruction
must be executed.
Example 4-1 shows a source code example that uses
the fast register stack.
EXAMPLE 4-1:FAST REGISTER STACK
CODE EXAMPLE
CALL SUB1, FAST;STATUS, WREG, BSR
;SAVED IN FAST REGISTER
;STACK
•
•
SUB1•
•
•
RETURN FAST;RESTORE VALUES SAVED
;IN FAST REGISTER STACK
4.4PCL, PCLATH and PCLATU
The Program Counter (PC) s pecifies the ad dress 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 th e PC<20 :16> bit s an d is not d irectly
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 address sequential
instructions in the program memo ry.
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 a n operatio n 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 useful
for computed offsets to the PC (see Section 4.8.1“Computed GOTO”).
DS41159D-page 40 2004 Microchip Technology Inc.
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 Cou nter (PC) is increme nted 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
are shown in Figure4-4.
FIGURE 4-4:CLOCK/INSTRUCTION CYCLE
PIC18FXX8
Q2Q3Q4
OSC1
Q1
Q2
Q3
Q4
PC
OSC2/CLKO
(RC Mode)
Q1
PC
Fetch INST (PC)
Execute INST (PC – 2)Fetch INST (PC + 2)
Q1
4.6Instruction Flow/Pipelining
An “Instruction Cycle” consists of four Q cycles (Q1,
Q2, Q3 and Q4). The instruc ti on fe tch and execute are
pipelined such that fetch takes one instruction cycle,
while decode and execute take 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),
two cycles are required to complete the instruction
(Example 4-2).
A fetch cycle begins with the Program Counter (PC)
incrementing in Q1.
In the execution cy cle, the fetc hed instructi on is latche d
into the “Instruction Register” (IR) in cycle Q1. This
instruction is then decoded and executed during the
Q2, Q3 and Q4 cycle s. Dat a memory is read during Q2
(operand read) and written during Q4 (destination
write).
Q2Q3Q4
PC + 2PC + 4
Execute INST (PC)Fetch INST (PC + 4)
Q2Q3Q4
Q1
Execute INST (PC + 2)
4.7Instructions in Program Memory
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-3 shows an
example of how instruction words are stored in the
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 “PCL,PCLATH and PCLATU”).
The CALL and GOTO instructions have an absolute
program memory address embedded into the instruction. Since instructions are always stored on word
boundaries, the data contained in the instruction is a
word address. The word address is written to PC<20:1>,
which accesses the desired byte address in program
memory. Instruction #2 in Example 4-3 shows how the
instruction “GOTO 000006h” is encoded in the program
memory. Program branch instructions that encode a
relative address offset operate in the same manner . The
offset value stored in a br anch instruction represent s the
number of single-word instructions by which the PC will
be offset. Section 25.0 “Instruction Set Summary”
provides further details of the instruction set.
Note:All instructions are single cycle, except for any program branches. These take two cycles, since the fetch instruction is
“flushed” from the pipeli ne while the new instruction is being fetch ed and then executed.
EXAMPLE 4-3:INST RUCTIONS IN PROGRAM MEMORY
InstructionOpcodeMemoryAddress
—000007h
MOVLW 055h0E55h55h000008h
0Eh000009h
GOTO 000006h0EF03h, 0F000h03h00000Ah
0EFh00000Bh
00h00000Ch
0F0h00000Dh
MOVFF 123h, 456h0C123h, 0F456h23 h00000Eh
0C1h00000Fh
56h000010h
0F4h000011h
—000012h
DS41159D-page 42 2004 Microchip Technology Inc.
PIC18FXX8
4.7.1TWO-WORD INSTRUCTIONS
The PIC18FXX8 devices have 4 two-word instructions:
MOVFF, CALL, GOTO and LFSR. The 4 Most Significant bits of the second word are set to ‘1’s and indicate
a special NOP instruction. The lower 12 bits of the
second word contain the data to be used by the
instruction. 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
necessary when the two-word instruction is preceded by
a conditional instruction that changes the PC. A program
example that demonstrates this concept is shown in
Example 4-4. Refer to Section 25.0 “Instruction Set
Summary” for further details of the instruction set.
4.8Look-up Tables
Look-up tables are implemented two ways. These are:
• Computed GOTO
• Table Reads
4.8.1COMPUTED GOTO
A computed GOTO is accomplish ed by adding an offset
to the program counter (ADDWF PCL).
A look-up 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 routi ne 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 v alue (val ue in WR EG) speci fies the number
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.
Note 1: The LSb of PCL is fixed to a value of ‘0’.
Hence, computed GOTO to an odd addre ss
is not possible.
2: The ADDWF PCL instruction does not
update PCLATH/PCLATU. A read op e r ation on PCL must be performed to update
PCLATH and PCLATU.
4.8.2TABLE READS/TABLE WRITES
A better method of storing data in program memory
allows 2 bytes of data to be stored in each instruction
location.
Look-up table data may be stored as 2 bytes per
program word by using table reads and writes. The
T abl e Pointer (TBLP TR) specifi es the byte add ress and
the T a ble Latch (TABLA T) contains the dat a 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 6.1 “Table Reads and Table Writes”.
EXAMPLE 4-4:TWO-WORD INSTRUCTIONS
CASE 1:
Object CodeSource Code
0110 0110 0000 0000TSTFSZREG1; is RAM location 0?
1100 0001 0010 0011MOVFFREG1, REG2 ; No, execute 2-word instruction
1111 0100 0101 0110; 2nd operand holds address of REG2
0010 0100 0000 0000ADDWFREG3; continue code
CASE 2:
Object CodeSource Code
0110 0110 0000 0000TSTFSZREG1; is RAM location 0?
1100 0001 0010 0011MOVFFREG1, REG2 ; Yes
1111 0100 0101 0110; 2nd operand becomes NOP
0010 0100 0000 0000ADDWFREG3; continue code
2004 Microchip Technology Inc.DS41159D-page 43
PIC18FXX8
4.9Data Memory Organization
The data memory i s impl emented as static RAM. Each
register in the data memory has a 12-bit address,
allowing up to 4096 byt es of data mem ory. Figure 4-6
shows the data memory organization for the
PIC18FXX8 devices.
The data memory map is divided into as many as
16 banks that cont a in 2 56 by tes ea ch. The lower 4 bits
of the Bank Select Register (BSR<3:0>) select which
bank will be accessed. T he upper 4 bits for the BSR are
not implemented.
The data memory contains Special Function Registers
(SFRs) and General Purpose Registers (GPRs). The
SFRs are used for control and status of the controller
and peripheral functio ns, while GPRs are us ed for data
storage and scratchpad operations in the user’s application. The SFRs start at the last location of Bank 15
(FFFh) and grow downwards. GPRs start at the first
location of Bank 0 and grow upwards. Any read of an
unimplemented location will read as ‘0’s.
The entire data memory may be accessed directly or
indirectly. Direct addressing may require th e us e of th e
BSR register. Indirect addressing requires the use of
the File Select Register (FSR). Each FSR holds a
12-bit 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 m ay be accompli shed by indirec t
addressing or by the use of th e MOVFF instruction. The
MOVFF instruction is a two-word/two-cycle instruction,
that moves a value from one register to another.
To ensure that commonly used registers (SFRs and
select GPRs) can be accessed in a single cycle,
regardless of the current BSR values, an Access Bank
is implemented. A s egment of Ban k 0 and a segm ent of
Bank 15 comprise the Access RAM. Section 4.10“Access Bank” provides a detailed description of the
Access RAM.
4.9.1GENERAL PURPOSE
REGISTER FILE
The register file can be accessed either directly or
indirectly . Indirect ad dressing operates through the File
Select Registers (FSR). The operation of indirect
addressing is shown in Section 4.12 “IndirectAddressing, INDF and FSR Registers”.
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
instructions. Bank 15 (F00h to FFFh) contains SFRs.
All other banks of da t a m em ory c on tain GPR registers,
starting with Bank 0.
4.9.2SPECIAL FUNCTION REGISTERS
The Special Function Registers (SFRs) are registers
used by the CPU an d peripheral mod ules for control ling
the desired operation of the device. These reg isters are
implemented as static RAM. A list of these registers is
given in Table 4-1.
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 i n this section, while those relate d
to the operation of the peripheral features are
described in the section of that periphe ral feature.
The SFRs are typically distributed among the
peripherals whose functions they control.
The unused SFR locations will be unimplemented and
read as ‘0’s. See Table 4-1 for addresses for the SFRs.
DS41159D-page 44 2004 Microchip Technology Inc.
FIGURE 4-5:DATA MEMORY MAP FOR PIC18F248/448
PIC18FXX8
BSR<3:0>
= 0000
= 0001
= 0010
= 0011
= 1110
Bank 0
Bank 1
Bank 2
Bank 3
to
Bank 14
Data Memory Map
00h
FFh
00h
FFh
00h
FFh
Access RAM
Read ‘00h’
GPR
GPR
GPR
Unused
000h
05Fh
060h
0FFh
100h
1FFh
200h
300h
Access Bank
Access Bank Low
(GPR)
Access Bank High
(SFR)
When a = 0,
the BSR is ignored and
the Access Bank is used.
The first 96 bytes are
general purpose RAM
(from Bank 0).
The next 160 bytes are
Special Function
Registers (from Bank 15).
00h
5Fh
60h
FFh
= 1111
Bank 15
00h
FFh
Unused
SFR
EFFh
F00h
F5Fh
F60h
FFFh
When a = 1,
the BSR is used to specify
the RAM location that the
instruction uses.
2004 Microchip Technology Inc.DS41159D-page 45
PIC18FXX8
FIGURE 4-6:DATA MEMORY MAP FOR PIC18F258/458
BSR<3:0>
= 0000
= 0001
= 0010
= 0011
= 0100
= 0101
= 0110
= 1110
= 1111
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’
SFR
SFR
000h
05Fh
060h
0FFh
100h
1FFh
200h
2FFh
300h
3FFh
400h
4FFh
500h
5FFh
600h
EFFh
F00h
F5Fh
F60h
FFFh
Access Bank
Access Bank low
(GPR)
Access Bank high
(SFR)
When a = 0,
the BSR is ignored and the
Access Bank is used.
The first 96 bytes are
general purpose RAM
(from Bank 0).
The next 160 bytes are
Special Function Re gisters
(from Bank 15).
00h
5Fh
60h
FFh
When a = 1,
the BSR i s used to s pecif y
the RAM location that the
instruction uses.
2: This is not a physical register.
3: Contents of register are dependent on WIN2:WIN0 bits in the CANCON register.
4: CANSTAT register is repeated i n th ese lo cation s to si mpli fy appl icati on firm ware. Un ique n ames a re g iven
for each instance of the CANSTAT register due to the Microchip header file requirement.
5: These registers are not implemented on the PIC18F248 and PIC18F258.
2004 Microchip Technology Inc.DS41159D-page 47
PIC18FXX8
TABLE 4-1:SPECIAL FUNCTION REGISTER MAP (CONTINUED)
Note:Shaded registers are available in Bank 15, while the rest are in Access Bank low.
Note 1: Unim plemented registers are read as ‘0’.
2: This is not a physical register.
3: Contents of register are dependent on WIN2:WIN0 bits in the CANCON register.
4: CANSTAT register is repeat ed in th ese lo cation s to si mpli fy appl icati on f irmware. Un iqu e name s are g iven
for each instance of the CANSTAT register due to the Microchip header file requirement.
5: These registers are not implemented on the PIC18F248 and PIC18F258.
DS41159D-page 48 2004 Microchip Technology Inc.
PIC18FXX8
TABLE 4-2:REGISTER FILE SUMMARY
File NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TOSU
TOSHTop-of-Stack High Byte (TOS<15:8>)0000 000030, 38
TOSLTop-of-Stack Low Byte (TOS<7:0>)0000 000030, 38
STKPTRSTKFULSTKUNF
PCLATU
PCLATHHolding Register for PC<15:8>0000 000030, 40
PCLPC Low Byte (PC<7:0>)0000 000030, 40
TBLPTRU
TBLPTRHProgram Memory Table Pointer Hig h By te (TBL P TR < 15: 8> )0000 000030, 68
TBLPTRLProgram Memory Table Pointer Low Byte (TBLPTR<7:0>)0000 000030, 68
TABL ATProgram Memory Table Latc h0000 000030, 68
PRODHProduct Register High Bytexxxx xxxx30, 75
PRODLProduct Register Low Bytexxxx xxxx30, 75
INTCONGIE/GIEHPEIE/GIELTMR0IEINT0IERBIETMR0IFINT0IFRBIF0000 000x30, 79
INTCON2RBPU
INTCON3INT2IPINT1IP
INDF0Uses contents of FSR 0 t o addr es s da t a me mo ry – v al ue of FS R0 not ch an ge d (n ot a ph y sic a l reg ist e r)N/A30, 55
POSTINC0Uses contents of FSR0 to a ddr ess da t a m emor y – v alu e of FS R0 pos t -in cr em en ted (n ot a ph y sic a l reg i ste r)N/A30, 55
POSTDEC0Uses conte nt s of FSR 0 t o a ddres s da t a mem or y – v alu e of FS R0 pos t-i n cr em en ted (n ot a ph y sic a l reg i ste r)N/A30, 55
PREINC0Uses contents of FSR0 to a ddr es s da ta mem or y – v alu e of FSR 0 pre -i ncr em en te d (n ot a p hys ica l reg i ste r)N/A30, 55
PLUSW0Uses content s of FSR 0 to a ddr es s da ta mem or y – v alu e of FSR 0 of fs et by W (no t a phy si ca l re gi ste r) N/A30, 55
FSR0H
FSR0LIndirect Data Memory Address Pointer 0 Low Bytexxxx xxxx30, 55
WREGWorking Registerxxxx xxxx30, 55
INDF1Uses contents of FSR 1 t o addr es s da t a me mo ry – v al ue of FS R1 not ch an ge d (n ot a ph y sic a l reg ist e r)N/A30, 55
POSTINC1Uses contents of FSR1 to a ddr ess da t a m emor y – v alu e of FS R1 pos t -in cr em en ted (n ot a ph y sic a l reg i ste r)N/A30, 55
POSTDEC1Uses conte nt s of FSR 1 t o a ddres s da t a mem or y – v alu e of FS R1 pos t-i n cr em en ted (n ot a ph y sic a l reg i ste r)N/A30, 55
PREINC1Uses contents of FSR1 to a ddr es s da ta mem or y – v alu e of FSR 1 pre -i ncr em en te d (n ot a p hys ica l reg i ste r)N/A30, 55
PLUSW1Uses content s of FSR 1 to a ddr es s da ta mem or y – v alu e of FSR 1 of fs et by W (no t a phy si ca l re gi ste r)N/A30, 55
FSR1H
FSR1LIndirect Data Memory Address Pointer 1 Low Bytexxxx xxxx31, 55
BSR
INDF2Uses contents of FSR 2 t o addr es s da t a me mo ry – v al ue of FS R2 not ch an ge d (n ot a ph y sic a l reg ist e r)N/A31, 55
POSTINC2Uses contents of FSR2 to a ddr ess da t a m emor y – v alu e of FS R2 pos t -in cr em en ted (n ot a ph y sic a l reg i ste r)N/A31, 55
POSTDEC2Uses conte nt s of FSR 2 t o a ddres s da t a mem or y – v alu e of FS R2 pos t-i n cr em en ted (n ot a ph y sic a l reg i ste r)N/A31, 55
PREINC2Uses contents of FSR2 to a ddr es s da ta mem or y – v alu e of FSR 2 pre -i ncr em en te d (n ot a p hys ica l reg i ste r)N/A31, 55
PLUSW2Uses content s of FSR 2 to a ddr es s da ta mem or y – v alu e of FSR 2 of fs et by W (no t a phy si ca l re gi ste r)N/A31, 55
FSR2H
FSR2LIndirect Data Memory Address Pointer 2 Low Bytexxxx xxxx31, 55
STATUS
TMR0HTimer0 Register High Byte0000 000031, 111
TMR0LTimer0 Register Low By texxxx xxxx31, 111
T0CONTMR0ONT08BITT0CST0SEPSAT0PS2T0PS1T0PS01111 111131, 109
OSCCON
LVDCON
WDTCON
RCONIPEN
Legend:x = unknown , u = unchanged, - = unimplemented, q = value depends on condition
Note 1:These registers or register bits are not implemented on the PIC18F248 and PIC18F258 and read as ‘0’s.
2:Bit 21 of the TBLPTRU allows access to the device configuration bits.
3:RA6 and associated bits are configured as port pins in RCIO and ECIO Oscillator mode only and read ‘0’ in all other oscillator modes.
SSPCON2GCENACKSTATACKDTACKENRCENPENRSENSEN0000 000031, 155
ADRESHA/D Result Register High Bytexxxx xxxx31, 243
ADRESLA/D Result Register Low Bytexxxx xxxx31, 243
ADCON0ADCS1ADCS0CHS2CHS1CHS0GO/DONE
ADCON1ADFMADCS2
CCPR1HCapture/Comp ar e/ P WM R egi st e r 1 Hi gh Byt exxxx xxxx32, 124
CCPR1LCapture/Comp are/ P WM R eg ist e r 1 Lo w By texxxx xxxx32, 124
CCP1CON
ECCPR1H
ECCPR1L
ECCP1CON
ECCP1DEL
ECCPAS
CVRCON
CMCON
TMR3HTimer3 Register High Bytexxxx xxxx32, 121
TMR3LTimer3 Register Low Bytexxxx xxxx32, 121
Legend:x = unknown , u = unchanged, - = unimplemented, q = value depends on condition
Note 1:These registers or register bits are not implemented on the PIC18F248 and PIC18F258 and read as ‘0’s.
(1)
(1)
(1)
(1)
(1)
(1)
2:Bit 21 of the TBLPTRU allows access to the device configuration bits.
3:RA6 and associated bits are configured as port pins in RCIO and ECIO Oscillator mode only and read ‘0’ in all other oscillator modes.
——DC1B1DC1B0CCP1M3CCP1M2CCP1M1CCP1M0 --00 000032, 123
Enhanced Capt u re/C o mp a re /PWM Re gis t er 1 Hig h By texxxx xxxx32, 133
Enhanced Capture/Compare/PWM Register 1 Low Bytexxxx xxxx32, 133
TRISCData Direction Control Register for PORTC1111 111133, 100
TRISBData Direction Control Register for PORTB1111 111133, 96
(3)
TRISA
LAT E
LAT D
(1)
(1)
—Data Direction Control Register for PORTA-111 111133, 93
—————Read PORTE Data Latch, Write
Read PORTD Data Latch, Write PORTD Data Latch
(1)
PORTE Data Latch
(1)
---- -xxx33, 104
xxxx xxxx33, 102
LATCRead PORTC Data Latch, Write PORTC Data Latchxxxx xxxx33, 100
LAT BRead PORTB Data La tch, Write PORTB Data La tc hxxxx xxxx33, 96
(3)
LATA
PORTE
PORTD
(1)
(1)
—Read PORT A Data Latch, Write POR TA Data Latch-xxx xxxx33, 93
—————Read PORTE pins, Write PORTE Data
Read PORTD pins, Write PORTD Data Latch
(1)
Latch
(1)
---- -xxx33, 104
xxxx xxxx33, 102
PORTCRead PORTC pins, Write PORTC Data Latchxxxx xxxx33, 100
PORTBRead PORTB pins, Wri t e PORTB Data Latchxxxx xxxx33, 96
(3)
PORTA
—Read PORTA pins, Wri t e PO R TA Data Latch-x0x 000033, 93
TXERRCNTTEC7TEC6TEC5TEC4TEC3TEC2TEC1TEC00000 000033, 209
RXERRCNTREC7REC6REC5REC4REC3REC2REC1REC00000 000033, 214
COMSTATRXB0OVFL RXB1OVFLTXBOTXBPRXBPTXWARNRXWARNEWARN 0000 000033, 205
CIOCON
BRGCON3
Legend:x = unknown , u = unchanged, - = unimplemented, q = value depends on condition
Note 1:These registers or register bits are not implemented on the PIC18F248 and PIC18F258 and read as ‘0’s.
2:Bit 21 of the TBLPTRU allows access to the device configuration bits.
3:RA6 and associated bits are configured as port pins in RCIO and ECIO Oscillator mode only and read ‘0’ in all other oscillator modes.
Legend:x = unknown , u = unchanged, - = unimplemented, q = value depends on condition
Note 1:These registers or register bits are not implemented on the PIC18F248 and PIC18F258 and read as ‘0’s.
2:Bit 21 of the TBLPTRU allows access to the device configuration bits.
3:RA6 and associated bits are configured as port pins in RCIO and ECIO Oscillator mode only and read ‘0’ in all other oscillator modes.
Legend:x = unknown , u = unchanged, - = unimplemented, q = value depends on condition
Note 1:These registers or register bits are not implemented on the PIC18F248 and PIC18F258 and read as ‘0’s.
2:Bit 21 of the TBLPTRU allows access to the device configuration bits.
3:RA6 and associated bits are configured as port pins in RCIO and ECIO Oscillator mode only and read ‘0’ in all other oscillator modes.
The Access Bank is an arch itectural e nhanc ement th at
is very useful for C compiler code optimization. The
techniques used by the C compiler are also useful for
programs written in assembly.
This data memory region can be used for:
• Intermediate computational values
• Local variables of subroutines
• Faster context saving/switching of variables
• Common variables
• Faster evaluation/control of SFRs (no banking)
The Access Bank is comprised of the upper 160 bytes
in Bank 15 (SFRs) and the lower 96 bytes in Bank 0.
These two sections will be referred to as Access Bank
High and Access Bank Low, respectively. Figure 4-6
indicates the Access Bank areas.
A bit in the instruction word spec ifie s if the opera tion is
to occur in the bank spec ifi ed by the BSR register or in
the Access Bank.
When forced in the Access Bank (a = 0), the last
address in Access Bank Low is followed by the first
address in Access Bank High. Access Ban k High m aps
most of the Special Function Registers so that these
registers can be accessed without any software
overhead.
4.1 1Bank Select Register (BSR)
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.
A MOVLB instruction has been provided in the
instruction set to assist in selecting banks.
If the currently selected bank is not implemented, any
read will return all ‘0’s and all writes are ignored. The
Stat us register bit s will be set/clea red as appropriate for
the instruction performed.
Each Bank extends up to FFh (256 bytes). All data
memory is implemented as static RAM.
A MOVFF instruction ignores the BSR since the 12-bit
addresses are embedded into the instruction word.
Section 4.12 “Indirect Addressing, INDF and FSR
Registers” provides a description of indirect address-
ing, which allows linear addressing of the entire RAM
space.
FIGURE 4-7:DIRECT ADDRESSING
Direct Addressing
BSR<3:0>7
Bank Select
Note 1: For register file map detail, see Table 4-1.
(2)
2: The access bit of t he inst ruction can b e used to force an override of the selected bank (BSR<3: 0>) t o the
registers of the Access Bank.
3: The MOVFF instruct ion embeds the entire 12-bit address in the instruction.
Location Select
From Opcode
Data
Memory
(3)
(1)
(3)
0
00h01h0Eh0Fh
000h
0FFh
100h
1FFh
0E00h
0EFFh
Bank 0Bank 1Bank 14 Bank 15
0F00h
0FFFh
DS41159D-page 54 2004 Microchip Technology Inc.
PIC18FXX8
4.12Indirect Addressing, INDF and
FSR Registers
Indirect addressing is a mode of addressing dat a memory where the data memory address in the instruction
is not fixed. A SFR register is used as a pointer to the
data memory location that i s to be read or written. Since
this pointer is in RAM, the cont en t s c an be mo difi ed by
the program. This can be useful for data tables in the
data memory and for software stacks. Figure 4-8
shows the operation of indirect addressing. This shows
the moving of the value to the data memory address
specified b y the value of the FSR register.
Indirect addressing is po ss ible by us ing one of the INDF
registers. Any instruction usi ng the INDF register actually
accesses the regis ter indic ated by the File Select R egister, 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 addres s w hic h is sh own in Figure 4-8.
The INDFn (0 ≤ n ≤ 2) register is not a physical regi ster .
Addressing INDFn actually addresses the register
whose address is contained in the FSRn register
(FSRn is a pointer). This is indirect addressing.
Example 4-5 shows a simple use of indire ct addressin g
to clear the RAM in Bank 1 (locations 100h-1FFh) in a
minimum number of instructions.
EXAMPLE 4-5:HOW TO CLEAR RAM
(BANK 1) USING
INDIRECT ADDRESSING
LFSRFSR0, 100h ;
NEXTCLRFPOSTINC0; Clear INDF
; register
; & inc pointer
BTFSSFSR0H, 1; All done
; w/ Bank1?
BRANEXT; 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 bits 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 address indicated by FSR0H:FSR0L.
A read from INDF1 reads the data from the address
indicated by FSR1H:FSR1L. INDFn can be used in
code anywhere an operand can be used.
If INDF0, INDF1 or INDF2 are read 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 equivale nt to a NOP instruction and the
Status bits are not affected.
4.12.1INDIRECT ADDRESSING
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 addressing.
• When data access is done to one of the five
When using the auto-increment or auto-decrement
features, the effect on the FSR is not reflected in the
Status register. For example, if the indirect address
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 overflows from an
increment, FSRnH will be incremented automatically.
Adding these features allows the FSRn to be used as a
software stack pointer in addition to its uses for table
operations in data mem ory.
Each FSR has an address associated with it that
performs an indexed indirect access. When a data
access to this INDFn location (PLUSWn) occurs, the
FSRn is configur ed to add t he 2’s complement value in
the WREG register and the value in FSR to form the
address before an indirect access. The FSR value is not
changed.
If an FSR register c ontains a value that in dicates one of
the INDFn, an indirect read 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
post-increment/decrement functions.
OPERATION
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 th e value of the
WREG or the FSRn register after an indirect
access (no change) – PLUSWn
2004 Microchip Technology Inc.DS41159D-page 55
PIC18FXX8
FIGURE 4-8:INDIRECT ADDRESSING
Indirect Addressing
FSR Register
11 8 7
FSRnHFSRnL
Location Select
0
0000h
Data
Memory
Note 1: For register file map detail, see Table 4-1.
(1)
0FFFh
DS41159D-page 56 2004 Microchip Technology Inc.
PIC18FXX8
4.13Status Register
The St atus register , sho wn 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 fiv e bits is d isabled. These bits are set
or cleared accordi ng to th e d ev ic e log ic . Th ere fore , th e
result of an instruction with the Status register 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 signed arith metic (2’s comp lement). It indicate s whether the result of the ALU
operation was negative (ALU MSb = 1).
1 = Result was negative
0 = Result was positive
bit 3OV: Overflow bit
This bit is used for signed arithmetic (2’s complement). It indicates an overflow of the 7-bit
magnitude which causes the sign bit (bit 7) to change state.
1 = Overflow occurred for signed arithmetic (in this arithmetic operation)
0 = No overflow occurred
bit 2Z: 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 SUBWF 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 s econd operand. For rotate (RRCF, RRNCF, RLCF and RLNCF)
instructions, this bit is loaded with either bit 4 or bit 3 of the source register.
bit 0C: Carry/Borrow
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 2’s
bit
the polarity is reversed. A subtraction is executed by adding the 2’s
For example, CLRF STATUS will clear the upper three
bits and set the Z bit. Thi s leav es the Status register as
000u u1uu (where u = unchanged).
It is recommended, therefore, that only BCF, BSF,SWAPF, MOVFF and MOVWF instructions 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 which do not affect the
status bits, see Table 25-2.
Note:The C and DC bits operate as a Borrow
and Digit Borrow bit respectively, in
subtraction.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
2004 Microchip Technology Inc.DS41159D-page 57
PIC18FXX8
4.14RCON Register
The Reset Control (RCON) register contains flag bits
that allow differentiation between the sources of a
device Reset. These flags include the TO
BOR
and RI bits. This register is readable and writ able.
, PD, POR,
Note 1: If the BOREN configuration bit is set,
BOR
BOREN configuration bit is clear, BOR
unknown on Power-on Reset.
The BOR status bit is a “don’t ca re” and is
not necessarily predictable if the brownout circuit is disab led (the BORE N configuration bit is clea r). BOR
by the user and checked on subsequent
Resets to see if it is clear, indicating a
brown-out has occurred.
2: It is recommended th at the POR
after a Power-on Reset has been
detected, so that subsequent Power-on
Resets may be detected.
REGISTER 4-3:RCON: RESET CONTROL REGISTER
R/W-0U-0U-0R/W-1R/WR/WR/W-0R/W-0
IPEN
bit 7bit 0
bit 7IPEN: Interrupt Priority Enable bit
1 =Enable priority levels on interrupts
0 =Disable priority levels on interrupts (PIC16CXXX Compatibility mode)
bit 6-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)
1 =After power-up or by the CLRWDT instruction
0 =By execution of the SLEEP instruction
: 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
is ‘1’ on Power-on Reset. If the
is
must then be set
bit be set
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
DS41159D-page 58 2004 Microchip Technology Inc.
PIC18FXX8
5.0DATA EEPROM MEMORY
The data EEPROM is readable and writable during
normal operation over the entire V
memory is not directly mapped in the register file
space. Instead, it is indirectly addressed through the
Special Function Registers (SFR).
There are four SFRs used to read and write the
program and data EEPROM memory. These registers
are:
• EECON1
• EECON2
• EEDATA
• EEADR
The EEPROM data memory allows byte read and write.
When interfacing to the data memory block, EEDATA
holds the 8-bit data for read/write and EEADR holds the
address of the EEPROM locati on bein g access ed. The
PIC18FXX8 devices have 256 bytes of data EEPROM
with an address range from 00h to FFh.
The EEPROM data memory is rated for high erase/
write cycles. A byt e write autom atically er ases the loc ation and writes the new data (erase-before-write). The
write time is controlled by an on-chip timer. The write
time will vary with vo ltag e and tempe rat ure, as wel l as
from chip-to-chip. Please refer to the specifications for
exact limits.
DD range. Th e data
5.1 EEADR Register
The address register can address up to a maximum of
256 bytes of data EEPROM.
5.2EECON1 and EECON2 Registers
EECON1 is the control register for EEPROM memory
accesses.
EECON2 is not a physical register. Reading EECON2
will read all ‘0’s. The EECON2 register is used
exclusively in the EEPROM write sequence.
Control bits, RD
tions, respectively. These bits cannot be cleared, only
set, in software. They are cleared in hardware at the
completion of the read or write operation. The inability
to clear the WR bit in software prevents the accidental
or premature termination of a write operation.
The WREN bit, when set, will allow a write operation.
On power-up, the WREN bit is c lear . Th e WRERR bit is
set when a write operation is interrupted by a MCLR
Reset, or a WDT Time-out Reset, during normal operation. In these situations, the user can check the
WRERR bit and rewrite the location. It is necessary to
reload the data and address regi sters (EEDATA and
EEADR) due to the Reset condition forcing the
contents of the registers to zero.
Note:Interrupt flag bit, EEIF in the PIR2 registe r ,
and WR, initiate read an d w ri te opera-
is set when write is complete. It must be
cleared in software.
2004 Microchip Technology Inc.DS41159D-page 59
PIC18FXX8
REGISTER 5-1:EECON1: EEPROM CONTROL REGISTER 1
R/W-xR/W-xU-0R/W-0R/W-xR/W-0R/S-0R/S-0
EEPGDCFGS
bit 7bit 0
bit 7EEPGD: Flash Program or Data EEPROM Memory Select bit
1 = Access progra m Flash mem ory
0 = Access data EEPROM memory
bit 6CFGS: Flash Program/Data EE or Configuration Select bit
1 = Access Configuration registers
0 = Access program Flash or data EEPROM memory
bit 5Unimplemented: Read as ‘0’
bit 4FREE: Flash Row Erase Enable bit
1 = Erase the program memory row addressed by TBLPTR on the next WR
(reset by hardware)
0 = Perform write only
bit 3WRERR: Write Error Flag bit
1 = A write operation is prematurely terminated
(any MCLR
0 =The write operation completed
Note:When a WRERR occurs, the EEPGD or FREE bits are not cleared. This allows
bit 2WREN: Write Enable bit
1 = Allows write cycles
0 = Inhibits write to the EEPROM or Flash memory
bit 1WR
bit 0RD
: Write Contro l bit
1 = Initiates a data EEPROM erase/wri te cycle or a progra m memory erase cycle or write cycle
(The operation is self-tim ed and the bit is cleared by hardware once write is co mplete. The
WR bit can only be set (not cleared) in software.)
0 = Write cycle is complete
: Read Control bit
1 = Initiates an EEPROM read
(Read takes one cycl e. RD
in software. RD
0 = Does not initiate an EEPROM read
or any WDT Reset during self-timed programming in normal operation)
tracing of the error condition.
bit cannot be set when EEPGD = 1.)
—FREEWRERRWRENWRRD
is cleared in hardware. The RD bit c an on ly be set (not cleare d)
command
Legend:
R = Readable bitW = Writable bitS = Settable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
DS41159D-page 60 2004 Microchip Technology Inc.
PIC18FXX8
5.3Reading the Data EEPROM
Memory
T o read a d ata memory loca tion, the user must write the
address to the EEADR register, clear the EEPGD and
CFGS control bits (EECON1<7:6>) and then set
control bit RD
the very next instruction cycle of the EEDATA register;
therefore, it can be read by the next instruction.
EEDATA will hold this value until another read
operation or until it is written to by the user (during a
write operation).
EXAMPLE 5-1:DATA EEPROM READ
MOVLWDATA_EE_ADDR;
MOVWFEEADR;Data Memory Address
BCFEECON1, EEPGD;Point to DATA memory
BCSEECON1, CFGS;
BSFEECON1, RD;EEPROM Read
MOVFEEDATA, W;W = EEDATA
(EECON1<0>). The data is available in
;to read
5.4Writing to the Data EEPROM
Memory
To write an EEPROM data location, the address must
first be written to the EEADR register and the da ta written to the EEDATA register. Then, the sequence in
Example 5-2 must be fol lowed to initia te the write cy cle.
The write will not initiate if the above sequence is not
exactly followed (write 55h to EECON2, write 0AAh to
EECON2, then set WR
recommended that interrupts be disabled during this
code segment.
Additionally, the WREN bit in EECON1 must be set to
enable writes. This mechanism prevents accidental
writes to data EEPROM due to unexpected code execution (i.e., runaway programs). The WREN bit should
be kept clear at all times, except when updating the
EEPROM. The WREN bit is not cleared by hardware.
After a write sequence has been initiated, clearing the
WREN bit will not aff ect the current write cycle. The WR
bit will be inhibited from being set unless the WREN bit
is set. The WREN bit must be set on a previous ins truction. Both WR and WREN can not be set with the same
instruction.
At the completion of the write cycle, the WR
cleared in hardware and th e EEPROM Write Complete
Interrupt Flag bit (EEIF) is set. The user may either
enable this interrupt or roll this bit. EEIF must be
cleared by software.
bit) for each byte. It is strongly
bit is
EXAMPLE 5-2:DATA EEPROM WRITE
MOVLWDATA_EE_ADDR;
MOVWFEEADR; Data Memory Address to read
MOVLWDATA_EE_DATA;
MOVWFEEDATA; Data Memory Value to write
BCFEECON1, EEPGD ; Point to DATA memory
BCFEECON1, CFGS; Access program FLASH or Data EEPROM memory
BSFEECON1, WREN; Enable writes
RequiredMOVLW55h;
SequenceMOVWFEECON2; Write 55h
BCFINTCON, GIE; Disable interrupts
MOVLW0AAh;
MOVWFEECON2; Write AAh
BSFEECON1, WR; Set WR bit to begin write
BSFINTCON, GIE; Enable interrupts
.; user code execution
.
.
BCFEECON1, WREN; Disable writes on write complete (EEIF set)
2004 Microchip Technology Inc.DS41159D-page 61
PIC18FXX8
5.5Write Verify
Depending on the application, good programming
practice may dictate that the value written to the
memory should be verified against the original value.
This should be used in applications where excessive
writes can stress bits near the specification limit.
Generally, a write failure will be a bit which was written
as a ‘1’, but reads back as a ‘0’ (due to leakage off the
cell).
5.6Protection Against Spurious Write
There are conditions when the device may not want to
write to the data EEPROM memory. To protect against
spurious EEPROM writes, various mechanisms have
been built -in. On powe r-up, the WR EN bit is cl eared.
Also, the Power-up Timer (72 ms duration) prevents
EEPROM write.
The write initiate se quence and the WREN bit together
reduce the probability of an accidental write during
brown-out, power glitch or software malfunction.
5.7Operation During Code-Protect
Data EEPROM memory has its own code-protect
mechanism. External read and write operations are
disabled if either of these mechanisms are enabled.
The microcontroller itself can both read and write to the
internal data EEPROM, regardless of the state of the
code-protect configuration bit. Refer to Section 24.0“Special Features of the CPU” for additional
information.
5.8Using the Data EEPROM
The data EEPROM is a hi gh-endu rance, byte a ddress able array that has been optimized for the storage of
frequently changing information (e.g., program
variables or other data that are updated often).
Frequently changing values will typically be updated
more often than specification D124 or D124A. If this is
not the case, an ar ray r efr esh m ust be pe rfor med . For
this reason, variables that change infrequently (such as
constants, IDs, calibration, etc.) should be stored in
Flash program memory. A simple data EEPROM
refresh routine is shown in Example 5-3.
Note:If data EEPROM is only used to store
constants an d/or data that changes rarely,
an array refresh is likely not required. See
specification D124 or D124A.
EXAMPLE 5-3:DATA EEPROM REFRESH ROUTINE
CLRFEEADR; Start at address 0
BCFEECON1, CFGS; Set for memory
BCFEECON1, EEPGD; Set for Data EEPROM
BCFINTCON, GIE; Disable interrupts
BSFEECON1, WREN; Enable writes
Loop; Loop to refresh array
BSFEECON1, RD; Read current address
MOVLW55h;
MOVWFEECON2; Write 55h
MOVLW0AAh;
MOVWFEECON2; Write AAh
BSFEECON1, WR; Set WR bit to begin write
BTFSCEECON1, WR; Wait for write to complete
BRA$-2
INCFSZEEADR, F; Increment address
BRALoop; Not zero, do it again
The Flash program memory is readable, writable and
erasable during normal operation over the entire V
range.
A read from program memory is executed on one byte
at a time. A write to program memory is executed on
blocks of 8 byt es at a time. Program memory is erase d
in blocks of 64 bytes at a time. A bulk erase operation
may not be issued from user code.
Writing or erasing program memory will cease instruction fetches until the operation is complete. The
program memory cannot be accessed during the write
or erase, therefore, code cannot execute. An internal
programming timer terminates program memory writes
and erases.
A value written to progra m memory does not nee d to be
a valid instruction. Executing a program memory
location that forms an invalid instruction results in a
NOP.
DD
6.1Table Reads and Table Writes
In order to read and write program memory, there are
two operations that allow the processor to move bytes
between the program memory space and the data
RAM:
• Table Read (TBLRD)
• Table Write (TBLWT)
The program memory space is 16 bits wide, while the
data RAM space is 8 bits wide. Table reads and table
writes move data between these two memory spaces
through an 8-bit register (TABLAT).
Table read operations retrieve data from program
memory and place it into the data RAM space.
Figure 6-1 shows the operation of a table read with
program memory and data RAM.
T abl e write ope rations sto re data from the dat a memor y
space into holding registers in program memory. The
procedure to write th e co ntents of the holding registers
into program memory is detailed in Section 6.5“Writing to Flash Program Memory”. Figure 6-2
shows the operation of a table write with program
memory and data RAM.
Table operations work with byte entities. A table block
containing d ata, rather than prog ram instruct ions, is n ot
required to be word aligned. Therefore, a table block
can start and en d at any byte ad dress. If a table wr ite is
being used to write executable code into program
memory, program instructions will need to be word
aligned.
FIGURE 6-1:TABLE READ OPERATION
Table Pointer
TBLPTRU
Note 1: Table Pointer points to a byte in program memory.
TBLPTRH TBLPTRL
(1)
Program Memory
(TBLPTR)
Instruction: TBLRD*
Program Memory
Table Latch (8-bit)
TABLAT
2004 Microchip Technology Inc.DS41159D-page 65
PIC18FXX8
FIGURE 6-2:TABLE WRITE OPERATION
Instruction: TBLWT*
Program Memory
Table Pointer
TBLPTRU
Note 1: T able Pointer actually points to one of eight holding registers, the address of which is determined by TBLPTRL<2:0>.
The process for physically writing data to the program memory array is discussed in Section 6.5 “Writing to FlashProgram Memory”.
TBLPTRH TBLPTRL
(1)
Program Memory
(TBLPTR)
Holding Registers
Table Latch (8-bit)
TABLAT
6.2Control Registers
Several control registers are used in conjunction with
the TBLRD and TBLWT instructions. These include the:
• EECON1 register
• EECON2 register
• TABLAT register
• TBLPTR registers
6.2.1EECON1 AND EECON2 REGISTERS
EECON1 is the control register for memory accesses.
EECON2 is not a physical register. Reading EECON2
will read all ‘0’s. The EECON2 register is used
exclusively in the memory write and erase sequences.
Control bit EEPGD determines if the access will be a
program or data EEPROM memory access. When
clear, any subsequent operations will operate on the
data EEPROM memory. When set, any subsequent
operations will operate on the program memory.
Control bit CFGS determin es if the access will be to the
Configuration/Calibration registers or to program
memory/data EEPROM memory. When set,
subsequent operations will operate on Configuration
registers regardless of EEPGD (see Section 24.0“Special Features o f the CPU”). Wh en clear , memory
selection access is determined by EEPGD.
The FREE bit, when set, will allow a program memory
erase operation. When the FREE bit is set, the erase
operation is initiated on the next WR
command. When
FREE is clear , only wr ite s are enab led .
The WREN bit, when set, will allow a write operation.
On power-up, the WREN bit is c lear . T he WRERR bit is
set when a write operation is interrupted by a MCLR
Reset or a WDT Time-out Reset during normal operation. In these situations, the user can check the
WRERR bit and rewrite the location. It is necessary to
reload the data and address regi sters (EEDATA and
EEADR) due to Reset values of zero.
Control bits, RD
and WR, initiate read an d w ri te operations, respectively. These bits cannot be cleared, only
set, in software. They are cleared in hardware at the
completion of the read or write operation. The inability
to clear the WR
bit in software prevents the accidental
or premature termination of a write operation. The RD
bit cannot be set when accessing program memory
(EEPGD = 1).
Note:Interrupt flag bit, EEIF in the PIR2 r egi ste r,
is set when write is complete. It must be
cleared in software.
DS41159D-page 66 2004 Microchip Technology Inc.
REGISTER 6-1:EECON1: EEPROM CONTROL REGISTER 1
R/W-xR/W-xU-0R/W-0R/W-xR/W-0R/S-0R/S-0
EEPGDCFGS
bit 7bit 0
bit 7EEPGD: Flash Program or Data EEPROM Memory Select bit
1 = Access progra m Flash mem ory
0 = Access data EEPROM memory
bit 6CFGS: Flash Program/Data EE or Configuration Select bit
1 = Access Configuration registers
0 = Access program Flash or data EEPROM memory
bit 5Unimplemented: Read as ‘0’
bit 4FREE: Flash Row Erase Enable bit
1 = Erase the program memory row addressed by TBLPTR on the next WR
(cleared by completion of erase operation)
0 = Perform write only
bit 3WRERR: Write Error Flag bit
1 = A write operation is prematurely terminated
(any MCLR
0 =The write operation completed
Note:When a WRERR occurs, the EEPGD and CFGS bits are not cleared. This allows
bit 2WREN: Write Enable bit
1 = Allows write cycles
0 = Inhibits write to the EEPROM or Flash memory
bit 1WR
bit 0RD
: Write Control bit
1 = Initiates a data EEPROM erase/wri te cycle or a progra m memory erase cycle or write cycle
(The operation is self-tim ed and the bit is cleared by hardware once write is co mplete. The
WR bit can only be set (not cleared) in software.)
0 = Write cycle to the EEPROM is complete
: Read Control bit
1 = Initiates an EEPROM read
(Read takes one cycl e. RD
in software. RD
0 = Does not initiate an EEPROM read
or any WDT Reset during self-timed programming in normal operation)
tracing of the error condition.
bit cannot be set when EEPGD = 1.)
—FREEWRERRWRENWRRD
is cleared in hardware. The RD bit c an on ly be set (not cleare d)
PIC18FXX8
command
Legend:
R = Readable bitW = Writable bitS = Settable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
2004 Microchip Technology Inc.DS41159D-page 67
PIC18FXX8
6.2.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 RAM.
6.2.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, Table
Pointer High Byte and Table Pointer Low Byte
(TBLPTRU:TBLPTRH:TBLPTRL). These three registers join to form a 22-bit wide po inter. The low-order
21 bits allow the device to address up to 2 Mbytes of
program memory sp ace. Th e 22nd b it allow s acce ss to
the device ID, the user ID and the configuration bits.
The Table Pointer, TBLPTR, is used by the TBLRD and
TBLWT instructions. These instructions can update the
TBLPTR in one of four ways based on the table
operation. These operations are shown in Table 6-1.
These operations on the TBLPTR only affect the
low-order 21 bits.
6.2.4TABLE POINTER BOUNDARIES
TBLPTR is used in reads, writes and erases of the
Flash program memory.
When a TBLRD is executed, all 22 bits of the Table
Pointer determine which byte is read from program
memory into TABLAT.
When a TBLWT is executed, the three LSbs of the Table
Pointer (TBLPTR<2:0>) determine which of the eight
program memory holding registers is written to. When
the timed write to program memory (long write) begins,
the 19 MSbs of the Table Pointer, TBLPTR
(TBLPTR<21:3>), will determine which program
memory block of 8 bytes is written to. For more detail,
see Section 6.5 “Writing to Flash Program Memory”.
When an erase of program memory is executed, the
16 MSbs of the Table Pointer (TBLPTR<21 :6>) point to
the 64-byte block that will be erased. The Least
Significant bits (TBLPTR<5:0>) are ignored.
Figure 6-3 describes the relevant boundaries of
TBLPTR based on Flash program memory operations.
TABLE 6-1:TABLE POINTER OPERATIONS WITH TBLRD AND TBLWT INSTRUCTIONS
ExampleOperation on Table Pointer
TBLRD*
TBLWT*
TBLRD*+
TBLWT*+
TBLRD*TBLWT*-
TBLRD+*
TBLWT+*
TBLPTR is incremented after the read/write
TBLPTR is decremented after the read/write
TBLPTR is incremented before the read /write
TBLPTR is not modified
FIGURE 6-3:TABLE POINTER BOUNDARIES BASED ON OPERATION
2116 15870
DS41159D-page 68 2004 Microchip Technology Inc.
TBLPTRU
ERASE – TBLPTR<21:6>
TBLPTRH
WRITE – TBLPTR<21:3>
READ – TBLPTR<21:0>
TBLPTRL
PIC18FXX8
6.3Reading the Flash Program
Memory
The TBLRD instruction is used to retrieve data from
program memory and places it into data RAM. Table
reads from program memory are pe rformed one by te at
a time.
TBLPTR points to a byte address in program space.
Executing TBLRD places the byte pointed to into
TABLAT. In addition, TBLPTR can be modified
automatically for the next table read operation.
The internal program memory is typically organize d by
words. The Least Significant b it of th e address selects
between the high and low bytes of the word. Figure 6-4
shows the interface between the internal program
memory and the TABLAT.
FIGURE 6-4:READS FROM FLASH PROGRAM MEMORY
Program Memory
(Even Byte Address)
(Odd Byte Address)
TBLPTR = xxxxx1
Instruction Register
(IR)
FETCH
TBLRD
EXAMPLE 6-1:READING A FLASH PROGRAM MEMORY WORD
MOVLWCODE_ADDR_UPPER; Load TBLPTR with the base
MOVWFTBLPTRU; address of the word
MOVLWCODE_ADDR_HIGH
MOVWFTBLPTRH
MOVLWCODE_ADDR_LOW
READ_WORD
MOVWFTBLPTRL
TBLRD*+; read into TABLAT and increment
MOVFTABLAT, W ; get data
MOVWFWORD_LSB
TBLRD*+; read into TABLAT and increment
MOVFTABLAT, W ; get data
MOVWFWORD_MSB
TBLPTR = xxxxx0
TABLAT
Read Register
2004 Microchip Technology Inc.DS41159D-page 69
PIC18FXX8
6.4Erasing Flash Program Memory
The minimum eras e block is 32 wo rds or 64 b ytes. Only
through the use of an external programmer, or through
ICSP control, can larger blocks of program memory be
bulk erased. Word erase in the Flash array is not
supported.
When initiating an erase sequence from the microcontroller itself, a blo ck of 64 bytes of program memo ry
is erased. The Most Significant 16 bits of the
TBLPTR<21:6> point to the block being erased.
TBLPTR<5:0> are ignored.
The EECON1 register comma nds the erase operation.
The EEPGD bit must be set to point to the Flash
program memory. The WREN bit must be set to enable
write operations. The F REE bit is set to select an erase
operation.
For protection, the wri te i ni tiat e s equ enc e f or EECO N2
must be used.
A long write is nec essa ry for erasin g the i nternal Flash.
Instruction execution is halted while in a long write
6.4.1FLASH PROGRAM MEMORY
ERASE SEQUENCE
The sequence of events for erasing a block of internal
program memory location is:
1.Load Table Pointer with address of row being
erased.
2.Set the EECON1 register for the erase operation:
• set the EEPGD bit to point to program memory;
• clear the CFGS bit to access program memory;
• set the WREN bit to enable writes;
• set the FREE bit to enable the erase.
3.Disable interrupts.
4.Write 55h to EECON2.
5.Write 0AAh to EECON2.
6.Set the WR
cycle.
7.The CPU will stall for duration of the erase
(about 2 ms using internal timer).
8.Re-enable interrupts.
cycle. The long write will be terminated by the internal
programming timer.
EXAMPLE 6-2:ERASING A FLASH PROGRAM MEMORY ROW
MOVLWupper (CODE_ADDR); load TBLPTR with the base
MOVWFTBLPTRU ; address of the memory block
MOVLWhigh (CODE_ADDR)
MOVWFTBLPTRH
MOVLWlow (CODE_ADDR)
MOVWFTBLPTRL
The minimum programmi ng block is 4 words or 8 bytes .
Word or byte programming is not supported.
Table writes are used internally to load the holding
registers needed to program the Flash memory. There
are 8 holding registers used by the table writes for
programming.
Since the Table Latch (TABLAT) is only a single byte,
the TBLWT instruction has to be executed 8 times for
each programming operation. All of the table write
operations will es sentially be short writes, b ecause only
the holding registe rs a re wr itten. A t the e nd of u pdatin g
8 registers, the EECON1 register mu st be written to, to
start the programming operation with a long write.
The long write is necessary for programming the internal Flash. Instruc tion exe cution i s halted while i n a long
write cycle. The long write will be terminated by the
internal programming timer.
The EEPROM on-chip timer controls the write time.
The write/erase voltages are generated by an on-chip
charge pump rated t o operate over t he voltag e range of
the device for byte or word operations.
6.5.1FLASH PROGRAM MEMORY WRITE
SEQUENCE
The sequenc e of events for program ming an internal
program memory location should be:
1.Read 64 bytes into RAM.
2.Update data values in RAM as necessary.
3.Load Table Pointer with address being erased.
4.Do the row erase procedure.
5.Load Table Pointer with address of first byte
being written.
6.Write the first 8 bytes into the holding registers
using the TBLWT instruction, auto-increment
may be used.
7.Set the EECON1 register for th e write operatio n:
• set the EEPGD bit to point to program memory;
• clear the CFGS bit to access program memory;
• set the WREN to enable byte writes.
8.Disable interrupts.
9.Write 55h to EECON2.
10. Write AAh to EECON2.
11. S et the WR
12. The CPU will stall for dura tion of t he write (about
2 ms using internal timer).
13. Re-enable interrupts.
14. Repeat steps 6-14 seven times to write
64 bytes.
15. Verify the memory (table read).
This procedure will require about 18 ms to update one
row of 64 bytes of memory. An example of the required
code is given in Example 6-3.
bit. This will begin the write cycle.
Note:Before setting the WR bit, the Table
Pointer address needs to be within the
intended address range of the 8 bytes in
the holding registers.
FIGURE 6-5:TABLE WRITES TO FLASH PROGRAM MEMORY
TABLAT
Write Register
888
TBLPTR = xxxxx2
Holding Register
Program Memory
TBLPTR = xxxxx0
Holding Register
8
TBLPTR = xxxxx1
Holding Register
TBLPTR = xxxxx7
Holding Register
2004 Microchip Technology Inc.DS41159D-page 71
PIC18FXX8
EXAMPLE 6-3:WRITING TO FLASH PROGRAM MEMORY
MOVLWD'64; number of bytes in erase block
MOVWFCOUNTER
MOVLWhigh (BUFFER_ADDR); point to buffer
MOVWFFSR0H
MOVLWlow (BUFFER_ADDR)
MOVWFFSR0L
MOVLWupper (CODE_ADDR); Load TBLPTR with the base
MOVWFTBLPTRU; address of the memory block
MOVLWhigh (CODE_ADDR)
MOVWFTBLPTRH
MOVLWlow (CODE_ADDR)
READ_BLOCK
MODIFY_WORD
ERASE_BLOCK
RequiredMOVWFEECON2 ; write 55H
SequenceMOVLW0AAh
WRITE_BUFFER_BACK
PROGRAM_LOOP
WRITE_WORD_TO_HREGS
MOVWFTBLPTRL
TBLRD*+; read into TABLAT, and inc
MOVFTABLAT, W ; get data
MOVWFPOSTINC0; store data
DECFSZ COUNTER ; done?
BRAREAD_BLOCK; repeat
MOVLWDATA_ADDR_HIGH; point to buffer
MOVWFFSR0H
MOVLWDATA_ADDR_LOW
MOVWFFSR0L
MOVLWNEW_DATA_LOW; update buffer word
MOVWFPOSTINC0
MOVLWNEW_DATA_HIGH
MOVWFINDF0
MOVLWupper (CODE_ADDR); load TBLPTR with the base
MOVWFTBLPTRU ; address of the memory block
MOVLWhigh (CODE_ADDR)
MOVWFTBLPTRH
MOVLWlow (CODE_ADDR)
MOVWFTBLPTRL
BSFEECON1, EEPGD; point to FLASH program memory
BCFEECON1, CFGS; access FLASH program memory
BSFEECON1, WREN; enable write to memory
BSFEECON1, FREE; enable Row Erase operation
BCFINTCON, GIE; disable interrupts
MOVLW55h
MOVLW8 ; number of write buffer groups of 8 bytes
MOVWFCOUNTER_HI
MOVLWhigh (BUFFER_ADDR); point to buffer
MOVWFFSR0H
MOVLWlow (BUFFER_ADDR)
MOVWFFSR0L
MOVLW8 ; number of bytes in holding register
MOVWFCOUNTER
MOVFWPOSTINC0, W; get low byte of buffer data
MOVWFTABLAT; present data to table latch
TBLWT+* ; write data, perform a short write
; to internal TBLWT holding register.
DECFSZ COUNTER ; loop until buffers are full
BRAWRITE_WORD_TO_HREGS
DS41159D-page 72 2004 Microchip Technology Inc.
EXAMPLE 6-3:WRITING TO FLASH PROGRAM MEMORY (CONTINUED)
WRITE_WORD_TO_HREGS
MOVFWPOSTINC0, W; get low byte of buffer data
MOVWFTABLAT; present data to table latch
TBLWT+* ; write data, perform a short write
; to internal TBLWT holding register.
DECFSZ COUNTER ; loop until buffers are full
BRAWRITE_WORD_TO_HREGS
PROGRAM_MEMORY
BSFEECON1, EEPGD; point to FLASH program memory
BCFEECON1, CFGS; access FLASH program memory
BSFEECON1, WREN; enable write to memory
BCFINTCON, GIE; disable interrupts
MOVLW55h; write 55h
RequiredMOVWFEECON2
SequenceMOVLW0AAh; write 0AAh
MOVWFEECON2 ; start program (CPU stall)
BSFEECON1, WR
NOP
BSFINTCON, GIE; re-enable interrupts
DECFSZ COUNTER_HI; loop until done
BRA PROGRAM_LOOP
BCFEECON1, WREN; disable write to memory
PIC18FXX8
6.5.2WRITE VERIFY
Depending on the application, good programming
practice may dictate that the value written to the
memory should be verified against the original value.
This should be used in applications where excessive
writes can stress bits near the specification limit.
6.5.3UNEXPECTED TERMINATION OF
WRITE OPERATION
If a write is termin ate d b y a n u npl anned event, such as
loss of power or an unexpected Reset, the memory
location just pr ogrammed shou ld be verifi ed and rep rogrammed if neede d.The WRERR bit i s set when a w rite
operation is interrupted by a MCLR
Time-out Reset during normal operation. In these
situations, users can ch eck the WRERR bit and rewrite
the location.
Reset or a WDT
6.5.4PROTECTION AGAINST SPURIOUS
WRITES
To reduce the probability against spurious writes to
Flash program memory, the write initiate sequence
must also be followed. See Section 24.0 “Special
Features of the CPU” for more detail.
6.6Flash Program Operation During
Code Protection
See Section 24.0 “Special Features of the CPU” for
details on code protection of Flash program memory.
2004 Microchip Technology Inc.DS41159D-page 73
PIC18FXX8
TABLE 6-2:REGISTERS ASSOCIATED WITH PROGRAM FLASH MEMORY
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TBLPTRU
TBPLTRH Program Memory Table Pointer High Byte (TBLPTR<15:8>)0000 0000 0000 0000
TBLPTRL P rogram Mem ory Table Pointer Low Byte (TBLPTR<7:0>)0000 0000 0000 0000
TABLATProgram Memory Table Latch0000 0000 0000 0000
INTCONGIE/GIEHPEIE/
EECON2 EEPROM Control Register 2 (not a physical register)——
EECON1EEPGDCFGS
IPR2
PIR2
PIE2
Legend:x = unknown, u = unchanged, - = unimplemented, read as ‘0’.
Note 1:These registers or register bits are not implemented on the PIC18F248 and PIC18F258 and read as ‘0’s.
Shaded cells are not used during Flash/EEPROM access.
Value on:
POR, BOR
--00 0000 --00 0000
(1)
-1-1 1111 -1-1 1111
(1)
-0-0 0000 -0-0 0000
(1)
-0-0 0000 -0-0 0000
Value on
all other
Resets
DS41159D-page 74 2004 Microchip Technology Inc.
PIC18FXX8
7.08 x 8 HARDWARE MULTIPLIER
7.1Introduction
An 8 x 8 hardware multiplier is included in the ALU of
the PIC18FXX8 devices. By making the multiply a
hardware operatio n, i t co mp letes in a single instruction
cycle. This is an unsign ed multiply that gives a 16-bit
result. The result is store d in the 1 6-bit pro duct reg ister
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 size requ ire me nt s for multiply
algorithms
The performance increas e allows the device to be used
in applications previously reserved for Digital Signal
Processors.
Table 7-1 shows a perf ormance comparison be tween
Enhanced devices using the single-cycle hardware
multiply and performing the same function without the
hardware multiply.
7.2Operation
Example 7-1 shows the sequence to do an 8 x 8
unsigned multiply. Only one instruction is required
when one argument of the multiply is already loaded in
the WREG register.
Example 7-2 shows the se quence to do an 8 x 8 si gned
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.
MOVF ARG1, W
MULWF ARG2 ; ARG1 * ARG2 ->
; PRODH:PRODL
BTFSC ARG2, SB ; Test Sign Bit
SUBWF PRODH ; PRODH = PRODH
; - ARG1
MOVF ARG2, W
BTFSC ARG1, SB ; Test Sign Bit
SUBWF PRODH ; PRODH = PRODH
; - ARG2
TABLE 7-1:PERFORMANCE COMPARISON
Program
RoutineMultiply Method
8 x 8 unsigned
8 x 8 signed
16 x 16 unsigned
16 x 16 signed
Without hardware multiply13696.9 µs27.6 µs69 µs
Hardware multiply11100 ns400 ns1 µs
Without hardware multiply33919.1 µs36.4 µs91 µs
Hardware multiply66600 ns2.4 µs6 µs
Without hardware multiply2124224.2 µs96.8 µs242 µs
Hardware multiply24242.4 µs9.6 µs24 µs
Without hardware multiply5225425.4 µs102.6 µs254 µs
Hardware multiply36363.6 µs14.4 µs36 µs
Memory
(Words)
Cycles
(Max)
Time
@ 40 MHz@ 10 MHz@ 4 MHz
2004 Microchip Technology Inc.DS41159D-page 75
PIC18FXX8
Example 7-3 shows the sequence to do a 16 x 16
unsigned multiply. Equation 7-1 shows the algorithm
that is used. The 32-bit re sult is st ored in four re gisters,
RES3:RES0.
Example 7-4 shows the sequence to do a 16 x 16
signed multiply. Equation 7-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 argu ment p air’s Most Signi ficant bit (M Sb)
is tested and the appropriate subtractions are done.
The PIC18FXX8 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 000008h and the low prio rity interrupt vector
is at 000018h. High priority interrupt events will
override any low priority interrupts that may be in
progress.
There are 13 registers that are use d to c ontrol interru pt
operation. These registers are:
• RCON
•INTCON
• INTCON2
• INTCON3
• PIR1, PIR2, PIR3
• PIE1, PIE2, PIE3
• IPR1, IPR2, IPR3
It is recommended that the Microchip header files,
supplied with MPLAB
names in these registers. This allows the assembler/
compiler to automatical ly ta ke care of the pla ceme nt of
these bits 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 program execution to
branch to the interrupt vector address when the
flag bit is set
• Priority bit to select high priority or low priority
The interrupt priority feature is enabled by setting the
IPEN bit (RCON register). When interrupt priority is
enabled, there are two bits that enable interrupts
globally . Setti ng the GIEH bit (INTC ON<7>) enable s all
interrupts. Setting the GIEL bit (INTCON register)
enables all interrupts that have the priority bit cleared.
When the interrupt flag, enable bit and appropriate
global interrupt enable bit are set, the interrupt wi ll vector immediately to address 000008h or 000018h,
depending on the priority level. Individual inte rrupts can
be disabled through their corresponding enable bits.
®
IDE, be used for the symbolic bit
When the IPEN bit is cleared (default state), the
interrupt priority feature is disabled and interrupts are
compatible with PICmicro
Compatibilit y mode, the in terrupt prior ity bits for each
source have no effe ct. T he PEIE b it (IN TCO N regi st er)
enables/disables all peripheral interrupt sources. The
GIE bit (INTCON register) enab les/disables all in terrupt
sources. All interrupts branch to address 000008h in
Compatibility mode.
When an interrupt is responded to, the global interrupt
enable bit is cleared to disable further interrupts. If the
IPEN bit is cleared, this is the GIE bit. If interru pt priority
levels are used, this wi ll be either the GIEH or G IEL 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 be cleared in s oftware be fore re-enab ling
interrupts to avoid recursive interrupts.
The “return from interrupt” instruction, RETFIE, exits
the interrupt routine and set s the GIE bit (GIEH or GI EL
if priority levels are used), which re-enables interrupts.
For external interrupt events, such as the INT pins or
the PORTB input chang e interrupt, the i nterrupt latenc y
will be three to four instruction cycles. The exact
latency is the same for one or two-cycle instructions.
Individual interrupt flag bits are set regardless of the
status of their corresponding enable bit or the GIE bit.
Note:Do not use the MOVFF instruction to modify
any of the interrupt control registers while
any interrupt is enabled. Doing so may
cause erratic microcontroller behavior.
®
mid-range devices. In
2004 Microchip Technology Inc.DS41159D-page 77
PIC18FXX8
FIGURE 8-1:INTERRUPT LOGIC
Peripheral Interrupt Flag bit
Peripheral Interrupt Enable bit
Peripheral Interrupt Priority bit
TMR1IF
TMR1IE
TMR1IP
XXXXIF
XXXXIE
XXXXIP
Additional Peripheral Interrupts
High Priority Interrupt Generation
Low Priority Interrupt Generation
IPEN
TMR0IF
TMR0IE
TMR0IP
RBIF
RBIE
RBIP
INT0IF
INT0IE
INT1IF
INT1IE
INT1IP
INT2IF
INT2IE
INT2IP
IPEN
GIEL/PEIE
IPEN
Wake-up if in Sleep mode
Interrupt to CPU
Vector to Location
0008h
GIE/GIEH
Peripheral Interrupt Flag bit
Peripheral Interrupt Enable bit
Peripheral Interrupt Priority bit
TMR1IF
TMR1IE
TMR1IP
XXXXIF
XXXXIE
XXXXIP
Additional Peripheral Interrupts
TMR0IF
TMR0IE
TMR0IP
RBIF
RBIE
RBIP
INT0IF
INT0IE
INT1IF
INT1IE
INT1IP
INT2IF
INT2IE
INT2IP
Interrupt to CPU
Vector to Location
0018h
PEIE/GIEL
GIE/GIEH
DS41159D-page 78 2004 Microchip Technology Inc.
PIC18FXX8
8.1INTCON Registers
The INTCON registers are readable an d writa ble registers which cont ain v arious enab le, pri ority and fla g bit s.
Because of the number of interrupts to be controlled,
PIC18FXX8 devices have three INTCON registers.
They are detai led in Register 8-1 through Register 8-3.
Note:Interrupt flag bi ts are set when an in terrupt
condition occurs regardless of the state of
its corresponding enable bit or the global
interrupt enable bit. User software should
ensure the appropriate interrupt flag bits
are clear prior to enabling an interrupt.
This feature allows software polling.
1 = Enables all unmasked interrupts
0 = Disables all interrupts
PEN (RCON<7>) = 1:
When I
1 = Enables all high priority interrupts
0 = Disables all priority interrupts
bit 6PEIE/GIEL: Peripheral Interrupt Enable bit
When IPEN (RCON<7>) =
1 = Enables all unmasked peripheral interrupts
0 = Disables all peripheral inter rupts
When IPEN (RCON<7>) =
1 = Enables all low priority peripheral interrupts
0 = Disables all low 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 int errupt
bit 2TMR0IF: TMR0 Overflow Interrupt Flag bit
1 = TMR0 register has overflowed (must be cleared in software)
0 = TMR0 register did no t overflow
bit 1INT0IF: INT0 External Interrupt Flag bit
1 = The INT0 external interrupt occurred (must be cleared in software by reading PORTB)
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
Note:A mismatch condition will continue to set this bit. Reading PORTB will end the
mismatch condition and allow the bit to be cleared.
0:
1:
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
2004 Microchip Technology Inc.DS41159D-page 79
PIC18FXX8
REGISTER 8-2:INTCON2: INTERRUPT CONTROL REGISTER 2
R/W-1R/W-1R/W-1U-0U-0R/W-1U-0R/W-1
RBPUINTEDG0 INTEDG1——TMR0IP—RBIP
bit 7bit 0
bit 7RBPU
bit 6INTEDG0: External Interrupt 0 Edge Select bit
bit 5INTEDG1: External Interrupt 1 Edge Select bit
bit 4-3Unimplemented: Read as ‘0’
bit 2TMR0IP: TMR0 Overflow Interrupt Priority bit
bit 1Unimplemented: Read as ‘0’
bit 0RBIP: RB Port Change Interrupt Priority bit
: PORTB Pull-up Enable bit
1 = All PORTB pull-ups are disabled
0 = PORTB pull-ups are enabled by individual port latch values
1 = Interrupt on rising edge
0 = Interrupt on falling edge
1 = Interrupt on rising edge
0 = Interrupt on falling edge
1 = High priori ty
0 = Low priority
1 = High priority
0 = Low priority
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
Note:Interrupt flag bi ts a r e s et w h en an i nt e rr up t cond i t io n o cc urs r eg a rdl es s o f th e stat e
of its corresponding enable bit or the global interrupt enable bit. User software
should ensure the appropriate interrupt flag bits are clear prior to enabling an
interrupt. This feature allows software polling.
DS41159D-page 80 2004 Microchip Technology Inc.
REGISTER 8-3:INTCON3: INTERRUPT CONTROL REGISTER 3
R/W-1R/W-1U-0R/W-0R/W-0U-0R/W-0R/W-0
INT2IPINT1IP
bit 7bit 0
bit 7INT2IP: INT2 External Interrupt Priority bit
1 = High priori ty
0 = Low priority
bit 6INT1IP: INT1 External Interrupt Priority bit
1 = High priori ty
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
PIC18FXX8
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
Note:Interrupt flag bi ts a r e s et w h en an i nt e rr up t cond i t io n o cc urs r eg a rdl es s o f th e stat e
of its corresponding enable bit or the global interrupt enable bit. User software
should ensure the appropriate interrupt flag bits are clear prior to enabling an
interrupt. This feature allows software polling.
2004 Microchip Technology Inc.DS41159D-page 81
PIC18FXX8
8.2PIR Registers
The Peripheral Interrupt Request (PIR) registers
contain the individual flag bits for the peripheral
interrupts (Register 8-4 through Register 8-6). Due to
the number of peripheral interrupt sources, there are
three Peripheral Interrupt Request (Flag) registers
(PIR1, PIR2, PIR3).
Note 1: Interrupt flag bits are set when an interrupt
condition occurs rega rdless of the st ate of
its corresponding enable bit or the Global
Interrupt Enable bit, GIE (INTCON
register).
2: User software should en sure the appropri-
ate interrupt flag bits are cleared prior to
enabling an interrupt and after servicing
that interrupt.
bit 7IRXIF: Invalid Message Received Interrupt Flag bit
1 = An invalid message has occurred on the CAN bus
0 = An invalid message has not occurred on the CAN bus
bit 6WAKIF: Bus Activity Wake-up Interrupt Flag bit
1 = Activity on the CAN bus has occurred
0 = Activity on the CAN bus has not occurred
bit 5ERRIF: CAN bus Error Interrupt Flag bit
1 = An error has occurred in the CAN module (multiple sources)
0 = An error has not occurred in the CAN module
bit 4TXB2IF: Transmit Buffer 2 Interrupt Flag bit
1 = Transmit Buffer 2 has completed transmission of a message and may be reloaded
0 = Transmit Buffer 2 has not completed transmission of a message
bit 3TXB1IF: Transmit Buffer 1 Interrupt Flag bit
1 = Transmit Buffer 1 has completed transmission of a message and may be reloaded
0 = Transmit Buffer 1 has not completed transmission of a message
bit 2TXB0IF: Transmit Buffer 0 Interrupt Flag bit
1 = Transmit Buffer 0 has completed transmission of a message and may be reloaded
0 = Transmit Buffer 0 has not completed transmission of a message
bit 1RXB1IF: Receive Buffer 1 Interrupt Flag bit
1 = Receive Buffer 1 has received a new message
0 = Receive Buffer 1 has not received a new message
bit 0RXB0IF: Receive Buffer 0 Interrupt Flag bit
1 = Receive Buffer 0 has received a new message
0 = Receive Buffer 0 has not received a new message
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
DS41159D-page 84 2004 Microchip Technology Inc.
PIC18FXX8
8.3PIE Registers
The Peripheral Interrupt En abl e (PIE) registers contain
the individual enable bits for the peripheral interrupts
(Register 8-7 through Register 8-9). Due to the number
of peripher a l int er ru p t sour c e s, th er e ar e t hr e e Pe r i ph eral Interrupt Enable registers (PIE1, PIE2, PIE3).
When IPEN is clear, the PEIE bit must be set to enable
any of these peripheral interrupts.
bit 7IRXIE: Invalid CAN Message Received Interrupt Enable bit
1 = Enables the invalid CAN message received interrupt
0 = Disables the invalid CAN message received interrupt
bit 6WAKIE: Bus Activity Wake-up Interrupt Enable bit
1 = Enables the bus activity wake-up interrupt
0 = Disables the bus activity wake-up interrupt
bit 5ERRIE: CAN bus Error Interrupt Enable bit
1 = Enables the CAN bus error interrupt
0 = Disables the CAN bus error interrupt
bit 4TXB2IE: Transmit Buffer 2 Interrupt Enable bit
1 = Enables the Transmit Buffer 2 interrupt
0 = Disables th e Transmit Buffer 2 interrupt
bit 3TXB1IE: Transmit Buffer 1 Interrupt Enable bit
1 = Enables the Transmit Buffer 1 interrupt
0 = Disables th e Transmit Buffer 1 interrupt
bit 2TXB0IE: Transmit Buffer 0 Interrupt Enable bit
1 = Enables the Transmit Buffer 0 interrupt
0 = Disables th e Transmit Buffer 0 interrupt
bit 1RXB1IE: Receive Buffer 1 Interrupt Enable bit
1 = Enables the Receive Buffer 1 interrupt
0 = Disables the Receive Buffer 1 interrupt
bit 0RXB0IE: Receive Buffer 0 Interrupt Enable bit
1 = Enables the Receive Buffer 0 interrupt
0 = Disables the Receive Buffer 0 interrupt
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
2004 Microchip Technology Inc.DS41159D-page 87
PIC18FXX8
8.4IPR Registers
The Interrupt Priority (IPR) registe rs contain the indi vidual priority bits for the peripheral interrupts. Due to the
number of peripheral interrup t sour ces , the re are thre e
Peripheral Interrupt Priority registers (IPR1, IPR2 and
IPR3). The operation of the priority bits requires that
the Interrupt Priority Enable bit (IPEN) be set.
bit 7IRXIP: Invalid Message Received Interrupt Priority bit
1 =High priority
0 = Low priority
bit 6WAKIP: Bus Activity Wake-up Interrupt Priority bit
1 =High priority
0 = Low priority
bit 5ERRIP: CAN bus Error Interrupt Priority bit
1 =High priority
0 = Low priority
bit 4TXB2IP: Transmit Buffer 2 Interrupt Priority bit
1 =High priority
0 = Low priority
bit 3TXB1IP: Transmit Buffer 1 Interrupt Priority bit
1 =High priority
0 = Low priority
bit 2TXB0IP: Transmit Buffer 0 Interrupt Priority bit
1 =High priority
0 = Low priority
bit 1RXB1IP: Receive Buffer 1 Interrupt Priority bit
1 =High priority
0 = Low priority
bit 0RXB0IP: Receive Buffer 0 Interrupt Priority bit
1 =High priority
0 = Low priority
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
DS41159D-page 90 2004 Microchip Technology Inc.
8.5RCON Register
The Reset Control (RCON) register contains the IPEN
bit which is used to enable prioritized interrupts. The
functions of the other bits in this register are discussed
in more detail in Section 4.14 “RCON Register”.
REGISTER 8-13:RCON: RESET CONTROL REGISTER
R/W-0U-0U-0R/W-1R-1R-1R/W-0R/W-0
IPEN
bit 7bit 0
bit 7IPEN: Interrupt Priority Enable bit
1 = Enable priority levels on interrupts
0 = Disable priority levels on interrupts (PIC16CXXX Compatibility mode)
bit 6-5Unimplemented: Read as ‘0’
bit 4RI
bit 3TO: Watchdog Time-out Flag bit
bit 2PD: Power-down Detection Flag bit
bit 1POR: Power-on Reset Status bit
bit 0BOR: Brown-ou t Reset Status bit
: RESET Instruction Flag bit
For details of bit operation, see Register 4-3.
For details of bit operation, see Register 4-3.
For details of bit operation, see Register 4-3.
For details of bit operation, see Register 4-3.
For details of bit operation, see Register 4-3.
——RITOPDPORBOR
PIC18FXX8
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
2004 Microchip Technology Inc.DS41159D-page 91
PIC18FXX8
8.6INT Interrupts
External interrupts on the RB0/INT0, RB1/INT1 and
RB2/CANTX/INT2 pins are e dge triggered: eith er rising
if the corresponding INTEDGx bit is set in the
INTCON2 register, or falling if the INTEDGx bi t is c lear.
When a valid edge appears on the RBx/INTx pin, the
corresponding flag bit INTxIF is set. This interrupt can
be disabled by clearing the corresponding enable bit
INTxIE. Flag bit INTxIF must be cl eared in softwa re in
the Interrupt Service Routine before re-enabling the
interrupt. All external interrupts (INT0, INT1 and INT2)
can wake-up the proc essor from Sleep if b it INTxIE was
set prior to going into Sleep. If the Global Interrupt
Enable bit, GIE, is set, the processor will branch to the
interrupt vector following wake-up.
Interrupt priority for INT1 and INT2 is determ ined by the
value contained in the interrupt priority bits INT1IP
(INTCON3<6>) and INT2IP (INTCON3<7>). There is
no priority bi t ass ociate d with INT0; it is alwa ys a high
priority interrupt source.
8.7TMR0 Interrupt
In 8-bit mode (wh ich is the de fault), an ov erflow (FFh →
00h) in the TMR0 r egister wi ll set flag bit TMR0I F. 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 TMR0IE (INTCON register ). Interrupt pri ority
for Timer0 is determined by the value contained in the
interrupt priority bit TMR0IP (INTCON2 register). See
Section 11.0 “Timer0 Module” for further details.
8.8PORTB Interrupt-on-Change
An input change on PORTB<7:4> sets flag bit RBIF
(INTCON register). The interrupt can be enabled/
disabled by setting/clearing enable bit RBIE (INTCON
register). Interrupt priority for PORTB interrupt-onchange is determined by the value contained in the
interrupt priority bit RBIP (INTCON2 register).
8.9Context Saving During Interrupts
During an interrupt, the return PC v alue is sa ved on the
stack. Additionally, the WREG, Status and BSR
registers ar e saved on the fas t return stack. If a f ast
return from interrupt is not used (see Section 4.3 “FastRegister Stack”), the user may need to save the
WREG, St atus and BSR reg isters in softw are. De pending on the user’s application, other registers may also
need to be saved. Exam ple 8-1 saves and restores the
WREG, Status and BSR registers during an Interrupt
Service Routine.
EXAMPLE 8-1:SAVING STATUS, WREG AND BSR REGISTERS IN RAM
MOVWFW_TEMP; W_TEMP is in Low Access 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
DS41159D-page 92 2004 Microchip Technology Inc.
PIC18FXX8
9.0I/O PORTS
Depending on the device selected, there are up to five
general purpose I/O ports available on PIC18FXX8
devices. 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 ge neral purpose I/O pin.
Each port has three registers for its operation:
• TRIS register (Data Direction register)
• PORT register (rea ds the level s on the pins of the
device)
• LAT register (output latch)
The data latc h ( L AT register) is us ef u l f o r re a d- m od ify write operations on the value that the I/O pins are
driving.
9.1PORTA, TRISA and LATA
Registers
PORTA is a 7-bit wide, bidirectional port. The corresponding Data Direction register is TRISA. Setting a
TRISA bit (= 1) will make the correspondi ng PORT A pin
an input (i.e., put the corresponding output driver in a
high-impedance mode). Clearing a TRISA bit (= 0) will
make the correspondin g POR TA pin an output (i.e., put
the contents o f the output lat ch on the sel ected pin) . On
a Power-on Reset, these pins are configured 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.
Read-modify-write operations on the LATA register
read and write 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 Schmit t Trigger input and an open-drai n
output. All other RA port pins have TTL input levels an d
full CMOS output drivers.
The other PORTA pins are multiplexed with analog
inputs and the analog V
operation of each p in is sel ected by clearing /setting th e
control bits in the ADCON1 register (A/D Control
Register 1). On a Power-on Reset, these pins are
configured as analog inputs and read as ‘0’.
Note:On a Power-on Reset, RA5 and RA3:RA0
are configured as analog inputs and read
as ‘0’. RA6 and RA4 are configured as
digital inputs.
The TRISA register controls the direction of the RA
pins, even when they a re being used as analog inputs.
The user must ensure the bit s in the TRISA registe r are
maintained set, when using them as analog inputs.
REF+ and VREF- inputs. The
EXAMPLE 9-1:INITIA LIZING PORTA
CLRFPORTA ; Initialize PORTA by
CLRFLATA; Alternate method to clear
MOVLW 07h; Configure A/D
MOVWF ADCON1 ; for digital inputs
MOVLW 0CFh; Value used to initialize
MOVWF TRISA ; Set RA3:RA0 as inputs,
; clearing output data latches
; output data latches
; data direction
; RA5:RA4 as outputs
2004 Microchip Technology Inc.DS41159D-page 93
PIC18FXX8
t
FIGURE 9-1:RA3:RA0 AND RA5 PINS
BLOCK DIAGRAM
RD LATA
Data Bus
WR LATA
WR PORTA
WR TRISA
Analog
Input Mode
RD TRISA
RD PORTA
Note 1: I/O pins have diode protection to VDD and VSS.
or
SS Input (RA5 only)
To A/D Converter and LVD Modules
D
CK
Q
Data Latch
D
CK
TRIS Latch
Q
VDD
P
(1)
Q
Q
QD
EN
N
VSS
I/O pin
TTL
Input
Buffer
FIGURE 9-2:RA4/T0CKI PIN BLOCK
DIAGRAM
RD LATA
Data Bus
WR LATA or
WR PORTA
WR TRISA
RD TRISA
RD PORTA
TMR0 Clock Input
Note 1: I/O pin has diode protection to V
D
CK
Q
Data Latch
CK
Q
TRIS Latch
Q
QD
N
SS
V
TTL
Input
Buffer
QD
EN
SS only.
I/O pin
Schmit
Trigger
Input
Buffer
(1)
FIGURE 9-3:OSC2/CLKO/RA6 PIN BLOCK DIAGRAM
(FOSC = 101, 111)
CLKO (FOSC/4)
Data Bus
WR PORTA
WR TRISA
(FOSC = 100,
101, 110, 111)
RD TRISA
RD PORTA
OSC = 110, 100)
(F
Note 1: CLKO is 1/4 of FOSC.
2: I/O pin has diode protection to V
Data Latch
D
CK
TRIS Latch
D
CK
Q
Q
Q
Q
1
0
QD
Data Latch
DD and VSS.
From OSC1
EN
Oscillator
Circuit
VDD
P
N
SS
V
Schmitt
Trigger
Input Buffer
OSC2/CLKO
(2)
RA6 pin
DS41159D-page 94 2004 Microchip Technology Inc.
PIC18FXX8
TABLE 9-1:PORTA FUNCTIONS
NameBit#Buffer Function
RA0/AN0/CVREFbit 0TTLInput/output, analog input or analog comparator voltage reference
output.
RA1/AN1bit 1TTLInput/output or analog input.
RA2/AN2/V
RA3/AN3/VREF+bit 3TTLInput/output, analog input or VREF+.
RA4/T0CKIbit 4ST/OD Input/output, external clock input for Timer0, output is open-drain type.
RA5/AN4/SS/
OSC2/CLKO/RA6bit 6TTLOscillator clock output or input/output.
Legend: TTL = TTL input, ST = Schmitt Trigger input, OD = Open-Drain
REF-bit 2TTLInput/output, analog input or VREF-.
L VD I Nbit 5TTLInput/output, analog input, sl ave se lec t in pu t for s yn ch rono us se rial port
or Low-Voltage Detect input.
TABLE 9-2:SUMMARY OF REGISTERS ASSOCIATED WITH PORTA
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
PORTA
LATA—Latch A Data Output Register-xxx xxxx -uuu uuuu
TRISA—PORTA Data Direction Register-111 1111 -111 1111
ADCON1
Legend: x = unknown, u = unchanged, - = unimplemented locations read as ‘0’. Shaded cells are not used by PORTA.
PORTB is an 8-bit wide, bidirectional port. The corresponding Data Direction register is TRISB. Setting a
TRISB bit (= 1) will make the corresponding PORTB
pin an input (i.e., put the corres pondi ng outpu t drive r in
a high-impedance mode). Clearing a TRISB bit (= 0)
will make the c orrespond ing POR TB pi n an out put (i.e.,
put the contents of the outpu t latch on the selected pi n).
Read-modify-write operations on the LATB register,
read and write the latched output value for PORTB.
EXAMPLE 9-2:INITIALIZI NG PORTB
CLRFPORTB; Initialize PORTB by
; clearing output
; data latches
CLRFLATB; Alternate method
; to clear output
; data latches
MOVLW0CFh; Value used to
; initialize data
; direction
MOVWFTRISB; Set RB3:RB0 as inputs
; RB5:RB4 as outputs
; RB7:RB6 as inputs
Each of the PORTB pi ns has a w eak i nternal pul l-up. A
single cont rol bit can turn on a ll the pull-ups. This is
performed by clearing bit RBPU (INTCON2 register).
The weak pull-up is automatically turned off when the
port pin is c onfigured as an out put. The pull-ups are
disabled on a Power-on Reset.
Four of the PORTB pins (RB7:RB4) have an interrupton-change feature. Only pins configured as inputs can
cause this interrupt to occur (i.e., any RB7:RB4 pin
configured as an output is excluded from the interrupton-change comparison). The input pins (of RB7:RB4)
are compared with the old value latched on the last
read of PORTB. The “mismatch” outputs of RB7:RB4
are ORed together to generate the RB Port Change
Interrupt with Flag bit RBIF (INTCON register).
This interrupt can wake the device from Sleep. The
user, in 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 c ond it i on wi ll cont i n ue to s et f lag bi t RB IF.
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.
Note 1: While in Low-Voltage ICSP mode, the
RB5 pin can no longer be used as a
general purpose I/O pin and should not
be held low during normal operation to
protect against inadvertent ICSP mode
entry.
2: When using Low-Voltage ICSP Program-
ming (LVP), the pull-up on RB5 becomes
disabled. If TRISB bit 5 is cleared,
thereby setting RB5 as an output, LATB
bit 5 must also be cleared for proper
operation.
DS41159D-page 96 2004 Microchip Technology Inc.
PIC18FXX8
FIGURE 9-4:RB7:RB4 PINS BLOCK
DIAGRAM
DD
(2)
RBPU
Data Bus
WR LATB
or
WR PORTB
WR TRISB
RD TRISB
RD LATB
RD PORTB
Set RBIF
From other
RB7:RB4 pin s
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
bit(s) and clear the RBPU
Latch
QD
QD
bit (INTCON2 register).
V
TTL
Input
Buffer
EN
EN
DD and VSS.
P
Weak
Pull-up
I/O pin
Buffer
Q1
Q3
ST
FIGURE 9-5:RB1:RB0 PINS BLOCK
DIAGRAM
DD
(2)
RBPU
Data Bus
(1)
WR Port
WR TRIS
RD TRIS
RD Port
RBx/INTx
Note 1: I/O pins have diode protec tion to V
2: To enable weak pull-ups, set the appropriate TRIS
Data Latch
QD
CK
TRIS Latch
QD
CK
bit(s) and clear the RBPU
QD
Schmitt Trigger
Buffer
bit (INTCON2 register).
V
TTL
Input
Buffer
EN
DD and VSS.
P
Weak
Pull-up
I/O pin
(1)
2004 Microchip Technology Inc.DS41159D-page 97
PIC18FXX8
FIGURE 9-6:RB2/CANTX/INT2 PIN BLOCK DIAGRAM
OPMODE2:OPMODE0 = 000
ENDRHI
CANTX
RD LATB
Data Bus
WR PORTB or
WR LATB
WR TRISB
RD TRISB
RD PORTB
Note 1: I/O pin has diode protection to VDD and VSS.
Data Latch
D
Q
CK
Q
TRIS Latch
Q
D
Q
CK
0
1
FIGURE 9-7:RB3/CANRX PIN BLOCK DIAGRAM
CANCON<7:5>
(2)
RBPU
Data Bus
WR LATB or PORTB
WR TRISB
Data Latch
QD
CK
TRIS Latch
QD
CK
QD
EN
DD
V
Weak
P
Pull-up
I/O pin
VDD
P
N
V
(1)
SS
RB2/CANTX/
INT2 pin
Schmitt
Trigger
(1)
TTL
RD TRISB
RD LATB
RD PORTB
RB3 or CANRX
Schmitt Trigger
Buffer
Note 1: I/O pins have diode protection to VDD and VSS.
2: To enable weak pull-ups, set the appropriate TRIS bit(s) and clear the RBPU
DS41159D-page 98 2004 Microchip Technology Inc.
Input
Buffer
QD
EN
bit (INTCON2<7>).
Loading...
+ hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.