PIC18F2420/2520/4420/4520 Data SheetPIC18F2420/2520/4420/4520

412
© 2008 Microchip Technology Inc. DS39631E PIC18F2420/2520/4420/4520 Data Sheet 28/40/44-Pin Enhanced Flash Microcontrollers with 10-Bit A/D and nanoWatt Technology

Transcript of PIC18F2420/2520/4420/4520 Data SheetPIC18F2420/2520/4420/4520

  • © 2008 Microchip Technology Inc. DS39631E

    PIC18F2420/2520/4420/4520Data Sheet

    28/40/44-Pin Enhanced FlashMicrocontrollers with 10-Bit A/D

    and nanoWatt Technology

  • DS39631E-page ii © 2008 Microchip Technology Inc.

    Information contained in this publication regarding deviceapplications and the like is provided only for your convenienceand may be superseded by updates. It is your responsibility toensure that your application meets with your specifications.MICROCHIP MAKES NO REPRESENTATIONS ORWARRANTIES OF ANY KIND WHETHER EXPRESS ORIMPLIED, WRITTEN OR ORAL, STATUTORY OROTHERWISE, RELATED TO THE INFORMATION,INCLUDING BUT NOT LIMITED TO ITS CONDITION,QUALITY, PERFORMANCE, MERCHANTABILITY ORFITNESS FOR PURPOSE. Microchip disclaims all liabilityarising from this information and its use. Use of Microchipdevices in life support and/or safety applications is entirely atthe buyer’s risk, and the buyer agrees to defend, indemnify andhold harmless Microchip from any and all damages, claims,suits, or expenses resulting from such use. No licenses areconveyed, implicitly or otherwise, under any Microchipintellectual property rights.

    Trademarks

    The Microchip name and logo, the Microchip logo, Accuron, dsPIC, KEELOQ, KEELOQ logo, MPLAB, PIC, PICmicro, PICSTART, rfPIC, SmartShunt and UNI/O are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.

    FilterLab, Linear Active Thermistor, MXDEV, MXLAB, SEEVAL, SmartSensor and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A.

    Analog-for-the-Digital Age, Application Maestro, CodeGuard, dsPICDEM, dsPICDEM.net, dsPICworks, dsSPEAK, ECAN, ECONOMONITOR, FanSense, In-Circuit Serial Programming, ICSP, ICEPIC, Mindi, MiWi, MPASM, MPLAB Certified logo, MPLIB, MPLINK, mTouch, PICkit, PICDEM, PICDEM.net, PICtail, PIC32 logo, PowerCal, PowerInfo, PowerMate, PowerTool, REAL ICE, rfLAB, Select Mode, Total Endurance, WiperLock and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.

    SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.

    All other trademarks mentioned herein are property of their respective companies.

    © 2008, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.

    Printed on recycled paper.

    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 ourproducts. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such actsallow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.

    Microchip received ISO/TS-16949:2002 certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California and India. The Company’s quality system processes and procedures are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchip’s quality system for the design and manufacture of development systems is ISO 9001:2000 certified.

  • © 2008 Microchip Technology Inc. DS39631E-page 1

    PIC18F2420/2520/4420/4520

    Power Management Features:• Run: CPU on, Peripherals on• Idle: CPU off, Peripherals on• Sleep: CPU off, Peripherals off• Ultra Low 50nA Input Leakage• Run mode Currents Down to 11 μA Typical• Idle mode Currents Down to 2.5 μA Typical• Sleep mode Current Down to 100 nA Typical• Timer1 Oscillator: 900 nA, 32 kHz, 2V• Watchdog Timer: 1.4 μA, 2V Typical• Two-Speed Oscillator Start-up

    Flexible Oscillator Structure:• Four Crystal modes, up to 40 MHz• 4x Phase Lock Loop (PLL) – Available for Crystal

    and Internal Oscillators• Two External RC modes, up to 4 MHz• Two External Clock modes, up to 40 MHz• Internal Oscillator Block:

    - Fast wake from Sleep and Idle, 1 μs typical- 8 use-selectable frequencies, from 31 kHz to

    8 MHz- Provides a complete range of clock speeds

    from 31 kHz to 32 MHz when used with PLL- User-tunable to compensate for frequency drift

    • Secondary Oscillator using Timer1 @ 32 kHz• Fail-Safe Clock Monitor:

    - Allows for safe shutdown if peripheral clock stops

    Peripheral Highlights:• High-Current Sink/Source 25 mA/25 mA• Three Programmable External Interrupts• Four Input Change Interrupts• Up to 2 Capture/Compare/PWM (CCP) modules,

    one with Auto-Shutdown (28-pin devices)• Enhanced Capture/Compare/PWM (ECCP)

    module (40/44-pin devices only):- One, two or four PWM outputs- Selectable polarity- Programmable dead time- Auto-shutdown and auto-restart

    Peripheral Highlights (Continued):• Master Synchronous Serial Port (MSSP) module

    Supporting 3-Wire SPI (all 4 modes) and I2C™ Master and Slave modes

    • Enhanced Addressable USART module:- Supports RS-485, RS-232 and LIN/J2602- RS-232 operation using internal oscillator

    block (no external crystal required)- Auto-wake-up on Start bit- Auto-Baud Detect

    • 10-Bit, up to 13-Channel Analog-to-Digital (A/D)Converter module:- Auto-acquisition capability- Conversion available during Sleep

    • Dual Analog Comparators with Input Multiplexing• Programmable 16-Level High/Low-Voltage

    Detection (HLVD) module:- Supports interrupt on High/Low-Voltage Detection

    Special Microcontroller Features:• C Compiler Optimized Architecture:

    - Optional extended instruction set designed to optimize re-entrant code

    • 100,000 Erase/Write Cycle Enhanced Flash Program Memory Typical

    • 1,000,000 Erase/Write Cycle Data EEPROM Memory Typical

    • Flash/Data EEPROM Retention: 100 Years Typical• Self-Programmable under Software Control• Priority Levels for Interrupts• 8 x 8 Single-Cycle Hardware Multiplier• Extended Watchdog Timer (WDT):

    - Programmable period from 4 ms to 131s• Single-Supply 5V In-Circuit Serial

    Programming™ (ICSP™) via Two Pins• In-Circuit Debug (ICD) via Two Pins• Wide Operating Voltage Range: 2.0V to 5.5V• Programmable Brown-out Reset (BOR) with

    Software Enable Option-

    DeviceProgram Memory Data Memory

    I/O 10-BitA/D (ch)

    CCP/ECCP(PWM)

    MSSP

    EUSA

    RT

    Comp. Timers8/16-BitFlash(bytes)

    # Single-WordInstructions

    SRAM(bytes)

    EEPROM(bytes) SPI

    MasterI2C™

    PIC18F2420 16K 8192 768 256 25 10 2/0 Y Y 1 2 1/3PIC18F2520 32K 16384 1536 256 25 10 2/0 Y Y 1 2 1/3PIC18F4420 16K 8192 768 256 36 13 1/1 Y Y 1 2 1/3PIC18F4520 32K 16384 1536 256 36 13 1/1 Y Y 1 2 1/3

    28/40/44-Pin Enhanced Flash Microcontrollers with10-Bit A/D and nanoWatt Technology

  • PIC18F2420/2520/4420/4520

    DS39631E-page 2 © 2008 Microchip Technology Inc.

    Pin Diagrams

    PIC

    18F2

    520

    1011

    23456

    1

    87

    9

    121314 15

    1617181920

    232425262728

    2221

    MCLR/VPP/RE3RA0/AN0RA1/AN1

    RA2/AN2/VREF-/CVREFRA3/AN3/VREF+

    RA4/T0CKI/C1OUTRA5/AN4/SS/HLVDIN/C2OUT

    VSSOSC1/CLKI/RA7

    OSC2/CLKO/RA6RC0/T1OSO/T13CKIRC1/T1OSI/CCP2(1)

    RC2/CCP1RC3/SCK/SCL

    RB7/KBI3/PGDRB6//KBI2/PGCRB5/KBI1/PGMRB4/KBI0/AN11RB3/AN9/CCP2(1)

    RB2/INT2/AN8RB1/INT1/AN10RB0/INT0/FLT0/AN12VDDVSSRC7/RX/DTRC6/TX/CKRC5/SDORC4/SDI/SDA

    28-Pin SPDIP, SOIC

    PIC

    18F2

    420

    Note 1: RB3 is the alternate pin for CCP2 multiplexing.

    10 11

    23

    6

    1

    18192021

    22

    12 13 1415

    87

    1617

    232425262728

    9

    PIC18F2420

    RC

    0/T1

    OSO

    /T13

    CK

    I

    54

    RB

    7/KB

    I3/P

    GD

    RB

    6/KB

    I2/P

    GC

    RB

    5/KB

    I1/P

    GM

    RB

    4KB

    I0/A

    N11

    RB3/AN9/CCP2(1)RB2/INT2/AN8RB1/INT1/AN10RB0/INT0/FLT0/AN12VDDVSSRC7/RX/DT

    RC

    6/TX

    /CK

    RC

    5/S

    DO

    RC

    4/SD

    I/SD

    A

    MC

    LR/V

    PP/R

    E3

    RA

    0/A

    N0

    RA

    1/A

    N1

    RA2/AN2/VREF-/CVREFRA3/AN3/VREF+

    RA4/T0CKI/C1OUTRA5/AN4/SS/HLVDIN/C2OUT

    VSSOSC1/CLKI/RA7

    OSC2/CLKO/RA6

    RC

    1/T1

    OS

    I/CC

    P2(1

    )

    RC

    2/C

    CP

    1R

    C3/

    SC

    K/S

    CL

    PIC18F2520

    28-Pin QFN

    RB7/KBI3/PGDRB6/KBI2/PGCRB5/KBI1/PGMRB4/KBI0/AN11RB3/AN9/CCP2(1)

    RB2/INT2/AN8RB1/INT1/AN10RB0/INT0/FLT0/AN12VDDVSSRD7/PSP7/P1DRD6/PSP6/P1CRD5/PSP5/P1BRD4/PSP4RC7/RX/DTRC6/TX/CKRC5/SDORC4/SDI/SDARD3/PSP3RD2/PSP2

    MCLR/VPP/RE3RA0/AN0RA1/AN1

    RA2/AN2/VREF-/CVREFRA3/AN3/VREF+

    RA4/T0CKI/C1OUTRA5/AN4/SS/HLVDIN/C2OUT

    RE0/RD/AN5RE1/WR/AN6RE2/CS/AN7

    VDDVSS

    OSC1/CLKI/RA7OSC2/CLKO/RA6

    RC0/T1OSO/T13CKIRC1/T1OSI/CCP2(1)

    RC2/CCP1/P1ARC3/SCK/SCL

    RD0/PSP0RD1/PSP1

    1234567891011121314151617181920

    4039383736353433323130292827262524232221

    PIC

    18F4

    520

    40-Pin PDIP

    PIC

    18F4

    420

  • © 2008 Microchip Technology Inc. DS39631E-page 3

    PIC18F2420/2520/4420/4520Pin Diagrams (Cont.’d)

    Note 1: RB3 is the alternate pin for CCP2 multiplexing.

    1011

    23456

    1

    18 19 20 21 2212 13 14 15

    38

    87

    44 43 42 41 40 3916 17

    2930313233

    232425262728

    36 3435

    9

    PIC18F4420

    37

    RA3

    /AN

    3/V R

    EF+

    RA2

    /AN

    2/V R

    EF-/C

    VREF

    RA

    1/A

    N1

    RA

    0/A

    N0

    MC

    LR/V

    PP/R

    E3

    RB

    3/A

    N9/

    CC

    P2(

    1)

    RB

    7/K

    BI3/

    PG

    DR

    B6/

    KBI

    2/P

    GC

    RB

    5/K

    BI1

    /PG

    MR

    B4/

    KBI0

    /AN

    11NC

    RC

    6/TX

    /CK

    RC

    5/S

    DO

    RC

    4/S

    DI/S

    DA

    RD

    3/P

    SP3

    RD

    2/P

    SP2

    RD

    1/P

    SP1

    RD

    0/P

    SP0

    RC

    3/S

    CK/

    SCL

    RC

    2/C

    CP

    1/P

    1AR

    C1/

    T1O

    SI/C

    CP

    2(1)

    RC

    0/T1

    OSO

    /T13

    CKI

    OSC2/CLKO/RA6OSC1/CLKI/RA7VSSVSSVDDVDDRE2/CS/AN7RE1/WR/AN6RE0/RD/AN5RA5/AN4/SS/HLVDIN/C2OUTRA4/T0CKI/C1OUT

    RC7/RX/DTRD4/PSP4

    RD5/PSP5/P1BRD6/PSP6/P1CRD7/PSP7/P1D

    VSSVDDVDD

    RB0/INT0/FLT0/AN12RB1/INT1/AN10RB2/INT2/AN8

    44-pin QFN

    PIC18F4520

    1011

    23456

    1

    18 19 20 21 2212 13 14 15

    38

    87

    44 43 42 41 40 3916 17

    2930313233

    232425262728

    36 3435

    9

    PIC18F4420

    37

    RA

    3/A

    N3/

    VRE

    F+R

    A2/A

    N2/

    V REF

    -/CVR

    EFR

    A1/

    AN

    1R

    A0/

    AN

    0M

    CLR

    /VP

    P/R

    E3

    NC

    RB

    7/K

    BI3

    /PG

    DR

    B6/

    KB

    I2/P

    GC

    RB5

    /KB

    I1/P

    GM

    RB

    4/K

    BI0

    /AN

    11NC

    RC

    6/TX

    /CK

    RC

    5/S

    DO

    RC

    4/S

    DI/S

    DA

    RD

    3/P

    SP3

    RD

    2/P

    SP2

    RD

    1/P

    SP1

    RD

    0/P

    SP0

    RC

    3/S

    CK

    /SC

    LR

    C2/

    CC

    P1/

    P1A

    RC

    1/T1

    OSI

    /CC

    P2(

    1)

    NC

    NCRC0/T1OSO/T13CKIOSC2/CLKO/RA6OSC1/CLKI/RA7VSSVDDRE2/CS/AN7RE1/WR/AN6RE0/RD/AN5RA5/AN4/SS/HLVDIN/C2OUTRA4/T0CKI/C1OUT

    RC7/RX/DTRD4/PSP4

    RD5/PSP5/P1BRD6/PSP6/P1CRD7/PSP7/P1D

    VSSVDD

    RB0/INT0/FLT0/AN12RB1/INT1/AN10

    RB2/INT2/AN8RB3/AN9/CCP2(1)

    44-pin TQFP

    PIC18F4520

  • PIC18F2420/2520/4420/4520

    DS39631E-page 4 © 2008 Microchip Technology Inc.

    Table of Contents1.0 Device Overview .......................................................................................................................................................................... 72.0 Oscillator Configurations ............................................................................................................................................................ 233.0 Power-Managed Modes ............................................................................................................................................................. 334.0 Reset .......................................................................................................................................................................................... 415.0 Memory Organization ................................................................................................................................................................. 536.0 Flash Program Memory.............................................................................................................................................................. 737.0 Data EEPROM Memory ............................................................................................................................................................. 838.0 8 x 8 Hardware Multiplier............................................................................................................................................................ 899.0 Interrupts .................................................................................................................................................................................... 9110.0 I/O Ports ................................................................................................................................................................................... 10511.0 Timer0 Module ......................................................................................................................................................................... 12312.0 Timer1 Module ......................................................................................................................................................................... 12713.0 Timer2 Module ......................................................................................................................................................................... 13314.0 Timer3 Module ......................................................................................................................................................................... 13515.0 Capture/Compare/PWM (CCP) Modules ................................................................................................................................. 13916.0 Enhanced Capture/Compare/PWM (ECCP) Module................................................................................................................ 14717.0 Master Synchronous Serial Port (MSSP) Module .................................................................................................................... 16118.0 Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) ............................................................... 20119.0 10-Bit Analog-to-Digital Converter (A/D) Module ..................................................................................................................... 22320.0 Comparator Module.................................................................................................................................................................. 23321.0 Comparator Voltage Reference Module................................................................................................................................... 23922.0 High/Low-Voltage Detect (HLVD)............................................................................................................................................. 24323.0 Special Features of the CPU.................................................................................................................................................... 24924.0 Instruction Set Summary .......................................................................................................................................................... 26725.0 Development Support............................................................................................................................................................... 31726.0 Electrical Characteristics .......................................................................................................................................................... 32127.0 DC and AC Characteristics Graphs and Tables....................................................................................................................... 36128.0 Packaging Information.............................................................................................................................................................. 383Appendix A: Revision History............................................................................................................................................................. 395Appendix B: Device Differences......................................................................................................................................................... 395Appendix C: Migration from Mid-Range to Enhanced Devices .......................................................................................................... 396Appendix D: Migration from High-End to Enhanced Devices............................................................................................................. 396Index .................................................................................................................................................................................................. 397The Microchip Web Site ..................................................................................................................................................................... 407Customer Change Notification Service .............................................................................................................................................. 407Customer Support .............................................................................................................................................................................. 407Reader Response .............................................................................................................................................................................. 408PIC18F2420/2520/4420/4520 Product Identification System ............................................................................................................ 409

  • © 2008 Microchip Technology Inc. DS39631E-page 5

    PIC18F2420/2520/4420/4520

    TO OUR VALUED CUSTOMERSIt is our intention to provide our valued customers with the best documentation possible to ensure successful use of your Microchipproducts. To this end, we will continue to improve our publications to better suit your needs. Our publications will be refined andenhanced as new volumes and updates are introduced. If you have any questions or comments regarding this publication, please contact the Marketing Communications Department viaE-mail at [email protected] or fax the Reader Response Form in the back of this data sheet to (480) 792-4150. Wewelcome your feedback.

    Most Current Data SheetTo obtain the most up-to-date version of this data sheet, please register at our Worldwide Web site at:

    http://www.microchip.comYou 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).

    ErrataAn errata sheet, describing minor operational differences from the data sheet and recommended workarounds, may exist for currentdevices. As device/documentation issues become known to us, we will publish an errata sheet. The errata will specify the revisionof 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 areusing.

    Customer Notification SystemRegister on our web site at www.microchip.com to receive the most current information on all of our products.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 6 © 2008 Microchip Technology Inc.

    NOTES:

  • © 2008 Microchip Technology Inc. DS39631E-page 7

    PIC18F2420/2520/4420/4520

    1.0 DEVICE OVERVIEWThis document contains device-specific information forthe following devices:

    This family offers the advantages of all PIC18microcontrollers – namely, high computational perfor-mance at an economical price – with the addition ofhigh-endurance, Enhanced Flash program memory.On top of these features, the PIC18F2420/2520/4420/4520 family introduces design enhancements thatmake these microcontrollers a logical choice for manyhigh-performance, power sensitive applications.

    1.1 New Core Features

    1.1.1 nanoWatt TECHNOLOGYAll of the devices in the PIC18F2420/2520/4420/4520family incorporate a range of features that can signifi-cantly reduce power consumption during operation.Key items include:

    • Alternate Run Modes: By clocking the controller from the Timer1 source or the internal oscillator block, power consumption during code execution can be reduced by as much as 90%.

    • Multiple Idle Modes: The controller can also run with its CPU core disabled but the peripherals still active. In these states, power consumption can be reduced even further, to as little as 4% of normal operation requirements.

    • On-the-Fly Mode Switching: The power-managed modes are invoked by user code during operation, allowing the user to incorporate power-saving ideas into their application’s software design.

    • Low Consumption in Key Modules: The power requirements for both Timer1 and the Watchdog Timer are minimized. See Section 26.0 “Electrical Characteristics” for values.

    1.1.2 MULTIPLE OSCILLATOR OPTIONS AND FEATURES

    All of the devices in the PIC18F2420/2520/4420/4520family offer ten different oscillator options, allowingusers a wide range of choices in developing applicationhardware. These include:

    • Four Crystal modes, using crystals or ceramic resonators

    • Two External Clock modes, offering the option of using two pins (oscillator input and a divide-by-4 clock output) or one pin (oscillator input, with the second pin reassigned as general I/O)

    • Two External RC Oscillator modes with the same pin options as the External Clock modes

    • An internal oscillator block which provides an 8 MHz clock and an INTRC source (approximately 31 kHz), as well as a range of 6 user-selectable clock frequencies, between 125 kHz to 4 MHz, for a total of 8 clock frequencies. This option frees the two oscillator pins for use as additional general purpose I/O.

    • A Phase Lock Loop (PLL) frequency multiplier, available to both the High-Speed Crystal and Inter-nal Oscillator modes, which allows clock speeds of up to 40 MHz. Used with the internal oscillator, the PLL gives users a complete selection of clock speeds, from 31 kHz to 32 MHz – all without using an external crystal or clock circuit.

    Besides its availability as a clock source, the internaloscillator block provides a stable reference source thatgives the family additional features for robustoperation:

    • Fail-Safe Clock Monitor: This option constantly monitors the main clock source against a refer-ence signal provided by the internal oscillator. If a clock failure occurs, the controller is switched to the internal oscillator block, allowing for continued low-speed operation or a safe application shutdown.

    • Two-Speed Start-up: This option allows the internal oscillator to serve as the clock source from Power-on Reset, or wake-up from Sleep mode, until the primary clock source is available.

    • PIC18F2420 • PIC18LF2420• PIC18F2520 • PIC18LF2520• PIC18F4420 • PIC18LF4420• PIC18F4520 • PIC18LF4520

  • PIC18F2420/2520/4420/4520

    DS39631E-page 8 © 2008 Microchip Technology Inc.

    1.2 Other Special Features• Memory Endurance: The Enhanced Flash cells

    for both program memory and data EEPROM are rated to last for many thousands of erase/write cycles – up to 100,000 for program memory and 1,000,000 for EEPROM. Data retention without refresh is conservatively estimated to be greater than 40 years.

    • Self-Programmability: These devices can write to their own program memory spaces under internal software control. By using a bootloader routine located in the protected Boot Block at the top of program memory, it becomes possible to create an application that can update itself in the field.

    • Extended Instruction Set: The PIC18F2420/2520/4420/4520 family introduces an optional extension to the PIC18 instruction set, which adds 8 new instructions and an Indexed Addressing mode. This extension, enabled as a device con-figuration option, has been specifically designed to optimize re-entrant application code originally developed in high-level languages, such as C.

    • Enhanced CCP Module: In PWM mode, this module provides 1, 2 or 4 modulated outputs for controlling half-bridge and full-bridge drivers. Other features include auto-shutdown, for dis-abling PWM outputs on interrupt, or other select conditions, and auto-restart to reactivate outputs once the condition has cleared.

    • Enhanced Addressable USART: This serial communication module is capable of standard RS-232 operation and provides support for the LIN bus protocol. Other enhancements include automatic baud rate detection and a 16-bit Baud Rate Generator for improved resolution. When the microcontroller is using the internal oscillator block, the EUSART provides stable operation for applications that talk to the outside world without using an external crystal (or its accompanying power requirement).

    • 10-Bit A/D Converter: This module incorporates programmable acquisition time, allowing for a channel to be selected and a conversion to be initiated without waiting for a sampling period and thus, reducing code overhead.

    • Extended Watchdog Timer (WDT): This enhanced version incorporates a 16-bit prescaler, allowing an extended time-out range that is stable across operating voltage and temperature. See Section 26.0 “Electrical Characteristics” for time-out periods.

    1.3 Details on Individual Family Members

    Devices in the PIC18F2420/2520/4420/4520 family areavailable in 28-pin and 40/44-pin packages. Blockdiagrams for the two groups are shown in Figure 1-1and Figure 1-2.

    The devices are differentiated from each other in fiveways:

    1. Flash program memory (16 Kbytes forPIC18F2420/4420 devices and 32 Kbytes forPIC18F2520/4520 devices).

    2. A/D channels (10 for 28-pin devices, 13 for40/44-pin devices).

    3. I/O ports (3 bidirectional ports on 28-pin devices,5 bidirectional ports on 40/44-pin devices).

    4. CCP and Enhanced CCP implementation(28-pin devices have 2 standard CCPmodules, 40/44-pin devices have one standardCCP module and one ECCP module).

    5. Parallel Slave Port (present only on 40/44-pindevices).

    All other features for devices in this family are identical.These are summarized in Table 1-1.

    The pinouts for all devices are listed in Table 1-2 andTable 1-3.

    Like all Microchip PIC18 devices, members of thePIC18F2420/2520/4420/4520 family are available asboth standard and low-voltage devices. Standarddevices with Enhanced Flash memory, designated withan “F” in the part number (such as PIC18F2420),accommodate an operating VDD range of 4.2V to 5.5V.Low-voltage parts, designated by “LF” (such asPIC18LF2420), function over an extended VDD rangeof 2.0V to 5.5V.

  • © 2008 Microchip Technology Inc. DS39631E-page 9

    PIC18F2420/2520/4420/4520TABLE 1-1: DEVICE FEATURES

    Features PIC18F2420 PIC18F2520 PIC18F4420 PIC18F4520

    Operating Frequency DC – 40 MHz DC – 40 MHz DC – 40 MHz DC – 40 MHz

    Program Memory (Bytes) 16384 32768 16384 32768

    Program Memory (Instructions)

    8192 16384 8192 16384

    Data Memory (Bytes) 768 1536 768 1536

    Data EEPROM Memory (Bytes) 256 256 256 256

    Interrupt Sources 19 19 20 20

    I/O Ports Ports A, B, C, (E) Ports A, B, C, (E) Ports A, B, C, D, E Ports A, B, C, D, E

    Timers 4 4 4 4

    Capture/Compare/PWM Modules 2 2 1 1

    Enhanced Capture/Compare/PWM Modules

    0 0 1 1

    Serial Communications MSSP, Enhanced USART

    MSSP, Enhanced USART

    MSSP, Enhanced USART

    MSSP, Enhanced USART

    Parallel Communications (PSP) No No Yes Yes

    10-Bit Analog-to-Digital Module 10 Input Channels 10 Input Channels 13 Input Channels 13 Input Channels

    Resets (and Delays) POR, BOR, RESET Instruction, Stack Full, Stack

    Underflow (PWRT, OST), MCLR (optional), WDT

    POR, BOR, RESET Instruction, Stack Full, Stack

    Underflow (PWRT, OST), MCLR (optional), WDT

    POR, BOR, RESET Instruction, Stack Full, Stack

    Underflow (PWRT, OST), MCLR (optional), WDT

    POR, BOR, RESET Instruction, Stack Full, Stack

    Underflow (PWRT, OST), MCLR (optional), WDT

    Programmable High/Low-Voltage Detect

    Yes Yes Yes Yes

    Programmable Brown-out Reset Yes Yes Yes Yes

    Instruction Set 75 Instructions; 83 with Extended

    Instruction Set Enabled

    75 Instructions; 83 with Extended

    Instruction Set Enabled

    75 Instructions; 83 with Extended

    Instruction Set Enabled

    75 Instructions; 83 with Extended

    Instruction Set Enabled

    Packages 28-Pin SPDIP28-Pin SOIC28-Pin QFN

    28-Pin SPDIP28-Pin SOIC28-Pin QFN

    40-Pin PDIP44-Pin QFN44-Pin TQFP

    40-Pin PDIP44-Pin QFN44-Pin TQFP

  • PIC18F2420/2520/4420/4520

    DS39631E-page 10 © 2008 Microchip Technology Inc.

    FIGURE 1-1: PIC18F2420/2520 (28-PIN) BLOCK DIAGRAM

    InstructionDecode and

    Control

    PORTA

    PORTB

    PORTC

    RA4/T0CKI/C1OUTRA5/AN4/SS/HLVDIN/C2OUT

    RB0/INT0/FLT0/AN12

    RC0/T1OSO/T13CKIRC1/T1OSI/CCP2(1)RC2/CCP1RC3/SCK/SCLRC4/SDI/SDARC5/SDORC6/TX/CKRC7/RX/DT

    RA3/AN3/VREF+RA2/AN2/VREF-/CVREFRA1/AN1RA0/AN0

    RB1/INT1/AN10

    Data Latch

    Data Memory( 3.9 Kbytes )

    Address Latch

    Data Address12

    AccessBSR FSR0FSR1FSR2

    inc/declogic

    Address

    4 12 4

    PCH PCL

    PCLATH

    8

    31-Level Stack

    Program Counter

    PRODLPRODH

    8 x 8 Multiply

    8

    BITOP88

    ALU

    Address Latch

    Program Memory(16/32 Kbytes)

    Data Latch

    20

    8

    8

    Table Pointer

    inc/dec logic

    21

    8

    Data Bus

    Table Latch8

    IR

    12

    3

    ROM Latch

    RB2/INT2/AN8RB3/AN9/CCP2(1)

    PCLATU

    PCU

    OSC2/CLKO(3)/RA6

    Note 1: CCP2 is multiplexed with RC1 when Configuration bit, CCP2MX, is set, or RB3 when CCP2MX is not set.2: RE3 is only available when MCLR functionality is disabled.3: OSC1/CLKI and OSC2/CLKO are only available in select oscillator modes and when these pins are not being used as digital I/O.

    Refer to Section 2.0 “Oscillator Configurations” for additional information.

    RB4/KBI0/AN11RB5/KBI1/PGMRB6/KBI2/PGCRB7/KBI3/PGD

    EUSARTComparator MSSP 10-Bit ADC

    Timer2Timer1 Timer3Timer0

    CCP2

    HLVD

    CCP1

    BOR DataEEPROM

    W

    Instruction Bus

    STKPTR Bank

    8

    State MachineControl Signals

    Decode

    8

    8Power-up

    TimerOscillator

    Start-up TimerPower-on

    ResetWatchdog

    Timer

    OSC1(3)

    OSC2(3)

    VDD,

    Brown-outReset

    InternalOscillator

    Fail-SafeClock Monitor

    Precision

    ReferenceBand Gap

    VSS

    MCLR(2)

    Block

    INTRCOscillator

    8 MHzOscillator

    Single-SupplyProgramming

    In-CircuitDebugger

    T1OSO

    OSC1/CLKI(3)/RA7

    T1OSI

    PORTE

    MCLR/VPP/RE3(2)

  • © 2008 Microchip Technology Inc. DS39631E-page 11

    PIC18F2420/2520/4420/4520FIGURE 1-2: PIC18F4420/4520 (40/44-PIN) BLOCK DIAGRAM

    InstructionDecode and

    Control

    Data Latch

    Data Memory( 3.9 Kbytes )

    Address Latch

    Data Address12

    AccessBSR FSR0FSR1FSR2

    inc/declogic

    Address

    4 12 4

    PCH PCL

    PCLATH

    8

    31-Level Stack

    Program Counter

    PRODLPRODH

    8 x 8 Multiply

    8

    BITOP88

    ALU

    Address Latch

    Program Memory(16/32 Kbytes)

    Data Latch

    20

    8

    8

    Table Pointer

    inc/dec logic

    21

    8

    Data Bus

    Table Latch8

    IR

    12

    3

    ROM Latch

    PORTD

    RD0/PSP0

    PCLATU

    PCU

    PORTE

    MCLR/VPP/RE3(2)RE2/CS/AN7

    RE0/RD/AN5RE1/WR/AN6

    Note 1: CCP2 is multiplexed with RC1 when Configuration bit, CCP2MX, is set, or RB3 when CCP2MX is not set.2: RE3 is only available when MCLR functionality is disabled.3: OSC1/CLKI and OSC2/CLKO are only available in select oscillator modes and when these pins are not being used as digital I/O.

    Refer to Section 2.0 “Oscillator Configurations” for additional information.

    :RD4/PSP4

    EUSARTComparator MSSP 10-Bit ADC

    Timer2Timer1 Timer3Timer0

    CCP2

    HLVD

    ECCP1

    BOR DataEEPROM

    W

    Instruction Bus

    STKPTR Bank

    8

    State MachineControl Signals

    Decode

    8

    8Power-up

    TimerOscillator

    Start-up TimerPower-on

    ResetWatchdog

    Timer

    OSC1(3)

    OSC2(3)

    VDD,

    Brown-outReset

    InternalOscillator

    Fail-SafeClock Monitor

    Precision

    ReferenceBand Gap

    VSS

    MCLR(2)

    Block

    INTRCOscillator

    8 MHzOscillator

    Single-SupplyProgramming

    In-CircuitDebugger

    T1OSI

    T1OSO

    RD5/PSP5/P1BRD6/PSP6/P1CRD7/PSP7/P1D

    PORTA

    PORTB

    PORTC

    RA4/T0CKI/C1OUTRA5/AN4/SS/HLVDIN/C2OUT

    RB0/INT0/FLT0/AN12

    RC0/T1OSO/T13CKIRC1/T1OSI/CCP2(1)RC2/CCP1/P1ARC3/SCK/SCLRC4/SDI/SDARC5/SDORC6/TX/CKRC7/RX/DT

    RA3/AN3/VREF+RA2/AN2/VREF-/CVREFRA1/AN1RA0/AN0

    RB1/INT1/AN10RB2/INT2/AN8RB3/AN9/CCP2(1)

    OSC2/CLKO(3)/RA6

    RB4/KBI0/AN11RB5/KBI1/PGMRB6/KBI2/PGCRB7/KBI3/PGD

    OSC1/CLKI(3)/RA7

  • PIC18F2420/2520/4420/4520

    DS39631E-page 12 © 2008 Microchip Technology Inc.

    TABLE 1-2: PIC18F2420/2520 PINOUT I/O DESCRIPTIONS

    Pin NamePin Number

    PinType

    BufferType DescriptionSPDIP,

    SOIC QFN

    MCLR/VPP/RE3MCLR

    VPPRE3

    1 26I

    PI

    ST

    ST

    Master Clear (input) or programming voltage (input).Master Clear (Reset) input. This pin is an active-low Reset to the device.Programming voltage input.Digital input.

    OSC1/CLKI/RA7OSC1

    CLKI

    RA7

    9 6I

    I

    I/O

    ST

    CMOS

    TTL

    Oscillator crystal or external clock input.Oscillator crystal input or external clock source input. ST buffer when configured in RC mode; CMOS otherwise.External clock source input. Always associated with pin function, OSC1. (See related OSC1/CLKI, OSC2/CLKOpins.)General purpose I/O pin.

    OSC2/CLKO/RA6OSC2

    CLKO

    RA6

    10 7O

    O

    I/O

    TTL

    Oscillator crystal or clock output.Oscillator crystal output. Connects to crystal or resonator in Crystal Oscillator mode.In RC mode, OSC2 pin outputs CLKO which has 1/4 the frequency of OSC1 and denotes the instruction cycle rate. General purpose I/O pin.

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • © 2008 Microchip Technology Inc. DS39631E-page 13

    PIC18F2420/2520/4420/4520

    PORTA is a bidirectional I/O port.RA0/AN0

    RA0AN0

    2 27I/OI

    TTLAnalog

    Digital I/O.Analog input 0.

    RA1/AN1RA1AN1

    3 28I/OI

    TTLAnalog

    Digital I/O.Analog input 1.

    RA2/AN2/VREF-/CVREFRA2AN2VREF-CVREF

    4 1I/OIIO

    TTLAnalogAnalogAnalog

    Digital I/O.Analog input 2.A/D reference voltage (low) input.Comparator reference voltage output.

    RA3/AN3/VREF+RA3AN3VREF+

    5 2I/OII

    TTLAnalogAnalog

    Digital I/O.Analog input 3.A/D reference voltage (high) input.

    RA4/T0CKI/C1OUTRA4T0CKIC1OUT

    6 3I/OIO

    STST—

    Digital I/O. Timer0 external clock input.Comparator 1 output.

    RA5/AN4/SS/HLVDIN/C2OUT

    RA5AN4SSHLVDINC2OUT

    7 4

    I/OIIIO

    TTLAnalog

    TTLAnalog

    Digital I/O.Analog input 4. SPI slave select input.High/Low-Voltage Detect input.Comparator 2 output.

    RA6 See the OSC2/CLKO/RA6 pin.

    RA7 See the OSC1/CLKI/RA7 pin.

    TABLE 1-2: PIC18F2420/2520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number

    PinType

    BufferType DescriptionSPDIP,

    SOIC QFN

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 14 © 2008 Microchip Technology Inc.

    PORTB is a bidirectional I/O port. PORTB can be software programmed for internal weak pull-ups on all inputs.

    RB0/INT0/FLT0/AN12RB0INT0FLT0AN12

    21 18I/OIII

    TTLSTST

    Analog

    Digital I/O. External interrupt 0.PWM Fault input for CCP1.Analog input 12.

    RB1/INT1/AN10RB1INT1AN10

    22 19I/OII

    TTLST

    Analog

    Digital I/O.External interrupt 1.Analog input 10.

    RB2/INT2/AN8RB2INT2AN8

    23 20I/OII

    TTLST

    Analog

    Digital I/O.External interrupt 2.Analog input 8.

    RB3/AN9/CCP2RB3AN9CCP2(1)

    24 21I/OI

    I/O

    TTLAnalog

    ST

    Digital I/O.Analog input 9. Capture 2 input/Compare 2 output/PWM2 output.

    RB4/KBI0/AN11RB4KBI0AN11

    25 22I/OII

    TTLTTL

    Analog

    Digital I/O. Interrupt-on-change pin.Analog input 11.

    RB5/KBI1/PGMRB5KBI1PGM

    26 23I/OI

    I/O

    TTLTTLST

    Digital I/O.Interrupt-on-change pin.Low-Voltage ICSP™ Programming enable pin.

    RB6/KBI2/PGCRB6KBI2PGC

    27 24I/OI

    I/O

    TTLTTLST

    Digital I/O.Interrupt-on-change pin. In-Circuit Debugger and ICSP programming clock pin.

    RB7/KBI3/PGDRB7KBI3PGD

    28 25I/OI

    I/O

    TTLTTLST

    Digital I/O.Interrupt-on-change pin. In-Circuit Debugger and ICSP programming data pin.

    TABLE 1-2: PIC18F2420/2520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number

    PinType

    BufferType DescriptionSPDIP,

    SOIC QFN

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • © 2008 Microchip Technology Inc. DS39631E-page 15

    PIC18F2420/2520/4420/4520

    PORTC is a bidirectional I/O port.RC0/T1OSO/T13CKI

    RC0T1OSOT13CKI

    11 8I/OOI

    ST—ST

    Digital I/O.Timer1 oscillator output. Timer1/Timer3 external clock input.

    RC1/T1OSI/CCP2RC1T1OSICCP2(2)

    12 9I/OI

    I/O

    STAnalog

    ST

    Digital I/O.Timer1 oscillator input.Capture 2 input/Compare 2 output/PWM2 output.

    RC2/CCP1RC2CCP1

    13 10I/OI/O

    STST

    Digital I/O.Capture 1 input/Compare 1 output/PWM1 output.

    RC3/SCK/SCLRC3SCKSCL

    14 11I/OI/OI/O

    STSTST

    Digital I/O.Synchronous serial clock input/output for SPI mode.Synchronous serial clock input/output for I2C™ mode.

    RC4/SDI/SDARC4SDISDA

    15 12I/OI

    I/O

    STSTST

    Digital I/O.SPI data in.I2C data I/O.

    RC5/SDORC5SDO

    16 13I/OO

    ST—

    Digital I/O.SPI data out.

    RC6/TX/CKRC6TXCK

    17 14I/OO

    I/O

    ST—ST

    Digital I/O.EUSART asynchronous transmit. EUSART synchronous clock (see related RX/DT).

    RC7/RX/DTRC7RXDT

    18 15I/OI

    I/O

    STSTST

    Digital I/O.EUSART asynchronous receive.EUSART synchronous data (see related TX/CK).

    RE3 — — — — See MCLR/VPP/RE3 pin.VSS 8, 19 5, 16 P — Ground reference for logic and I/O pins.VDD 20 17 P — Positive supply for logic and I/O pins.

    TABLE 1-2: PIC18F2420/2520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number

    PinType

    BufferType DescriptionSPDIP,

    SOIC QFN

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 16 © 2008 Microchip Technology Inc.

    TABLE 1-3: PIC18F4420/4520 PINOUT I/O DESCRIPTIONS

    Pin NamePin Number Pin

    TypeBufferType DescriptionPDIP QFN TQFP

    MCLR/VPP/RE3MCLR

    VPPRE3

    1 18 18I

    PI

    ST

    ST

    Master Clear (input) or programming voltage (input).Master Clear (Reset) input. This pin is an active-low Reset to the device.Programming voltage input.Digital input.

    OSC1/CLKI/RA7OSC1

    CLKI

    RA7

    13 32 30I

    I

    I/O

    ST

    CMOS

    TTL

    Oscillator crystal or external clock input.Oscillator crystal input or external clock source input. ST buffer when configured in RC mode; analog otherwise.External clock source input. Always associated with pin function, OSC1. (See related OSC1/CLKI, OSC2/CLKO pins.)General purpose I/O pin.

    OSC2/CLKO/RA6OSC2

    CLKO

    RA6

    14 33 31O

    O

    I/O

    TTL

    Oscillator crystal or clock output.Oscillator crystal output. Connects to crystalor resonator in Crystal Oscillator mode.In RC mode, OSC2 pin outputs CLKO whichhas 1/4 the frequency of OSC1 and denotesthe instruction cycle rate. General purpose I/O pin.

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • © 2008 Microchip Technology Inc. DS39631E-page 17

    PIC18F2420/2520/4420/4520

    PORTA is a bidirectional I/O port.RA0/AN0

    RA0AN0

    2 19 19I/OI

    TTLAnalog

    Digital I/O.Analog input 0.

    RA1/AN1RA1AN1

    3 20 20I/OI

    TTLAnalog

    Digital I/O.Analog input 1.

    RA2/AN2/VREF-/CVREFRA2AN2VREF-CVREF

    4 21 21I/OIIO

    TTLAnalogAnalogAnalog

    Digital I/O.Analog input 2.A/D reference voltage (low) input. Comparator reference voltage output.

    RA3/AN3/VREF+RA3AN3VREF+

    5 22 22I/OII

    TTLAnalogAnalog

    Digital I/O.Analog input 3.A/D reference voltage (high) input.

    RA4/T0CKI/C1OUTRA4T0CKIC1OUT

    6 23 23I/OIO

    STST—

    Digital I/O.Timer0 external clock input.Comparator 1 output.

    RA5/AN4/SS/HLVDIN/C2OUT

    RA5AN4SSHLVDINC2OUT

    7 24 24

    I/OIIIO

    TTLAnalog

    TTLAnalog

    Digital I/O.Analog input 4.SPI slave select input.High/Low-Voltage Detect input.Comparator 2 output.

    RA6 See the OSC2/CLKO/RA6 pin.

    RA7 See the OSC1/CLKI/RA7 pin.

    TABLE 1-3: PIC18F4420/4520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number Pin

    TypeBufferType DescriptionPDIP QFN TQFP

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 18 © 2008 Microchip Technology Inc.

    PORTB is a bidirectional I/O port. PORTB can be software programmed for internal weak pull-ups on all inputs.

    RB0/INT0/FLT0/AN12RB0INT0FLT0AN12

    33 9 8I/OIII

    TTLSTST

    Analog

    Digital I/O.External interrupt 0.PWM Fault input for Enhanced CCP1.Analog input 12.

    RB1/INT1/AN10RB1INT1AN10

    34 10 9I/OII

    TTLST

    Analog

    Digital I/O.External interrupt 1.Analog input 10.

    RB2/INT2/AN8RB2INT2AN8

    35 11 10I/OII

    TTLST

    Analog

    Digital I/O.External interrupt 2.Analog input 8.

    RB3/AN9/CCP2RB3AN9CCP2(1)

    36 12 11I/OI

    I/O

    TTLAnalog

    ST

    Digital I/O.Analog input 9. Capture 2 input/Compare 2 output/PWM2 output.

    RB4/KBI0/AN11RB4KBI0AN11

    37 14 14I/OII

    TTLTTL

    Analog

    Digital I/O.Interrupt-on-change pin.Analog input 11.

    RB5/KBI1/PGMRB5KBI1PGM

    38 15 15I/OI

    I/O

    TTLTTLST

    Digital I/O.Interrupt-on-change pin.Low-Voltage ICSP™ Programming enable pin.

    RB6/KBI2/PGCRB6KBI2PGC

    39 16 16I/OI

    I/O

    TTLTTLST

    Digital I/O.Interrupt-on-change pin. In-Circuit Debugger and ICSP programmingclock pin.

    RB7/KBI3/PGDRB7KBI3PGD

    40 17 17I/OI

    I/O

    TTLTTLST

    Digital I/O.Interrupt-on-change pin. In-Circuit Debugger and ICSP programmingdata pin.

    TABLE 1-3: PIC18F4420/4520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number Pin

    TypeBufferType DescriptionPDIP QFN TQFP

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • © 2008 Microchip Technology Inc. DS39631E-page 19

    PIC18F2420/2520/4420/4520

    PORTC is a bidirectional I/O port.RC0/T1OSO/T13CKI

    RC0T1OSOT13CKI

    15 34 32I/OOI

    ST—ST

    Digital I/O.Timer1 oscillator output. Timer1/Timer3 external clock input.

    RC1/T1OSI/CCP2RC1T1OSICCP2(2)

    16 35 35I/OI

    I/O

    STCMOS

    ST

    Digital I/O.Timer1 oscillator input.Capture 2 input/Compare 2 output/PWM2 output.

    RC2/CCP1/P1ARC2CCP1P1A

    17 36 36I/OI/OO

    STST—

    Digital I/O.Capture 1 input/Compare 1 output/PWM1 output.Enhanced CCP1 output.

    RC3/SCK/SCLRC3SCK

    SCL

    18 37 37I/OI/O

    I/O

    STST

    ST

    Digital I/O.Synchronous serial clock input/output forSPI mode.Synchronous serial clock input/output for I2C™ mode.

    RC4/SDI/SDARC4SDISDA

    23 42 42I/OI

    I/O

    STSTST

    Digital I/O.SPI data in.I2C data I/O.

    RC5/SDORC5SDO

    24 43 43I/OO

    ST—

    Digital I/O.SPI data out.

    RC6/TX/CKRC6TXCK

    25 44 44I/OOI/O

    ST—ST

    Digital I/O.EUSART asynchronous transmit.EUSART synchronous clock (see related RX/DT).

    RC7/RX/DTRC7RXDT

    26 1 1I/OI

    I/O

    STSTST

    Digital I/O.EUSART asynchronous receive.EUSART synchronous data (see related TX/CK).

    TABLE 1-3: PIC18F4420/4520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number Pin

    TypeBufferType DescriptionPDIP QFN TQFP

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 20 © 2008 Microchip Technology Inc.

    PORTD is a bidirectional I/O port or a Parallel Slave Port (PSP) for interfacing to a microprocessor port. These pins have TTL input buffers when PSP module is enabled.

    RD0/PSP0RD0PSP0

    19 38 38I/OI/O

    STTTL

    Digital I/O.Parallel Slave Port data.

    RD1/PSP1RD1PSP1

    20 39 39I/OI/O

    STTTL

    Digital I/O.Parallel Slave Port data.

    RD2/PSP2RD2PSP2

    21 40 40I/OI/O

    STTTL

    Digital I/O.Parallel Slave Port data.

    RD3/PSP3RD3PSP3

    22 41 41I/OI/O

    STTTL

    Digital I/O.Parallel Slave Port data.

    RD4/PSP4RD4PSP4

    27 2 2I/OI/O

    STTTL

    Digital I/O.Parallel Slave Port data.

    RD5/PSP5/P1BRD5PSP5P1B

    28 3 3I/OI/OO

    STTTL—

    Digital I/O.Parallel Slave Port data.Enhanced CCP1 output.

    RD6/PSP6/P1CRD6PSP6P1C

    29 4 4I/OI/OO

    STTTL—

    Digital I/O.Parallel Slave Port data.Enhanced CCP1 output.

    RD7/PSP7/P1DRD7PSP7P1D

    30 5 5I/OI/OO

    STTTL—

    Digital I/O.Parallel Slave Port data.Enhanced CCP1 output.

    TABLE 1-3: PIC18F4420/4520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number Pin

    TypeBufferType DescriptionPDIP QFN TQFP

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • © 2008 Microchip Technology Inc. DS39631E-page 21

    PIC18F2420/2520/4420/4520

    PORTE is a bidirectional I/O port.

    RE0/RD/AN5RE0RD

    AN5

    8 25 25I/OI

    I

    STTTL

    Analog

    Digital I/O.Read control for Parallel Slave Port (see also WR and CS pins).Analog input 5.

    RE1/WR/AN6RE1WR

    AN6

    9 26 26I/OI

    I

    STTTL

    Analog

    Digital I/O.Write control for Parallel Slave Port (see CS and RD pins).Analog input 6.

    RE2/CS/AN7RE2CS

    AN7

    10 27 27I/OI

    I

    STTTL

    Analog

    Digital I/O.Chip Select control for Parallel Slave Port(see related RD and WR).Analog input 7.

    RE3 — — — — — See MCLR/VPP/RE3 pin.VSS 12, 31 6, 30,

    316, 29 P — Ground reference for logic and I/O pins.

    VDD 11, 32 7, 8, 28, 29

    7, 28 P — Positive supply for logic and I/O pins.

    NC — 13 12, 13, 33, 34

    — — No Connect.

    TABLE 1-3: PIC18F4420/4520 PINOUT I/O DESCRIPTIONS (CONTINUED)

    Pin NamePin Number Pin

    TypeBufferType DescriptionPDIP QFN TQFP

    Legend: TTL = TTL compatible input CMOS = CMOS compatible input or output ST = Schmitt Trigger input with CMOS levels I = Input O = Output P = Power

    Note 1: Default assignment for CCP2 when Configuration bit, CCP2MX, is set.2: Alternate assignment for CCP2 when Configuration bit, CCP2MX, is cleared.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 22 © 2008 Microchip Technology Inc.

    NOTES:

  • © 2008 Microchip Technology Inc. DS39631E-page 23

    PIC18F2420/2520/4420/4520

    2.0 OSCILLATOR CONFIGURATIONS

    2.1 Oscillator TypesPIC18F2420/2520/4420/4520 devices can be operatedin ten different oscillator modes. The user can programthe Configuration bits, FOSC, in ConfigurationRegister 1H to select one of these ten modes:

    1. LP Low-Power Crystal2. XT Crystal/Resonator3. HS High-Speed Crystal/Resonator4. HSPLL High-Speed Crystal/Resonator

    with PLL Enabled5. RC External Resistor/Capacitor with

    FOSC/4 Output on RA66. RCIO External Resistor/Capacitor with I/O

    on RA67. INTIO1 Internal Oscillator with FOSC/4 Output

    on RA6 and I/O on RA78. INTIO2 Internal Oscillator with I/O on RA6

    and RA79. EC External Clock with FOSC/4 Output10. ECIO External Clock with I/O on RA6

    2.2 Crystal Oscillator/Ceramic Resonators

    In XT, LP, HS or HSPLL Oscillator modes, a crystal orceramic resonator is connected to the OSC1 andOSC2 pins to establish oscillation. Figure 2-1 showsthe pin connections.

    The oscillator design requires the use of a parallel cutcrystal.

    FIGURE 2-1: CRYSTAL/CERAMIC RESONATOR OPERATION (XT, LP, HS OR HSPLL CONFIGURATION)

    TABLE 2-1: CAPACITOR SELECTION FOR CERAMIC RESONATORS

    Note: Use of a series cut crystal may give a fre-quency out of the crystal manufacturer’sspecifications.

    Typical Capacitor Values Used:

    Mode Freq OSC1 OSC2

    XT 3.58 MHz4.19 MHz

    4 MHz4 MHz

    15 pF15 pF30 pF50 pF

    15 pF15 pF30 pF50 pF

    Capacitor values are for design guidance only. Different capacitor values may be required to produceacceptable oscillator operation. The user should testthe performance of the oscillator over the expectedVDD and temperature range for the application.

    See the notes following Table 2-2 for additionalinformation.

    Note: When using resonators with frequenciesabove 3.5 MHz, the use of HS mode,rather than XT mode, is recommended.HS mode may be used at any VDD forwhich the controller is rated. If HS isselected, it is possible that the gain of theoscillator will overdrive the resonator.Therefore, a series resistor should beplaced between the OSC2 pin and theresonator. As a good starting point, therecommended value of RS is 330Ω.

    Note 1: See Table 2-1 and Table 2-2 for initial values ofC1 and C2.

    2: A series resistor (RS) may be required for ATstrip cut crystals.

    3: RF varies with the oscillator mode chosen.

    C1(1)

    C2(1)

    XTAL

    OSC2

    OSC1

    RF(3)

    Sleep

    To

    Logic

    PIC18FXXXXRS(2)

    Internal

  • PIC18F2420/2520/4420/4520

    DS39631E-page 24 © 2008 Microchip Technology Inc.

    TABLE 2-2: CAPACITOR SELECTION FOR CRYSTAL OSCILLATOR

    An external clock source may also be connected to theOSC1 pin in the HS mode, as shown in Figure 2-2.

    FIGURE 2-2: EXTERNAL CLOCK INPUT OPERATION (HS OSC CONFIGURATION)

    2.3 External Clock InputThe EC and ECIO Oscillator modes require an externalclock source to be connected to the OSC1 pin. There isno oscillator start-up time required after a Power-onReset or after an exit from Sleep mode.

    In the EC Oscillator mode, the oscillator frequencydivided by 4 is available on the OSC2 pin. This signalmay be used for test purposes or to synchronize otherlogic. Figure 2-3 shows the pin connections for the ECOscillator mode.

    FIGURE 2-3: EXTERNAL CLOCK INPUT OPERATION (EC CONFIGURATION)

    The ECIO Oscillator mode functions like the EC mode,except that the OSC2 pin becomes an additional gen-eral purpose I/O pin. The I/O pin becomes bit 6 ofPORTA (RA6). Figure 2-4 shows the pin connectionsfor the ECIO Oscillator mode.

    FIGURE 2-4: EXTERNAL CLOCK INPUT OPERATION (ECIO CONFIGURATION)

    Osc Type Crystal Freq

    Typical Capacitor Values Tested:

    C1 C2

    LP 32 kHz 30 pF 30 pFXT 1 MHz

    4 MHz15 pF15 pF

    15 pF15 pF

    HS 4 MHz10 MHz20 MHz25 MHz25 MHz

    15 pF15 pF15 pF0 pF

    15 pF

    15 pF15 pF15 pF5 pF

    15 pFCapacitor values are for design guidance only. These capacitors were tested with the crystals listedbelow for basic start-up and operation. These valuesare not optimized.Different capacitor values may be required to produceacceptable oscillator operation. The user should testthe performance of the oscillator over the expectedVDD and temperature range for the application.

    See the notes following this table for additionalinformation.

    Crystals Used:32 kHz 4 MHz25 MHz 10 MHz1 MHz 20 MHz

    Note 1: Higher capacitance increases the stabilityof the oscillator but also increases thestart-up time.

    2: When operating below 3V VDD, or whenusing certain ceramic resonators at anyvoltage, it may be necessary to use theHS mode or switch to a crystal oscillator.

    3: Since each resonator/crystal has its owncharacteristics, the user should consultthe resonator/crystal manufacturer forappropriate values of externalcomponents.

    4: Rs may be required to avoid overdrivingcrystals with low drive level specification.

    5: Always verify oscillator performance overthe VDD and temperature range that isexpected for the application.

    OSC1

    OSC2Open

    Clock fromExt. System PIC18FXXXX

    (HS Mode)

    OSC1/CLKI

    OSC2/CLKOFOSC/4

    Clock fromExt. System PIC18FXXXX

    OSC1/CLKI

    I/O (OSC2)RA6

    Clock fromExt. System PIC18FXXXX

  • © 2008 Microchip Technology Inc. DS39631E-page 25

    PIC18F2420/2520/4420/45202.4 RC OscillatorFor timing insensitive applications, the “RC” and“RCIO” device options offer additional cost savings.The actual oscillator frequency is a function of severalfactors:

    • supply voltage• values of the external resistor (REXT) and

    capacitor (CEXT)• operating temperature

    Given the same device, operating voltage and tempera-ture and component values, there will also be unit-to-unitfrequency variations. These are due to factors such as:

    • normal manufacturing variation• difference in lead frame capacitance between

    package types (especially for low CEXT values) • variations within the tolerance of limits of REXT

    and CEXT

    In the RC Oscillator mode, the oscillator frequencydivided by 4 is available on the OSC2 pin. This signalmay be used for test purposes or to synchronize otherlogic. Figure 2-5 shows how the R/C combination isconnected.

    FIGURE 2-5: RC OSCILLATOR MODE

    The RCIO Oscillator mode (Figure 2-6) functions likethe RC mode, except that the OSC2 pin becomes anadditional general purpose I/O pin. The I/O pinbecomes bit 6 of PORTA (RA6).

    FIGURE 2-6: RCIO OSCILLATOR MODE

    2.5 PLL Frequency MultiplierA Phase Locked Loop (PLL) circuit is provided as anoption for users who wish to use a lower frequencyoscillator circuit or to clock the device up to its highestrated frequency from a crystal oscillator. This may beuseful for customers who are concerned with EMI dueto high-frequency crystals or users who require higherclock speeds from an internal oscillator.

    2.5.1 HSPLL OSCILLATOR MODEThe HSPLL mode makes use of the HS Oscillatormode for frequencies up to 10 MHz. A PLL then multi-plies the oscillator output frequency by 4 to produce aninternal clock frequency up to 40 MHz. The PLLEN bitis not available in this oscillator mode.

    The PLL is only available to the crystal oscillator whenthe FOSC Configuration bits are programmed forHSPLL mode (= 0110).

    FIGURE 2-7: PLL BLOCK DIAGRAM (HS MODE)

    2.5.2 PLL AND INTOSCThe PLL is also available to the internal oscillator blockin selected oscillator modes. In this configuration, thePLL is enabled in software and generates a clock out-put of up to 32 MHz. The operation of INTOSC with thePLL is described in Section 2.6.4 “PLL in INTOSCModes”.

    OSC2/CLKO

    CEXT

    REXT

    PIC18FXXXX

    OSC1

    FOSC/4

    InternalClock

    VDD

    VSS

    Recommended values: 3 kΩ ≤ REXT ≤ 100 kΩCEXT > 20 pF

    CEXT

    REXT

    PIC18FXXXX

    OSC1 InternalClock

    VDD

    VSS

    Recommended values: 3 kΩ ≤ REXT ≤ 100 kΩCEXT > 20 pF

    I/O (OSC2)RA6

    MU

    X

    VCO

    LoopFilter

    CrystalOsc

    OSC2

    OSC1

    PLL Enable

    FINFOUT

    SYSCLK

    PhaseComparator

    HS Oscillator Enable

    ÷4

    (from Configuration Register 1H)

    HS Mode

  • PIC18F2420/2520/4420/4520

    DS39631E-page 26 © 2008 Microchip Technology Inc.

    2.6 Internal Oscillator BlockThe PIC18F2420/2520/4420/4520 devices include aninternal oscillator block which generates two differentclock signals; either can be used as the micro-controller’s clock source. This may eliminate the needfor external oscillator circuits on the OSC1 and/orOSC2 pins.

    The main output (INTOSC) is an 8 MHz clock sourcewhich can be used to directly drive the device clock. Italso drives a postscaler which can provide a range ofclock frequencies from 31 kHz to 4 MHz. The INTOSCoutput is enabled when a clock frequency from 125 kHzto 8 MHz is selected.

    The other clock source is the internal RC oscillator(INTRC), which provides a nominal 31 kHz output.INTRC is enabled if it is selected as the device clocksource; it is also enabled automatically when any of thefollowing are enabled:

    • Power-up Timer• Fail-Safe Clock Monitor• Watchdog Timer• Two-Speed Start-up

    These features are discussed in greater detail inSection 23.0 “Special Features of the CPU”.The clock source frequency (INTOSC direct, INTRCdirect or INTOSC postscaler) is selected by configuringthe IRCF bits of the OSCCON register (page 30).

    2.6.1 INTIO MODESUsing the internal oscillator as the clock source elimi-nates the need for up to two external oscillator pins,which can then be used for digital I/O. Two distinctconfigurations are available:

    • In INTIO1 mode, the OSC2 pin outputs FOSC/4, while OSC1 functions as RA7 for digital input and output.

    • In INTIO2 mode, OSC1 functions as RA7 and OSC2 functions as RA6, both for digital input and output.

    2.6.2 INTOSC OUTPUT FREQUENCYThe internal oscillator block is calibrated at the factoryto produce an INTOSC output frequency of 8.0 MHz.

    The INTRC oscillator operates independently of theINTOSC source. Any changes in INTOSC acrossvoltage and temperature are not necessarily reflectedby changes in INTRC and vice versa.

    2.6.3 OSCTUNE REGISTERThe internal oscillator’s output has been calibrated atthe factory but can be adjusted in the user’s applica-tion. This is done by writing to the OSCTUNE register(Register 2-1).

    When the OSCTUNE register is modified, the INTOSCfrequency will begin shifting to the new frequency. TheINTRC clock will reach the new frequency within8 clock cycles (approximately 8 * 32 μs = 256 μs). TheINTOSC clock will stabilize within 1 ms. Code execu-tion continues during this shift. There is no indicationthat the shift has occurred.

    The OSCTUNE register also implements the INTSRCand PLLEN bits, which control certain features of theinternal oscillator block. The INTSRC bit allows usersto select which internal oscillator provides the clocksource when the 31 kHz frequency option is selected.This is covered in greater detail in Section 2.7.1“Oscillator Control Register”. The PLLEN bit controls the operation of the frequencymultiplier, PLL, in internal oscillator modes.

    2.6.4 PLL IN INTOSC MODESThe 4x frequency multiplier can be used with the inter-nal oscillator block to produce faster device clockspeeds than are normally possible with an internaloscillator. When enabled, the PLL produces a clockspeed of up to 32 MHz.

    Unlike HSPLL mode, the PLL is controlled throughsoftware. The control bit, PLLEN (OSCTUNE), isused to enable or disable its operation.

    The PLL is available when the device is configured touse the internal oscillator block as its primary clocksource (FOSC = 1001 or 1000). Additionally, thePLL will only function when the selected output fre-quency is either 4 MHz or 8 MHz (OSCCON = 111or 110). If both of these conditions are not met, the PLLis disabled.

    The PLLEN control bit is only functional in those inter-nal oscillator modes where the PLL is available. In allother modes, it is forced to ‘0’ and is effectivelyunavailable.

    2.6.5 INTOSC FREQUENCY DRIFTThe factory calibrates the internal oscillator blockoutput (INTOSC) for 8 MHz. However, this frequencymay drift as VDD or temperature changes, which canaffect the controller operation in a variety of ways. It ispossible to adjust the INTOSC frequency by modifyingthe value in the OSCTUNE register. This has no effecton the INTRC clock source frequency.

    Tuning the INTOSC source requires knowing when tomake the adjustment, in which direction it should bemade, and in some cases, how large a change isneeded. Three compensation techniques are discussedin Section 2.6.5.1 “Compensating with theEUSART”, Section 2.6.5.2 “Compensating with theTimers” and Section 2.6.5.3 “Compensating with theCCP Module in Capture Mode”, but other techniquesmay be used.

  • © 2008 Microchip Technology Inc. DS39631E-page 27

    PIC18F2420/2520/4420/4520

    2.6.5.1 Compensating with the EUSART An adjustment may be required when the EUSARTbegins to generate framing errors or receives data witherrors while in Asynchronous mode. Framing errorsindicate that the device clock frequency is too high. Toadjust for this, decrement the value in OSCTUNE toreduce the clock frequency. On the other hand, errorsin data may suggest that the clock speed is too low. Tocompensate, increment OSCTUNE to increase theclock frequency.

    2.6.5.2 Compensating with the TimersThis technique compares device clock speed to somereference clock. Two timers may be used; one timer isclocked by the peripheral clock, while the other isclocked by a fixed reference source, such as theTimer1 oscillator.

    Both timers are cleared, but the timer clocked by thereference generates interrupts. When an interruptoccurs, the internally clocked timer is read and bothtimers are cleared. If the internally clocked timer valueis greater than expected, then the internal oscillatorblock is running too fast. To adjust for this, decrementthe OSCTUNE register.

    2.6.5.3 Compensating with the CCP Module in Capture Mode

    A CCP module can use free-running Timer1 (orTimer3), clocked by the internal oscillator block and anexternal event with a known period (i.e., AC power fre-quency). The time of the first event is captured in theCCPRxH:CCPRxL registers and is recorded for uselater. When the second event causes a capture, thetime of the first event is subtracted from the time of thesecond event. Since the period of the external event isknown, the time difference between events can becalculated.

    If the measured time is much greater than the calcu-lated time, the internal oscillator block is running toofast; to compensate, decrement the OSCTUNE register.If the measured time is much less than the calculatedtime, the internal oscillator block is running too slow; tocompensate, increment the OSCTUNE register.

    REGISTER 2-1: OSCTUNE: OSCILLATOR TUNING REGISTER

    R/W-0 R/W-0(1) U-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 INTSRC PLLEN(1) — TUN4 TUN3 TUN2 TUN1 TUN0

    bit 7 bit 0

    Legend:R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown

    bit 7 INTSRC: Internal Oscillator Low-Frequency Source Select bit1 = 31.25 kHz device clock derived from 8 MHz INTOSC source (divide-by-256 enabled)0 = 31 kHz device clock derived directly from INTRC internal oscillator

    bit 6 PLLEN: Frequency Multiplier PLL for INTOSC Enable bit(1)

    1 = PLL enabled for INTOSC (4 MHz and 8 MHz only)0 = PLL disabled

    bit 5 Unimplemented: Read as ‘0’bit 4-0 TUN: Frequency Tuning bits

    011111 = Maximum frequency• •• •000001 000000 = Center frequency. Oscillator module is running at the calibrated frequency.111111 • •• •100000 = Minimum frequency

    Note 1: Available only in certain oscillator configurations; otherwise, this bit is unavailable and reads as ‘0’. See Section 2.6.4 “PLL in INTOSC Modes” for details.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 28 © 2008 Microchip Technology Inc.

    2.7 Clock Sources and Oscillator Switching

    Like previous PIC18 devices, the PIC18F2420/2520/4420/4520 family includes a feature that allows thedevice clock source to be switched from the main oscil-lator to an alternate low-frequency clock source.PIC18F2420/2520/4420/4520 devices offer two alternateclock sources. When an alternate clock source is enabled,the various power-managed operating modes areavailable.

    Essentially, there are three clock sources for thesedevices:

    • Primary oscillators• Secondary oscillators• Internal oscillator block

    The primary oscillators include the External Crystaland Resonator modes, the External RC modes, theExternal Clock modes and the internal oscillator block.The particular mode is defined by the FOSC Con-figuration bits. The details of these modes are coveredearlier in this chapter.

    The secondary oscillators are those external sourcesnot connected to the OSC1 or OSC2 pins. Thesesources may continue to operate even after thecontroller is placed in a power-managed mode.

    PIC18F2420/2520/4420/4520 devices offer the Timer1oscillator as a secondary oscillator. This oscillator, in allpower-managed modes, is often the time base forfunctions such as a Real-Time Clock (RTC).

    Most often, a 32.768 kHz watch crystal is connectedbetween the RC0/T1OSO/T13CKI and RC1/T1OSIpins. Like the LP Oscillator mode circuit, loadingcapacitors are also connected from each pin to ground.

    The Timer1 oscillator is discussed in greater detail inSection 12.3 “Timer1 Oscillator”.In addition to being a primary clock source, the internaloscillator block is available as a power-managedmode clock source. The INTRC source is also used asthe clock source for several special features, such asthe WDT and Fail-Safe Clock Monitor.

    The clock sources for the PIC18F2420/2520/4420/4520devices are shown in Figure 2-8. See Section 23.0“Special Features of the CPU” for Configurationregister details.

    FIGURE 2-8: PIC18F2420/2520/4420/4520 CLOCK DIAGRAM

    4 x PLL

    FOSC

    Secondary Oscillator

    T1OSCENEnableOscillator

    T1OSO

    T1OSI

    Clock Source Option for Other Modules

    OSC1

    OSC2

    Sleep HSPLL, INTOSC/PLL

    LP, XT, HS, RC, EC

    T1OSC

    CPU

    Peripherals

    IDLEN

    Pos

    tsca

    ler

    MU

    X

    MU

    X

    8 MHz

    4 MHz

    2 MHz

    1 MHz

    500 kHz

    125 kHz

    250 kHz

    OSCCON

    111

    110

    101

    100

    011

    010

    001

    00031 kHz

    INTRCSource

    InternalOscillator

    Block

    WDT, PWRT, FSCM

    8 MHz

    Internal Oscillator

    (INTOSC)

    OSCCON

    ClockControl

    OSCCON

    Source8 MHz

    31 kHz (INTRC)

    OSCTUNE

    01

    OSCTUNE

    and Two-Speed Start-up

    Primary OscillatorPIC18F2420/2520/4420/4520

  • © 2008 Microchip Technology Inc. DS39631E-page 29

    PIC18F2420/2520/4420/45202.7.1 OSCILLATOR CONTROL REGISTERThe OSCCON register (Register 2-2) controls severalaspects of the device clock’s operation, both in full-poweroperation and in power-managed modes.

    The System Clock Select bits, SCS, select theclock source. The available clock sources are theprimary clock (defined by the FOSC Configura-tion bits), the secondary clock (Timer1 oscillator) andthe internal oscillator block. The clock source changesimmediately after one or more of the bits is written to,following a brief clock transition interval. The SCS bitsare cleared on all forms of Reset.

    The Internal Oscillator Frequency Select bits(IRCF) select the frequency output of the internaloscillator block to drive the device clock. The choicesare the INTRC source, the INTOSC source (8 MHz) orone of the frequencies derived from the INTOSC post-scaler (31.25 kHz to 4 MHz). If the internal oscillatorblock is supplying the device clock, changing the statesof these bits will have an immediate change on theinternal oscillator’s output. On device Resets, thedefault output frequency of the internal oscillator blockis set at 1 MHz.

    When a nominal output frequency of 31 kHz is selected(IRCF = 000), users may choose which internaloscillator acts as the source. This is done with theINTSRC bit in the OSCTUNE register (OSCTUNE).Setting this bit selects INTOSC as a 31.25 kHz clocksource by enabling the divide-by-256 output of theINTOSC postscaler. Clearing INTSRC selects INTRC(nominally 31 kHz) as the clock source.

    This option allows users to select the tunable and moreprecise INTOSC as a clock source, while maintainingpower savings with a very low clock speed. Regardlessof the setting of INTSRC, INTRC always remains theclock source for features such as the Watchdog Timerand the Fail-Safe Clock Monitor.

    The OSTS, IOFS and T1RUN bits indicate which clocksource is currently providing the device clock. TheOSTS bit indicates that the Oscillator Start-up Timer(OST) has timed out and the primary clock is providingthe device clock in primary clock modes. The IOFS bitindicates when the internal oscillator block has stabi-lized and is providing the device clock in RC Clockmodes. The T1RUN bit (T1CON) indicates whenthe Timer1 oscillator is providing the device clock insecondary clock modes. In power-managed modes,only one of these three bits will be set at any time. Ifnone of these bits are set, the INTRC is providing theclock or the internal oscillator block has just started andis not yet stable.

    The IDLEN bit determines if the device goes into Sleepmode or one of the Idle modes when the SLEEPinstruction is executed.

    The use of the flag and control bits in the OSCCONregister is discussed in more detail in Section 3.0“Power-Managed Modes”.

    2.7.2 OSCILLATOR TRANSITIONSPIC18F2420/2520/4420/4520 devices contain circuitryto prevent clock “glitches” when switching betweenclock sources. A short pause in the device clock occursduring the clock switch. The length of this pause is thesum of two cycles of the old clock source and three tofour cycles of the new clock source. This formulaassumes that the new clock source is stable.

    Clock transitions are discussed in greater detail inSection 3.1.2 “Entering Power-Managed Modes”.

    Note 1: The Timer1 oscillator must be enabled toselect the secondary clock source. TheTimer1 oscillator is enabled by setting theT1OSCEN bit in the Timer1 Control regis-ter (T1CON). If the Timer1 oscillatoris not enabled, then any attempt to selecta secondary clock source will be ignored.

    2: It is recommended that the Timer1oscillator be operating and stable beforeselecting the secondary clock source or avery long delay may occur while theTimer1 oscillator starts.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 30 © 2008 Microchip Technology Inc.

    REGISTER 2-2: OSCCON: OSCILLATOR CONTROL REGISTER

    R/W-0 R/W-1 R/W-0 R/W-0 R(1) R-0 R/W-0 R/W-0IDLEN IRCF2 IRCF1 IRCF0 OSTS IOFS SCS1 SCS0

    bit 7 bit 0

    Legend:R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown

    bit 7 IDLEN: Idle Enable bit1 = Device enters an Idle mode on SLEEP instruction 0 = Device enters Sleep mode on SLEEP instruction

    bit 6-4 IRCF: Internal Oscillator Frequency Select bits111 = 8 MHz (INTOSC drives clock directly)110 = 4 MHz101 = 2 MHz 100 = 1 MHz(3)011 = 500 kHz 010 = 250 kHz001 = 125 kHz000 = 31 kHz (from either INTOSC/256 or INTRC directly)(2)

    bit 3 OSTS: Oscillator Start-up Timer Time-out Status bit(1)

    1 = Oscillator Start-up Timer (OST) time-out has expired; primary oscillator is running0 = Oscillator Start-up Timer (OST) time-out is running; primary oscillator is not ready

    bit 2 IOFS: INTOSC Frequency Stable bit 1 = INTOSC frequency is stable0 = INTOSC frequency is not stable

    bit 1-0 SCS: System Clock Select bits1x = Internal oscillator block01 = Secondary (Timer1) oscillator00 = Primary oscillator

    Note 1: Reset state depends on state of the IESO Configuration bit.2: Source selected by the INTSRC bit (OSCTUNE), see text.3: Default output frequency of INTOSC on Reset.

  • © 2008 Microchip Technology Inc. DS39631E-page 31

    PIC18F2420/2520/4420/45202.8 Effects of Power-Managed Modes

    on the Various Clock SourcesWhen PRI_IDLE mode is selected, the designated pri-mary oscillator continues to run without interruption.For all other power-managed modes, the oscillatorusing the OSC1 pin is disabled. The OSC1 pin (andOSC2 pin, if used by the oscillator) will stop oscillating.

    In secondary clock modes (SEC_RUN andSEC_IDLE), the Timer1 oscillator is operating and pro-viding the device clock. The Timer1 oscillator may alsorun in all power-managed modes if required to clockTimer1 or Timer3.

    In internal oscillator modes (RC_RUN and RC_IDLE),the internal oscillator block provides the device clocksource. The 31 kHz INTRC output can be used directlyto provide the clock and may be enabled to supportvarious special features, regardless of the power-managed mode (see Section 23.2 “Watchdog Timer(WDT)”, Section 23.3 “Two-Speed Start-up” andSection 23.4 “Fail-Safe Clock Monitor” for moreinformation on WDT, Fail-Safe Clock Monitor and Two-Speed Start-up). The INTOSC output at 8 MHz may beused directly to clock the device or may be divideddown by the postscaler. The INTOSC output is disabledif the clock is provided directly from the INTRC output.

    If Sleep mode is selected, all clock sources arestopped. Since all the transistor switching currentshave been stopped, Sleep mode achieves the lowestcurrent consumption of the device (only leakagecurrents).

    Enabling any on-chip feature that will operate duringSleep will increase the current consumed during Sleep.The INTRC is required to support WDT operation. TheTimer1 oscillator may be operating to support a Real-Time Clock. Other features may be operating that do

    not require a device clock source (i.e., MSSP slave,PSP, INTx pins and others). Peripherals that may addsignificant current consumption are listed inSection 26.2 “DC Characteristics”.

    2.9 Power-up DelaysPower-up delays are controlled by two timers so that noexternal Reset circuitry is required for most applica-tions. The delays ensure that the device is kept inReset until the device power supply is stable under nor-mal circumstances and the primary clock is operatingand stable. For additional information on power-updelays, see Section 4.5 “Device Reset Timers”.The first timer is the Power-up Timer (PWRT), whichprovides a fixed delay on power-up (parameter 33,Table 26-10). It is enabled by clearing (= 0) thePWRTEN Configuration bit.

    The second timer is the Oscillator Start-up Timer(OST), intended to keep the chip in Reset until thecrystal oscillator is stable (LP, XT and HS modes). TheOST does this by counting 1024 oscillator cyclesbefore allowing the oscillator to clock the device.

    When the HSPLL Oscillator mode is selected, thedevice is kept in Reset for an additional 2 ms, followingthe HS mode OST delay, so the PLL can lock to theincoming clock frequency.

    There is a delay of interval, TCSD (parameter 38,Table 26-10), following POR, while the controllerbecomes ready to execute instructions. This delay runsconcurrently with any other delays. This may be theonly delay that occurs when any of the EC, RC or INTIOmodes are used as the primary clock source.

    TABLE 2-3: OSC1 AND OSC2 PIN STATES IN SLEEP MODE OSC Mode OSC1 Pin OSC2 Pin

    RC, INTIO1 Floating, external resistor should pull high At logic low (clock/4 output)RCIO Floating, external resistor should pull high Configured as PORTA, bit 6INTIO2 Configured as PORTA, bit 7 Configured as PORTA, bit 6ECIO Floating, pulled by external clock Configured as PORTA, bit 6EC Floating, pulled by external clock At logic low (clock/4 output)LP, XT and HS Feedback inverter disabled at quiescent

    voltage levelFeedback inverter disabled at quiescent voltage level

    Note: See Table 4-2 in Section 4.0 “Reset” for time-outs due to Sleep and MCLR Reset.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 32 © 2008 Microchip Technology Inc.

    NOTES:

  • © 2008 Microchip Technology Inc. Advance Information DS39631E-page 33

    PIC18F2420/2520/4420/4520

    3.0 POWER-MANAGED MODESPIC18F2420/2520/4420/4520 devices offer a total ofseven operating modes for more efficient power-management. These modes provide a variety ofoptions for selective power conservation in applicationswhere resources may be limited (i.e., battery-powereddevices).

    There are three categories of power-managed modes:

    • Run modes• Idle modes • Sleep mode

    These categories define which portions of the deviceare clocked and sometimes, what speed. The Run andIdle modes may use any of the three available clocksources (primary, secondary or internal oscillatorblock); the Sleep mode does not use a clock source.

    The power-managed modes include several power-saving features offered on previous PIC® devices. Oneis the clock switching feature, offered in other PIC18devices, allowing the controller to use the Timer1oscillator in place of the primary oscillator. Alsoincluded is the Sleep mode, offered by all PIC devices,where all device clocks are stopped.

    3.1 Selecting Power-Managed ModesSelecting a power-managed mode requires twodecisions: if the CPU is to be clocked or not and theselection of a clock source. The IDLEN bit(OSCCON) controls CPU clocking, while theSCS bits (OSCCON) select the clocksource. The individual modes, bit settings, clock sourcesand affected modules are summarized in Table 3-1.

    3.1.1 CLOCK SOURCESThe SCS bits allow the selection of one of threeclock sources for power-managed modes. They are:

    • the primary clock, as defined by the FOSC Configuration bits

    • the secondary clock (the Timer1 oscillator)• the internal oscillator block (for RC modes)

    3.1.2 ENTERING POWER-MANAGED MODES

    Switching from one power-managed mode to anotherbegins by loading the OSCCON register. TheSCS bits select the clock source and determinewhich Run or Idle mode is to be used. Changing thesebits causes an immediate switch to the new clocksource, assuming that it is running. The switch mayalso be subject to clock transition delays. These arediscussed in Section 3.1.3 “Clock Transitions andStatus Indicators” and subsequent sections.Entry to the power-managed Idle or Sleep modes istriggered by the execution of a SLEEP instruction. Theactual mode that results depends on the status of theIDLEN bit.

    Depending on the current mode and the mode beingswitched to, a change to a power-managed mode doesnot always require setting all of these bits. Manytransitions may be done by changing the oscillator selectbits, or changing the IDLEN bit, prior to issuing a SLEEPinstruction. If the IDLEN bit is already configuredcorrectly, it may only be necessary to perform a SLEEPinstruction to switch to the desired mode.

    TABLE 3-1: POWER-MANAGED MODES

    ModeOSCCON Bits Module Clocking

    Available Clock and Oscillator SourceIDLEN(1) SCS CPU Peripherals

    Sleep 0 N/A Off Off None – All clocks are disabledPRI_RUN N/A 00 Clocked Clocked Primary – LP, XT, HS, HSPLL, RC, EC and

    Internal Oscillator Block(2).This is the normal full-power execution mode.

    SEC_RUN N/A 01 Clocked Clocked Secondary – Timer1 OscillatorRC_RUN N/A 1x Clocked Clocked Internal Oscillator Block(2)

    PRI_IDLE 1 00 Off Clocked Primary – LP, XT, HS, HSPLL, RC, ECSEC_IDLE 1 01 Off Clocked Secondary – Timer1 OscillatorRC_IDLE 1 1x Off Clocked Internal Oscillator Block(2)

    Note 1: IDLEN reflects its value when the SLEEP instruction is executed.2: Includes INTOSC and INTOSC postscaler, as well as the INTRC source.

  • PIC18F2420/2520/4420/4520

    DS39631E-page 34 Advance Information © 2008 Microchip Technology Inc.

    3.1.3 CLOCK TRANSITIONS AND STATUS INDICATORS

    The length of the transition between clock sources isthe sum of two cycles of the old clock source and threeto four cycles of the new clock source. This formulaassumes that the new clock source is stable.

    Three bits indicate the current clock source and itsstatus. They are:

    • OSTS (OSCCON) • IOFS (OSCCON) • T1RUN (T1CON)

    In general, only one of these bits will be set while in agiven power-managed mode. When the OSTS bit isset, the primary clock is providing the device clock.When the IOFS bit is set, the INTOSC output isproviding a stable 8 MHz clock source to a divider thatactually drives the device clock. When the T1RUN bit isset, the Timer1 oscillator is providing the clock. If noneof these bits are set, then either the INTRC clocksource is clocking the device or the INTOSC source isnot yet stable.

    If the internal oscillator block is configured as theprimary clock source by the FOSC Configurationbits, then both the OSTS and IOFS bits may be setwhen in PRI_RUN or PRI_IDLE modes. This indicatesthat the primary clock (INTOSC output) is generating astable 8 MHz output. Entering another power-managedRC mode at the same frequency would clear the OSTSbit.

    3.1.4 MULTIPLE SLEEP COMMANDSThe power-managed mode that is invoked with theSLEEP instruction is determined by the setting of theIDLEN bit at the time the instruction is executed. Ifanother SLEEP instruction is executed, the device willenter the power-managed mode specified by IDLEN atthat time. If IDLEN has changed, the device will enterthe new power-managed mode specified by the newsetting.

    3.2 Run ModesIn the Run modes, clocks to both the core andperipherals are active. The difference between thesemodes is the clock source.

    3.2.1 PRI_RUN MODEThe PRI_RUN mode is the normal, full-power execu-tion mode of the microcontroller. This is also the defaultmode upon a device Reset unless Two-Speed Start-upis enabled (see Section 23.3 “Two-Speed Start-up”for details). In this mode, the OSTS bit is set. The IOFSbit may be set if the internal oscillator block is theprimary clock source (see Section 2.7.1 “OscillatorControl Register”).

    3.2.2 SEC_RUN MODEThe SEC_RUN mode is the compatible mode to the“clock switching” feature offered in other PIC18devices. In this mode, the CPU and peripherals areclocked from the Timer1 oscillator. This gives users theoption of lower power consumption while still using ahigh-accuracy clock source.

    SEC_RUN mode is entered by setting the SCSbits to ‘01’. The device clock source is switched to theTimer1 oscillator (see Figure 3-1), the primary oscilla-tor is shut down, the T1RUN bit (T1CON) is set andthe OSTS bit is cleared.

    On transitions from SEC_RUN mode to PRI_RUNmode, the peripherals and CPU continue to be clockedfrom the Timer1 oscillator while the primary clock isstarted. When the primary clock becomes ready, aclock switch back to the primary clock occurs (seeFigure 3-2). When the clock switch is complete, theT1RUN bit is cleared, the OSTS bit is set and theprimary clock is providing the clock. The IDLEN andSCS bits are not affected by the wake-up; the Timer1oscillator continues to run.

    Note 1: Caution should be used when modifying asingle IRCF bit. If VDD is less than 3V, it ispossible to sel