|
If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
Datasheet File OCR Text: |
? 2010-2012 microchip technology inc. ds41412f pic18(l)f2x/4xk22 data sheet 28/40/44-pin, low-power, high-performance microcontrollers with xlp technology
ds41412f-page 2 ? 2010-2012 microchip technology inc. 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 microchip devices in life support and/or safety applications is entirely at the buyer?s risk, and the buyer agrees to defend, indemnify and hold harmless microchip from any and all damages, claims, suits, or expenses resulting from such use. no licenses are conveyed, implicitly or otherwise, under any microchip intellectual property rights. trademarks the microchip name and logo, the microchip logo, dspic, k ee l oq , k ee l oq logo, mplab, pic, picmicro, picstart, pic 32 logo, rfpic and uni/o are registered trademarks of microchip technology incorporated in the u.s.a. and other countries. filterlab, hampshire, hi-tech c, linear active thermistor, mxdev, mxlab, seeval and the embedded control solutions company are registered trademarks of microchip technology incorporated in the u.s.a. analog-for-the-digital age, app lication maestro, chipkit, chipkit logo, codeguard, dspicdem, dspicdem.net, dspicworks, dsspeak, ecan, economonitor, fansense, hi-tide, in-circuit serial programming, icsp, mindi, miwi, mpasm, mplab certified logo, mplib, mplink, mtouch, omniscient code generation, picc, picc-18, picdem, picdem.net, pickit, pictail, real ice, rflab, select mode, total endurance, tsharc, uniwindriver, 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. ? 2010-2012, microchip technology incorporated, printed in the u.s.a., all rights reserved. printed on recycled paper. isbn: 9781620763131 note the following details of the code protection feature on microchip devices: ? microchip products meet the specification cont ained in their particular microchip data sheet. ? microchip believes that its family of products is one of the mo st secure families of its kind on the market today, when used i n 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 produc ts 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 c an 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 co mmitted to continuously improvi ng the code protection features of our products. attempts to break microchip?s c ode protection feature may be a violation of the digital millennium copyright act. if such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that act. microchip received iso/ts-16949:2009 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, k ee l oq ? 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. quality management s ystem certified by dnv == iso/ts 16949 == ? 2010-2012 microchip technology inc. ds41412f-page 3 pic18(l)f2x/4xk22 high-performance risc cpu: ? c compiler optimized architecture: - optional extended instruction set designed to optimize re-entrant code ? up to 1024 bytes data eeprom ? up to 64 kbytes linear program memory addressing ? up to 3896 bytes linear data memory address- ing ? up to 16 mips operation ? 16-bit wide instructions, 8-bit wide data path ? priority levels for interrupts ? 31-level, software accessible hardware stack ? 8 x 8 single-cycle hardware multiplier flexible oscillator structure: ? precision 16 mhz internal oscillator block: - factory calibrated to 1% - selectable frequencies, 31 khz to 16 mhz - 64 mhz performance available using pll ? no external components required ? four crystal modes up to 64 mhz ? two external clock modes up to 64 mhz ? 4x phase lock loop (pll) ? secondary oscillator using timer1 @ 32 khz ? fail-safe clock monitor: - allows for safe shutdown if peripheral clock stops - two-speed oscillator start-up analog features: ? analog-to-digital converter (adc) module: - 10-bit resolution, up to 30 external channels - auto-acquisition capability - conversion available during sleep - fixed voltage reference (fvr) channel - independent input multiplexing ? analog comparator module: - two rail-to-rail analog comparators - independent input multiplexing ? digital-to-analog converter (dac) module: - fixed voltage reference (fvr) with 1.024v, 2.048v and 4.096v output levels - 5-bit rail-to-rail resistive dac with positive and negative reference selection ? charge time measurement unit (ctmu) module: - supports capacitive touch sensing for touch screens and capacitive switches extreme low-power management pic18(l)f2x/4xk22 with xlp: ? sleep mode: 20 na, typical ? watchdog timer: 300 na, typical ? timer1 oscillator: 800 na @ 32 khz ? peripheral module disable special microcontroller features: ? 2.3v to 5.5v operation ? pic18fxxk22 devices ? 1.8v to 3.6v operation ? pic18lfxxk22 devices ? self-programmable under software control ? high/low-voltage detection (hlvd) module: - programmable 16-level - interrupt on high/low-voltage detection ? programmable brown-out reset (bor): - with software enable option - configurable shutdown in sleep ? extended watchdog timer (wdt): - programmable period from 4 ms to 131s ? in-circuit serial programming? (icsp?): - single-supply 3v ? in-circuit debug (icd) peripheral highlights: ? up to 35 i/o pins plus 1 input-only pin: - high-current sink/source 25 ma/25 ma - three programmable external interrupts - four programmable interrupt-on-change - nine programmable weak pull-ups - programmable slew rate ?sr latch: - multiple set/reset input options ? two capture/compare/pwm (ccp) modules ? three enhanced ccp (eccp) modules: - one, two or four pwm outputs - selectable polarity - programmable dead time - auto-shutdown and auto-restart - pwm steering ? two master synchronous serial port (mssp) modules: - 3-wire spi (supports all 4 modes) -i 2 c? master and slave modes with address mask 28/40/44-pin, low-power, high-performance microcontrollers with xlp technology pic18(l)f2x/4xk22 ds41412f-page 4 ? 2010-2012 microchip technology inc. ? two enhanced universal synchronous asynchronous receiver transmitter (eusart) modules: - supports rs-485, rs-232 and lin - rs-232 operation using internal oscillator - auto-wake-up on break - auto-baud detect device program memory data memory i/o (1) 10-bit a/d channels (2) ccp eccp (full-bridge) eccp (half-bridge) mssp eusart comparator ctmu bor/lvd sr latch 8-bit timer 16-bit timer flash (bytes) # single-word instructions sram (bytes) eeprom (bytes) spi i 2 c? pic18(l)f23k22 8k 4096 512 256 25 19 2 1 2 2 2 2 2 y y y 3 4 pic18(l)f24k22 16k 8192 768 256 25 19 2 1 2 2 2 2 2 yy y 34 pic18(l)f25k22 32k 16384 1536 256 25 19 2 1 2 2 2 2 2 yy y 34 pic18(l)f26k22 64k 32768 3896 1024 25 19 2 1 2 2 2 2 2 yy y 34 pic18(l)f43k22 8k 4096 512 256 36 30 2 2 1 2 2 2 2 yy y 34 pic18(l)f44k22 16k 8192 768 256 36 30 2 2 1 2 2 2 2 yy y 34 pic18(l)f45k22 32k 16384 1536 256 36 30 2 2 1 2 2 2 2 yy y 34 pic18(l)f46k22 64k 32768 3896 1024 36 30 2 2 1 2 2 2 2 yy y 34 note 1: one pin is input only. 2: channel count includes internal fvr and dac channels. ? 2010-2012 microchip technology inc. ds41412f-page 5 pic18(l)f2x/4xk22 pin diagrams (28-pin) 28-pin pdip, soic, ssop 10 11 2 3 4 5 6 1 8 7 9 12 13 14 15 16 17 18 19 20 23 24 25 26 27 28 22 21 mclr /v pp /re3 ra0 ra1 ra2 ra3 ra4 ra5 v ss ra7 ra6 rc0 rc1 rc2 rc3 rb7/pgd rb6/pgc rb5 rb4 rb3 rb2 rb1 rb0 v dd v ss rc7 rc6 rc5 rc4 pic18(l)f2xk22 10 11 2 3 6 1 18 19 20 21 22 12 13 14 15 8 7 16 17 23 24 25 26 27 28 9 rc0 5 4 rb7/pgd rb6/pgc rb5 rb4 rb3 rb2 rb1 rb0 v dd v ss rc7 rc6 rc5 rc4 mclr /v pp /re3 ra0 ra1 ra2 ra3 ra4 ra5/ v ss ra7 ra6 rc1 rc2 rc3 pic18(l)f2xk22 28-pin qfn, uqfn (1) note 1: the 28-pin uqfn package is available only for pic18(l)f23k22 and pic18(l)f24k22. pic18(l)f2x/4xk22 ds41412f-page 6 ? 2010-2012 microchip technology inc. pin diagrams (40-pin) 40-pin pdip rb7/pgd rb6/pgc rb5 rb4 rb3 rb2 rb1 rb0 v dd v ss rd7 rd6 rd5 rd4 rc7 rc6 rc5 rc4 rd3 rd2 mclr /v pp /re3 ra0 ra1 ra2 ra3 ra4 ra5 re0 re1 re2 v dd v ss ra7 ra6 rc0 rc1 rc2 rc3 rd0 rd1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 pic18(l)f4xk22 10 2 3 4 5 6 1 17 18 19 20 11 12 13 14 34 8 7 40 39 38 37 36 35 15 27 28 29 30 21 22 23 24 25 26 32 31 9 33 ra3 ra2 ra1 ra0 mclr /v pp /re3 rb3 pgd/rb7 pgc/rb6 rb5 rb4 rc6 rc5 rc4 rd3 rd2 rd1 rd0 rc3 rc2 rc1 ra6 ra7 v ss v dd re2 re1 re0 ra5 ra4 rc7 rd4 rd5 rd6 rd7 v ss v dd rb0 rb1 rb2 40-pin uqfn pic18(l)f4xk22 rc0 16 ? 2010-2012 microchip technology inc. ds41412f-page 7 pic18(l)f2x/4xk22 pin diagrams (44-pin) 44-pin qfn 44-pin tqfp 10 11 2 3 6 1 18 19 20 21 22 12 13 14 15 38 8 7 44 43 42 41 40 39 16 17 29 30 31 32 33 23 24 25 26 27 28 36 34 35 9 37 ra3 ra2 ra1 ra0 mclr /v pp /re3 nc pgd/rb7 pgc/rb6 rb5 rb4 nc rc6 rc5 rc4 rd3 rd2 rd1 rd0 rc3 rc2 nc nc rc0 ra6 ra7 v ss v dd re2 re1 re0 ra5 ra4 rc7 rd4 rd5 rd6 v ss v dd rb0 rb1 rb2 rb3 rd7 5 4 rc1 pic18(l)f4xk22 10 11 2 3 4 5 6 1 18 19 20 21 22 12 13 14 15 38 8 7 44 43 42 41 40 39 16 17 29 30 31 32 33 23 24 25 26 27 28 36 34 35 9 37 ra3 ra2 ra1 ra0 mclr /v pp /re3 rb3 pgd/rb7 pgc/rb6 rb5 rb4 nc rc6 rc5 rc4 rd3 rd2 rd1 rd0 rc3 rc2 rc1 rc0 ra6 ra7 v ss v ss v dd v dd re2 re1 re0 ra5 ra4 rc7 rd4 rd5 rd6 rd7 v ss v dd v dd rb0 rb1 rb2 pic18(l)f4xk22 pic18(l)f2x/4xk22 ds41412f-page 8 ? 2010-2012 microchip technology inc. table 1: pic18(l)f2xk22 pin summary 28-ssop, soic 28-spdip 28-qfn, uqfn i/o analog comparator ctmu sr latch reference (e)ccp eusart mssp timers interrupts pull-up basic 2 27 ra0 an0 c12in0- 3 28 ra1 an1 c12in1- 4 1 ra2 an2 c2in+ v ref - dacout 5 2 ra3 an3 c1in+ v ref + 6 3 ra4 c1out srq ccp5 t0cki 7 4 ra5 an4 c2out srnq hlvdin ss 1 10 7 ra6 osc2 clko 9 6 ra7 osc1 clki 21 18 rb0 an12 sri ccp4 flt0 ss 2int0y 22 19 rb1 an10 c12in3- p1c sck2 scl2 int1 y 23 20 rb2 an8 cted1 p1b sdi2 sda2 int2 y 24 21 rb3 an9 c12in2- cted2 ccp2 p2a (1) sdo2 y 25 22 rb4 an11 p1d t5g ioc y 26 23 rb5 an13 ccp3 p3a (4) p2b (3) t1g t3cki (2) ioc y 27 24 rb6 tx2/ck2 ioc y pgc 28 25 rb7 rx2/dt2 ioc y pgd 11 8 rc0 p2b (3) sosco t1cki t3cki (2) t3g 12 9 rc1 ccp2 p2a (1) sosci 13 10 rc2 an14 ctpls ccp1 p1a t5cki 14 11 rc3 an15 sck1 scl1 15 12 rc4 an16 sdi1 sda1 16 13 rc5 an17 sdo1 17 14 rc6 an18 ccp3 p3a (4) tx1/ck1 18 15 rc7 an19 p3b rx1/dt1 126 re3 mclr v pp 8 5 v ss 19 16 v ss 20 17 v dd note 1: ccp2/p2a multiplexed in fuses. 2: t3cki multiplexed in fuses. 3: p2b multiplexed in fuses. 4: ccp3/p3a multiplexed in fuses. ? 2010-2012 microchip technology inc. ds41412f-page 9 pic18(l)f2x/4xk22 table 2: pic18(l)f4xk22 pin summary 40-pdip 40-uqfn 44-tqfp 44-qfn i/o analog comparator ctmu sr latch reference (e)ccp eusart mssp timers interrupts pull-up basic 2 17 19 19 ra0 an0 c12in0- 3 18 20 20 ra1 an1 c12in1- 41921 21ra2an2c2in+ v ref - dacout 5 20 22 22 ra3 an3 c1in+ v ref + 6 21 23 23 ra4 c1out srq t0cki 7 22 24 24 ra5 an4 c2out srnq hlvdin ss 1 14 29 31 33 ra6 osc2 clko 13 28 30 32 ra7 osc1 clki 33 8 8 9 rb0 an12 sri flt0 int0 y 34 9 9 10 rb1 an10 c12in3- int1 y 35 10 10 11 rb2 an8 cted1 int2 y 36 11 11 12 rb3 an9 c12in2- cted2 ccp2 p2a (1) y 37 12 14 14 rb4 an11 t5g ioc y 38 13 15 15 rb5 an13 ccp3 p3a (3) t1g t3cki (2) ioc y 39 14 16 16 rb6 ioc y pgc 40 15 17 17 rb7 ioc y pgd 15 30 32 34 rc0 p2b (4) sosco t1cki t3cki (2) t3g 16 31 35 35 rc1 ccp2 (1) p2a sosci 17 32 36 36 rc2 an14 ctpls ccp1 p1a t5cki 18 33 37 37 rc3 an15 sck1 scl1 23 38 42 42 rc4 an16 sdi1 sda1 24 39 43 43 rc5 an17 sdo1 25 40 44 44 rc6 an18 tx1 ck1 26 1 1 1 rc7 an19 rx1 dt1 19 34 38 38 rd0 an20 sck2 scl2 20 35 39 39 rd1 an21 ccp4 sdi2 sda2 21 36 40 40 rd2 an22 p2b (4) 22 37 41 41 rd3 an23 p2c ss 2 27 2 2 2 rd4 an24 p2d sd02 28 3 3 3 rd5 an25 p1b 29 4 4 4 rd6 an26 p1c tx2 ck2 30 5 5 5 rd7 an27 p1d rx2 dt2 8 23 25 25 re0 an5 ccp3 p3a (3) note 1: ccp2 multiplexed in fuses. 2: t3cki multiplexed in fuses. 3: ccp3/p3a multiplexed in fuses. 4: p2b multiplexed in fuses. pic18(l)f2x/4xk22 ds41412f-page 10 ? 2010-2012 microchip technology inc. 9 24 26 26 re1 an6 p3b 10 25 27 27 re2 an7 ccp5 1 16 18 18 re3 y mclr v pp 11 32 7, 26 7 28 7,8 28, 29 v dd 12 31 6, 27 6 29 6 30, 31 v ss ??12, 13 33, 34 13 nc table 2: pic18(l)f4xk22 pin summary (continued) 40-pdip 40-uqfn 44-tqfp 44-qfn i/o analog comparator ctmu sr latch reference (e)ccp eusart mssp timers interrupts pull-up basic note 1: ccp2 multiplexed in fuses. 2: t3cki multiplexed in fuses. 3: ccp3/p3a multiplexed in fuses. 4: p2b multiplexed in fuses. ? 2010-2012 microchip technology inc. ds41412f-page 11 pic18(l)f2x/4xk22 table of contents 1.0 device overview ............................................................................................................. .......................................................... 13 2.0 oscillator module (with fail-safe clock monitor)) ........................................................................... ......................................... 27 3.0 power-managed modes ......................................................................................................... ................................................... 47 4.0 reset ....................................................................................................................... .................................................................. 59 5.0 memory organization ......................................................................................................... ....................................................... 69 6.0 flash program memory........................................................................................................ ..................................................... 95 7.0 data eeprom memory .......................................................................................................... ................................................ 105 8.0 8 x 8 hardware multiplier................................................................................................... ....................................................... 111 9.0 interrupts .................................................................................................................. ................................................................ 113 10.0 i/o ports .................................................................................................................. ................................................................ 135 11.0 timer0 module .............................................................................................................. .......................................................... 159 12.0 timer1/3/5 module with gate control........................................................................................ .............................................. 163 13.0 timer2/4/6 module .......................................................................................................... ........................................................ 175 14.0 capture/compare/pwm modules ................................................................................................ ........................................... 179 15.0 master synchronous serial port (mssp1 and mssp2) module .................................................................... .......................... 211 16.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) .................................................. ............ 267 17.0 analog-to-digital converter (adc) module ................................................................................... .......................................... 297 18.0 comparator module.......................................................................................................... ........................................................ 311 19.0 charge time measurement unit (ctmu)........................................................................................ ........................................ 321 20.0 sr l atch ............................................................................................................................... .................................................. 337 21.0 fixed voltage reference (fvr) .............................................................................................. ................................................ 343 22.0 digital-to-analog converter (dac) module ................................................................................... .......................................... 345 23.0 high/low-voltage detect (hlvd) ............................................................................................. ............................................... 349 24.0 special features of the cpu ................................................................................................ ................................................... 355 25.0 instruction set summary .................................................................................................... ..................................................... 373 26.0 development support........................................................................................................ ...................................................... 423 27.0 electrical characteristics ................................................................................................. ........................................................ 427 28.0 dc and ac characteristics graphs and tables ................................................................................ ...................................... 467 29.0 packaging information...................................................................................................... ....................................................... 523 appendix a: revision history................................................................................................... ......................................................... 545 appendix b: device differences................................................................................................. ....................................................... 546 index .......................................................................................................................... ....................................................................... 547 the microchip web site ......................................................................................................... ........................................................... 557 customer change notification service ........................................................................................... .................................................. 557 customer support............................................................................................................... .............................................................. 557 reader response ................................................................................................................ ............................................................. 558 product identification system .................................................................................................. ......................................................... 559 pic18(l)f2x/4xk22 ds41412f-page 12 ? 2010-2012 microchip technology inc. to our valued customers it is our intention to provide our valued customers with the be st documentation possible to ensure successful use of your micro chip 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 comments regardi ng this publication, please contact the marketing communications 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 s heet, 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 vers ion number, (e.g., ds30000a is version a of document ds30000). errata an errata sheet, describing minor operational differences fr om the data sheet and recommended workarounds, may exist for curren t devices. as device/documentation issues become known to us, we will publish an errata sheet. the errata will specify the revisi on 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, re vision 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. ? 2010-2012 microchip technology inc. ds41412f-page 13 pic18(l)f2x/4xk22 1.0 device overview this document contains device specific information for the following devices: this family offers the advantages of all pic18 microcontrollers ? namely, high computational performance at an economical price ? with the addition of high-endurance, flash program memory. on top of these features, the pic18(l)f2x/4xk22 family introduces design enhancements that make these microcontrollers a logical choice for many high- performance, power sensitive applications. 1.1 new core features 1.1.1 xlp technology all of the devices in the pic18(l)f2x/4xk22 family incorporate a range of features that can significantly 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 27.0 ?electri- cal characteristics? for values. 1.1.2 multiple oscillator options and features all of the devices in the pic18(l)f2x/4xk22 family offer ten different oscillator options, allowing users a wide range of choices in developing application hardware. 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 contains a 16 mhz hfintosc oscillator and a 31 khz lfintosc oscillator, which together provide eight user selectable clock frequencies, from 31 khz to 16 mhz. 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 external and internal oscillator modes, which allows clock speeds of up to 64 mhz. used with the internal oscillator, the pll gives users a complete selection of clock speeds, from 31 khz to 64 mhz ? all without using an external crystal or clock circuit. besides its availability as a clock source, the internal oscillator block provides a stable reference source that gives the family additional features for robust operation: ? fail-safe clock monitor: this option constantly monitors the main clock source against a reference signal provided by the lfintosc. if a clock failure occurs, the controller is switched to the internal oscillator block, allowing for continued 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. ? pic18f23k22 ? pic18lf23k22 ? pic18f24k22 ? pic18lf24k22 ? pic18f25k22 ? pic18lf25k22 ? pic18f26k22 ? pic18lf26k22 ? pic18f43k22 ? pic18lf43k22 ? pic18f44k22 ? pic18lf44k22 ? pic18f45k22 ? pic18lf45k22 ? pic18f46k22 ? pic18lf46k22 pic18(l)f2x/4xk22 ds41412f-page 14 ? 2010-2012 microchip technology inc. 1.2 other special features ? memory endurance: the flash cells for both program memory and data eeprom are rated to last for many thousands of erase/write cycles ? up to 10k for program memory and 100k 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 inter- nal 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 pic18(l)f2x/ 4xk22 family introduces an optional extension to the pic18 instruction set, which adds eight new instructions and an indexed addressing mode. this extension, enabled as a device configuration 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 disabling pwm outputs on interrupt or other select conditions - auto-restart, to reactivate outputs once the condition has cleared - output steering to selectively enable one or more of four outputs to provide the pwm signal. ? enhanced addressable eusart: 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, reduce code overhead. ? extended watchdog timer (wdt): this enhanced version incorporates a 16-bit postscaler, allowing an extended time-out range that is stable across operating voltage and temperature. see section 27.0 ?electrical characteristics? for time-out periods. ? charge time measurement unit (ctmu) ? sr latch output: 1.3 details on individual family members devices in the pic18(l)f2x/4xk22 family are avail- able in 28-pin and 40/44-pin packages. the block dia- gram for the device family is shown in figure 1-1 . the devices have the following differences: 1. flash program memory 2. data memory sram 3. data memory eeprom 4. a/d channels 5. i/o ports 6. eccp modules (full/half bridge) 7. input voltage range/power consumption all other features for devices in this family are identical. these are summarized in ta b l e 1 - 1 . the pinouts for all devices are listed in the pin summary tables: tab l e 1 and tab le 2 , and i/o description tables: table 1-2 and ta bl e 1 - 3 . ? 2010-2012 microchip technology inc. ds41412f-page 15 pic18(l)f2x/4xk22 table 1-1: device features features pic18f23k22 pic18lf23k22 pic18f24k22 pic18lf24k22 pic18f25k22 pic18lf25k22 pic18f26k22 pic18lf26k22 pic18f43k22 pic18lf43k22 pic18f44k22 pic18lf44k22 pic18f45k22 pic18lf45k22 pic18f46k22 pic18lf46k22 program memory (bytes) 8192 16384 32768 65536 8192 16384 32768 65536 program memory (instructions) 4096 8192 16384 32768 4096 8192 16384 32768 data memory (bytes) 512 768 1536 3896 512 768 1536 3896 data eeprom memory (bytes) 256 256 256 1024 256 256 256 1024 i/o ports a, b, c, e (1) a, b, c, e (1) a, b, c, e (1) a, b, c, e (1) a, b, c, d, e a, b, c, d, e a, b, c, d, e a, b, c, d, e capture/compare/pwm mod- ules (ccp) 22222222 enhanced ccp modules (eccp) - half bridge 22221111 enhanced ccp modules (eccp) - full bridge 11112222 10-bit analog-to-digital module (adc) 2 internal 17 input 2 internal 17 input 2 internal 17 input 2 internal 17 input 2 internal 28 input 2 internal 28 input 2 internal 28 input 2 internal 28 input packages 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 28-pin uqfn 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 28-pin uqfn 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 40-pin pdip 40-pin uqfn 44-pin qfn 44-pin tqfp 40-pin pdip 40-pin uqfn 44-pin qfn 44-pin tqfp 40-pin pdip 40-pin uqfn 44-pin qfn 44-pin tqfp 40-pin pdip 40-pin uqfn 44-pin qfn 44-pin tqfp interrupt sources 33 timers (16-bit) 4 serial communications 2 mssp, 2 eusart sr latch ye s charge time measurement unit module (ctmu) ye s programmable high/low-voltage detect (hlvd) ye s programmable brown-out reset (bor) ye s resets (and delays) por, bor, reset instruction, stack overflow, stack underflow (pwrt, ost), mclr , wdt instruction set 75 instructions; 83 with extended instruction set enabled operating frequency dc - 64 mhz note 1: porte contains the single re3 read-only bit. pic18(l)f2x/4xk22 ds41412f-page 16 ? 2010-2012 microchip technology inc. figure 1-1: pic18(l)f2x/4xk22 family block diagram instruction decode and control data latch data memory address latch data address<12> 12 access bsr fsr0 fsr1 fsr2 inc/dec logic address 4 12 4 pch pcl pclath 8 31-level stack program counter prodl prodh 8 x 8 multiply 8 bitop 8 8 alu<8> 20 8 8 table pointer<21> inc/dec logic 21 8 data bus<8> table latch 8 ir 12 3 rom latch pclatu pcu note 1: re3 is only available when mclr functionality is disabled. 2: osc1/clkin and osc2/clkout are only available in select oscillato r modes and when these pins are not being used as digital i/o. refer to section 2.0 ?oscillator module (with fail-safe clock monitor)? for additional information. 3: full-bridge operation for pic18(l)f4xk22, half-bridge operation for pic18(l)f2xk22. eusart1 comparators mssp1 10-bit adc timer2 timer1 ctmu timer0 ccp4 hlvd eccp1 bor data eeprom w instruction bus <16> stkptr bank 8 state machine control signals decode 8 8 power-up timer oscillator start-up timer power-on reset watchdog timer osc1 (2) osc2 (2) brown-out reset internal oscillator fail-safe clock monitor precision reference band gap mclr (1) block lfintosc oscillator 16 mhz oscillator single-supply programming in-circuit debugger sosco sosci fvr fvr fvr dac address latch program memory (8/16/32/64 kbytes) data latch porta ra0:ra7 portb rb0:rb7 portc rc0:rc7 portd rd0:rd7 timer4 timer6 timer3 timer5 sr latch eusart2 mssp2 ccp5 eccp2 (3) c1/c2 eccp3 porte re0:re2 re3 (1) dac ? 2010-2012 microchip technology inc. ds41412f-page 17 pic18(l)f2x/4xk22 table 1-2: pic18(l)f2xk22 pinout i/o descriptions pin number pin name pin type buffer type description pdip, soic qfn, uqfn 227 ra0/c12in0-/an0 ra0 i/o ttl digital i/o. c12in0- i analog comparators c1 and c2 inverting input. an0 i analog analog input 0. 328 ra1/c12in1-/an1 ra1 i/o ttl digital i/o. c12in1- i analog comparators c1 and c2 inverting input. an1 i analog analog input 1. 41 ra2/c2in+/an2/dacout/v ref - ra2 i/o ttl digital i/o. c2in+ i analog comparator c2 non-inverting input. an2 i analog analog input 2. dacout o analog dac reference output. v ref - i analog a/d reference voltage (low) input. 52 ra3/c1in+/an3/v ref + ra3 i/o ttl digital i/o. c1in+ i analog comparator c1 non-inverting input. an3 i analog analog input 3. v ref + i analog a/d reference voltage (high) input. 63 ra4/ccp5/c1out/srq/t0cki ra4 i/o st digital i/o. ccp5 i/o st capture 5 input/compare 5 output/pwm 5 output. c1out o cmos comparator c1 output. srq o ttl sr latch q output. t0cki i st timer0 external clock input. 74 ra5/c2out/srnq/s s1 /hlvdin/an4 ra5 i/o ttl digital i/o. c2out o cmos comparator c2 output. srnq o ttl sr latch q output. s s1 i ttl spi slave select input (mssp). hlvdin i analog high/low-voltage detect input. an4 i analog analog input 4. 10 7 ra6/clko/osc2 ra6 i/o ttl digital i/o. clko o in rc mode, osc2 pin outputs clkout which has 1/4 the frequency of osc1 and denotes the instruction cycle rate. osc2 o oscillator crystal output. connects to crystal or resonator in crystal oscillator mode. 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 pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. pic18(l)f2x/4xk22 ds41412f-page 18 ? 2010-2012 microchip technology inc. 9 6 ra7/clki/osc1 ra7 i/o ttl digital i/o. clki i cmos external clock source input. always associated with pin function osc1. osc1 i st oscillator crystal input or external clock source input st buffer when configured in rc mode; cmos otherwise. 21 18 rb0/int0/ccp4/flt0/sri/s s2 /an12 rb0 i/o ttl digital i/o. int0 i st external interrupt 0. ccp4 i/o st capture 4 input/compare 4 output/pwm 4 output. flt0 i st pwm fault input for eccp auto-shutdown. sri i st sr latch input. s s2 i ttl spi slave select input (mssp). an12 i analog analog input 12. 22 19 rb1/int1/p1c/sck2/scl2/c12in3-/an10 rb1 i/o ttl digital i/o. int1 i st external interrupt 1. p1c o cmos enhanced ccp1 pwm output. sck2 i/o st synchronous serial clock input/output for spi mode (mssp). scl2 i/o st synchronous serial clock input/output for i 2 c? mode (mssp). c12in3- i analog comparators c1 and c2 inverting input. an10 i analog analog input 10. 23 20 rb2/int2/cted1/p1b/sdi2/sda2/an8 rb2 i/o ttl digital i/o. int2 i st external interrupt 2. cted1 i st ctmu edge 1 input. p1b o cmos enhanced ccp1 pwm output. sdi2 i st spi data in (mssp). sda2 i/o st i 2 c? data i/o (mssp). an8 i analog analog input 8. 24 21 rb3/cted2/p2a/ccp2/sdo2/c12in2-/an9 rb3 i/o ttl digital i/o. cted2 i st ctmu edge 2 input. p2a o cmos enhanced ccp2 pwm output. ccp2 (2) i/o st capture 2 input/compare 2 output/pwm 2 output. sdo2 o ? spi data out (mssp). c12in2- i analog comparators c1 and c2 inverting input. an9 i analog analog input 9. table 1-2: pic18(l)f2 xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip, soic qfn, uqfn 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 pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp 2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. ? 2010-2012 microchip technology inc. ds41412f-page 19 pic18(l)f2x/4xk22 25 22 rb4/ioc0/p1d/t5g/an11 rb4 i/o ttl digital i/o. ioc0 i ttl interrupt-on-change pin. p1d o cmos enhanced ccp1 pwm output. t5g i st timer5 external clock gate input. an11 i analog analog input 11. 26 23 rb5/ioc1/p2b/p3a/ccp3/t3cki/t1g/an13 rb5 i/o ttl digital i/o. ioc1 i ttl interrupt-on-change pin. p2b (1) o cmos enhanced ccp2 pwm output. p3a (1) o cmos enhanced ccp3 pwm output. ccp3 (1) i/o st capture 3 input/compare 3 output/pwm 3 output. t3cki (2) i st timer3 clock input. t1g i st timer1 external clock gate input. an13 i analog analog input 13. 27 24 rb6/ioc2/tx2/ck2/pgc rb6 i/o ttl digital i/o. ioc2 i ttl interrupt-on-change pin. tx2 o ? eusart asynchronous transmit. ck2 i/o st eusart synchronous clock (see related rxx/dtx). pgc i/o st in-circuit debugger and icsp? programming clock pin. 28 25 rb7/ioc3/rx2/dt2/pgd rb7 i/o ttl digital i/o. ioc3 i ttl interrupt-on-change pin. rx2 i st eusart asynchronous receive. dt2 i/o st eusart synchronous data (see related txx/ckx). pgd i/o st in-circuit debugger and icsp? programming data pin. 11 8 rc0/p2b/t3cki/t3g/t1cki/sosco rc0 i/o ttl digital i/o. p2b (2) o cmos enhanced ccp1 pwm output. t3cki (1) i st timer3 clock input. t3g i st timer3 external clock gate input. t1cki i st timer1 clock input. sosco o ? secondary oscillator output. 12 9 rc1/p2a/ccp2/sosci rc1 i/o ttl digital i/o. p2a o cmos enhanced ccp2 pwm output. ccp2 (1) i/o st capture 2 input/compare 2 output/pwm 2 output. sosci i analog secondary oscillator input. table 1-2: pic18(l)f2xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip, soic qfn, uqfn 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 pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. pic18(l)f2x/4xk22 ds41412f-page 20 ? 2010-2012 microchip technology inc. 13 10 rc2/ctpls/p1a/ccp1/t5cki/an14 rc2 i/o ttl digital i/o. ctpls o ? ctmu pulse generator output. p1a o cmos enhanced ccp1 pwm output. ccp1 i/o st capture 1 input/compare 1 output/pwm 1 output. t5cki i st timer5 clock input. an14 i analog analog input 14. 14 11 rc3/sck1/scl1/an15 rc3 i/o ttl digital i/o. sck1 i/o st synchronous serial clock input/output for spi mode (mssp). scl1 i/o st synchronous serial clock input/output for i 2 c? mode (mssp). an15 i analog analog input 15. 15 12 rc4/sdi1/sda1/an16 rc4 i/o ttl digital i/o. sdi1 i st spi data in (mssp). sda1 i/o st i 2 c? data i/o (mssp). an16 i analog analog input 16. 16 13 rc5/sdo1/an17 rc5 i/o ttl digital i/o. sdo1 o ? spi data out (mssp). an17 i analog analog input 17. 17 14 rc6/p3a/ccp3/tx1/ck1/an18 rc6 i/o ttl digital i/o. p3a (2) o cmos enhanced ccp3 pwm output. ccp3 (2) i/o st capture 3 input/compare 3 output/pwm 3 output. tx1 o ? eusart asynchronous transmit. ck1 i/o st eusart synchronous clock (see related rxx/dtx). an18 i analog analog input 18. 18 15 rc7/p3b/rx1/dt1/an19 rc7 i/o ttl digital i/o. p3b o cmos enhanced ccp3 pwm output. rx1 i st eusart asynchronous receive. dt1 i/o st eusart synchronous data (see related txx/ckx). an19 i analog analog input 19. 1 26 re3/v pp /mclr re3 i st digital input. v pp p programming voltage input. mclr i st active-low master clear (device reset) input. table 1-2: pic18(l)f2 xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip, soic qfn, uqfn 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 pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp 2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. ? 2010-2012 microchip technology inc. ds41412f-page 21 pic18(l)f2x/4xk22 20 17 v dd p ? positive supply for logic and i/o pins. 8, 19 5, 16 v ss p ? ground reference for logic and i/o pins. table 1-2: pic18(l)f2xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip, soic qfn, uqfn 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 pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. table 1-3: pic18(l)f4xk22 pinout i/o descriptions pin number pin name pin type buffer type description pdip tqfp qfn uqfn 21919 17 ra0/c12in0-/an0 ra0 i/o ttl digital i/o. c12in0- i analog comparators c1 and c2 inverting input. an0 i analog analog input 0. 32020 18 ra1/c12in1-/an1 ra1 i/o ttl digital i/o. c12in1- i analog comparators c1 and c2 inverting input. an1 i analog analog input 1. 42121 19 ra2/c2in+/an2/dacout/v ref - ra2 i/o ttl digital i/o. c2in+ i analog comparator c2 non-inverting input. an2 i analog analog input 2. dacout o analog dac reference output. v ref - i analog a/d reference voltage (low) input. 52222 20 ra3/c1in+/an3/v ref + ra3 i/o ttl digital i/o. c1in+ i analog comparator c1 non-inverting input. an3 i analog analog input 3. v ref + i analog a/d reference voltage (high) input. 62323 21 ra4/c1out/srq/t0cki ra4 i/o st digital i/o. c1out o cmos comparator c1 output. srq o ttl sr latch q output. t0cki i st timer0 external clock input. 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 pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. pic18(l)f2x/4xk22 ds41412f-page 22 ? 2010-2012 microchip technology inc. 72424 22 ra5/c2out/srnq/s s1 /hlvdin/an4 ra5 i/o ttl digital i/o. c2out o cmos comparator c2 output. srnq o ttl sr latch q output. s s1 i ttl spi slave select input (mssp1). hlvdin i analog high/low-voltage detect input. an4 i analog analog input 4. 14 31 33 29 ra6/clko/osc2 ra6 i/o ttl digital i/o. clko o ? in rc mode, osc2 pin outputs clkout which has 1/4 the frequency of osc1 and denotes the instruction cycle rate. osc2 o ? oscillator crystal output. connects to crystal or resonator in crystal oscillator mode. 13 30 32 28 ra7/clki/osc1 ra7 i/o ttl digital i/o. clki i cmos external clock source input. always associated with pin function osc1. osc1 i st oscillator crystal input or external clock source input st buffer when configured in rc mode; cmos otherwise. 33 8 9 8 rb0/int0/flt0/sri/an12 rb0 i/o ttl digital i/o. int0 i st external interrupt 0. flt0 i st pwm fault input for eccp auto-shutdown. sri i st sr latch input. an12 i analog analog input 12. 34 9 10 9 rb1/int1/c12in3-/an10 rb1 i/o ttl digital i/o. int1 i st external interrupt 1. c12in3- i analog comparators c1 and c2 inverting input. an10 i analog analog input 10. 35 10 11 10 rb2/int2/cted1/an8 rb2 i/o ttl digital i/o. int2 i st external interrupt 2. cted1 i st ctmu edge 1 input. an8 i analog analog input 8. 36 11 12 11 rb3/cted2/p2a/ccp2/c12in2-/an9 rb3 i/o ttl digital i/o. cted2 i st ctmu edge 2 input. p2a (2) o cmos enhanced ccp2 pwm output. ccp2 (2) i/o st capture 2 input/compare 2 output/pwm 2 output. c12in2- i analog comparators c1 and c2 inverting input. an9 i analog analog input 9. table 1-3: pic18(l)f4 xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip tqfp qfn uqfn 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 pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. ? 2010-2012 microchip technology inc. ds41412f-page 23 pic18(l)f2x/4xk22 37 14 14 12 rb4/ioc0/t5g/an11 rb4 i/o ttl digital i/o. ioc0 i ttl interrupt-on-change pin. t5g i st timer5 external clock gate input. an11 i analog analog input 11. 38 15 15 13 rb5/ioc1/p3a/ccp3/t3cki/t1g/an13 rb5 i/o ttl digital i/o. ioc1 i ttl interrupt-on-change pin. p3a (1) o cmos enhanced ccp3 pwm output. ccp3 (1) i/o st capture 3 input/compare 3 output/pwm 3 output. t3cki (2) i st timer3 clock input. t1g i st timer1 external clock gate input. an13 i analog analog input 13. 39 16 16 14 rb6/ioc2/pgc rb6 i/o ttl digital i/o. ioc2 i ttl interrupt-on-change pin. pgc i/o st in-circuit debugger and icsp? programming clock pin. 40 17 17 15 rb7/ioc3/pgd rb7 i/o ttl digital i/o. ioc3 i ttl interrupt-on-change pin. pgd i/o st in-circuit debugger and icsp? programming data pin. 15 32 34 30 rc0/p2b/t3cki/t3g/t1cki/sosco rc0 i/o st digital i/o. p2b (2) o cmos enhanced ccp1 pwm output. t3cki (1) i st timer3 clock input. t3g i st timer3 external clock gate input. t1cki i st timer1 clock input. sosco o ? secondary oscillator output. 16 35 35 31 rc1/p2a/ccp2/sosci rc1 i/o st digital i/o. p2a (1) o cmos enhanced ccp2 pwm output. ccp2 (1) i/o st capture 2 input/compare 2 output/pwm 2 output. sosci i analog secondary oscillator input. 17 36 36 32 rc2/ctpls/p1a/ccp1/t5cki/an14 rc2 i/o st digital i/o. ctpls o ? ctmu pulse generator output. p1a o cmos enhanced ccp1 pwm output. ccp1 i/o st capture 1 input/compare 1 output/pwm 1 output. t5cki i st timer5 clock input. an14 i analog analog input 14. table 1-3: pic18(l)f4xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip tqfp qfn uqfn 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 pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. pic18(l)f2x/4xk22 ds41412f-page 24 ? 2010-2012 microchip technology inc. 18 37 37 33 rc3/sck1/scl1/an15 rc3 i/o st digital i/o. sck1 i/o st synchronous serial clock input/output for spi mode (mssp). scl1 i/o st synchronous serial clock input/output for i 2 c? mode (mssp). an15 i analog analog input 15. 23 42 42 38 rc4/sdi1/sda1/an16 rc4 i/o st digital i/o. sdi1 i st spi data in (mssp). sda1 i/o st i 2 c? data i/o (mssp). an16 i analog analog input 16. 24 43 43 39 rc5/sdo1/an17 rc5 i/o st digital i/o. sdo1 o ? spi data out (mssp). an17 i analog analog input 17. 25 44 44 40 rc6/tx1/ck1/an18 rc6 i/o st digital i/o. tx1 o ? eusart asynchronous transmit. ck1 i/o st eusart synchronous clock (see related rxx/ dtx). an18 i analog analog input 18. 26 1 1 1 rc7/rx1/dt1/an19 rc7 i/o st digital i/o. rx1 i st eusart asynchronous receive. dt1 i/o st eusart synchronous data (see related txx/ ckx). an19 i analog analog input 19. 19 38 38 34 rd0/sck2/scl2/an20 rd0 i/o st digital i/o. sck2 i/o st synchronous serial clock input/output for spi mode (mssp). scl2 i/o st synchronous serial clock input/output for i 2 c? mode (mssp). an20 i analog analog input 20. 20 39 39 35 rd1/ccp4/sdi2/sda2/an21 rd1 i/o st digital i/o. ccp4 i/o st capture 4 input/compare 4 output/pwm 4 output. sdi2 i st spi data in (mssp). sda2 i/o st i 2 c? data i/o (mssp). an21 i analog analog input 21. table 1-3: pic18(l)f4 xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip tqfp qfn uqfn 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 pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. ? 2010-2012 microchip technology inc. ds41412f-page 25 pic18(l)f2x/4xk22 21 40 40 36 rd2/p2b/an22 rd2 i/o st digital i/o p2b (1) o cmos enhanced ccp2 pwm output. an22 i analog analog input 22. 22 41 41 37 rd3/p2c/s s2 /an23 rd3 i/o st digital i/o. p2c o cmos enhanced ccp2 pwm output. ss2 i ttl spi slave select input (mssp). an23 i analog analog input 23. 27 2 2 2 rd4/p2d/sdo2/an24 rd4 i/o st digital i/o. p2d o cmos enhanced ccp2 pwm output. sdo2 o ? spi data out (mssp). an24 i analog analog input 24. 28 3 3 3 rd5/p1b/an25 rd5 i/o st digital i/o. p1b o cmos enhanced ccp1 pwm output. an25 i analog analog input 25. 29 4 4 4 rd6/p1c/tx2/ck2/an26 rd6 i/o st digital i/o. p1c o cmos enhanced ccp1 pwm output. tx2 o ? eusart asynchronous transmit. ck2 i/o st eusart synchronous clock (see related rxx/ dtx). an26 i analog analog input 26. 30 5 5 5 rd7/p1d/rx2/dt2/an27 rd7 i/o st digital i/o. p1d o cmos enhanced ccp1 pwm output. rx2 i st eusart asynchronous receive. dt2 i/o st eusart synchronous data (see related txx/ ckx). an27 i analog analog input 27. 82525 23 re0/p3a/ccp3/an5 re0 i/o st digital i/o. p3a (2) o cmos enhanced ccp3 pwm output. ccp3 (2) i/o st capture 3 input/compare 3 output/pwm 3 output. an5 i analog analog input 5. 92626 24 re1/p3b/an6 re1 i/o st digital i/o. p3b o cmos enhanced ccp3 pwm output. an6 i analog analog input 6. table 1-3: pic18(l)f4xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip tqfp qfn uqfn 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 pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. pic18(l)f2x/4xk22 ds41412f-page 26 ? 2010-2012 microchip technology inc. 10 27 27 25 re2/ccp5/an7 re2 i/o st digital i/o. ccp5 i/o st capture 5 input/compare 5 output/pwm 5 output an7 i analog analog input 7. 11818 16 re3/v pp /mclr re3 i st digital input. v pp p programming voltage input. mclr i st active-low master clear (device reset) input. 11,32 7, 28 7, 8, 28, 29 7, 26 v dd p ? positive supply for logic and i/o pins. 12,31 6, 29 6,30, 31 6, 27 v ss p ? ground reference for logic and i/o pins. 12,13, 33,34 13 nc table 1-3: pic18(l)f4 xk22 pinout i/o descriptions (continued) pin number pin name pin type buffer type description pdip tqfp qfn uqfn 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 pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3/p3a and ccp2/p2a when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. ? 2010-2012 microchip technology inc. ds41412e-page 27 pic18(l)f2x/4xk22 2.0 oscillator module (with fail-safe clock monitor) 2.1 overview the oscillator module has a wide variety of clock sources and selection features that allow it to be used in a wide range of applications while maximizing perfor- mance and minimizing power consumption. figure 2-1 illustrates a block diagram of the oscillator module. clock sources can be configured from external oscillators, quartz crystal resonators, ceramic resonators and resistor-capacitor (rc) circuits. in addition, the system clock source can be configured from one of three internal oscillators, with a choice of speeds selectable via software. additional clock features include: ? selectable system clock source between external or internal sources via software. ? two-speed start-up mode, which minimizes latency between external oscillator start-up and code execution. ? fail-safe clock monitor (fscm) designed to detect a failure of the external clock source (lp, xt, hs, ec or rc modes) and switch automatically to the internal oscillator. ? oscillator start-up timer (ost) ensures stability of crystal oscillator sources. the primary clock module can be configured to provide one of six clock sources as the primary clock. 1. rc external resistor/capacitor 2. lp low-power crystal 3. xt crystal/resonator 4. intosc internal oscillator 5. hs high-speed crystal/resonator 6. ec external clock the hs and ec oscillator circuits can be optimized for power consumption and oscillator speed using settings in fosc<3:0>. additional fosc<3:0> selections enable ra6 to be used as i/o or clko (f osc /4) for rc, ec and intosc oscillator modes. primary clock modes are selectable by the fosc<3:0> bits of the config1h configuration register. the primary clock operation is further defined by these configuration and register bits: 1. priclken (config1h<5>) 2. prisd (osccon2<2>) 3. pllcfg (config1h<4>) 4. pllen (osctune<6>) 5. hfofst (config3h<3>) 6. ircf<2:0> (osccon<6:4>) 7. mfiosel (osccon2<4>) 8. intsrc (osctune<7>) the hfintosc, mfintosc and lfintosc are factory calibrated high, medium and low-frequency oscillators, respectively, which are used as the internal clock sources. pic18(l)f2x/4xk22 ds41412e-page 28 ? 2010-2012 microchip technology inc. figure 2-1: simplified oscillator system block diagram note 1: details in figure 2-4 . 2: details in figure 2-2 . 3: details in figure 2-3 . 4: details in table 2-1 . 5: the primary oscillator mux uses the intosc branch when fosc<3:0> = 100x. sosco sosci secondary oscillator (sosc) secondary oscillator (1) osc2 osc1 primary oscillator (2) (osc) primary oscillator 0 1 fosc<3:0> (5) pll_select (3) (4) 0 1 4xpll intosc primary clock module low-power mode event switch (scs<1:0>) 01 00 1x secondary oscillator 2 primary clock intosc clock switch mux intosc ircf<2:0> mfiosel intsrc hf-16 mh z hf-8 mh z hf-4 mh z hf-2 mh z hf-1 mh z hf-31.25 kh z hf-250 kh z hf-500 kh z hfintosc mfintosc lfintosc (16 mhz) (500 khz) (31.25 khz) intosc divide circuit internal oscillator mux (3) mf-31.25 kh z mf-250 kh z mf-500 kh z lf-31.25 khz 3 3 internal oscillator soscout priclken prisd en ? 2010-2012 microchip technology inc. ds41412e-page 29 pic18(l)f2x/4xk22 2.2 oscillator control the osccon, osccon2 and osctune registers ( register 2-1 to register 2-3 ) control several aspects of the device clock?s operation, both in full-power operation and in power-managed modes. ? main system clock selection (scs) ? primary oscillator circuit shutdown (prisd) ? secondary oscillator enable (soscgo) ? primary clock frequency 4x multiplier (pllen) ? internal frequency selection bits (ircf, intsrc) ? clock status bits (osts, hfiofs, mfiofs, lfiofs. soscrun, pllrdy) ? power management selection (idlen) 2.2.1 main system clock selection the system clock select bits, scs<1:0>, select the main clock source. the available clock sources are ? primary clock defined by the fosc<3:0> bits of config1h. the primary clock can be the primary oscillator, an external clock, or the internal oscillator block. ? secondary clock (secondary oscillator) ? internal oscillator block (hfintosc, mfintosc and lfintosc). the clock source changes immediately after one or more of the bits is written to, following a brief clock transition interval. the scs bits are cleared to select the primary clock on all forms of reset. 2.2.2 internal frequency selection the internal oscillator frequency select bits (ircf<2:0>) select the frequency output of the internal oscillator block. the choices are the lfintosc source (31.25 khz), the mfintosc source (31.25 khz, 250 khz or 500 khz) and the hfintosc source (16 mhz) or one of the frequencies derived from the hfintosc postscaler (31.25 khz to 8 mhz). if the internal oscillator block is supplying the main clock, changing the states of these bits will have an immedi- ate change on the internal oscillator?s output. on device resets, the output frequency of the internal oscillator is set to the default frequency of 1 mhz. 2.2.3 low frequency selection when a nominal output frequency of 31.25 khz is selected (ircf<2:0> = 000 ), users may choose which internal oscillator acts as the source. this is done with the intsrc bit of the osctune register and mfiosel bit of the osccon2 register. see figure 2-2 and register 2-1 for specific 31.25 khz selection. this option allows users to select a 31.25 khz clock (mfintosc or hfintosc) that can be tuned using the tun<5:0> bits in osctune register, while maintaining power savings with a very low clock speed. lfintosc always remains the clock source for features such as the watchdog timer and the fail-safe clock monitor, regardless of the setting of intsrc and mfiosel bits this option allows users to select the tunable and more precise hfintosc as a clock source, while maintaining power savings with a very low clock speed. 2.2.4 power management the idlen bit of the osccon register determines whether the device goes into sleep mode or one of the idle modes when the sleep instruction is executed. pic18(l)f2x/4xk22 ds41412e-page 30 ? 2010-2012 microchip technology inc. figure 2-2: internal oscillator mux block diagram figure 2-3: pll_select block diagram 111 110 101 100 00 1 000 intosc 250 kh z 500 kh z 31.25 kh z 1 0 1 0 11 10 0 x ircf<2:0> mfiosel intsrc hf-16 mh z hf-8 mh z hf-4 mh z hf-2 mh z hf-1 mh z lf-31.25 k h z mf-31.25 k h z hf-31.25 k h z hf-250 k h z mf-250 k h z hf-500 k h z mf-500 k h z 3 011 010 pll_select pllcfg fosc<3:0> = 100x pllen table 2-1: pll_select truth table primary clock mux source fosc<3:0> pllcfg pllen pll_select fosc (any source) 0000-1111 0 0 0 osc1/osc2 (external source) 0000-0111 1010-1111 1x1 011 intosc (internal source) 1000-1001 x 0 0 x11 ? 2010-2012 microchip technology inc. ds41412e-page 31 pic18(l)f2x/4xk22 figure 2-4: secondary oscilla tor and external clock inputs 0 1 1 0 en soscen soscgo t1soscen t3soscen t5soscen to clock switch module soscout secondary oscillator sosci sosco t1cki t3g t3cki soscen soscen soscen t3g t3cmx t1g t5g t5cki t5g t3cki t1g 1 0 1 0 t1soscen t1clk_ext_src t3clk_ext_src t5clk_ext_src t3soscen t5soscen pic18(l)f2x/4xk22 ds41412e-page 32 ? 2010-2012 microchip technology inc. 2.3 register definitions: oscillator control register 2-1: osccon: os cillator control register r/w-0 r/w-0 r/w-1 r/w-1 r-q r-0 r/w-0 r/w-0 idlen ircf<2:0> osts (1) hfiofs scs<1:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? q = depends on condition -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 idlen: idle enable bit 1 = device enters idle mode on sleep instruction 0 = device enters sleep mode on sleep instruction bit 6-4 ircf<2:0>: internal rc oscillator frequency select bits (2) 111 = hfintosc ? (16 mhz) 110 = hfintosc/2 ? (8 mhz) 101 = hfintosc/4 ? (4 mhz) 100 = hfintosc/8 ? (2 mhz) 011 = hfintosc/16 ? (1 mhz) (3) if intsrc = 0 and mfiosel = 0 : 010 = hfintosc/32 ? (500 khz) 001 = hfintosc/64 ? (250 khz) 000 = lfintosc ? (31.25 khz) if intsrc = 1 and mfiosel = 0 : 010 = hfintosc/32 ? (500 khz) 001 = hfintosc/64 ? (250 khz) 000 = hfintosc/512 ? (31.25 khz) if intsrc = 0 and mfiosel = 1 : 010 = mfintosc ? (500 khz) 001 = mfintosc/2 ? (250 khz) 000 = lfintosc ? (31.25 khz) if intsrc = 1 and mfiosel = 1 : 010 = mfintosc ? (500 khz) 001 = mfintosc/2 ? (250 khz) 000 = mfintosc/16 ? (31.25 khz) bit 3 osts: oscillator start-up time-out status bit 1 = device is running from the clock defined by fosc<3:0> of the config1h register 0 = device is running from the internal oscillator (hfintosc, mfintosc or lfintosc) bit 2 hfiofs: hfintosc frequency stable bit 1 = hfintosc frequency is stable 0 = hfintosc frequency is not stable bit 1-0 scs<1:0>: system clock select bit 1x = internal oscillator block 01 = secondary (sosc) oscillator 00 = primary clock (determined by fosc<3:0> in config1h). note 1: reset state depends on state of the ieso configuration bit. 2: intosc source may be determined by the intsrc bit in osctune and the mfiosel bit in osccon2. 3: default output frequency of hfintosc on reset. ? 2010-2012 microchip technology inc. ds41412e-page 33 pic18(l)f2x/4xk22 register 2-2: osccon2: osci llator control register 2 r-0/0 r-0/q u-0 r/w-0/0 r/w-0/u r/w-1/1 r-x/u r-0/0 pllrdy soscrun ? mfiosel soscgo (1) prisd mfiofs lfiofs bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? q = depends on condition ?1? = bit is set ?0? = bit is cleared x = bit is unknown -n/n = value at por and bor/value at all other resets bit 7 pllrdy: pll run status bit 1 = system clock comes from 4xpll 0 = system clock comes from an oscillator, other than 4xpll bit 6 soscrun: sosc run status bit 1 = system clock comes from secondary sosc 0 = system clock comes from an oscillator, other than sosc bit 5 unimplemented: read as ? 0 ?. bit 4 mfiosel: mfintosc select bit 1 = mfintosc is used in place of hfintosc frequencies of 500 khz, 250 khz and 31.25 khz 0 = mfintosc is not used bit 3 soscgo (1) : secondary oscillator start control bit 1 = secondary oscillator is enabled. 0 = secondary oscillator is shut off if no other sources are requesting it. bit 2 prisd: primary oscillator drive circuit shutdown bit 1 = oscillator drive circuit on 0 = oscillator drive circuit off (zero power) bit 1 mfiofs: mfintosc frequency stable bit 1 = mfintosc is stable 0 = mfintosc is not stable bit 0 lfiofs: lfintosc frequency stable bit 1 = lfintosc is stable 0 = lfintosc is not stable note 1: the soscgo bit is only reset on a por reset. pic18(l)f2x/4xk22 ds41412e-page 34 ? 2010-2012 microchip technology inc. 2.4 clock source modes clock source modes can be classified as external or internal. ? external clock modes rely on external circuitry for the clock source. examples are: clock modules (ec mode), quartz crystal resonators or ceramic resonators (lp, xt and hs modes) and resistor- capacitor (rc mode) circuits. ? internal clock sources are contained internally within the oscillator block. the oscillator block has three internal oscillators: the 16 mhz high- frequency internal oscillator (hfintosc), 500 khz medium-frequency internal oscillator (mfintosc) and the 31.25 khz low-frequency internal oscillator (lfintosc). the system clock can be selected between external or internal clock sources via the system clock select (scs<1:0>) bits of the osccon register. see section 2.11 ?clock switching? for additional information. 2.5 external clock modes 2.5.1 oscillator start-up timer (ost) when the oscillator module is configured for lp, xt or hs modes, the oscillator start-up timer (ost) counts 1024 oscillations from osc1. this occurs following a power-on reset (por) and when the power-up timer (pwrt) has expired (if configured), or a wake-up from sleep. during this time, the program counter does not increment and program execution is suspended. the ost ensures that the oscillator circuit, using a quartz crystal resonator or ceramic resonator, has started and is providing a stable system clock to the oscillator module. when switching between clock sources, a delay is required to allow the new clock to stabilize. these oscillator delays are shown in table 2-2 . in order to minimize latency between external oscillator start-up and code execution, the two-speed clock start-up mode can be selected (see section 2.12 ?two-speed clock start-up mode? ). table 2-2: oscillator delay examples 2.5.2 ec mode the external clock (ec) mode allows an externally generated logic level as the system clock source. when operating in this mode, an external clock source is connected to the osc1 input and the osc2 is available for general purpose i/o. figure 2-5 shows the pin connections for ec mode. the external clock (ec) offers different power modes, low power (eclp), medium power (ecmp) and high power (echp), selectable by the fosc<3:0> bits. each mode is best suited for a certain range of frequencies. the ranges are: ? eclp ? below 500 khz ? ecmp ? between 500 khz and 16 mhz ? echp ? above 16 mhz the oscillator start-up timer (ost) is disabled when ec mode is selected. therefore, there is no delay in operation after a power-on reset (por) or wake-up from sleep. because the pic ? mcu design is fully static, stopping the external clock input will have the effect of halting the device while leaving all data intact. upon restarting the external clock, the device will resume operation as if no time had elapsed. figure 2-5: external clock (ec) mode operation switch from switch to frequency oscillator delay sleep/por lfintosc mfintosc hfintosc 31.25 khz 31.25 khz to 500 khz 31.25 khz to 16 mhz oscillator warm-up delay (t warm ) sleep/por ec, rc dc ? 64 mhz 2 instruction cycles lfintosc (31.25 khz) ec, rc dc ? 64 mhz 1 cycle of each sleep/por lp, xt, hs 32 khz to 40 mhz 1024 clock cycles (ost) sleep/por 4xpll 32 mhz to 64 mhz 1024 clock cycles (ost) + 2 ms lfintosc (31.25 khz) lfintosc hfintosc 31.25 khz to 16 mhz 1 ? s (approx.) osc1/clkin osc2/clkout (1) i/o clock from ext. system pic ? mcu note 1: alternate pin functions are listed in section 1.0 ?device overview? . ? 2010-2012 microchip technology inc. ds41412e-page 35 pic18(l)f2x/4xk22 2.5.3 lp, xt, hs modes the lp, xt and hs modes support the use of quartz crystal resonators or ceramic resonators connected to osc1 and osc2 ( figure 2-6 ). the mode selects a low, medium or high gain setting of the internal inverter- amplifier to support various resonator types and speed. lp oscillator mode selects the lowest gain setting of the internal inverter-amplifier. lp mode current consumption is the least of the three modes. this mode is best suited to drive resonators with a low drive level specification, for example, tuning fork type crystals. xt oscillator mode selects the intermediate gain setting of the internal inverter-amplifier. xt mode current consumption is the medium of the three modes. this mode is best suited to drive resonators with a medium drive level specification. hs oscillator mode offers a medium power (mp) and a high power (hp) option selectable by the fosc<3:0> bits. the mp selections are best suited for oscillator frequencies between 4 mhz and 16 mhz. the hp selection has the highest gain setting of the internal inverter-amplifier and is best suited for frequencies above 16 mhz. hs mode is best suited for resonators that require a high drive setting. figure 2-6: quartz crystal operation (lp, xt or hs mode) figure 2-7: ceramic resonator operation (xt or hs mode) note 1: a series resistor (r s ) may be required for quartz crystals with low drive level. 2: the value of r f varies with the oscillator mode selected (typically between 2 m ? to 10 m ?? . c1 c2 quartz r s (1) osc1/clkin r f (2) sleep to internal logic pic ? mcu crystal osc2/clkout note 1: quartz crystal characteristics vary according to type, package and manufacturer. the user should consult the manufacturer data sheets for specifications and recommended application. 2: always verify oscillator performance over the v dd and temperature range that is expected for the application. 3: for oscillator design assistance, refer to the following microchip application notes: ? an826, ? crystal oscillator basics and crystal selection for rfpic ? and pic ? devices ? (ds00826) ? an849, ? basic pic ? oscillator design ? (ds00849) ? an943, ? practical pic ? oscillator analysis and design ? (ds00943) ? an949, ? making your oscillator work ? (ds00949) note 1: a series resistor (r s ) may be required for ceramic resonators with low drive level. 2: the value of r f varies with the oscillator mode selected (typically between 2 m ? to 10 m ?? . 3: an additional parallel feedback resistor (r p ) may be required for proper ceramic resonator operation. c1 c2 ceramic r s (1) osc1/clkin r f (2) sleep to internal logic pic ? mcu r p (3) resonator osc2/clkout pic18(l)f2x/4xk22 ds41412e-page 36 ? 2010-2012 microchip technology inc. 2.5.4 external rc modes the external resistor-capacitor (rc) modes support the use of an external rc circuit. this allows the designer maximum flexibility in frequency choice while keeping costs to a minimum when clock accuracy is not required. there are two modes: rc and rcio. 2.5.4.1 rc mode in rc mode, the rc circuit connects to osc1. osc2/ clkout outputs the rc oscillator frequency divided by four. this signal may be used to provide a clock for external circuitry, synchronization, calibration, test or other application requirements. figure 2-8 shows the external rc mode connections. figure 2-8: external rc modes 2.5.4.2 rcio mode in rcio mode, the rc circuit is connected to osc1. osc2 becomes a general purpose i/o pin. the rc oscillator frequency is a function of the supply voltage, the resistor (r ext ) and capacitor (c ext ) values and the operating temperature. other factors affecting the oscillator frequency are: ? input threshold voltage variation ? component tolerances ? packaging variations in capacitance the user also needs to take into account variation due to tolerance of external rc components used. 2.6 internal clock modes the oscillator module has three independent, internal oscillators that can be configured or selected as the system clock source. 1. the hfintosc (high-frequency internal oscillator) is factory calibrated and operates at 16 mhz. the frequency of the hfintosc can be user-adjusted via software using the osctune register ( register 2-3 ). 2. the mfintosc (medium-frequency internal oscillator) is factory calibrated and operates at 500 khz. the frequency of the mfintosc can be user-adjusted via software using the osctune register ( register 2-3 ). 3. the lfintosc (low-frequency internal oscillator) is factory calibrated and operates at 31.25 khz. the lfintosc cannot be user- adjusted, but is designed to be stable over temperature and voltage. the system clock speed can be selected via software using the internal oscillator frequency select bits ircf<2:0> of the osccon register. the system clock can be selected between external or internal clock sources via the system clock selection (scs<1:0>) bits of the osccon register. see section 2.11 ?clock switching? for more information. 2.6.1 intosc with i/o or clockout two of the clock modes selectable with the fosc<3:0> bits of the config1h configuration register configure the internal oscillator block as the primary oscillator. mode selection determines whether osc2/clkout/ ra7 will be configured as general purpose i/o (ra7) or f osc /4 (clkout). in both modes, osc1/clkin/ra7 is configured as general purpose i/o. see section 24.0 ?special features of the cpu? for more information. the clkout signal may be used to provide a clock for external circuitry, synchronization, calibration, test or other application requirements. osc2/clkout (1) c ext r ext pic ? mcu osc1/clkin f osc /4 or internal clock v dd v ss recommended values: 10 k ? ? r ext ? 100 k ? c ext > 20 pf note 1: alternate pin functions are listed in section 1.0 ?device overview? . 2: output depends upon rc or rcio clock mode. i/o (2) ? 2010-2012 microchip technology inc. ds41412e-page 37 pic18(l)f2x/4xk22 2.6.1.1 osctune register the hfintosc/mfintosc oscillator circuits are factory calibrated but can be adjusted in software by writing to the tun<5:0> bits of the osctune register ( register 2-3 ). the default value of the tun<5:0> is ? 000000 ?. the value is a 6-bit two?s complement number. when the osctune register is modified, the hfintosc/mfintosc frequency will begin shifting to the new frequency. code execution continues during this shift. there is no indication that the shift has occurred. the tun<5:0> bits in osctune do not affect the lfintosc frequency. operation of features that depend on the lfintosc clock source frequency, such as the power-up timer (pwrt), watchdog timer (wdt), fail-safe clock monitor (fscm) and peripherals, are not affected by the change in frequency. the osctune register also implements the intsrc and pllen bits, which control certain features of the internal oscillator block. the intsrc bit allows users to select which internal oscillator provides the clock source when the 31.25 khz frequency option is selected. this is covered in greater detail in section 2.2.3 ?low frequency selection? . the pllen bit controls the operation of the frequency multiplier, pll, for all primary external clock sources and internal oscillator modes. however, the pll is intended for operation with clock sources between 4 mhz and 16 mhz. for more details about the function of the pllen bit, see section 2.8.2 ?pll in hfin- tosc modes? 2.7 register definitions: oscillator tuning register 2-3: osctune: osci llator tuning register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 intsrc pllen (1) tun<5:0> 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 bit 1 = 31.25 khz device clock derived from the mfintosc or hfintosc source 0 = 31.25 khz device clock derived directly from lfintosc internal oscillator bit 6 pllen: frequency multiplier 4xpll for hfintosc enable bit (1) 1 = pll enabled 0 = pll disabled bit 5-0 tun<5:0>: frequency tuning bits ? use to adjust mfintosc and hfintosc frequencies 011111 = maximum frequency 011110 = ? ? ? 000001 = 000000 = oscillator module (hfintosc and mfintosc) are running at the factory calibrated frequency. 111111 = ? ? ? 100000 = minimum frequency note 1: the pllen bit is active for all the primary clock sources (internal or external) and is designed to operate with clock frequencies between 4 mhz and 16 mhz. pic18(l)f2x/4xk22 ds41412e-page 38 ? 2010-2012 microchip technology inc. 2.7.1 lfintosc the low-frequency internal oscillator (lfintosc) is a 31.25 khz internal clock source. the lfintosc is not tunable, but is designed to be stable across temper- ature and voltage. see section 27.0 ?electrical char- acteristics? for the lfintosc accuracy specifications. the output of the lfintosc can be a clock source to the primary clock or the intosc clock (see figure 2-1 ). the lfintosc is also the clock source for the power- up timer (pwrt), watchdog timer (wdt) and fail-safe clock monitor (fscm). 2.7.2 frequency select bits (ircf) the hfintosc (16 mhz) and mfintosc (500 mhz) outputs connect to a divide circuit that provides frequencies of 16 mhz to 31.25 khz. these divide circuit frequencies, along with the 31.25 khz lfintosc output, are multiplexed to provide a single intosc clock output (see figure 2-1 ). the ircf<2:0> bits of the osccon register, the mfiosel bit of the osccon2 register and the intsrc bit of the osctune register, select the output frequency of the internal oscillators. one of eight frequencies can be selected via software: ?16 mhz ?8 mhz ?4 mhz ?2 mhz ? 1 mhz (default after reset) ? 500 khz (mfintosc or hfintosc) ? 250 khz (mfintosc or hfintosc) ? 31 khz (lfintosc, mfintosc or hfintosc) 2.7.3 intosc frequency drift the factory calibrates the internal oscillator block outputs (hfintosc/mfintosc) for 16 mhz/500 khz. however, this frequency may drift as v dd or temperature changes. it is possible to adjust the hfintosc/mfintosc fre- quency by modifying the value of the tun<5:0> bits in the osctune register. this has no effect on the lfintosc clock source frequency. tuning the hfintosc/mfintosc source requires knowing when to make the adjustment, in which direc- tion it should be made and, in some cases, how large a change is needed. three possible compensation tech- niques are discussed in the following sections. however, other techniques may be used. 2.7.3.1 compensating with the eusart an adjustment may be required when the eusart begins to generate framing errors or receives data with errors while in asynchronous mode. framing errors indicate that the device clock frequency is too high; to adjust for this, decrement the value in osctune to reduce the clock frequency. on the other hand, errors in data may suggest that the clock speed is too low; to compensate, increment osctune to increase the clock frequency. 2.7.3.2 compensating with the timers this technique compares device clock speed to some reference clock. two timers may be used; one timer is clocked by the peripheral clock, while the other is clocked by a fixed reference source, such as the timer1 oscillator. both timers are cleared, but the timer clocked by the reference generates interrupts. when an interrupt occurs, the internally clocked timer is read and both timers are cleared. if the internally clocked timer value is greater than expected, then the internal oscillator block is running too fast. to adjust for this, decrement the osctune register. 2.7.3.3 compensating with the ccp module in capture mode a ccp module can use free running timer1, timer3 or timer5 clocked by the internal oscillator block and an external event with a known period (i.e., ac power frequency). the time of the first event is captured in the ccprxh:ccprxl registers and is recorded for use later. when the second event causes a capture, the time of the first event is subtracted from the time of the second event. since the period of the external event is known, the time difference between events can be calculated. if the measured time is much greater than the calcu- lated time, the internal oscillator block is running too fast; to compensate, decrement the osctune register. if the measured time is much less than the calculated time, the internal oscillator block is running too slow; to compensate, increment the osctune register. ? 2010-2012 microchip technology inc. ds41412e-page 39 pic18(l)f2x/4xk22 2.8 pll frequency multiplier a phase-locked loop (pll) circuit is provided as an option for users who wish to use a lower frequency oscillator circuit or to clock the device up to its highest rated frequency from the crystal oscillator. this may be useful for customers who are concerned with emi due to high-frequency crystals or users who require higher clock speeds from an internal oscillator. 2.8.1 pll in external oscillator modes the pll can be enabled for any of the external oscillator modes using the osc1/osc2 pins by either setting the pllcfg bit (config1h<4>), or setting the pllen bit (osctune<6>). the pll is designed for input frequencies of 4 mhz up to 16 mhz. the pll then multiplies the oscillator output frequency by four to produce an internal clock frequency up to 64 mhz. oscillator frequencies below 4 mhz should not be used with the pll. 2.8.2 pll in hfintosc modes the 4x frequency multiplier can be used with the internal oscillator block to produce faster device clock speeds than are normally possible with the internal oscillator. when enabled, the pll multiplies the hfintosc by four to produce clock rates up to 64 mhz. unlike external clock modes, when internal clock modes are enabled, the pll can only be controlled through software. the pllen control bit of the osctune register is used to enable or disable the pll operation when the hfintosc is used. the pll is designed for input frequencies of 4 mhz up to 16 mhz. pic18(l)f2x/4xk22 ds41412e-page 40 ? 2010-2012 microchip technology inc. 2.9 effects of power-managed modes on the various clock sources for more information about the modes discussed in this section see section 3.0 ?power-managed modes? . a quick reference list is also available in ta b l e 3 - 1 . when pri_idle mode is selected, the designated primary oscillator continues to run without interruption. for all other power-managed modes, the oscillator using the osc1 pin is disabled. the osc1 pin (and osc2 pin, if used by the oscillator) will stop oscillating. in secondary clock modes (sec_run and sec_idle), the secondary oscillator (sosc) is operating and providing the device clock. the secondary oscillator may also run in all power- managed modes if required to clock timer1, timer3 or timer5. in internal oscillator modes (intosc_run and intosc_idle), the internal oscillator block provides the device clock source. the 31.25 khz lfintosc output can be used directly to provide the clock and may be enabled to support various special features, regardless of the power-managed mode (see section 24.3 ?watchdog timer (wdt)? , section 2.12 ?two-speed clock start-up mode? and section 2.13 ?fail-safe clock monitor? for more information on wdt, fail-safe clock monitor and two- speed start-up). the hfintosc and mfintosc outputs may be used directly to clock the device or may be divided down by the postscaler. the hfintosc and mfintosc outputs are disabled when the clock is provided directly from the lfintosc output. when the sleep mode is selected, all clock sources are stopped. since all the transistor switching currents have been stopped, 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 lfintosc is required to support wdt operation. other features may be operating that do not require a device clock source (i.e., ssp slave, psp, intn pins and others). peripherals that may add significant current consumption are listed in section 27.8 ?dc characteristics: input/output characteristics, pic18(l)f2x/4xk22? . 2.10 power-up delays power-up delays are controlled by two timers, 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 is stable under normal circumstances and the primary clock is operating and stable. for additional information on power-up delays, see section 4.6 ?device reset timers? . the first timer is the power-up timer (pwrt), which provides a fixed delay on power-up. it is enabled by clearing (= 0 ) the pwrten configuration bit. the second timer is the oscillator start-up timer (ost), intended to keep the chip in reset until the crystal oscillator is stable (lp, xt and hs modes). the ost does this by counting 1024 oscillator cycles before allowing the oscillator to clock the device. when the pll is enabled with external oscillator modes, the device is kept in reset for an additional 2 ms, following the ost delay, so the pll can lock to the incoming clock frequency. there is a delay of interval t csd , following por, while the controller becomes ready to execute instructions. this delay runs concurrently with any other delays. this may be the only delay that occurs when any of the ec, rc or intiosc modes are used as the primary clock source. when the hfintosc is selected as the primary clock, the main system clock can be delayed until the hfintosc is stable. this is user selectable by the hfofst bit of the config3h configuration register. when the hfofst bit is cleared, the main system clock is delayed until the hfintosc is stable. when the hfofst bit is set, the main system clock starts immediately. in either case, the hfiofs bit of the osccon register can be read to determine whether the hfintosc is operating and stable. ? 2010-2012 microchip technology inc. ds41412e-page 41 pic18(l)f2x/4xk22 table 2-3: osc1 and osc2 pin states in sleep mode 2.11 clock switching the system clock source can be switched between external and internal clock sources via software using the system clock select (scs<1:0>) bits of the osccon register. pic18(l)f2x/4xk22 devices contain circuitry to pre- vent clock ?glitches? when switching between clock sources. a short pause in the device clock occurs dur- ing the clock switch. the length of this pause is the sum of two cycles of the old clock source and three to four cycles of the new clock source. this formula assumes that the new clock source is stable. clock transitions are discussed in greater detail in section 3.1.2 ?entering power-managed modes? . 2.11.1 system clock select (scs<1:0>) bits the system clock select (scs<1:0>) bits of the osccon register select the system clock source that is used for the cpu and peripherals. ? when scs<1:0> = 00 , the system clock source is determined by configuration of the fosc<3:0> bits in the config1h configuration register. ? when scs<1:0> = 10 , the system clock source is chosen by the internal oscillator frequency selected by the intsrc bit of the osctune register, the mfiosel bit of the osccon2 register and the ircf<2:0> bits of the osccon register. ? when scs<1:0> = 01 , the system clock source is the 32.768 khz secondary oscillator shared with timer1, timer3 and timer5. after a reset, the scs<1:0> bits of the osccon register are always cleared. 2.11.2 oscillator start-up time-out status (osts) bit the oscillator start-up time-out status (osts) bit of the osccon register indicates whether the system clock is running from the external clock source, as defined by the fosc<3:0> bits in the config1h configuration register, or from the internal clock source. in particular, when the primary oscillator is the source of the primary clock, osts indicates that the oscillator start-up timer (ost) has timed out for lp, xt or hs modes. osc mode osc1 pin osc2 pin rc, intosc with clkout floating, external resistor should pull high at logic low (clock/4 output) rc with io floating, external resistor should pull high configured as porta, bit 6 intosc with io configured as porta, bit 7 configured as porta, bit 6 ec with io floating, pulled by external clock configured as porta, bit 6 ec with clkout floating, pulled by external clock at logic low (clock/4 output) lp, xt, hs feedback inverter disabled at quiescent voltage level feedback 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. note: any automatic clock switch, which may occur from two-speed start-up or fail- safe clock monitor, does not update the scs<1:0> bits of the osccon register. the user can monitor the soscrun, mfiofs and lfiofs bits of the osccon2 register, and the hfiofs and osts bits of the osccon register to determine the current system clock source. pic18(l)f2x/4xk22 ds41412e-page 42 ? 2010-2012 microchip technology inc. 2.11.3 clock switch timing when switching between one oscillator and another, the new oscillator may not be operating which saves power (see figure 2-9 ). if this is the case, there is a delay after the scs<1:0> bits of the osccon register are modified before the frequency change takes place. the osts and iofs bits of the osccon register will reflect the current active status of the external and hfintosc oscillators. the timing of a frequency selection is as follows: 1. scs<1:0> bits of the osccon register are mod- ified. 2. the old clock continues to operate until the new clock is ready. 3. clock switch circuitry waits for two consecutive rising edges of the old clock after the new clock ready signal goes true. 4. the system clock is held low starting at the next falling edge of the old clock. 5. clock switch circuitry waits for an additional two rising edges of the new clock. 6. on the next falling edge of the new clock the low hold on the system clock is released and new clock is switched in as the system clock. 7. clock switch is complete. see figure 2-1 for more details. if the hfintosc is the source of both the old and new frequency, there is no start-up delay before the new frequency is active. this is because the old and new frequencies are derived from the hfintosc via the postscaler and multiplexer. start-up delay specifications are located in section 27.0 ?electrical characteristics? , under ac specifications (oscillator module). 2.12 two-speed clock start-up mode two-speed start-up mode provides additional power savings by minimizing the latency between external oscillator start-up and code execution. in applications that make heavy use of the sleep mode, two-speed start-up will remove the external oscillator start-up time from the time spent awake and can reduce the overall power consumption of the device. this mode allows the application to wake-up from sleep, perform a few instructions using the hfintosc as the clock source and go back to sleep without waiting for the primary oscillator to become stable. when the oscillator module is configured for lp, xt or hs modes, the oscillator start-up timer (ost) is enabled (see section 2.5.1 ?oscillator start-up timer (ost)? ). the ost will suspend program execution until 1024 oscillations are counted. two-speed start-up mode minimizes the delay in code execution by operating from the internal oscillator as the ost is counting. when the ost count reaches 1024 and the osts bit of the osccon register is set, program execution switches to the external oscillator. 2.12.1 two-speed start-up mode configuration two-speed start-up mode is enabled when all of the following settings are configured as noted: ? two-speed start-up mode is enabled when the ieso of the config1h configuration register is set. ? scs<1:0> (of the osccon register) = 00 . ? fosc<2:0> bits of the config1h configuration register are configured for lp, xt or hs mode. two-speed start-up mode becomes active after: ? power-on reset (por) and, if enabled, after power-up timer (pwrt) has expired, or ? wake-up from sleep. note: executing a sleep instruction will abort the oscillator start-up time and will cause the osts bit of the osccon register to remain clear. ? 2010-2012 microchip technology inc. ds41412e-page 43 pic18(l)f2x/4xk22 2.12.2 two-speed start-up sequence 1. wake-up from power-on reset or sleep. 2. instructions begin executing by the internal oscillator at the frequency set in the ircf<2:0> bits of the osccon register. 3. ost enabled to count 1024 external clock cycles. 4. ost timed out. external clock is ready. 5. osts is set. 6. clock switch finishes according to figure 2-9 2.12.3 checking two-speed clock status checking the state of the osts bit of the osccon register will confirm if the microcontroller is running from the external clock source, as defined by the fosc<2:0> bits in config1h configuration register, or the internal oscillator. osts = 0 when the external oscillator is not ready, which indicates that the system is running from the internal oscillator. figure 2-9: clock switch timing old clock new clock ircf <2:0> system clock start-up time (1) clock sync running high speed low speed select old select new new clk ready low speed high speed old clock new clock ircf <2:0> system clock start-up time (1) clock sync running select old select new new clk ready note 1: start-up time includes t ost (1024 t osc ) for external clocks, plus t pll (approx. 2 ms) for hspll mode. pic18(l)f2x/4xk22 ds41412e-page 44 ? 2010-2012 microchip technology inc. 2.13 fail-safe clock monitor the fail-safe clock monitor (fscm) allows the device to continue operating should the external oscillator fail. the fscm can detect oscillator failure any time after the oscillator start-up timer (ost) has expired. the fscm is enabled by setting the fcmen bit in the config1h configuration register. the fscm is applicable to all external oscillator modes (lp, xt, hs, ec, rc and rcio). figure 2-10: fscm block diagram 2.13.1 fail-safe detection the fscm module detects a failed oscillator by comparing the external oscillator to the fscm sample clock. the sample clock is generated by dividing the lfintosc by 64 (see figure 2-10 ). inside the fail detector block is a latch. the external clock sets the latch on each falling edge of the external clock. the sample clock clears the latch on each rising edge of the sample clock. a failure is detected when an entire half- cycle of the sample clock elapses before the primary clock goes low. 2.13.2 fail-safe operation when the external clock fails, the fscm switches the device clock to an internal clock source and sets the bit flag oscfif of the pir2 register. the oscfif flag will generate an interrupt if the oscfie bit of the pie2 register is also set. the device firmware can then take steps to mitigate the problems that may arise from a failed clock. the system clock will continue to be sourced from the internal clock source until the device firmware successfully restarts the external oscillator and switches back to external operation. an automatic transition back to the failed clock source will not occur. the internal clock source chosen by the fscm is determined by the ircf<2:0> bits of the osccon register. this allows the internal oscillator to be configured before a failure occurs. 2.13.3 fail-safe condition clearing the fail-safe condition is cleared by either one of the following: ?any reset ? by toggling the scs1 bit of the osccon register both of these conditions restart the ost. while the ost is running, the device continues to operate from the intosc selected in osccon. when the ost times out, the fail-safe condition is cleared and the device automatically switches over to the external clock source. the fail-safe condition need not be cleared before the oscfif flag is cleared. 2.13.4 reset or wake-up from sleep the fscm is designed to detect an oscillator failure after the oscillator start-up timer (ost) has expired. the ost is used after waking up from sleep and after any type of reset. the ost is not used with the ec or rc clock modes so that the fscm will be active as soon as the reset or wake-up has completed. . external lfintosc 64 s r q 31 khz (~32 ? s) 488 hz (~2 ms) clock monitor latch clock failure detected oscillator clock q sample clock note: due to the wide range of oscillator start-up times, the fail-safe circuit is not active during oscillator start-up (i.e., after exiting reset or sleep). after an appropriate amount of time, the user should check the osts bit of the osccon register to verify the oscillator start-up and that the system clock switchover has successfully completed. note: when the device is configured for fail- safe clock monitoring in either hs, xt, or ls oscillator modes then the ieso config- uration bit should also be set so that the clock will automatically switch from the internal clock to the external oscillator when the ost times out. ? 2010-2012 microchip technology inc. ds41412e-page 45 pic18(l)f2x/4xk22 figure 2-11: fscm timing diagram oscfif system clock output sample clock failure detected oscillator failure note: the system clock is normally at a much higher frequency than the sample clock. the relative frequencies in this example have been chosen for clarity. (q) te s t test test clock monitor output table 2-4: registers associated with clock sources name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 osccon idlen ircf<2:0> osts hfiofs scs<1:0> 32 osccon2 pllrdy soscrun ? mfiosel soscgo prisd mfiofs lfiofs 33 osctune intsrc pllen tun<5:0> 37 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by clock sources. table 2-5: configuration registers associated with clock sources name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config1h ieso fcmen pri clken pllcfg fosc<3:0> 357 config2l ? ? ? borv<1:0> boren<1:0> p wrten 358 config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for clock sources. pic18(l)f2x/4xk22 ds41412e-page 46 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 47 pic18(l)f2x/4xk22 3.0 power-managed modes pic18(l)f2x/4xk22 devices offer a total of seven operating modes for more efficient power manage- ment. these modes provide a variety of options for selective power conservation in applications where resources may be limited (i.e., battery-powered devices). there are three categories of power-managed modes: ? run modes ? idle modes ? sleep mode these categories define which portions of the device are clocked and sometimes, what speed. the run and idle modes may use any of the three available clock sources (primary, secondary or internal oscillator block). the sleep mode does not use a clock source. the power-managed modes include several power- saving features offered on previous pic ? microcontroller devices. one of the clock switching features allows the controller to use the secondary oscillator (sosc) in place of the primary oscillator. also included is the sleep mode, offered by all pic ? microcontroller devices, where all device clocks are stopped. 3.1 selecting power-managed modes selecting a power-managed mode requires two decisions: ? whether or not the cpu is to be clocked ? the selection of a clock source the idlen bit (osccon<7>) controls cpu clocking, while the scs<1:0> bits (osccon<1:0>) select the clock source. the individual modes, bit settings, clock sources and affected modules are summarized in table 3-1 . 3.1.1 clock sources the scs<1:0> bits allow the selection of one of three clock sources for power-managed modes. they are: ? the primary clock, as defined by the fosc<3:0> configuration bits ? the secondary clock (the sosc oscillator) ? the internal oscillator block 3.1.2 entering power-managed modes switching from one power-managed mode to another begins by loading the osccon register. the scs<1:0> bits select the clock source and determine which run or idle mode is to be used. changing these bits causes an immediate switch to the new clock source, assuming that it is running. the switch may also be subject to clock transition delays. refer to section 2.11 ?clock switching? for more information. entry to the power-managed idle or sleep modes is triggered by the execution of a sleep instruction. the actual mode that results depends on the status of the idlen bit. depending on the current mode and the mode being switched to, a change to a power-managed mode does not always require setting all of these bits. many transitions may be done by changing the oscillator select bits, or changing the idlen bit, prior to issuing a sleep instruction. if the idlen bit is already configured correctly, it may only be necessary to perform a sleep instruction to switch to the desired mode. table 3-1: power- managed modes mode osccon bits module clocking available clock and oscillator source idlen (1) scs<1:0> cpu peripherals sleep 0 n/a off off none ? all clocks are disabled pri_run n/a 00 clocked clocked primary ? lp, xt, hs, rc, ec and internal oscillator block (2) . this is the normal full-power execution mode. sec_run n/a 01 clocked clocked secondary ? sosc oscillator rc_run n/a 1x clocked clocked internal oscillator block (2) pri_idle 100 off clocked primary ? lp, xt, hs, hspll, rc, ec sec_idle 101 off clocked secondary ? sosc oscillator rc_idle 11x off clocked internal oscillator block (2) note 1: idlen reflects its value when the sleep instruction is executed. 2: includes hfintosc and hfintosc postscal er, as well as the lfintosc source. pic18(l)f2x/4xk22 ds41412f-page 48 ? 2010-2012 microchip technology inc. 3.1.3 multiple functions of the sleep command the power-managed mode that is invoked with the sleep instruction is determined by the value of the idlen bit at the time the instruction is executed. if idlen = 0 , when sleep is executed, the device enters the sleep mode and all clocks stop and minimum power is consumed. if idlen = 1 , when sleep is executed, the device enters the idle mode and the system clock continues to supply a clock to the peripherals but is disconnected from the cpu. 3.2 run modes in the run modes, clocks to both the core and peripherals are active. the difference between these modes is the clock source. 3.2.1 pri_run mode the pri_run mode is the normal, full-power execution mode of the microcontroller. this is also the default mode upon a device reset, unless two-speed start-up is enabled (see section 2.12 ?two-speed clock start-up mode? for details). in this mode, the device is operated off the oscillator defined by the fosc<3:0> bits of the config1h configuration register. 3.2.2 sec_run mode in sec_run mode, the cpu and peripherals are clocked from the secondary external oscillator. this gives users the option of lower power consumption while still using a high accuracy clock source. sec_run mode is entered by setting the scs<1:0> bits to ? 01 ?. when sec_run mode is active, all of the following are true: ? the device clock source is switched to the sosc oscillator (see figure 3-1 ) ? the primary oscillator is shut down ? the soscrun bit (osccon2<6>) is set ? the osts bit (osccon2<3>) is cleared on transitions from sec_run mode to pri_run mode, the peripherals and cpu continue to be clocked from the sosc oscillator, while the primary clock is started. when the primary clock becomes ready, a clock switch back to the primary clock occurs (see figure 3-2 ). when the clock switch is complete, the soscrun bit is cleared, the osts bit is set and the primary clock is providing the clock. the idlen and scs bits are not affected by the wake-up and the sosc oscillator continues to run. 3.2.3 rc_run mode in rc_run mode, the cpu and peripherals are clocked from the internal oscillator block using the intosc multiplexer. in this mode, the primary clock is shut down. when using the lfintosc source, this mode provides the best power conservation of all the run modes, while still executing code. it works well for user applications which are not highly timing-sensitive or do not require high-speed clocks at all times. if the primary clock source is the internal oscillator block ? either lfintosc or intosc (mfintosc or hfintosc) ? there are no distinguishable differences between the pri_run and rc_run modes during execution. entering or exiting rc_run mode, however, causes a clock switch delay. therefore, if the primary clock source is the internal oscillator block, using rc_run mode is not recommended. this mode is entered by setting the scs1 bit to ? 1 ?. to maintain software compatibility with future devices, it is recommended that the scs0 bit also be cleared, even though the bit is ignored. when the clock source is switched to the intosc multiplexer (see figure 3-1 ), the primary oscillator is shut down and the osts bit is cleared. the ircf<2:0> bits (osccon<6:4>) may be modified at any time to immediately change the clock speed. when the ircf bits and the intsrc bit are all clear, the intosc output (hfintosc/mfintosc) is not enabled and the hfiofs and mfiofs bits will remain clear. there will be no indication of the current clock source. the lfintosc source is providing the device clocks. if the ircf bits are changed from all clear (thus, enabling the intosc output) or if intsrc or mfiosel is set, then the hfiofs or mfiofs bit is set after the intosc output becomes stable. for details, see tab l e 3 - 2 . clocks to the device continue while the intosc source stabilizes after an interval of t iobst . if the ircf bits were previously at a non-zero value, or if intsrc was set before setting scs1 and the intosc source was already stable, then the hfiofs or mfiofs bit will remain set. note: the secondary external oscillator should already be running prior to entering sec_run mode. if the soscgo bit or any of the txsoscen bits are not set when the scs<1:0> bits are set to ? 01 ?, entry to sec_run mode will not occur until soscgo bit is set and secondary external oscillator is ready. ? 2010-2012 microchip technology inc. ds41412f-page 49 pic18(l)f2x/4xk22 on transitions from rc_run mode to pri_run mode, the device continues to be clocked from the intosc multiplexer while the primary clock is started. when the primary clock becomes ready, a clock switch to the pri- mary clock occurs (see figure 3-3 ). when the clock switch is complete, the hfiofs or mfiofs bit is cleared, the osts bit is set and the primary clock is providing the device clock. the idlen and scs bits are not affected by the switch. the lfintosc source will continue to run if either the wdt or the fail-safe clock monitor is enabled. figure 3-1: transition timing for entry to sec_run mode figure 3-2: transition timing from sec_run mode to pri_run mode (hspll) q4 q3 q2 osc1 peripheral program q1 sosci q1 counter clock cpu clock pc + 2 pc 123 n-1n clock transition (1) q4 q3 q2 q1 q3 q2 pc + 4 note 1: clock transition typically occurs within 2-4 t osc . q1 q3 q4 osc1 peripheral program pc sosc pll clock q1 pc + 4 q2 output q3 q4 q1 cpu clock pc + 2 clock counter q2 q2 q3 note1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. 2: clock transition typically occurs within 2-4 t osc . scs<1:0> bits changed t pll (1) 12 n-1n clock osts bit set transition (2) t ost (1) pic18(l)f2x/4xk22 ds41412f-page 50 ? 2010-2012 microchip technology inc. figure 3-3: transition timing from rc_run mode to pri_run mode table 3-2: internal oscillator frequency stability bits ircf<2:0> intsrc mfiosel selected oscillator selected oscillator stable when: 000 0 x lfintosc lfiofs = 1 000 1 0 hfintosc hfiofs = 1 000 1 1 mfintosc mfiofs = 1 010 or 001 x 0 hfintosc hfiofs = 1 010 or 001 x 1 mfintosc mfiofs = 1 011 ? 111 x x hfintosc hfiofs = 1 q1 q3 q4 osc1 peripheral program pc intosc pll clock q1 pc + 4 q2 output q3 q4 q1 cpu clock pc + 2 clock counter q2 q2 q3 note1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. 2: clock transition typically occurs within 2-4 t osc . scs<1:0> bits changed t pll (1) 12 n-1n clock osts bit set transition (2) multiplexer t ost (1) ? 2010-2012 microchip technology inc. ds41412f-page 51 pic18(l)f2x/4xk22 3.3 sleep mode the power-managed sleep mode in the pic18(l)f2x/ 4xk22 devices is identical to the legacy sleep mode offered in all other pic ? microcontroller devices. it is entered by clearing the idlen bit of the osccon register and executing the sleep instruction. this shuts down the selected oscillator ( figure 3-4 ) and all clock source status bits are cleared. entering the sleep mode from either run or idle mode does not require a clock switch. this is because no clocks are needed once the controller has entered sleep. if the wdt is selected, the lfintosc source will continue to operate. if the sosc oscillator is enabled, it will also continue to run. when a wake event occurs in sleep mode (by interrupt, reset or wdt time-out), the device will not be clocked until the clock source selected by the scs<1:0> bits becomes ready (see figure 3-5 ), or it will be clocked from the internal oscillator block if either the two-speed start-up or the fail-safe clock monitor are enabled (see section 24.0 ?special features of the cpu? ). in either case, the osts bit is set when the primary clock is providing the device clocks. the idlen and scs bits are not affected by the wake-up. 3.4 idle modes the idle modes allow the controller?s cpu to be selectively shut down while the peripherals continue to operate. selecting a particular idle mode allows users to further manage power consumption. if the idlen bit is set to a ? 1 ? when a sleep instruction is executed, the peripherals will be clocked from the clock source selected by the scs<1:0> bits; however, the cpu will not be clocked. the clock source status bits are not affected. setting idlen and executing a sleep instruc- tion provides a quick method of switching from a given run mode to its corresponding idle mode. if the wdt is selected, the lfintosc source will con- tinue to operate. if the sosc oscillator is enabled, it will also continue to run. since the cpu is not executing instructions, the only exits from any of the idle modes are by interrupt, wdt time-out, or a reset. when a wake event occurs, cpu execution is delayed by an interval of t csd while it becomes ready to execute code. when the cpu begins executing code, it resumes with the same clock source for the current idle mode. for example, when waking from rc_idle mode, the internal oscillator block will clock the cpu and peripherals (in other words, rc_run mode). the idlen and scs bits are not affected by the wake-up. while in any idle mode or the sleep mode, a wdt time-out will result in a wdt wake-up to the run mode currently specified by the scs<1:0> bits. figure 3-4: transition timi ng for entry to sleep mode q4 q3 q2 osc1 peripheral sleep program q1 q1 counter clock cpu clock pc + 2 pc pic18(l)f2x/4xk22 ds41412f-page 52 ? 2010-2012 microchip technology inc. figure 3-5: transition timing for wake from sleep (hspll) 3.4.1 pri_idle mode this mode is unique among the three low-power idle modes, in that it does not disable the primary device clock. for timing sensitive applications, this allows for the fastest resumption of device operation with its more accurate primary clock source, since the clock source does not have to ?warm-up? or transition from another oscillator. pri_idle mode is entered from pri_run mode by setting the idlen bit and executing a sleep instruc- tion. if the device is in another run mode, set idlen first, then clear the scs bits and execute sleep . although the cpu is disabled, the peripherals continue to be clocked from the primary clock source specified by the fosc<3:0> configuration bits. the osts bit remains set (see figure 3-6 ). when a wake event occurs, the cpu is clocked from the primary clock source. a delay of interval t csd is required between the wake event and when code execution starts. this is required to allow the cpu to become ready to execute instructions. after the wake- up, the osts bit remains set. the idlen and scs bits are not affected by the wake-up (see figure 3-7 ). 3.4.2 sec_idle mode in sec_idle mode, the cpu is disabled but the peripherals continue to be clocked from the sosc oscillator. this mode is entered from sec_run by set- ting the idlen bit and executing a sleep instruction. if the device is in another run mode, set the idlen bit first, then set the scs<1:0> bits to ? 01 ? and execute sleep . when the clock source is switched to the sosc oscillator, the primary oscillator is shut down, the osts bit is cleared and the soscrun bit is set. when a wake event occurs, the peripherals continue to be clocked from the sosc oscillator. after an interval of t csd following the wake event, the cpu begins exe- cuting code being clocked by the sosc oscillator. the idlen and scs bits are not affected by the wake-up; the sosc oscillator continues to run (see figure 3-7 ). figure 3-6: transition timing for entry to idle mode q3 q4 q1 q2 osc1 peripheral program pc pll clock q3 q4 output cpu clock q1 q2 q3 q4 q1 q2 clock counter pc + 6 pc + 4 q1 q2 q3 q4 wake event note1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. t ost (1) t pll (1) osts bit set pc + 2 note: the sosc oscillator should already be running prior to entering sec_idle mode. at least one of the secondary oscil- lator enable bits (soscgo, t1soscen, t3soscen or t5soscen) must be set when the sleep instruction is executed. otherwise, the main system clock will con- tinue to operate in the previously selected mode and the corresponding idle mode will be entered (i.e., pri_idle or rc_idle). q1 peripheral program pc pc + 2 osc1 q3 q4 q1 cpu clock clock counter q2 ? 2010-2012 microchip technology inc. ds41412f-page 53 pic18(l)f2x/4xk22 figure 3-7: transition timing for wake from idle to run mode 3.4.3 rc_idle mode in rc_idle mode, the cpu is disabled but the periph- erals continue to be clocked from the internal oscillator block from the hfintosc multiplexer output. this mode allows for controllable power conservation during idle periods. from rc_run, this mode is entered by setting the idlen bit and executing a sleep instruction. if the device is in another run mode, first set idlen, then set the scs1 bit and execute sleep . it is recommended that scs0 also be cleared, although its value is ignored, to maintain software compatibility with future devices. the hfintosc multiplexer may be used to select a higher clock frequency by modifying the ircf bits before executing the sleep instruction. when the clock source is switched to the hfintosc multiplexer, the primary oscillator is shut down and the osts bit is cleared. if the ircf bits are set to any non-zero value, or either the intsrc or mfiosel bits are set, the hfintosc output is enabled. either the hfiofs or the mfiofs bits become set, after the hfintosc output stabilizes after an interval of t iobst . for information on the hfiofs and mfiofs bits, see ta bl e 3 - 2 . clocks to the peripherals continue while the hfintosc source stabilizes. the hfiofs and mfiofs bits will remain set if the ircf bits were previously set at a non-zero value or if intsrc was set before the sleep instruction was executed and the hfintosc source was already stable. if the ircf bits and intsrc are all clear, the hfintosc output will not be enabled, the hfiofs and mfiofs bits will remain clear and there will be no indication of the current clock source. when a wake event occurs, the peripherals continue to be clocked from the hfintosc multiplexer output. after a delay of t csd following the wake event, the cpu begins executing code being clocked by the hfintosc multiplexer. the idlen and scs bits are not affected by the wake-up. the lfintosc source will continue to run if either the wdt or the fail-safe clock monitor is enabled. osc1 peripheral program pc cpu clock q1 q3 q4 clock counter q2 wake event t csd pic18(l)f2x/4xk22 ds41412f-page 54 ? 2010-2012 microchip technology inc. 3.5 exiting idle and sleep modes an exit from sleep mode or any of the idle modes is triggered by any one of the following: ? an interrupt ?a reset ? a watchdog time-out this section discusses the triggers that cause exits from power-managed modes. the clocking subsystem actions are discussed in each of the power-managed modes (see section 3.2 ?run modes? , section 3.3 ?sleep mode? and section 3.4 ?idle modes? ). 3.5.1 exit by interrupt any of the available interrupt sources can cause the device to exit from an idle mode or the sleep mode to a run mode. to enable this functionality, an interrupt source must be enabled by setting its enable bit in one of the intcon or pie registers. the exit sequence is initiated when the corresponding interrupt flag bit is set. the instruction immediately following the sleep instruction is executed on all exits by interrupt from idle or sleep modes. code execution then branches to the interrupt vector if the gie/gieh bit of the intcon register is set, otherwise code execution continues without branching (see section 9.0 ?interrupts? ). a fixed delay of interval t csd following the wake event is required when leaving sleep and idle modes. this delay is required for the cpu to prepare for execution. instruction execution resumes on the first clock cycle following this delay. 3.5.2 exit by wdt time-out a wdt time-out will cause different actions depending on which power-managed mode the device is in when the time-out occurs. if the device is not executing code (all idle modes and sleep mode), the time-out will result in an exit from the power-managed mode (see section 3.2 ?run modes? and section 3.3 ?sleep mode? ). if the device is executing code (all run modes), the time-out will result in a wdt reset (see section 24.3 ?watchdog timer (wdt)? ). the wdt timer and postscaler are cleared by any one of the following: ? executing a sleep instruction ? executing a clrwdt instruction ? the loss of the currently selected clock source when the fail-safe clock monitor is enabled ? modifying the ircf bits in the osccon register when the internal oscillator block is the device clock source 3.5.3 exit by reset exiting sleep and idle modes by reset causes code execution to restart at address 0. see section 4.0 ?reset? for more details. the exit delay time from reset to the start of code execution depends on both the clock sources before and after the wake-up and the type of oscillator. 3.5.4 exit without an oscillator start-up delay certain exits from power-managed modes do not invoke the ost at all. there are two cases: ? pri_idle mode, where the primary clock source is not stopped and ? the primary clock source is not any of the lp, xt, hs or hspll modes. in these instances, the primary clock source either does not require an oscillator start-up delay since it is already running (pri_idle), or normally does not require an oscillator start-up delay (rc, ec, intosc, and intoscio modes). however, a fixed delay of interval t csd following the wake event is still required when leaving sleep and idle modes to allow the cpu to prepare for execution. instruction execution resumes on the first clock cycle following this delay. ? 2010-2012 microchip technology inc. ds41412f-page 55 pic18(l)f2x/4xk22 3.6 selective peripheral module control idle mode allows users to substantially reduce power consumption by stopping the cpu clock. even so, peripheral modules still remain clocked, and thus, con- sume power. there may be cases where the applica- tion needs what idle mode does not provide: the allocation of power resources to the cpu processing with minimal power consumption from the peripherals. pic18(l)f2x/4xk22 family devices address this requirement by allowing peripheral modules to be selectively disabled, reducing or eliminating their power consumption. this can be done with control bits in the peripheral module disable (pmd) registers. these bits generically named xxxmd are located in control registers pmd0, pmd1 or pmd2. setting the pmd bit for a module disables all clock sources to that module, reducing its power consumption to an absolute minimum. in this state, power to the control and status registers associated with the peripheral is removed. writes to these registers have no effect and read values are invalid. clearing a set pmd bit restores power to the associated control and status registers, thereby setting those registers to their default values. 3.7 register definitions: peripheral module disable register 3-1: pmd0: peripheral module disable register 0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 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 uart2md: uart2 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 6 uart1md: uart1 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 5 tmr6md: timer6 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 4 tmr5md: timer5 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 3 tmr4md: timer4 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 2 tmr3md: timer3 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 1 tmr2md: timer2 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 0 tmr1md: timer1 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power pic18(l)f2x/4xk22 ds41412f-page 56 ? 2010-2012 microchip technology inc. register 3-2: pmd1: peripheral module disable register 1 r/w-0 r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 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 mssp2md: mssp2 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 6 mssp1md: mssp1 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 5 unimplemented: read as ? 0 ? bit 4 ccp5md: ccp5 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 3 ccp4md: ccp4 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 2 ccp3md: ccp3 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 1 ccp2md: ccp2 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 0 ccp1md: ccp1 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power ? 2010-2012 microchip technology inc. ds41412f-page 57 pic18(l)f2x/4xk22 register 3-3: pmd2: peripheral module disable register 2 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? ? ? ctmumd cmp2md cmp1md adcmd 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-4 unimplemented: read as ? 0 ? bit 3 ctmumd: ctmu peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 2 cmp2md: comparator c2 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 1 cmp1md: comparator c1 peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power bit 0 adcmd: adc peripheral module disable control bit 1 = module is disabled, clock source is disconnected, module does not draw digital power 0 = module is enabled, clock source is connected, module draws digital power pic18(l)f2x/4xk22 ds41412f-page 58 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 59 pic18(l)f2x/4xk22 4.0 reset the pic18(l)f2x/4xk22 devices differentiate between various kinds of reset: a) power-on reset (por) b) mclr reset during normal operation c) mclr reset during power-managed modes d) watchdog timer (wdt) reset (during execution) e) programmable brown-out reset (bor) f) reset instruction g) stack full reset h) stack underflow reset this section discusses resets generated by mclr , por and bor and covers the operation of the various start-up timers. stack reset events are covered in section 5.2.0.1 ?stack full and underflow resets? . wdt resets are covered in section 24.3 ?watchdog timer (wdt)? . a simplified block diagram of the on-chip reset circuit is shown in figure 4-1 . 4.1 rcon register device reset events are tracked through the rcon register ( register 4-1 ). the lower five bits of the register indicate that a specific reset event has occurred. in most cases, these bits can only be cleared by the event and must be set by the application after the event. the state of these flag bits, taken together, can be read to indicate the type of reset that just occurred. this is described in more detail in section 4.7 ?reset state of registers? . the rcon register also has control bits for setting interrupt priority (ipen) and software control of the bor (sboren). interrupt priority is discussed in section 9.0 ?interrupts? . bor is covered in section 4.5 ?brown-out reset (bor)? . figure 4-1: simplified block di agram of on-chip reset circuit external reset mclr v dd osc1 wdt time-out v dd detect ost/pwrt lfintosc por ost (2) 10-bit ripple counter pwrt (2) 11-bit ripple counter enable ost (1) enable pwrt note 1: see table 4-2 for time-out situations. 2: pwrt and ost counters are reset by por and bor. see sections 4.4 and 4.5 . brown-out reset boren reset instruction stack pointer stack full/underflow reset sleep ( )_idle 1024 cycles 65.5 ms 32 ? s mclre s r q chip_reset pic18(l)f2x/4xk22 ds41412f-page 60 ? 2010-2012 microchip technology inc. 4.2 register definitions: reset control register 4-1: rcon: re set control register r/w-0/0 r/w-q/u u-0 r/w-1/q r-1/q r-1/q r/w-q/u r/w-0/q ipen sboren (1) ?ri to pd por (2) bor bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? ?1? = bit is set ?0? = bit is cleared -n/n = value at por and bor/value at all other resets x = bit is unknown u = unchanged q = depends on condition bit 7 ipen: interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts (pic16cxxx compatibility mode) bit 6 sboren: bor software enable bit (1) if boren<1:0> = 01 : 1 = bor is enabled 0 = bor is disabled if boren<1:0> = 00 , 10 or 11 : bit is disabled and read as ? 0 ?. bit 5 unimplemented: read as ? 0 ? bit 4 ri : reset instruction flag bit 1 = the reset instruction was not executed (set by firmware or power-on reset) 0 = the reset instruction was executed causing a device reset (must be set in firmware after a code-executed reset occurs) bit 3 to : watchdog time-out flag bit 1 = set by power-up, clrwdt instruction or sleep instruction 0 = a wdt time-out occurred bit 2 pd : power-down detection flag bit 1 = set by power-up or by the clrwdt instruction 0 = set by execution of the sleep instruction bit 1 por : power-on reset status bit (2) 1 = no power-on reset occurred 0 = a power-on reset occurred (must be set in software after a power-on reset occurs) bit 0 bor : brown-out reset status bit (3) 1 = a brown-out reset has not occurred (set by firmware only) 0 = a brown-out reset occurred (must be set by firmware after a por or brown-out reset occurs) note 1: when config2l[2:1] = 01 , then the sboren reset state is ? 1 ?; otherwise, it is ? 0 ?. 2: the actual reset value of por is determined by the type of device reset. see the notes following this register and section 4.7 ?reset state of registers? for additional information. 3: see table 4-1 . note 1: brown-out reset is indicated when bor is ? 0 ? and por is ? 1 ? (assuming that both por and bor were set to ? 1 ? by firmware immediately after por). 2: it is recommended that the por bit be set after a power-on reset has been detected so that subsequent power-on resets may be detected. ? 2010-2012 microchip technology inc. ds41412f-page 61 pic18(l)f2x/4xk22 4.3 master clear (mclr ) the mclr pin provides a method for triggering an external reset of the device. a reset is generated by holding the pin low. these devices have a noise filter in the mclr reset path which detects and ignores small pulses. an internal weak pull-up is enabled when the pin is configured as the mclr input. the mclr pin is not driven low by any internal resets, including the wdt. in pic18(l)f2x/4xk22 devices, the mclr input can be disabled with the mclre configuration bit. when mclr is disabled, the pin becomes a digital input. see section 10.6 ?porte registers? for more information. 4.4 power-on reset (por) a power-on reset pulse is generated on-chip whenever v dd rises above a certain threshold. this allows the device to start in the initialized state when v dd is adequate for operation. to take advantage of the por circuitry either leave the pin floating, or tie the mclr pin through a resistor to v dd . this will eliminate external rc components usually needed to create a power-on reset delay. a minimum rise rate for v dd is specified. for a slow rise time, see figure 4-2 . when the device starts normal operation (i.e., exits the reset condition), device operating parameters (volt- age, frequency, temperature, etc.) must be met to ensure proper operation. if these conditions are not met, the device must be held in reset until the operat- ing conditions are met. por events are captured by the por bit of the rcon register. the state of the bit is set to ? 0 ? whenever a por occurs; it does not change for any other reset event. por is not reset to ? 1 ? by any hardware event. to capture multiple events, the user must manually set the bit to ? 1 ? by software following any por. figure 4-2: external power-on reset circuit (for slow v dd power-up) note 1: external power-on reset circuit is required only if the v dd power-up slope is too slow. the diode d helps discharge the capacitor quickly when v dd powers down. 2: 15 k ? < r < 40 k ? is recommended to make sure that the voltage drop across r does not violate the device?s electrical specification. 3: r1 ? 1 k ? will limit any current flowing into mclr from external capacitor c, in the event of mclr /v pp pin breakdown, due to electrostatic discharge (esd) or electrical overstress (eos). c r1 r d v dd mclr v dd pic ? mcu pic18(l)f2x/4xk22 ds41412f-page 62 ? 2010-2012 microchip technology inc. 4.5 brown-out reset (bor) pic18(l)f2x/4xk22 devices implement a bor circuit that provides the user with a number of configuration and power-saving options. the bor is controlled by the borv<1:0> and boren<1:0> bits of the config2l configuration register. there are a total of four bor configurations which are summarized in ta b l e 4 - 1 . the bor threshold is set by the borv<1:0> bits. if bor is enabled (any values of boren<1:0>, except ? 00 ?), any drop of v dd below v bor for greater than t bor will reset the device. a reset may or may not occur if v dd falls below v bor for less than t bor . the chip will remain in brown-out reset until v dd rises above v bor . if the power-up timer is enabled, it will be invoked after v dd rises above v bor ; it then will keep the chip in reset for an additional time delay, t pwrt . if v dd drops below v bor while the power-up timer is running, the chip will go back into a brown-out reset and the power-up timer will be initialized. once v dd rises above v bor , the power-up timer will execute the additional time delay. bor and the power-on timer (pwrt) are independently configured. enabling bor reset does not automatically enable the pwrt. the bor circuit has an output that feeds into the por circuit and rearms the por within the operating range of the bor. this early rearming of the por ensures that the device will remain in reset in the event that v dd falls below the operating range of the bor circuitry. 4.5.1 detecting bor when bor is enabled, the bor bit always resets to ? 0 ? on any bor or por event. this makes it difficult to determine if a bor event has occurred just by reading the state of bor alone. a more reliable method is to simultaneously check the state of both por and bor . this assumes that the por and bor bits are reset to ? 1 ? by software immediately after any por event. if bor is ? 0 ? while por is ? 1 ?, it can be reliably assumed that a bor event has occurred. 4.5.2 software enabled bor when boren<1:0> = 01 , the bor can be enabled or disabled by the user in software. this is done with the sboren control bit of the rcon register. setting sboren enables the bor to function as previously described. clearing sboren disables the bor entirely. the sboren bit operates only in this mode; otherwise it is read as ? 0 ?. placing the bor under software control gives the user the additional flexibility of tailoring the application to the environment without having to reprogram the device to change bor configuration. it also allows the user to tailor device power consumption in software by eliminating the incremental current that the bor consumes. while the bor current is typically very small, it may have some impact in low-power applications. 4.5.3 disabling bor in sleep mode when boren<1:0> = 10 , the bor remains under hardware control and operates as previously described. whenever the device enters sleep mode, however, the bor is automatically disabled. when the device returns to any other operating mode, bor is automatically re-enabled. this mode allows for applications to recover from brown-out situations, while actively executing code, when the device requires bor protection the most. at the same time, it saves additional power in sleep mode by eliminating the small incremental bor current. 4.5.4 minimum bor enable time enabling the bor also enables the fixed voltage reference (fvr) when no other peripheral requiring the fvr is active. the bor becomes active only after the fvr stabilizes. therefore, to ensure bor protection, the fvr settling time must be considered when enabling the bor in software or when the bor is automatically enabled after waking from sleep. if the bor is disabled, in software or by reentering sleep before the fvr stabilizes, the bor circuit will not sense a bor condition. the fvrst bit of the vrefcon0 register can be used to determine fvr stability. note: even when bor is under software control, the bor reset voltage level is still set by the borv<1:0> configuration bits. it cannot be changed by software. ? 2010-2012 microchip technology inc. ds41412f-page 63 pic18(l)f2x/4xk22 4.6 device reset timers pic18(l)f2x/4xk22 devices incorporate three separate on-chip timers that help regulate the power- on reset process. their main function is to ensure that the device clock is stable before code is executed. these timers are: ? power-up timer (pwrt) ? oscillator start-up timer (ost) ? pll lock time-out 4.6.1 power-up timer (pwrt) the power-up timer (pwrt) of pic18(l)f2x/4xk22 devices is an 11-bit counter which uses the lfintosc source as the clock input. this yields an approximate time interval of 2048 x 32 ? s=65.6ms. while the pwrt is counting, the device is held in reset. the power-up time delay depends on the lfintosc clock and will vary from chip-to-chip due to temperature and process variation. the pwrt is enabled by clearing the pwrten configuration bit. 4.6.2 oscillator 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. this ensures that the crystal oscillator or resonator has started and stabilized. the ost time-out is invoked only for xt, lp and hs modes and only on power-on reset, or on exit from all power-managed modes that stop the external oscillator. 4.6.3 pll lock time-out with the pll enabled, the time-out sequence following a power-on reset is slightly different from other oscillator modes. a separate timer is used to provide a fixed time- out that is sufficient for the pll to lock to the main oscillator frequency. this pll lock time-out (t pll ) is typically 2 ms and follows the oscillator start-up time-out. 4.6.4 time-out sequence on power-up, the time-out sequence is as follows: 1. after the por pulse has cleared, pwrt time-out is invoked (if enabled). 2. then, the ost is activated. the total time-out will vary based on oscillator configuration and the status of the pwrt. figure 4-3 , figure 4-4 , figure 4-5 , figure 4-6 and figure 4-7 all depict time-out sequences on power-up, with the power-up timer enabled and the device operating in hs oscillator mode. figures 4-3 through 4-6 also apply to devices operating in xt or lp modes. for devices in rc mode and with the pwrt disabled, on the other hand, there will be no time-out at all. since the time-outs occur from the por pulse, if mclr is kept low long enough, all time-outs will expire, after which, bringing mclr high will allow program execution to begin immediately ( figure 4-5 ). this is useful for testing purposes or to synchronize more than one pic ? mcu device operating in parallel. table 4-1: bor configurations bor configuration status of sboren (rcon<6>) bor operation boren1 boren0 00 unavailable bor disabled; must be enabled by reprogramming the configuration bits. 01 available bor enabled by software; operation controlled by sboren. 10 unavailable bor enabled by hardware in r un and idle modes, disabled during sleep mode. 11 unavailable bor enabled by hardware; must be di sabled by reprogramming the configuration bits. pic18(l)f2x/4xk22 ds41412f-page 64 ? 2010-2012 microchip technology inc. figure 4-3: time-out sequ ence on power-up (mclr tied to v dd , v dd rise < t pwrt ) figure 4-4: time-out sequ ence on power-up (mclr not tied to v dd ): case 1 table 4-2: time-out in various situations oscillator configuration power-up (2) and brown-out exit from power-managed mode pwrten = 0 pwrten = 1 hspll 66 ms (1) + 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) hs, xt, lp 66 ms (1) + 1024 t osc 1024 t osc 1024 t osc ec, ecio 66 ms (1) ?? rc, rcio 66 ms (1) ?? intio1, intio2 66 ms (1) ?? note 1: 66 ms (65.5 ms) is the nominal power-up timer (pwrt) delay. 2: 2 ms is the nominal time required for the pll to lock. t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset ? 2010-2012 microchip technology inc. ds41412f-page 65 pic18(l)f2x/4xk22 figure 4-5: time-out sequ ence on power-up (mclr not tied to v dd ): case 2 figure 4-6: slow rise time (mclr tied to v dd , v dd rise > t pwrt ) v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset 0v 5v t pwrt t ost pic18(l)f2x/4xk22 ds41412f-page 66 ? 2010-2012 microchip technology inc. figure 4-7: time-out sequence on por w/pll enabled (mclr tied to v dd ) t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset pll time-out t pll note: t ost = 1024 clock cycles. t pll ? 2 ms max. first three stages of the pwrt timer. ? 2010-2012 microchip technology inc. ds41412f-page 67 pic18(l)f2x/4xk22 4.7 reset state of registers some registers are unaffected by a reset. their status is unknown on por and unchanged by all other resets. all other registers are forced to a ?reset state? depending on the type of reset that occurred. 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 , to , pd , por and bor , are set or cleared differently in different reset situations, as indicated in table 4-3 . these bits are used by software to determine the nature of the reset. table 5-2 describes the reset states for all of the special function registers. the table identifies differences between power-on reset (por)/brown- out reset (bor) and all other resets, (i.e., master clear, wdt resets, stkful, stkunf, etc.). additionally, the table identifies register bits that are changed when the device receives a wake-up from wdt or other interrupts. table 4-3: status bits, their signifi cance and the initialization condition for rcon register condition program counter rcon register stkptr register sboren ri to pd por bor stkful stkunf power-on reset 0000h 1 11100 0 0 reset instruction 0000h u (2) 0uuuu u u brown-out reset 0000h u (2) 111u0 u u mclr during power-managed run modes 0000h u (2) u1uuu u u mclr during power-managed idle modes and sleep mode 0000h u (2) u10uu u u wdt time-out during full power or power-managed run mode 0000h u (2) u0uuu u u mclr during full power execution 0000h u (2) uuuuu u u stack full reset (stvren = 1 ) 0000h u (2) uuuuu 1 u stack underflow reset (stvren = 1 ) 0000h u (2) uuuuu u 1 stack underflow error (not an actual reset, stvren = 0 ) 0000h u (2) uuuuu u 1 wdt time-out during power- managed idle or sleep modes pc + 2 u (2) u00uu u u interrupt exit from power- managed modes pc + 2 (1) u (2) uu0uu u u legend: u = unchanged note 1: when the wake-up is due to an interrupt and the gieh or giel bits are set, the pc is loaded with the interrupt vector (008h or 0018h). 2: reset state is ? 1 ? for sboren and unchanged for all other resets when software bor is enabled (boren<1:0> configuration bits = 01 ). otherwise, the reset state is ? 0 ?. table 4-4: registers associated with resets name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page rcon ipen sboren ?ri to pd por bor 60 stkptr stkful stkunf ? stkptr<4:0> 72 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for resets. pic18(l)f2x/4xk22 ds41412f-page 68 ? 2010-2012 microchip technology inc. table 4-5: configuratio n registers associated with resets name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config2l ? ? ? borv<1:0> boren<1:0> pwrten 358 config2h ? ? wdps<3:0> wdten<1:0> 359 config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 config4l debug xinst ? ? ? lvp ? strven 361 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for resets. ? 2010-2012 microchip technology inc. ds41412f-page 69 pic18(l)f2x/4xk22 5.0 memory organization there are three types of memory in pic18 enhanced microcontroller devices: ? program memory ? data ram ? data eeprom as harvard architecture devices, the data and program memories use separate buses; this allows for concurrent access of the two memory spaces. the data eeprom, for practical purposes, can be regarded as a peripheral device, since it is addressed and accessed through a set of control registers. additional detailed information on the operation of the flash program memory is provided in section 6.0 ?flash program memory? . data eeprom is discussed separately in section 7.0 ?data eeprom memory? . 5.1 program memory organization pic18 microcontrollers implement a 21-bit program counter, which is capable of addressing a 2-mbyte program memory space. accessing a location between the upper boundary of the physically implemented memory and the 2-mbyte address will return all ? 0 ?s (a nop instruction). this family of devices contain the following: ? pic18(l)f23k22, pic18(l)f43k22: 8 kbytes of flash memory, up to 4,096 single-word instructions ? pic18(l)f24k22, pic18(l)f44k22: 16 kbytes of flash memory, up to 8,192 single-word instructions ? pic18(l)f25k22, pic18(l)f45k22: 32 kbytes of flash memory, up to 16,384 single-word instruc- tions ? pic18(l)f26k22, pic18(l)f46k22: 64 kbytes of flash memory, up to 37,768 single-word instructions pic18 devices have two interrupt vectors. the reset vector address is at 0000h and the interrupt vector addresses are at 0008h and 0018h. the program memory map for pic18(l)f2x/4xk22 devices is shown in figure 5-1 . memory block details are shown in figure 20-2 . pic18(l)f2x/4xk22 ds41412f-page 70 ? 2010-2012 microchip technology inc. figure 5-1: program memory map and stack for pic18(l)f2x/4xk22 devices 5.1.1 program counter the program counter (pc) specifies the address of the instruction to fetch for execution. the pc is 21 bits wide and is contained in three separate 8-bit registers. the low byte, known as the pcl register, is both readable and writable. the high byte, or pch register, contains the pc<15:8> bits; it is not directly readable or writable. updates to the pch register are performed through the pclath register. the upper byte is called pcu. this register contains the pc<20:16> bits; it is also not directly readable or writable. updates to the pcu register are performed through the pclatu register. the contents of pclath and pclatu are transferred to the program counter by any operation that writes pcl. similarly, the upper two bytes of the program counter are transferred to pclath and pclatu by an operation that reads pcl. this is useful for computed offsets to the pc (see section 5.2.2.1 ?computed goto? ). the pc addresses bytes in the program memory. to prevent the pc from becoming misaligned with word instructions, the least significant bit of pcl is fixed to a value of ? 0 ?. the pc increments by two to address sequential instructions in the program memory. the call , rcall , goto and program branch instructions write to the program counter directly. for these instructions, the contents of pclath and pclatu are not transferred to the program counter. 5.1.2 return address stack the return address stack allows any combination of up to 31 program calls and interrupts to occur. the pc is pushed onto the stack when a call or rcall instruction is executed or an interrupt is acknowledged. the pc value is pulled off the stack on a return, retlw or a retfie instruction. pclatu and pclath are not affected by any of the return or call instructions. the stack operates as a 31-word by 21-bit ram and a 5-bit stack pointer, stkptr. the stack space is not part of either program or data space. the stack pointer is readable and writable and the address on the top of the stack is readable and writable through the top-of- stack (tos) special file registers. data can also be pushed to, or popped from the stack, using these registers. pc<20:0> stack level 1 ? stack level 31 reset vector low priority interrupt vector ? ? call,rcall,return retfie,retlw 21 0000h 0018h on-chip program memory high priority interrupt vector 0008h user memory space 1fffffh 4000h 3fffh read ? 0 ? 200000h 8000h 7fffh on-chip program memory read ? 0 ? 1fffh 2000h on-chip program memory read ? 0 ? pic18(l)f25k22 pic18(l)f45k22 pic18(l)f24k22 pic18(l)f44k22 pic18(l)f23k22 pic18(l)f43k22 read ? 0 ? ffffh pic18(l)f26k22 pic18(l)f46k22 on-chip program memory 10000h ? 2010-2012 microchip technology inc. ds41412f-page 71 pic18(l)f2x/4xk22 a call type instruction causes a push onto the stack; the stack pointer is first incremented and the location pointed to by the stack pointer is written with the contents of the pc (already pointing to the instruction following the call ). a return type instruction causes a pop from the stack; the contents of the location pointed to by the stkptr are transferred to the pc and then the stack pointer is decremented. the stack pointer is initialized to ? 00000 ? after all resets. there is no ram associated with the location corresponding to a stack pointer value of ? 00000 ?; this is only a reset value. status bits indicate if the stack is full or has overflowed or has underflowed. 5.1.2.1 top-of-stack access only the top of the return address stack (tos) is readable and writable. a set of three registers, tosu:tosh:tosl, hold the contents of the stack location pointed to by the stkptr register ( figure 5-2 ). this allows users to implement a software stack if necessary. after a call, rcall or interrupt, the software can read the pushed value by reading the tosu:tosh:tosl registers. these values can be placed on a user defined software stack. at return time, the software can return these values to tosu:tosh:tosl and do a return. the user must disable the global interrupt enable (gie) bits while accessing the stack to prevent inadvertent stack corruption. figure 5-2: return address stack and associated registers 5.1.2.2 return stack pointer (stkptr) the stkptr register ( register 5-1 ) contains the stack pointer value, the stkful (stack full) status bit and the stkunf (stack underflow) status bits. the value of the stack pointer can be 0 through 31. the stack pointer increments before values are pushed onto the stack and decrements after values are popped off the stack. on reset, the stack pointer value will be zero. the user may read and write the stack pointer value. this feature can be used by a real-time operating system (rtos) for return stack maintenance. after the pc is pushed onto the stack 31 times (without popping any values off the stack), the stkful bit is set. the stkful bit is cleared by software or by a por. the action that takes place when the stack becomes full depends on the state of the stvren (stack over- flow reset enable) configuration bit. (refer to section 24.1 ?configuration bits? 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 zero. if stvren is cleared, the stkful bit will be set on the 31st push and the stack pointer will increment to 31. any additional pushes will not overwrite the 31 st push and stkptr will remain at 31. when the stack has been popped enough times to unload the stack, the next pop will return a value of zero to the pc and sets the stkunf bit, while the stack pointer remains at zero. the stkunf bit will remain set until cleared by software or until a por occurs. 00011 001a34h 11111 11110 11101 00010 00001 00000 00010 return address stack <20:0> to p - o f - st a c k 000d58h tosl tosh tosu 34h 1ah 00h stkptr<4:0> top-of-stack registers stack pointer 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. this is not the same as a reset, as the contents of the sfrs are not affected. pic18(l)f2x/4xk22 ds41412f-page 72 ? 2010-2012 microchip technology inc. 5.1.2.3 push and pop instructions since the top-of-stack is readable and writable, the ability to push values onto the stack and pull values off the stack without disturbing normal program execution is a desirable feature. the pic18 instruction set includes two instructions, push and pop , that permit the tos to be manipulated under software control. tosu, tosh and tosl can be modified to place data or a return address on the stack. the push instruction places the current pc value onto the stack. this increments the stack pointer and loads the current pc value onto the stack. the pop instruction discards the current tos by decre- menting the stack pointer. the previous value pushed onto the stack then becomes the tos value. 5.2 register definitions: stack pointer 5.2.0.1 stack full and underflow resets device resets on stack overflow and stack underflow conditions are enabled by setting the stvren bit in configuration register 4l. when stvren is set, a full or underflow will set the appropriate stkful or stkunf bit and then cause a device reset. when stvren is cleared, a full or underflow condition will set the appropriate stkful or stkunf bit but not cause a device reset. the stkful or stkunf bits are cleared by the user software or a power-on reset. 5.2.1 fast register stack a fast register stack is provided for the status, wreg and bsr registers, to provide a ?fast return? option for interrupts. the stack for each register is only one level deep and is neither readable nor writable. it is loaded with the current value of the corresponding register when the processor vectors for an interrupt. all inter- rupt sources will push values into the stack registers. the values in the registers are then loaded back into their associated registers if the retfie, fast instruction is used to return from the interrupt. if both low and high priority interrupts are enabled, the stack registers cannot be used reliably to return from 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. in these cases, users must save the key registers by software during a low priority interrupt. if interrupt priority is not used, all interrupts may use the fast register stack for returns from interrupt. if no interrupts are used, the fast register stack can be used to restore the status, wreg and bsr registers at the end of a subroutine call. to use the fast register stack for a subroutine call, a call label , fast instruction must be executed to save the status, wreg and bsr registers to the fast register stack. a return , fast instruction is then executed to restore these registers from the fast register stack. example 5-1 shows a source code example that uses the fast register stack during a subroutine call and return. register 5-1: stkptr: stack pointer register r/c-0 r/c-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 stkful (1) stkunf (1) ? stkptr<4:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 stkful: stack full flag bit (1) 1 = stack became full or overflowed 0 = stack has not become full or overflowed bit 6 stkunf: stack underflow flag bit (1) 1 = stack underflow occurred 0 = stack underflow did not occur bit 5 unimplemented: read as ? 0 ? bit 4-0 stkptr<4:0>: stack pointer location bits note 1: bit 7 and bit 6 are cleared by user software or by a por. ? 2010-2012 microchip technology inc. ds41412f-page 73 pic18(l)f2x/4xk22 example 5-1: fast register stack code example 5.2.2 look-up tables in program memory there may be programming situations that require the creation of data structures, or look-up tables, in program memory. for pic18 devices, look-up tables can be implemented in two ways: ? computed goto ? table reads 5.2.2.1 computed goto a computed goto is accomplished by adding an offset to the program counter. an example is shown in example 5-2 . a look-up table can be formed with an addwf pcl instruction and a group of retlw nn instructions. the w register is loaded with an offset into the table before executing a call to that table. the first instruction of the called routine is the addwf pcl instruction. the next instruction executed will be one of the retlw nn instructions that returns the value ? nn ? to the calling function. the offset value (in wreg) specifies the number of bytes that the program counter should advance and should be multiples of two (lsb = 0 ). in this method, only one data byte may be stored in each instruction location and room on the return address stack is required. example 5-2: computed goto using an offset value 5.2.2.2 table reads and table writes a better method of storing data in program memory allows two bytes of data to be stored in each instruction location. look-up table data may be stored two bytes per program word by using table reads and writes. the table pointer (tblptr) register specifies the byte address and the table latch (tablat) register contains the data that is read from or written to program memory. data is transferred to or from program memory one byte at a time. table read and table write operations are discussed further in section 6.1 ?table reads and table writes? . call sub1, fast ;status, wreg, bsr ;saved in fast register ;stack ? ? sub1 ? ? return, fast ;restore values saved ;in fast register stack movf offset, w call table org nn00h table addwf pcl retlw nnh retlw nnh retlw nnh . . . pic18(l)f2x/4xk22 ds41412f-page 74 ? 2010-2012 microchip technology inc. 5.3 pic18 instruction cycle 5.3.1 clocking scheme the microcontroller clock input, whether from an internal or external source, is internally divided by four to generate four non-overlapping quadrature clocks (q1, q2, q3 and q4). internally, the program counter is incremented on every q1; the instruction is fetched from the program memory and latched into the instruction register during q4. the instruction is decoded and executed during the following q1 through q4. the clocks and instruction execution flow are shown in figure 5-3 . 5.3.2 instruction flow/pipelining an ?instruction cycle? consists of four q cycles: q1 through q4. the instruction fetch and execute are pipelined in such a manner that a fetch takes one instruction cycle, while the 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 ), then two cycles are required to complete the instruction ( example 5-3 ). a fetch cycle begins with the program counter (pc) incrementing in q1. in the execution cycle, the fetched instruction is latched into the instruction register (ir) in cycle q1. this instruction is then decoded and executed during the q2, q3 and q4 cycles. data memory is read during q2 (operand read) and written during q4 (destination write). figure 5-3: clock/ instruction cycle example 5-3: instruction pipeline flow q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 q1 q2 q3 q4 pc osc2/clkout (rc mode) pc pc + 2 pc + 4 fetch inst (pc) execute inst (pc ? 2) fetch inst (pc + 2) execute inst (pc) fetch inst (pc + 4) execute inst (pc + 2) internal phase clock all instructions are single cycle, except for any program branches. these take two cycles since the fetch instruction is ?flushed? from the pipeline while the new instruction is being fetched and then executed. t cy 0t cy 1t cy 2t cy 3t cy 4t cy 5 1. movlw 55h fetch 1 execute 1 2. movwf portb fetch 2 execute 2 3. bra sub_1 fetch 3 execute 3 4. bsf porta, bit3 (forced nop) fetch 4 flush ( nop ) 5. instruction @ address sub_1 fetch sub_1 execute sub_1 ? 2010-2012 microchip technology inc. ds41412f-page 75 pic18(l)f2x/4xk22 5.3.3 instructions in program memory the program memory is addressed in bytes. instructions are stored as either 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 ). to maintain alignment with instruction boundaries, the pc increments in steps of two and the lsb will always read ? 0 ? (see section 5.1.1 ?program counter? ). figure 5-4 shows an example of how instruction words are stored in the program memory. the call and goto instructions have the 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 figure 5-4 shows how the instruction goto 0006h is encoded in the program memory. program branch instructions, which encode a relative address offset, operate in the same manner. the offset value stored in a branch instruction represents the number of single-word instructions that the pc will be offset by. section 25.0 ?instruction set summary? provides further details of the instruction set. figure 5-4: instructions in program memory 5.3.4 two-word instructions the standard pic18 instruction set has four two-word instructions: call , movff , goto and lsfr . in all cases, the second word of the instruction always has ? 1111 ? as its four most significant bits; the other 12 bits are literal data, usually a data memory address. the use of ? 1111 ? in the 4 msbs of an instruction specifies a special form of nop . if the instruction is executed in proper sequence ? immediately after the first word ? the data in the second word is accessed and used by the instruction sequence. if the first word is skipped for some reason and the second word is executed by itself, a nop is executed instead. this is necessary for cases when the two-word instruction is preceded by a conditional instruction that changes the pc. example 5-4 shows how this works. example 5-4: two- word instructions word address lsb = 1 lsb = 0 ? program memory byte locations ? ? 000000h 000002h 000004h 000006h instruction 1: movlw 055h 0fh 55h 000008h instruction 2: goto 0006h efh 03h 00000ah f0h 00h 00000ch instruction 3: movff 123h, 456h c1h 23h 00000eh f4h 56h 000010h 000012h 000014h note: see section 5.8 ?pic18 instruction execution and the extended instruc- tion set? for information on two-word instructions in the extended instruction set. case 1: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; no, skip this word 1111 0100 0101 0110 ; execute this word as a nop 0010 0100 0000 0000 addwf reg3 ; continue code case 2: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; yes, execute this word 1111 0100 0101 0110 ; 2nd word of instruction 0010 0100 0000 0000 addwf reg3 ; continue code pic18(l)f2x/4xk22 ds41412f-page 76 ? 2010-2012 microchip technology inc. 5.4 data memory organization the data memory in pic18 devices is implemented as static ram. each register in the data memory has a 12-bit address, allowing up to 4096 bytes of data memory. the memory space is divided into as many as 16 banks that contain 256 bytes each. figures 5-5 through 5-7 show the data memory organization for the pic18(l)f2x/4xk22 devices. 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 functions, while gprs are used for data storage and scratchpad operations in the user?s application. any read of an unimplemented location will read as ? 0 ?s. the instruction set and architecture allow operations across all banks. the entire data memory may be accessed by direct, indirect or indexed addressing modes. addressing modes are discussed later in this subsection. to ensure that commonly used registers (sfrs and select gprs) can be accessed in a single cycle, pic18 devices implement an access bank. this is a 256-byte memory space that provides fast access to sfrs and the lower portion of gpr bank 0 without using the bank select register (bsr). section 5.4.2 ?access bank? provides a detailed description of the access ram. 5.4.1 bank select register (bsr) large areas of data memory require an efficient addressing scheme to make rapid access to any address possible. ideally, this means that an entire address does not need to be provided for each read or write operation. for pic18 devices, this is accom- plished with a ram banking scheme. this divides the memory space into 16 contiguous banks of 256 bytes. depending on the instruction, each location can be addressed directly by its full 12-bit address, or an 8-bit low-order address and a 4-bit bank pointer. most instructions in the pic18 instruction set make use of the bank pointer, known as the bank select register (bsr). this sfr holds the 4 most significant bits of a location?s address; the instruction itself includes the 8 least significant bits. only the four lower bits of the bsr are implemented (bsr<3:0>). the upper four bits are unused; they will always read ? 0 ? and cannot be written to. the bsr can be loaded directly by using the movlb instruction. the value of the bsr indicates the bank in data memory; the eight bits in the instruction show the location in the bank and can be thought of as an offset from the bank?s lower boundary. the relationship between the bsr?s value and the bank division in data memory is shown in figures 5-5 through 5-7 . since up to 16 registers may share the same low-order address, the user must always be careful to ensure that the proper bank is selected before performing a data read or write. for example, writing what should be program data to an 8-bit address of f9h while the bsr is 0fh will end up resetting the program counter. while any bank can be selected, only those banks that are actually implemented can be read or written to. writes to unimplemented banks are ignored, while reads from unimplemented banks will return ? 0 ?s. even so, the status register will still be affected as if the operation was successful. the data memory maps in figures 5-5 through 5-7 indicate which banks are implemented. in the core pic18 instruction set, only the movff instruction fully specifies the 12-bit address of the source and target registers. this instruction ignores the bsr completely when it executes. all other instructions include only the low-order address as an operand and must use either the bsr or the access bank to locate their target registers. note: the operation of some aspects of data memory are changed when the pic18 extended instruction set is enabled. see section 5.7 ?data memory and the extended instruction set? for more information. ? 2010-2012 microchip technology inc. ds41412f-page 77 pic18(l)f2x/4xk22 figure 5-5: data memory map for pic18( l)f23k22 and pic18(l)f43k22 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh f60h fffh 00h 5fh 60h ffh access bank 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 second 160 bytes are special function registers (from bank 15). when ?a? = 1 : the bsr specifies the bank used by the instruction. f5fh f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h ffh 00h gpr gpr sfr (1) access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 unused read 00h unused f38h f37h note 1: addresses f38h through f5fh are also used by sfrs, but are not part of the access ram. users must always use the complete address or load the proper bsr value to access these registers. sfr pic18(l)f2x/4xk22 ds41412f-page 78 ? 2010-2012 microchip technology inc. figure 5-6: data memory map for pic18( l)f24k22 and pic18(l)f44k22 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh f60h fffh 00h 5fh 60h ffh access bank 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 second 160 bytes are special function registers (from bank 15). when ?a? = 1 : the bsr specifies the bank used by the instruction. f5fh f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h ffh 00h gpr gpr sfr (1) access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h gpr ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 unused read 00h unused sfr note 1: addresses f38h through f5fh are also used by sfrs, but are not part of the access ram. users must always use the complete address or load the proper bsr value to access these registers. f38h f37h ? 2010-2012 microchip technology inc. ds41412f-page 79 pic18(l)f2x/4xk22 figure 5-7: data memory map for pic18( l)f25k22 and pic18(l)f45k22 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh f60h fffh 00h 5fh 60h ffh access bank 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 second 160 bytes are special function registers (from bank 15). when ?a? = 1 : the bsr specifies the bank used by the instruction. f5fh f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h ffh 00h gpr gpr sfr (1) access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h gpr ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 unused read 00h unused gpr gpr gpr sfr note 1: addresses f38h through f5fh are also used by sfrs, but are not part of the access ram. users must always use the complete address or load the proper bsr value to access these registers. f38h f37h pic18(l)f2x/4xk22 ds41412f-page 80 ? 2010-2012 microchip technology inc. figure 5-8: data memory map for pic18( l)f26k22 and pic18(l)f46k22 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh 00h 5fh 60h ffh access bank 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 second 160 bytes are special function registers (from bank 15). when ?a? = 1 : the bsr specifies the bank used by the instruction. 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h gpr gpr access ram high access ram low bank 2 = 0110 = 0010 (sfrs) 2ffh 200h 3ffh 300h 4ffh 400h 5ffh 500h 6ffh 600h 7ffh 700h 8ffh 800h 9ffh 900h affh a00h bffh b00h cffh c00h dffh d00h e00h bank 3 bank 4 bank 5 bank 6 bank 7 bank 8 bank 9 bank 10 bank 11 bank 12 bank 13 ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h gpr ffh 00h = 0011 = 0100 = 0101 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 gpr gpr gpr gpr gpr gpr gpr gpr gpr gpr gpr gpr note 1: addresses f38h through f5fh are also used by sfrs, but are not part of the access ram. users must always use the complete address or load the proper bsr value to access these registers. f60h fffh f5fh f00h ffh 00h sfr (1) gpr sfr f38h f37h ? 2010-2012 microchip technology inc. ds41412f-page 81 pic18(l)f2x/4xk22 figure 5-9: use of the bank select register (direct addressing) note 1: the access ram bit of the instruction can be used to force an override of the selected bank (bsr<3:0>) to the registers of the access bank. 2: the movff instruction embeds the entire 12-bit address in the instruction. data memory bank select (2) 7 0 from opcode (2) 0000 000h 100h 200h 300h f00h e00h fffh bank 0 bank 1 bank 2 bank 14 bank 15 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh 00h ffh bank 3 through bank 13 0011 11111111 7 0 bsr (1) pic18(l)f2x/4xk22 ds41412f-page 82 ? 2010-2012 microchip technology inc. 5.4.2 access bank while the use of the bsr with an embedded 8-bit address allows users to address the entire range of data memory, it also means that the user must always ensure that the correct bank is selected. otherwise, data may be read from or written to the wrong location. this can be disastrous if a gpr is the intended target of an operation, but an sfr is written to instead. verifying and/or changing the bsr for each read or write to data memory can become very inefficient. to streamline access for the most commonly used data memory locations, the data memory is configured with an access bank, which allows users to access a mapped block of memory without specifying a bsr. the access bank consists of the first 96 bytes of mem- ory (00h-5fh) in bank 0 and the last 160 bytes of mem- ory (60h-ffh) in block 15. the lower half is known as the ?access ram? and is composed of gprs. this upper half is also where the device?s sfrs are mapped. these two areas are mapped contiguously in the access bank and can be addressed in a linear fashion by an 8-bit address (figures 5-5 through 5-7 ). the access bank is used by core pic18 instructions that include the access ram bit (the ?a? parameter in the instruction). when ?a? is equal to ? 1 ?, the instruction uses the bsr and the 8-bit address included in the opcode for the data memory address. when ?a? is ? 0 ?, however, the instruction is forced to use the access bank address map; the current value of the bsr is ignored entirely. using this ?forced? addressing allows the instruction to operate on a data address in a single cycle, without updating the bsr first. for 8-bit addresses of 60h and above, this means that users can evaluate and operate on sfrs more efficiently. the access ram below 60h is a good place for data values that the user might need to access rapidly, such as immediate computational results or common program variables. access ram also allows for faster and more code efficient context saving and switching of variables. the mapping of the access bank is slightly different when the extended instruction set is enabled (xinst configuration bit = 1 ). this is discussed in more detail in section 5.7.3 ?mapping the access bank in indexed literal offset mode? . 5.4.3 general purpose register file pic18 devices may have banked memory in the gpr area. this is data ram, which is available for use by all instructions. gprs start at the bottom of bank 0 (address 000h) and grow upwards towards the bottom of the sfr area. gprs are not initialized by a power-on reset and are unchanged on all other resets. 5.4.4 special function registers the special function registers (sfrs) are registers used by the cpu and peripheral modules for controlling the desired operation of the device. these registers are implemented as static ram. sfrs start at the top of data memory (fffh) and extend downward to occupy the top portion of bank 15 (f38h to fffh). a list of these registers is given in ta b l e 5 - 1 and tab l e 5 - 2 . the sfrs can be classified into two sets: those associated with the ?core? device functionality (alu, resets and interrupts) and those related to the peripheral functions. the reset and interrupt registers are described in their respective chapters, while the alu?s status register is described later in this section. registers related to the operation of a peripheral feature are described in the chapter for that peripheral. the sfrs are typically distributed among the peripherals whose functions they control. unused sfr locations are unimplemented and read as ? 0 ?s. ? 2010-2012 microchip technology inc. ds41412f-page 83 pic18(l)f2x/4xk22 table 5-1: special function register map for pic18(l)f2x/4xk22 devices address name address name address name address name address name fffh tosu fd7h tmr0h fafh spbrg1 f87h ? (2) f5fh ccpr3h ffeh tosh fd6h tmr0l faeh rcreg1 f86h ? (2) f5eh ccpr3l ffdh tosl fd5h t0con fadh txreg1 f85h ? (2) f5dh ccp3con ffch stkptr fd4h ? (2) fach txsta1 f84h porte f5ch pwm3con ffbh pclatu fd3h osccon fabh rcsta1 f83h portd (3) f5bh eccp3as ffah pclath fd2h osccon2 faah eeadrh (4) f82h portc f5ah pstr3con ff9h pcl fd1h wdtcon fa9h eeadr f81h portb f59h ccpr4h ff8h tblptru fd0h rcon fa8h eedata f80h porta f58h ccpr4l ff7h tblptrh fcfh tmr1h fa7h eecon2 (1) f7fh ipr5 f57h ccp4con ff6h tblptrl fceh tmr1l fa6h eecon1 f7eh pir5 f56h ccpr5h ff5h tablat fcdh t1con fa5h ipr3 f7dh pie5 f55h ccpr5l ff4h prodh fcch t1gcon fa4h pir3 f7ch ipr4 f54h ccp5con ff3h prodl fcbh ssp1con3 fa3h pie3 f7bh pir4 f53h tmr4 ff2h intcon fcah ssp1msk fa2h ipr2 f7ah pie4 f52h pr4 ff1h intcon2 fc9h ssp1buf fa1h pir2 f79h cm1con0 f51h t4con ff0h intcon3 fc8h ssp1add fa0h pie2 f78h cm2con0 f50h tmr5h fefh indf0 (1) fc7h ssp1stat f9fh ipr1 f77h cm2con1 f4fh tmr5l feeh postinc0 (1) fc6h ssp1con1 f9eh pir1 f76h spbrgh2 f4eh t5con fedh postdec0 (1) fc5h ssp1con2 f9dh pie1 f75h spbrg2 f4dh t5gcon fech preinc0 (1) fc4h adresh f9ch hlvdcon f74h rcreg2 f4ch tmr6 febh plusw0 (1) fc3h adresl f9bh osctune f73h txreg2 f4bh pr6 feah fsr0h fc2h adcon0 f9ah ? (2) f72h txsta2 f4ah t6con fe9h fsr0l fc1h adcon1 f99h ? (2) f71h rcsta2 f49h ccptmrs0 fe8h wreg fc0h adcon2 f98h ? (2) f70h baudcon2 f48h ccptmrs1 fe7h indf1 (1) fbfh ccpr1h f97h ? (2) f6fh ssp2buf f47h srcon0 fe6h postinc1 (1) fbeh ccpr1l f96h trise f6eh ssp2add f46h srcon1 fe5h postdec1 (1) fbdh ccp1con f95h trisd (3) f6dh ssp2stat f45h ctmuconh fe4h preinc1 (1) fbch tmr2 f94h trisc f6ch ssp2con1 f44h ctmuconl fe3h plusw1 (1) fbbh pr2 f93h trisb f6bh ssp2con2 f43h ctmuicon fe2h fsr1h fbah t2con f92h trisa f6ah ssp2msk f42h vrefcon0 fe1h fsr1l fb9h pstr1con f91h ? (2) f69h ssp2con3 f41h vrefcon1 fe0h bsr fb8h baudcon1 f90h ? (2) f68h ccpr2h f40h vrefcon2 fdfh indf2 (1) fb7h pwm1con f8fh ? (2) f67h ccpr2l f3fh pmd0 fdeh postinc2 (1) fb6h eccp1as f8eh ? (2) f66h ccp2con f3eh pmd1 fddh postdec2 (1) fb5h ? (2) f8dh late (3) f65h pwm2con f3dh pmd2 fdch preinc2 (1) fb4h t3gcon f8ch latd (3) f64h eccp2as f3ch ansele fdbh plusw2 (1) fb3h tmr3h f8bh latc f63h pstr2con f3bh anseld fdah fsr2h fb2h tmr3l f8ah latb f62h iocb f3ah anselc fd9h fsr2l fb1h t3con f89h lata f61h wpub f39h anselb fd8h status fb0h spbrgh1 f88h ? (2) f60h slrcon f38h ansela note 1: this is not a physical register. 2: unimplemented registers are read as ? 0 ?. 3: pic18(l)f4xk22 devices only. 4: pic18(l)f26k22 and pic18(l)f46k22 devices only. pic18(l)f2x/4xk22 ds41412f-page 84 ? 2010-2012 microchip technology inc. table 5-2: register file summary for pic18(l)f2x/4xk22 devices address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on p or , bor fffh tosu ? ? ? top-of-stack, upper byte (tos<20:16>) ---0 0000 ffeh tosh top-of-stack, high byte (tos<15:8>) 0000 0000 ffdh tosl top-of-stack, low byte (tos<7:0>) 0000 0000 ffch stkptr stkful stkunf ?stkptr<4:0> 00-0 0000 ffbh pclatu ? ? ? holding register for pc<20:16> ---0 0000 ffah pclath holding register for pc<15:8> 0000 0000 ff9h pcl holding register for pc<7:0> 0000 0000 ff8h tblptru ? ? program memory table pointer upper byte(tblptr<21:16>) --00 0000 ff7h tblptrh program memory table pointer high byte(tblptr<15:8>) 0000 0000 ff6h tblptrl program memory table pointer low byte(tblptr<7:0>) 0000 0000 ff5h tablat program memory table latch 0000 0000 ff4h prodh product register, high byte xxxx xxxx ff3h prodl product register, low byte xxxx xxxx ff2h intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x ff1h intcon2 rbpu intedg0 intedg1 intedg2 ?tmr0ip ?rbip 1111 -1-1 ff0h intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 11-0 0-00 fefh indf0 uses contents of fsr0 to address data memory ? value of fsr0 not changed (not a physical register) ---- ---- feeh postinc0 uses contents of fsr0 to address data memory ? value of fsr0 post-incremented (not a physical register) ---- ---- fedh postdec0 uses contents of fsr0 to address data memory ? value of fsr0 post-decremented (not a physical register) ---- ---- fech preinc0 uses contents of fsr0 to address data memory ? value of fsr0 pre-incremented (not a physical register) ---- ---- febh plusw0 uses contents of fsr0 to address data memory ? value of fsr0 pre-incremented (not a physical register) ? value of fsr0 offset by w ---- ---- feah fsr0h ? ? ? ? indirect data memory address pointer 0, high byte ---- 0000 fe9h fsr0l indirect data memory address pointer 0, low byte xxxx xxxx fe8h wreg working register xxxx xxxx fe7h indf1 uses contents of fsr1 to address data memory ? value of fsr1 not changed (not a physical register) ---- ---- fe6h postinc1 uses contents of fsr1 to address data memory ? value of fsr1 post-incremented (not a physical register) ---- ---- fe5h postdec1 uses contents of fsr1 to address data memory ? value of fsr1 post-decremented (not a physical register) ---- ---- fe4h preinc1 uses contents of fsr1 to address data memory ? value of fsr1 pre-incremented (not a physical register) ---- ---- fe3h plusw1 uses contents of fsr1 to address data memory ? value of fsr1 pre-incremented (not a physical register) ? value of fsr1 offset by w ---- ---- fe2h fsr1h ? ? ? ? indirect data memory address pointer 1, high byte ---- 0000 fe1h fsr1l indirect data memory address pointer 1, low byte xxxx xxxx fe0h bsr ? ? ? ? bank select register ---- 0000 fdfh indf2 uses contents of fsr2 to address data memory ? value of fsr2 not changed (not a physical register) ---- ---- fdeh postinc2 uses contents of fsr2 to address data memory ? value of fsr2 post-incremented (not a physical register) ---- ---- fddh postdec2 uses contents of fsr2 to address data memory ? value of fsr2 post-decremented (not a physical register) ---- ---- fdch preinc2 uses contents of fsr2 to address data memory ? value of fsr2 pre-incremented (not a physical register) ---- ---- fdbh plusw2 uses contents of fsr2 to address data memory ? value of fsr2 pre-incremented (not a physical register) ? value of fsr2 offset by w ---- ---- fdah fsr2h ? ? ? ? indirect data memory address pointer 2, high byte ---- 0000 fd9h fsr2l indirect data memory address pointer 2, low byte xxxx xxxx fd8h status ? ? ?novzdcc ---x xxxx fd7h tmr0h timer0 register, high byte 0000 0000 fd6h tmr0l timer0 register, low byte xxxx xxxx fd5h t0con tmr0on t08bit t0cs t0se psa t0ps<2:0> 1111 1111 fd3h osccon idlen ircf<2:0> osts hfiofs scs<1:0> 0011 q000 fd2h osccon2 pllrdy soscrun ? mfiosel soscgo prisd mfiofs lfiofs 00-0 01x0 legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: pic18(l)f4xk22 devices only. 2: pic18(l)f2xk22 devices only. 3: pic18(l)f23/24k22 and pic18(l)f43/44k22 devices only. 4: pic18(l)f26k22 and pic18(l)f46k22 devices only. ? 2010-2012 microchip technology inc. ds41412f-page 85 pic18(l)f2x/4xk22 fd1hwdtcon???? ? ? ?swdten ---- ---0 fd0h rcon ipen sboren ?ri to pd por bor 01-1 1100 fcfh tmr1h holding register for the most significant byte of the 16-bit tmr1 register xxxx xxxx fceh tmr1l least significant byte of the 16-bit tmr1 register xxxx xxxx fcdh t1con tmr1cs<1:0> t1ckps<1:0> t1soscen t1sync t1rd16 tmr1on 0000 0000 fcch t1gcon tmr1ge t1gpol t1gtm t1gspm t1ggo/ done t1gval t1gss<1:0> 0000 xx00 fcbh ssp1con3 acktim pcie scie boen sdaht sbcde ahen dhen 0000 0000 fcah ssp1msk ssp1 mask register bits 1111 1111 fc9h ssp1buf ssp1 receive buffer/transmit register xxxx xxxx fc8h ssp1add ssp1 address register in i 2 c slave mode. ssp1 baud rate reload register in i 2 c master mode 0000 0000 fc7h ssp1stat smp cke d/a psr/w ua bf 0000 0000 fc6h ssp1con1 wcol sspov sspen ckp sspm<3:0> 0000 0000 fc5h ssp1con2 gcen ackstat ackdt acken rcen pen rsen sen 0000 0000 fc4h adresh a/d result, high byte xxxx xxxx fc3h adresl a/d result, low byte xxxx xxxx fc2h adcon0 ? chs<4:0> go/done adon --00 0000 fc1h adcon1 trigsel ? ? ? pvcfg<1:0> nvcfg<1:0> 0--- 0000 fc0h adcon2 adfm ? acqt<2:0> adcs<2:0> 0-00 0000 fbfh ccpr1h capture/compare/pwm register 1, high byte xxxx xxxx fbeh ccpr1l capture/compare/pwm register 1, low byte xxxx xxxx fbdh ccp1con p1m<1:0> dc1b<1:0> ccp1m<3:0> 0000 0000 fbch tmr2 timer2 register 0000 0000 fbbh pr2 timer2 period register 1111 1111 fbah t2con ? t2outps<3:0> tmr2on t2ckps<1:0> -000 0000 fb9h pstr1con ? ? ? str1sync str1d str1c str1b str1a ---0 0001 fb8h baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 0100 0-00 fb7h pwm1con p1rsen p1dc<6:0> 0000 0000 fb6h eccp1as ccp1ase ccp1as<2:0> pss1ac<1:0> pss1bd<1:0> 0000 0000 fb4h t3gcon tmr3ge t3gpol t3gtm t3gspm t3ggo/ done t3gval t3gss<1:0> 0000 0x00 fb3h tmr3h holding register for the most significant byte of the 16-bit tmr3 register xxxx xxxx fb2h tmr3l least significant byte of the 16-bit tmr3 register xxxx xxxx fb1h t3con tmr3cs<1:0> t3ckps<1:0> t3soscen t3sync t3rd16 tmr3on 0000 0000 fb0h spbrgh1 eusart1 baud rate generator, high byte 0000 0000 fafh spbrg1 eusart1 baud rate generator, low byte 0000 0000 faeh rcreg1 eusart1 receive register 0000 0000 fadh txreg1 eusart1 transmit register 0000 0000 fach txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 fabh rcsta1 spen rx9 sren cren adden ferr oerr rx9d 0000 000x faah eeadrh (5) ? ? ? ? ? ? eeadr<9:8> ---- --00 fa9h eeadr eeadr<7:0> 0000 0000 fa8h eedata eeprom data register 0000 0000 fa7h eecon2 eeprom control register 2 (not a physical register) ---- --00 fa6h eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 fa5h ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 0000 0000 fa4h pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 0000 0000 fa3h pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 0000 0000 table 5-2: register file summary for pic18(l)f2x/4xk22 devices (continued) address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on p or , bor legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: pic18(l)f4xk22 devices only. 2: pic18(l)f2xk22 devices only. 3: pic18(l)f23/24k22 and pic18(l)f43/44k22 devices only. 4: pic18(l)f26k22 and pic18(l)f46k22 devices only. pic18(l)f2x/4xk22 ds41412f-page 86 ? 2010-2012 microchip technology inc. fa2h ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 1111 1111 fa1h pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 0000 0000 fa0h pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 0000 0000 f9fh ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip -111 1111 f9eh pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if -000 0000 f9dh pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie -000 0000 f9ch hlvdcon vdirmag bgvst irvst hlvden hlvdl<3:0> 0000 0000 f9bh osctune intsrc pllen tun<5:0> 00xx xxxx f96h trise wpue3 ? ? ? ?trise2 (1) trise1 (1) trise0 (1) 1--- -111 f95h trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 1111 1111 f94h trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 1111 1111 f93h trisb trisb7 trisb6 trisb5 tr isb4 trisb3 trisb2 trisb1 trisb0 1111 1111 f92h trisa trisa7 trisa6 trisa5 tr isa4 trisa3 trisa2 trisa1 trisa0 1111 1111 f8dh late (1) ? ? ? ? ? late2 late1 late0 ---- -xxx f8ch latd (1) latd7 latd6 latd5 latd4 latd3 latd2 latd1 latd0 xxxx xxxx f8bh latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 xxxx xxxx f8ah latb latb7 latb6 latb5 latb4 latb3 latb2 latb1 latb0 xxxx xxxx f89h lata lata7 lata6 lata5 lata4 lata3 lata2 lata1 lata0 xxxx xxxx f84h porte (2) ? ? ? ?re3 ? ? ? ---- x--- porte (1) ? ? ? ?re3re2re1re0 ---- x000 f83h portd (1) rd7 rd6 rd5 rd4 rd3 rd2 rd1 rd0 0000 0000 f82h portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 0000 00xx f81h portb rb7 rb6 rb5 rb4 rb3 rb2 rb1 rb0 xxx0 0000 f80h porta ra7 ra6 ra5 ra4 ra3 ra2 ra1 ra0 xx0x 0000 f7fh ipr5 ? ? ? ? ? tmr6ip tmr5ip tmr4ip ---- -111 f7eh pir5 ? ? ? ? ? tmr6if tmr5if tmr4if ---- -111 f7dh pie5 ? ? ? ? ? tmr6ie tmr5ie tmr4ie ---- -000 f7ch ipr4 ? ? ? ? ? ccp5ip ccp4ip ccp3ip ---- -000 f7bh pir4 ? ? ? ? ? ccp5if ccp4if ccp3if ---- -000 f7ah pie4 ? ? ? ? ? ccp5ie ccp4ie ccp3ie ---- -000 f79h cm1con0 c1on c1out c1oe c1pol c1sp c1r c1ch<1:0> 0000 1000 f78h cm2con0 c2on c2out c2oe c2pol c2sp c2r c2ch<1:0> 0000 1000 f77h cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 0000 0000 f76h spbrgh2 eusart2 baud rate generator, high byte 0000 0000 f75h spbrg2 eusart2 baud rate generator, low byte 0000 0000 f74h rcreg2 eusart2 receive register 0000 0000 f73h txreg2 eusart2 transmit register 0000 0000 f72h txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 f71h rcsta2 spen rx9 sren cren adden ferr oerr rx9d 0000 000x f70h baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 01x0 0-00 f6fh ssp2buf ssp2 receive buffer/transmit register xxxx xxxx f6eh ssp2add ssp2 address register in i 2 c slave mode. ssp2 baud rate reload register in i 2 c master mode 0000 0000 f6dh ssp2stat smp cke d/a psr/w ua bf 0000 0000 f6ch ssp2con1 wcol sspov sspen ckp sspm<3:0> 0000 0000 f6bh ssp2con2 gcen ackstat ackdt acken rcen pen rsen sen 0000 0000 f6ah ssp2msk ssp1 mask register bits 1111 1111 f69h ssp2con3 acktim pcie scie boen sdaht sbcde ahen dhen 0000 0000 table 5-2: register file summary for pic18(l)f2x/4xk22 devices (continued) address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on p or , bor legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: pic18(l)f4xk22 devices only. 2: pic18(l)f2xk22 devices only. 3: pic18(l)f23/24k22 and pic18(l)f43/44k22 devices only. 4: pic18(l)f26k22 and pic18(l)f46k22 devices only. ? 2010-2012 microchip technology inc. ds41412f-page 87 pic18(l)f2x/4xk22 f68h ccpr2h capture/compare/pwm register 2, high byte xxxx xxxx f67h ccpr2l capture/compare/pwm register 2, low byte xxxx xxxx f66h ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 0000 0000 f65h pwm2con p2rsen p2dc<6:0> 0000 0000 f64h eccp2as ccp2ase ccp2as<2:0> pss2ac<1:0> pss2bd<1:0> 0000 0000 f63h pstr2con ? ? ? str2sync str2d str2c str2b str2a ---0 0001 f62h iocb iocb7 iocb6 iocb5 iocb4 ? ? ? ? 1111 ---- f61h wpub wpub7 wpub6 wpub5 wpub4 wpub3 wpub2 wpub1 wpub0 1111 1111 f60h slrcon (2) ???? ?slrcslrbslra ---- -111 slrcon (1) ? ? ? slre slrd slrc slrb slra ---1 1111 f5fh ccpr3h capture/compare/pwm register 3, high byte xxxx xxxx f5eh ccpr3l capture/compare/pwm register 3, low byte xxxx xxxx f5dh ccp3con p3m<1:0> dc3b<1:0> ccp3m<3:0> 0000 0000 f5ch pwm3con p3rsen p3dc<6:0> 0000 0000 f5bh eccp3as ccp3ase ccp3as<2:0> pss3ac<1:0> pss3bd<1:0> 0000 0000 f5ah pstr3con ? ? ? str3sync str3d str3c str3b str3a ---0 0001 f59h ccpr4h capture/compare/pwm register 4, high byte xxxx xxxx f58h ccpr4l capture/compare/pwm register 4, low byte xxxx xxxx f57h ccp4con ? ? dc4b<1:0> ccp4m<3:0> --00 0000 f56h ccpr5h capture/compare/pwm register 5, high byte xxxx xxxx f55h ccpr5l capture/compare/pwm register 5, low byte xxxx xxxx f54h ccp5con ? ? dc5b<1:0> ccp5m<3:0> --00 0000 f53h tmr4 timer4 register 0000 0000 f52h pr4 timer4 period register 1111 1111 f51h t4con ? t4outps<3:0> tmr4on t4ckps<1:0> -000 0000 f50h tmr5h holding register for the most significant byte of the 16-bit tmr5 register 0000 0000 f4fh tmr5l least significant byte of the 16-bit tmr5 register 0000 0000 f4eh t5con tmr5cs<1:0> t5ckps<1:0> t5soscen t5sync t5rd16 tmr5on 0000 0000 f4dh t5gcon tmr5ge t5gpol t5gtm t5gspm t5ggo/ done t5gval t5gss<1:0> 0000 0x00 f4ch tmr6 timer6 register 0000 0000 f4bh pr6 timer6 period register 1111 1111 f4ah t6con ? t6outps<3:0> tmr6on t6ckps<1:0> -000 0000 f49h ccptmrs0 c3tsel<1:0> ? c2tsel<1:0> ? c1tsel<1:0> 00-0 0-00 f48h ccptmrs1 ? ? ? ? c5tsel<1:0> c4tsel<1:0> ---- 0000 f47h srcon0 srlen srclk<2:0> srqen srnqen srps srpr 0000 0000 f46h srcon1 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e 0000 0000 f45h ctmuconh ctmuen ? ctmusidl tgen edgen edgseqen idissen cttrig 0000 0000 f44h ctmuconl edg2pol edg2sel<1:0> edg1pol edg1sel<1:0> edg2stat edg1stat 0000 0000 f43h ctmuicon itrim<5:0> irng<1:0> 0000 0000 f42h vrefcon0 fvren fvrst fvrs<1:0> ? ? ? ? 0001 ---- f41h vrefcon1 dacen daclps dacoe ? dacpss<1:0> ? dacnss 000- 00-0 f40h vrefcon2 ? ? ? dacr<4:0> ---0 0000 f3fh pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 0000 0000 f3eh pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 00-0 0000 f3dh pmd2 ? ? ? ? ctmumd cmp2md cmp1md adcmd ---- 0000 f3ch ansele (1) ? ? ? ? ? anse2 anse1 anse0 ---- -111 f3bh anseld (1) ansd7 ansd6 ansd5 ansd4 ansd3 ansd2 ansd1 ansd0 1111 1111 table 5-2: register file summary for pic18(l)f2x/4xk22 devices (continued) address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on p or , bor legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: pic18(l)f4xk22 devices only. 2: pic18(l)f2xk22 devices only. 3: pic18(l)f23/24k22 and pic18(l)f43/44k22 devices only. 4: pic18(l)f26k22 and pic18(l)f46k22 devices only. pic18(l)f2x/4xk22 ds41412f-page 88 ? 2010-2012 microchip technology inc. f3ah anselc ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ? ? 1111 11-- f39h anselb ? ? ansb5 ansb4 ansb3 ansb2 ansb1 ansb0 --11 1111 f38h ansela ? ? ansa5 ? ansa3 ansa2 ansa1 ansa0 --1- 1111 table 5-2: register file summary for pic18(l)f2x/4xk22 devices (continued) address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on p or , bor legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: pic18(l)f4xk22 devices only. 2: pic18(l)f2xk22 devices only. 3: pic18(l)f23/24k22 and pic18(l)f43/44k22 devices only. 4: pic18(l)f26k22 and pic18(l)f46k22 devices only. ? 2010-2012 microchip technology inc. ds41412f-page 89 pic18(l)f2x/4xk22 5.4.5 status register the status register, shown in register 5-2 , contains the arithmetic status of the alu. as with any other sfr, it can be the operand for any instruction. if the status register is the destination for an instruc- tion that affects the z, dc, c, ov or n bits, the results of the instruction are not written; instead, the status register is updated according to the instruction per- formed. therefore, the result of an instruction with the status register as its destination may be different than intended. as an example, clrf status will set the z bit and leave the remaining status bits unchanged (? 000u u1uu ?). it is recommended 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 in the status register. for other instructions that do not affect status bits, see the instruction set summaries in section 25.2 ?extended instruction set? and table 25-3 . 5.5 register definitions: status note: the c and dc bits operate as the borrow and digit borrow bits, respectively, in subtraction. register 5-2: status: status register u-0 u-0 u-0 r/w-x r/w-x r/w-x r/w-x r/w-x ? ? ? nov z dc (1) c (1) 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-5 unimplemented: read as ? 0 ? bit 4 n: negative bit this bit is used for signed arithmetic (two?s complement). it indicates whether the result was negative (alu msb = 1 ). 1 = result was negative 0 = result was positive bit 3 ov: overflow bit this bit is used for signed arithmetic (two?s complement). it indicates an overflow of the 7-bit magnitude which causes the sign bit (bit 7 of the result) to change state. 1 = overflow occurred for signed arithmetic (in this arithmetic operation) 0 = no overflow occurred bit 2 z: zero bit 1 = the result of an arithmetic or logic operation is zero 0 = the result of an arithmetic or logic operation is not zero bit 1 dc: digit carry/borrow bit ( addwf , addlw,sublw,subwf instructions) (1) 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 bit 0 c: carry/borrow bit ( addwf , addlw, sublw, subwf instructions) (1) 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 1: for borrow , the polarity is reversed. a subtraction is executed by adding the two?s complement of the second operand. for rotate ( rrf , rlf ) instructions, this bit is loaded with either the high-order or low-order bit of the source register. pic18(l)f2x/4xk22 ds41412f-page 90 ? 2010-2012 microchip technology inc. 5.6 data addressing modes while the program memory can be addressed in only one way ? through the program counter ? information in the data memory space can be addressed in several ways. for most instructions, the addressing mode is fixed. other instructions may use up to three modes, depending on which operands are used and whether or not the extended instruction set is enabled. the addressing modes are: ? inherent ? literal ?direct ?indirect an additional addressing mode, indexed literal offset, is available when the extended instruction set is enabled (xinst configuration bit = 1 ). its operation is discussed in greater detail in section 5.7.1 ?indexed addressing with literal offset? . 5.6.1 inherent and literal addressing many pic18 control instructions do not need any argu- ment at all; they either perform an operation that glob- ally affects the device or they operate implicitly on one register. this addressing mode is known as inherent addressing. examples include sleep , reset and daw . other instructions work in a similar way but require an additional explicit argument in the opcode. this is known as literal addressing mode because they require some literal value as an argument. examples include addlw and movlw , which respectively, add or move a literal value to the w register. other examples include call and goto , which include a 20-bit program memory address. 5.6.2 direct addressing direct addressing specifies all or part of the source and/or destination address of the operation within the opcode itself. the options are specified by the arguments accompanying the instruction. in the core pic18 instruction set, bit-oriented and byte- oriented instructions use some version of direct addressing by default. all of these instructions include some 8-bit literal address as their least significant byte. this address specifies either a register address in one of the banks of data ram ( section 5.4.3 ?general purpose register file? ) or a location in the access bank ( section 5.4.2 ?access bank? ) as the data source for the instruction. the access ram bit ?a? determines how the address is interpreted. when ?a? is ? 1 ?, the contents of the bsr ( section 5.4.1 ?bank select register (bsr)? ) are used with the address to determine the complete 12-bit address of the register. when ?a? is ? 0 ?, the address is interpreted as being a register in the access bank. addressing that uses the access ram is sometimes also known as direct forced addressing mode. a few instructions, such as movff , include the entire 12-bit address (either source or destination) in their opcodes. in these cases, the bsr is ignored entirely. the destination of the operation?s results is determined by the destination bit ?d?. when ?d? is ? 1 ?, the results are stored back in the source register, overwriting its origi- nal contents. when ?d? is ? 0 ?, the results are stored in the w register. instructions without the ?d? argument have a destination that is implicit in the instruction; their destination is either the target register being operated on or the w register. 5.6.3 indirect addressing indirect addressing allows the user to access a location in data memory without giving a fixed address in the instruction. this is done by using file select registers (fsrs) as pointers to the locations which are to be read or written. since the fsrs are themselves located in ram as special file registers, they can also be directly manipulated under program control. this makes fsrs very useful in implementing data struc- tures, such as tables and arrays in data memory. the registers for indirect addressing are also implemented with indirect file operands (indfs) that permit automatic manipulation of the pointer value with auto-incrementing, auto-decrementing or offsetting with another value. this allows for efficient code, using loops, such as the example of clearing an entire ram bank in example 5-5 . example 5-5: how to clear ram (bank 1) using indirect addressing note: the execution of some instructions in the core pic18 instruction set are changed when the pic18 extended instruction set is enabled. see section 5.7 ?data memory and the extended instruction set? for more information. lfsr fsr0, 100h ; next clrf postinc0 ; clear indf ; register then ; inc pointer btfss fsr0h, 1 ; all done with ; bank1? bra next ; no, clear next continue ; yes, continue ? 2010-2012 microchip technology inc. ds41412f-page 91 pic18(l)f2x/4xk22 5.6.3.1 fsr registers and the indf operand at the core of indirect addressing are three sets of reg- isters: fsr0, fsr1 and fsr2. each represents a pair of 8-bit registers, fsrnh and fsrnl. each fsr pair holds a 12-bit value, therefore, the four upper bits of the fsrnh register are not used. the 12-bit fsr value can address the entire range of the data memory in a linear fashion. the fsr register pairs, then, serve as pointers to data memory locations. indirect addressing is accomplished with a set of indirect file operands, indf0 through indf2. these can be thought of as ?virtual? registers: they are mapped in the sfr space but are not physically implemented. reading or writing to a particular indf register actually accesses its corresponding fsr register pair. a read from indf1, for example, reads the data at the address indicated by fsr1h:fsr1l. instructions that use the indf registers as operands actually use the contents of their corresponding fsr as a pointer to the instruction?s target. the indf operand is just a convenient way of using the pointer. because indirect addressing uses a full 12-bit address, data ram banking is not necessary. thus, the current contents of the bsr and the access ram bit have no effect on determining the target address. 5.6.3.2 fsr registers and postinc, postdec, preinc and plusw in addition to the indf operand, each fsr register pair also has four additional indirect operands. like indf, these are ?virtual? registers which cannot be directly read or written. accessing these registers actually accesses the location to which the associated fsr register pair points, and also performs a specific action on the fsr value. they are: ? postdec: accesses the location to which the fsr points, then automatically decrements the fsr by 1 afterwards ? postinc: accesses the location to which the fsr points, then automatically increments the fsr by 1 afterwards ? preinc: automatically increments the fsr by one, then uses the location to which the fsr points in the operation ? plusw: adds the signed value of the w register (range of -127 to 128) to that of the fsr and uses the location to which the result points in the operation. in this context, accessing an indf register uses the value in the associated fsr register without changing it. similarly, accessing a plusw register gives the fsr value an offset by that in the w register; however, neither w nor the fsr is actually changed in the operation. accessing the other virtual registers changes the value of the fsr register. figure 5-10: indirect addressing fsr1h:fsr1l 0 7 data memory 000h 100h 200h 300h f00h e00h fffh bank 0 bank 1 bank 2 bank 14 bank 15 bank 3 through bank 13 addwf, indf1, 1 0 7 using an instruction with one of the indirect addressing registers as the operand.... ...uses the 12-bit address stored in the fsr pair associated with that register.... ...to determine the data memory location to be used in that operation. in this case, the fsr1 pair contains ecch. this means the contents of location ecch will be added to that of the w register and stored back in ecch. xxxx 1110 11001100 pic18(l)f2x/4xk22 ds41412f-page 92 ? 2010-2012 microchip technology inc. operations on the fsrs with postdec, postinc and preinc affect the entire register pair; that is, roll- overs of the fsrnl register from ffh to 00h carry over to the fsrnh register. on the other hand, results of these operations do not change the value of any flags in the status register (e.g., z, n, ov, etc.). the plusw register can be used to implement a form of indexed addressing in the data memory space. by manipulating the value in the w register, users can reach addresses that are fixed offsets from pointer addresses. in some applications, this can be used to implement some powerful program control structure, such as software stacks, inside of data memory. 5.6.3.3 operations by fsrs on fsrs indirect addressing operations that target other fsrs or virtual registers represent special cases. for example, using an fsr to point to one of the virtual registers will not result in successful operations. as a specific case, assume that fsr0h:fsr0l contains fe7h, the address of indf1. attempts to read the value of the indf1 using indf0 as an operand will return 00h. attempts to write to indf1 using indf0 as the operand will result in a nop . on the other hand, using the virtual registers to write to an fsr pair may not occur as planned. in these cases, the value will be written to the fsr pair but without any incrementing or decrementing. thus, writing to either the indf2 or postdec2 register will write the same value to the fsr2h:fsr2l. since the fsrs are physical registers mapped in the sfr space, they can be manipulated through all direct operations. users should proceed cautiously when working on these registers, particularly if their code uses indirect addressing. similarly, operations by indirect addressing are generally permitted on all other sfrs. users should exercise the appropriate caution that they do not inadvertently change settings that might affect the operation of the device. 5.7 data memory and the extended instruction set enabling the pic18 extended instruction set (xinst configuration bit = 1 ) significantly changes certain aspects of data memory and its addressing. specifi- cally, the use of the access bank for many of the core pic18 instructions is different; this is due to the intro- duction of a new addressing mode for the data memory space. what does not change is just as important. the size of the data memory space is unchanged, as well as its linear addressing. the sfr map remains the same. core pic18 instructions can still operate in both direct and indirect addressing mode; inherent and literal instructions do not change at all. indirect addressing with fsr0 and fsr1 also remain unchanged. 5.7.1 indexed addressing with literal offset enabling the pic18 extended instruction set changes the behavior of indirect addressing using the fsr2 register pair within access ram. under the proper conditions, instructions that use the access bank ? that is, most bit-oriented and byte-oriented instructions ? can invoke a form of indexed addressing using an offset specified in the instruction. this special addressing mode is known as indexed addressing with literal offset, or indexed literal offset mode. when using the extended instruction set, this addressing mode requires the following: ? the use of the access bank is forced (?a? = 0 ) and ? the file address argument is less than or equal to 5fh. under these conditions, the file address of the instruction is not interpreted as the lower byte of an address (used with the bsr in direct addressing), or as an 8-bit address in the access bank. instead, the value is interpreted as an offset value to an address pointer, specified by fsr2. the offset and the contents of fsr2 are added to obtain the target address of the operation. 5.7.2 instructions affected by indexed literal offset mode any of the core pic18 instructions that can use direct addressing are potentially affected by the indexed literal offset addressing mode. this includes all byte-oriented and bit-oriented instructions, or almost one-half of the standard pic18 instruction set. instructions that only use inherent or literal addressing modes are unaffected. additionally, byte-oriented and bit-oriented instructions are not affected if they do not use the access bank (access ram bit is ? 1 ?), or include a file address of 60h or above. instructions meeting these criteria will continue to execute as before. a comparison of the different possible addressing modes when the extended instruction set is enabled is shown in figure 5-11 . those who desire to use byte-oriented or bit-oriented instructions in the indexed literal offset mode should note the changes to assembler syntax for this mode. this is described in more detail in section 25.2.1 ?extended instruction syntax? . ? 2010-2012 microchip technology inc. ds41412f-page 93 pic18(l)f2x/4xk22 figure 5-11: comparing addressing options for bit-oriented and byte-oriented instructions (extended instruction set enabled) example instruction: addwf, f, d, a (opcode: 0010 01da ffff ffff ) when ?a? = 0 and f ? 60h: the instruction executes in direct forced mode. ?f? is inter- preted as a location in the access ram between 060h and 0ffh. this is the same as locations f60h to fffh (bank 15) of data memory. locations below 60h are not available in this addressing mode. when ?a? = 0 and f ??? 5fh: the instruction executes in indexed literal offset mode. ?f? is interpreted as an offset to the address value in fsr2. the two are added together to obtain the address of the target register for the instruction. the address can be anywhere in the data memory space. note that in this mode, the correct syntax is now: addwf [k], d where ?k? is the same as ?f?. when ?a? = 1 (all values of f): the instruction executes in direct mode (also known as direct long mode). ?f? is inter- preted as a location in one of the 16 banks of the data memory space. the bank is designated by the bank select register (bsr). the address can be in any implemented bank in the data memory space. 000h 060h 100h f00h f60h fffh valid range 00h 60h ffh data memory access ram bank 0 bank 1 through bank 14 bank 15 sfrs 000h 060h 100h f00h f60h fffh data memory bank 0 bank 1 through bank 14 bank 15 sfrs fsr2h fsr2l ffffffff 001001da ffffffff 001001da 000h 060h 100h f00h f60h fffh data memory bank 0 bank 1 through bank 14 bank 15 sfrs for ?f? bsr 00000000 pic18(l)f2x/4xk22 ds41412f-page 94 ? 2010-2012 microchip technology inc. 5.7.3 mapping the access bank in indexed literal offset mode the use of indexed literal offset addressing mode effectively changes how the first 96 locations of access ram (00h to 5fh) are mapped. rather than containing just the contents of the bottom section of bank 0, this mode maps the contents from a user defined ?window? that can be located anywhere in the data memory space. the value of fsr2 establishes the lower bound- ary of the addresses mapped into the window, while the upper boundary is defined by fsr2 plus 95 (5fh). addresses in the access ram above 5fh are mapped as previously described (see section 5.4.2 ?access bank? ). an example of access bank remapping in this addressing mode is shown in figure 5-12 . remapping of the access bank applies only to opera- tions using the indexed literal offset mode. operations that use the bsr (access ram bit is ? 1 ?) will continue to use direct addressing as before. 5.8 pic18 instruction execution and the extended instruction set enabling the extended instruction set adds eight additional commands to the existing pic18 instruction set. these instructions are executed as described in section 25.2 ?extended instruction set? . figure 5-12: remapping the access bank with indexed literal offset addressing data memory 000h 100h 200h f60h f00h fffh bank 1 bank 15 bank 2 through bank 14 sfrs addwf f, d, a fsr2h:fsr2l = 120h locations in the region from the fsr2 pointer (120h) to the pointer plus 05fh (17fh) are mapped to the bottom of the access ram (000h-05fh). special file registers at f60h through fffh are mapped to 60h through ffh, as usual. bank 0 addresses below 5fh can still be addressed by using the bsr. access bank 00h 60h ffh sfrs bank 1 ?window? bank 0 window example situation: 120h 17fh 5fh bank 1 ? 2010-2012 microchip technology inc. ds41412f-page 95 pic18(l)f2x/4xk22 6.0 flash program memory the flash program memory is readable, writable and erasable during normal operation over the entire v dd range. a read from program memory is executed one byte at a time. a write to program memory is executed on blocks of 64 bytes at a time. program memory is erased in blocks of 64 bytes at a time. a bulk erase operation cannot 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 program memory does not need to be a valid instruction. executing a program memory location that forms an invalid instruction results in a nop . 6.1 table 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). the table read operation retrieves one byte of data directly from program memory and places it into the tablat register. figure 6-1 shows the operation of a table read. the table write operation stores one byte of data from the tablat register into a write block holding register. the procedure to write the contents of the holding registers into program memory is detailed in section 6.6 ?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. tables containing data, rather than program instructions, are not required to be word aligned. therefore, a table can start and end at any byte address. if a table write 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 (1) table latch (8-bit) program memory tblptrh tblptrl tablat tblptru instruction: tblrd * note 1: table pointer register points to a byte in program memory. program memory (tblptr) pic18(l)f2x/4xk22 ds41412f-page 96 ? 2010-2012 microchip technology inc. figure 6-2: table write operation 6.2 control 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.1 eecon1 and eecon2 registers the eecon1 register ( register 6-1 ) is the control register for memory accesses. the eecon2 register is not a physical register; it is used exclusively in the memory write and erase sequences. reading eecon2 will read all ? 0 ?s. the eepgd control bit determines if the access will be a program or data eeprom memory access. when eepgd is clear, any subsequent operations will operate on the data eeprom memory. when eepgd is set, any subsequent operations will operate on the program memory. the cfgs control bit determines if the access will be to the configuration/calibration registers or to program memory/data eeprom memory. when cfgs is set, subsequent operations will operate on configuration registers regardless of eepgd (see section 24.0 ?special features of the cpu? ). when cfgs is clear, memory selection access is determined by eepgd. the free bit allows the program memory erase operation. when free is set, an erase operation is initiated on the next wr command. when free is clear, only writes are enabled. the wren bit, when set, will allow a write operation. the wren bit is clear on power-up. the wrerr bit is set by hardware when the wr bit is set and cleared when the internal programming timer expires and the write operation is complete. the wr control bit initiates write operations. the wr bit cannot be cleared, only set, by firmware. then wr bit is cleared by hardware at the completion of the write operation. table pointer (1) table latch (8-bit) tblptrh tblptrl tablat program memory (tblptr ? 2010-2012 microchip technology inc. ds41412f-page 97 pic18(l)f2x/4xk22 6.3 register definitions: memory control register 6-1: eecon1: data eeprom control 1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 legend: r = readable bit w = writable bit s = bit can be set by software, but not cleared u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data eeprom or configuration select bit 1 = access configuration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as ? 0 ? bit 4 free: flash row (block) erase enable bit 1 = erase the program memory block addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write-only bit 3 wrerr: flash program/data eeprom error flag bit (1) 1 = a write operation is prematurely terminated (any reset during self-timed programming in normal operation, or an improper write attempt) 0 = the write operation completed bit 2 wren: flash program/data eeprom write enable bit 1 = allows write cycles to flash program/data eeprom 0 = inhibits write cycles to flash program/data eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) by software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared by hardware. the rd bit can only be set (not cleared) by software. rd bit cannot be set when eepgd = 1 or cfgs = 1 .) 0 = does not initiate an eeprom read note 1: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition. pic18(l)f2x/4xk22 ds41412f-page 98 ? 2010-2012 microchip technology inc. 6.3.1 tablat ? table latch register the table latch (tablat) is an 8-bit register mapped into the sfr space. the table latch register is used to hold 8-bit data during data transfers between program memory and data ram. 6.3.2 tblptr ? table pointer register the table pointer (tblptr) register 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 regis- ters join to form a 22-bit wide pointer. the low-order 21 bits allow the device to address up to 2 mbytes of program memory space. the 22nd bit allows access to the device id, the user id and the configuration bits. the table pointer register, 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 on the tblptr affect only the low-order 21 bits. 6.3.3 table 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 tblptr determine which byte is read from program memory directly into the tablat register. when a tblwt is executed the byte in the tablat register is written, not to flash memory but, to a holding register in preparation for a program memory write. the holding registers constitute a write block which varies depending on the device (see table 6-1 ).the 3, 4, or 5 lsbs of the tblptrl register determine which specific address within the holding register block is written to. the msbs of the table pointer have no effect during tblwt operations. when a program memory write is executed the entire holding register block is written to the flash memory at the address determined by the msbs of the tblptr. the 3, 4, or 5 lsbs are ignored during flash memory writes. for more detail, see section 6.6 ?writing to flash program memory? . when an erase of program memory is executed, the 16 msbs of the table pointer register (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 wi th tblrd and tblwt instructions figure 6-3: table pointer bo undaries based on operation example operation on table pointer tblrd* tblwt* tblptr is not modified tblrd*+ tblwt*+ tblptr is incremented after the read/write tblrd*- tblwt*- tblptr is decremented after the read/write tblrd+* tblwt+* tblptr is incremented before the read/write 21 16 15 87 0 table erase/write table write table read ? tblptr<21:0> tblptrl tblptrh tblptru tblptr ? 2010-2012 microchip technology inc. ds41412f-page 99 pic18(l)f2x/4xk22 6.4 reading the flash program memory the tblrd instruction retrieves data from program memory and places it into data ram. table reads from program memory are performed one byte 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 organized by words. the least significant bit of the 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 example 6-1: reading a flash program memory word (even byte address) program memory (odd byte address) tblrd tablat tblptr = xxxxx1 fetch instruction register (ir) read register tblptr = xxxxx0 movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the word movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_word tblrd*+ ; read into tablat and increment movf tablat, w ; get data movwf word_even tblrd*+ ; read into tablat and increment movfw tablat, w ; get data movf word_odd pic18(l)f2x/4xk22 ds41412f-page 100 ? 2010-2012 microchip technology inc. 6.5 erasing flash program memory the minimum erase block is 32 words or 64 bytes. 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 block of 64 bytes of program memory is erased. the most significant 16 bits of the tblptr<21:6> point to the block being erased. the tblptr<5:0> bits are ignored. the eecon1 register commands 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 free bit is set to select an erase operation. the write initiate sequence for eecon2, shown as steps 4 through 6 in section 6.5.1 ?flash program memory erase sequence? , is used to guard against accidental writes. this is sometimes referred to as a long write. a long write is necessary for erasing the internal flash. instruction execution is halted during the long write cycle. the long write is terminated by the internal programming timer. 6.5.1 flash program memory erase sequence the sequence of events for erasing a block of internal program memory is: 1. load table pointer register with address of block being erased. 2. set the eecon1 register for the erase operation: ? set eepgd bit to point to program memory; ? clear the cfgs bit to access program memory; ? set wren bit to enable writes; ? set free bit to enable the erase. 3. disable interrupts. 4. write 55h to eecon2. 5. write 0aah to eecon2. 6. set the wr bit. this will begin the block erase cycle. 7. the cpu will stall for duration of the erase (about 2 ms using internal timer). 8. re-enable interrupts. example 6-2: erasing a flash program memory block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl erase_block bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bsf eecon1, free ; enable block erase operation bcf intcon, gie ; disable interrupts required movlw 55h sequence movwf eecon2 ; write 55h movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start erase (cpu stall) bsf intcon, gie ; re-enable interrupts ? 2010-2012 microchip technology inc. ds41412f-page 101 pic18(l)f2x/4xk22 6.6 writing to flash program memory the programming block size is 64 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 only as many holding registers as there are bytes in a write block (64 bytes). since the table latch (tablat) is only a single byte, the tblwt instruction needs to be executed 64 times for each programming operation. all of the table write operations will essentially be short writes because only the holding registers are written. after all the holding registers have been written, the programming operation of that block of memory is started by configuring the eecon1 register for a program memory write and performing the long write sequence. the long write is necessary for programming the internal flash. instruction execution is halted during a long write cycle. the long write will be terminated by the internal programming timer. the eeprom on-chip timer controls the write time. the write/erase voltages are generated by an on-chip charge pump, rated to operate over the voltage range of the device. figure 6-5: table writes to flash program memory 6.6.1 flash program memory write sequence the sequence of events for programming 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 register with address being erased. 4. execute the block erase procedure. 5. load table pointer register with address of first byte being written. 6. write the 64-byte block into the holding registers with auto-increment. 7. set the eecon1 register for the write operation: ? set eepgd bit to point to program memory; ? clear the cfgs bit to access program memory; ? set wren to enable byte writes. 8. disable interrupts. 9. write 55h to eecon2. 10. write 0aah to eecon2. 11. set the wr bit. this will begin the write cycle. 12. the cpu will stall for duration of the write (about 2 ms using internal timer). 13. re-enable interrupts. 14. verify the memory (table read). this procedure will require about 6 ms to update each write block of memory. an example of the required code is given in example 6-3 . note: the default value of the holding registers on device resets and after write operations is ffh. a write of ffh to a holding register does not modify that byte. this means that individual bytes of program memory may be modified, provided that the change does not attempt to change any bit from a ? 0 ? to a ? 1 ?. when modifying individual bytes, it is not necessary to load all holding registers before executing a long write operation. tablat tblptr = xxxxyy (1) tblptr = xxxx01 tblptr = xxxx00 write register tblptr = xxxx02 program memory holding register holding register holding register holding register 8 8 8 8 note 1: yy = 3f for 64 byte write blocks. note: before setting the wr bit, the table pointer address needs to be within the intended address range of the bytes in the holding registers. pic18(l)f2x/4xk22 ds41412f-page 102 ? 2010-2012 microchip technology inc. example 6-3: writing to flash program memory movlw d'64? ; number of bytes in erase block movwf counter movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_block tblrd*+ ; read into tablat, and inc movf tablat, w ; get data movwf postinc0 ; store data decfsz counter ; done? bra read_block ; repeat modify_word movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw new_data_low ; update buffer word movwf postinc0 movlw new_data_high movwf indf0 erase_block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bsf eecon1, free ; enable erase operation bcf intcon, gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start erase (cpu stall) bsf intcon, gie ; re-enable interrupts tblrd*- ; dummy read decrement movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l write_buffer_back movlw blocksize ; number of bytes in holding register movwf counter movlw d?64?/blocksize ; number of write blocks in 64 bytes movwf counter2 write_byte_to_hregs movf postinc0, w ; get low byte of buffer data movwf tablat ; present data to table latch tblwt+* ; write data, perform a short write ; to internal tblwt holding register. ? 2010-2012 microchip technology inc. ds41412f-page 103 pic18(l)f2x/4xk22 example 6-3: writing to flash program memory (continued) 6.6.2 write 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.6.3 unexpected termination of write operation if a write is terminated by an unplanned event, such as loss of power or an unexpected reset, the memory location just programmed should be verified and reprogrammed if needed. if the write operation is interrupted by a mclr reset or a wdt time-out reset during normal operation, the wrerr bit will be set which the user can check to decide whether a rewrite of the location(s) is needed. 6.6.4 protection against spurious writes to protect 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.7 flash program operation during code protection see section 24.5 ?program verification and code protection? for details on code protection of flash program memory. decfsz counter ; loop until holding registers are full bra write_word_to_hregs program_memory bsf eecon1, eepgd ; point to flash program memory bcf eecon1, cfgs ; access flash program memory bsf eecon1, wren ; enable write to memory bcf intcon, gie ; disable interrupts movlw 55h required movwf eecon2 ; write 55h sequence movlw 0aah movwf eecon2 ; write 0aah bsf eecon1, wr ; start program (cpu stall) dcfsz counter2 ; repeat for remaining write blocks bra write_byte_to_hregs ; bsf intcon, gie ; re-enable interrupts bcf eecon1, wren ; disable write to memory table 6-2: registers associated with program flash memory name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page tblptru ? ? program memory table pointer upper byte (tblptr<21:16>) ? tblptrh program memory table pointer high byte (tblptr<15:8>) ? tblptrl program memory table pointer low byte (tblptr<7:0>) ? tablat program memory table latch ? intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 eecon2 eeprom control register 2 (not a physical register) ? eecon1 eepgd cfgs ? free wrerr wren wr rd 97 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 legend: ? = unimplemented, read as ? 0 ?. shaded bits are not used during flash/eeprom access. pic18(l)f2x/4xk22 ds41412f-page 104 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 105 pic18(l)f2x/4xk22 7.0 data eeprom memory the data eeprom is a nonvolatile memory array, separate from the data ram and program memory, which is used for long-term storage of program data. it is not directly mapped in either the register file or program memory space but is indirectly addressed through the special function registers (sfrs). the eeprom is readable and writable during normal operation over the entire v dd range. four sfrs are used to read and write to the data eeprom as well as the program memory. they are: ? eecon1 ? eecon2 ? eedata ? eeadr ? eeadrh the data eeprom allows byte read and write. when interfacing to the data memory block, eedata holds the 8-bit data for read/write and the eeadr:eeadrh register pair hold the address of the eeprom location being accessed. the eeprom data memory is rated for high erase/write cycle endurance. a byte write automatically erases the location and writes the new data (erase-before-write). the write time is controlled by an on-chip timer; it will vary with voltage and temperature as well as from chip- to-chip. please refer to the data eeprom memory parameters in section 27.0 ?electrical characteris- tics? for limits. 7.1 eeadr and eeadrh registers the eeadr register is used to address the data eeprom for read and write operations. the 8-bit range of the register can address a memory range of 256 bytes (00h to ffh). the eeadrh register expands the range to 1024 bytes by adding an additional two address bits. 7.2 eecon1 and eecon2 registers access to the data eeprom is controlled by two registers: eecon1 and eecon2. these are the same registers which control access to the program memory and are used in a similar manner for the data eeprom. the eecon1 register ( register 7-1 ) is the control register for data and program memory access. control bit eepgd determines if the access will be to program or data eeprom memory. when the eepgd bit is clear, operations will access the data eeprom memory. when the eepgd bit is set, program memory is accessed. control bit, cfgs, determines if the access will be to the configuration registers or to program memory/data eeprom memory. when the cfgs bit is set, subsequent operations access configuration registers. when the cfgs bit is clear, the eepgd bit selects either program flash or data eeprom memory. the wren bit, when set, will allow a write operation. on power-up, the wren bit is clear. the wrerr bit is set by hardware when the wr bit is set and cleared when the internal programming timer expires and the write operation is complete. the wr control bit initiates write operations. the bit can be set but not cleared by software. it is cleared only by hardware at the completion of the write operation. control bits, rd and wr, start read and erase/write operations, respectively. these bits are set by firmware and cleared by hardware at the completion of the operation. the rd bit cannot be set when accessing program memory (eepgd = 1 ). program memory is read using table read instructions. see section 6.1 ?table reads and table writes? regarding table reads. the eecon2 register is not a physical register. it is used exclusively in the memory write and erase sequences. reading eecon2 will read all ? 0 ?s. note: during normal operation, the wrerr may read as ? 1 ?. this can indicate that a write operation was prematurely termi- nated by a reset, or a write operation was attempted improperly. note: the eeif interrupt flag bit of the pir2 register is set when the write is complete. it must be cleared by software. pic18(l)f2x/4xk22 ds41412f-page 106 ? 2010-2012 microchip technology inc. register 7-1: eecon1: data eeprom control 1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 legend: r = readable bit w = writable bit s = bit can be set by software, but not cleared u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 eepgd: flash program or data eeprom memory select bit 1 = access flash program memory 0 = access data eeprom memory bit 6 cfgs: flash program/data eeprom or configuration select bit 1 = access configuration registers 0 = access flash program or data eeprom memory bit 5 unimplemented: read as ? 0 ? bit 4 free: flash row (block) erase enable bit 1 = erase the program memory block addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write-only bit 3 wrerr: flash program/data eeprom error flag bit (1) 1 = a write operation is prematurely terminated (any reset during self-timed programming in normal operation, or an improper write attempt) 0 = the write operation completed bit 2 wren: flash program/data eeprom write enable bit 1 = allows write cycles to flash program/data eeprom 0 = inhibits write cycles to flash program/data eeprom bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) by software.) 0 = write cycle to the eeprom is complete bit 0 rd: read control bit 1 = initiates an eeprom read (read takes one cycle. rd is cleared by hardware. the rd bit can only be set (not cleared) by software. rd bit cannot be set when eepgd = 1 or cfgs = 1 .) 0 = does not initiate an eeprom read note 1: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition. ? 2010-2012 microchip technology inc. ds41412f-page 107 pic18(l)f2x/4xk22 7.3 reading the data eeprom memory to read a data memory location, the user must write the address to the eeadr register, clear the eepgd control bit of the eecon1 register and then set control bit, rd. the data is available on the very next instruc- tion cycle; therefore, the eedata register 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). the basic process is shown in example 7-1 . 7.4 writing to the data eeprom memory to write an eeprom data location, the address must first be written to the eeadr register and the data writ- ten to the eedata register. the sequence in example 7-2 must be followed to initiate the write cycle. the write will not begin if this sequence is not exactly followed (write 55h to eecon2, write 0aah to eecon2, then set wr bit) for each byte. it is strongly 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, eecon1, eeadr and eedata cannot be modified. the wr bit will be inhibited from being set unless the wren bit is set. both wr and wren cannot be set with the same instruction. at the completion of the write cycle, the wr bit is cleared by hardware and the eeprom interrupt flag bit, eeif, is set. the user may either enable this interrupt or poll this bit. eeif must be cleared by software. 7.5 write 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. example 7-1: data eeprom read example 7-2: data eeprom write movlw data_ee_addr ; movwf eeadr ; data memory address to read bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access eeprom bsf eecon1, rd ; eeprom read movf eedata, w ; w = eedata movlw data_ee_addr_low ; movwf eeadr ; data memory address to write movlw data_ee_addr_hi ; movwf eeadrh ; movlw data_ee_data ; movwf eedata ; data memory value to write bcf eecon1, eepgd ; point to data memory bcf eecon1, cfgs ; access eeprom bsf eecon1, wren ; enable writes bcf intcon, gie ; disable interrupts movlw 55h ; required movwf eecon2 ; write 55h sequence movlw 0aah ; movwf eecon2 ; write 0aah bsf eecon1, wr ; set wr bit to begin write bsf intcon, gie ; enable interrupts ; user code execution bcf eecon1, wren ; disable writes on write complete (eeif set) pic18(l)f2x/4xk22 ds41412f-page 108 ? 2010-2012 microchip technology inc. 7.6 operation during code-protect data eeprom memory has its own code-protect bits in configuration words. external read and write operations are disabled if code protection is 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. 7.7 protection against spurious write there are conditions when the user may not want to write to the data eeprom memory. to protect against spurious eeprom writes, various mechanisms have been implemented. on power-up, the wren bit is cleared. in addition, writes to the eeprom are blocked during the power-up timer period (t pwrt ). the write initiate sequence and the wren bit together help prevent an accidental write during brown-out, power glitch or software malfunction. 7.8 using the data eeprom the data eeprom is a high-endurance, byte addressable array that has been optimized for the storage of frequently changing information (e.g., program variables or other data that are updated often). when variables in one section change frequently, while variables in another section do not change, it is possible to exceed the total number of write cycles to the eeprom without exceeding the total number of write cycles to a single byte. refer to the data eeprom memory parameters in section 27.0 ?electrical characteristics? for write cycle limits. if this is the case, then an array refresh must be performed. 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 7-3 . example 7-3: data eeprom refresh routine note: if data eeprom is only used to store constants and/or data that changes rarely, an array refresh is likely not required. see specification. clrf eeadr ; start at address 0 bcf eecon1, cfgs ; set for memory bcf eecon1, eepgd ; set for data eeprom bcf intcon, gie ; disable interrupts bsf eecon1, wren ; enable writes loop ; loop to refresh array bsf eecon1, rd ; read current address movlw 55h ; movwf eecon2 ; write 55h movlw 0aah ; movwf eecon2 ; write 0aah bsf eecon1, wr ; set wr bit to begin write btfsc eecon1, wr ; wait for write to complete bra $-2 incfsz eeadr, f ; increment address bra loop ; not zero, do it again bcf eecon1, wren ; disable writes bsf intcon, gie ; enable interrupts ? 2010-2012 microchip technology inc. ds41412f-page 109 pic18(l)f2x/4xk22 table 7-1: registers associated with data eeprom memory name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 eeadr eeadr7 eeadr6 eeadr5 eeadr4 eeadr3 eeadr2 eeadr1 eeadr0 ? eeadrh (1) ? ? ? ? ? ? eeadr9 eeadr8 ? eedata eeprom data register ? eecon2 eeprom control register 2 (not a physical register) ? eecon1 eepgd cfgs ? free wrerr wren wr rd 106 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 legend: ? = unimplemented, read as ? 0 ?. shaded bits are not used during eeprom access. note 1: pic18(l)f26k22 and pic18(l)f46k22 only. pic18(l)f2x/4xk22 ds41412f-page 110 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 111 pic18(l)f2x/4xk22 8.0 8 x 8 hardware multiplier 8.1 introduction all pic18 devices include an 8 x 8 hardware multiplier as part of the alu. the multiplier performs an unsigned operation and yields a 16-bit result that is stored in the product register pair, prodh:prodl. the multiplier?s operation does not affect any flags in the status register. making multiplication a hardware operation allows it to be completed in a single instruction cycle. this has the advantages of higher computational throughput and reduced code size for multiplication algorithms and allows the pic18 devices to be used in many applica- tions previously reserved for digital signal processors. a comparison of various hardware and software multiply operations, along with the savings in memory and execution time, is shown in tab l e 8 - 1 . 8.2 operation example 8-1 shows the instruction sequence for an 8 x 8 unsigned multiplication. only one instruction is required when one of the arguments is already loaded in the wreg register. example 8-2 shows the sequence to do an 8 x 8 signed multiplication. to account for the sign bits of the arguments, each argument?s most significant bit (msb) is tested and the appropriate subtractions are done. example 8-1: 8 x 8 unsigned multiply routine example 8-2: 8 x 8 signed multiply routine movf arg1, w ; mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl movf arg1, w mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl btfsc arg2, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg1 movf arg2, w btfsc arg1, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg2 table 8-1: performance comparison for various multiply operations routine multiply method program memory (words) cycles (max) time @ 64 mhz @ 40 mhz @ 10 mhz @ 4 mhz 8 x 8 unsigned without hardware multiply 13 69 4.3 ? s6.9 ? s27.6 ? s69 ? s hardware multiply 1 1 62.5 ns 100 ns 400 ns 1 ? s 8 x 8 signed without hardware multiply 33 91 5.7 ? s9.1 ? s36.4 ? s91 ? s hardware multiply 6 6 375 ns 600 ns 2.4 ? s6 ? s 16 x 16 unsigned without hardware multiply 21 242 15.1 ? s24.2 ? s96.8 ? s242 ? s hardware multiply 28 28 1.8 ? s2.8 ? s11.2 ? s28 ? s 16 x 16 signed without hardware multiply 52 254 15.9 ? s25.4 ? s 102.6 ? s254 ? s hardware multiply 35 40 2.5 ? s4.0 ? s16.0 ? s40 ? s pic18(l)f2x/4xk22 ds41412f-page 112 ? 2010-2012 microchip technology inc. example 8-3 shows the sequence to do a 16 x 16 unsigned multiplication. equation 8-1 shows the algorithm that is used. the 32-bit result is stored in four registers (res<3:0>). equation 8-1: 16 x 16 unsigned multiplication algorithm example 8-3: 16 x 16 unsigned multiply routine example 8-4 shows the sequence to do a 16 x 16 signed multiply. equation 8-2 shows the algorithm used. the 32-bit result is stored in four registers (res<3:0>). to account for the sign bits of the argu- ments, the msb for each argument pair is tested and the appropriate subtractions are done. equation 8-2: 16 x 16 signed multiplication algorithm example 8-4: 16 x 16 signed multiply routine res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) movf arg1l, w mulwf arg2l ; arg1l * arg2l-> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h-> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h-> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l-> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 ) + (arg1h ? arg2l ? 2 8 ) + (arg1l ? arg2h ? 2 8 ) + (arg1l ? arg2l) + (-1 ? arg2h<7> ? arg1h:arg1l ? 2 16 ) + (-1 ? arg1h<7> ? arg2h:arg2l ? 2 16 ) movf arg1l, w mulwf arg2l ; arg1l * arg2l -> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h -> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; btfss arg2h, 7 ; arg2h:arg2l neg? bra sign_arg1 ; no, check arg1 movf arg1l, w ; subwf res2 ; movf arg1h, w ; subwfb res3 ; sign_arg1 btfss arg1h, 7 ; arg1h:arg1l neg? bra cont_code ; no, done movf arg2l, w ; subwf res2 ; movf arg2h, w ; subwfb res3 ; cont_code : ? 2010-2012 microchip technology inc. ds41412f-page 113 pic18(l)f2x/4xk22 9.0 interrupts the pic18(l)f2x/4xk22 devices have multiple interrupt sources and an interrupt priority feature that allows most interrupt sources to be assigned a high or low priority level (int0 does not have a priority bit, it is always a high priority). the high priority interrupt vector is at 0008h and the low priority interrupt vector is at 0018h. a high priority interrupt event will interrupt a low priority interrupt that may be in progress. there are 19 registers used to control interrupt operation. these registers are: ? intcon, intcon2, intcon3 ? pir1, pir2, pir3, pir4, pir5 ? pie1, pie2, pie3, pie4, pie5 ? ipr1, ipr2, ipr3, ipr4, ipr5 ? rcon it is recommended that the microchip header files sup- plied with mplab ? ide be used for the symbolic bit names in these registers. this allows the assembler/ compiler to automatically take care of the placement of these bits within the specified register. in general, interrupt sources have three bits to control their operation. they are: ? flag bit to indicate that an interrupt event occurred ? enable bit that allows program execution to branch to the interrupt vector address when the flag bit is set ? priority bit to select high priority or low priority 9.1 mid-range compatibility when the ipen bit is cleared (default state), the interrupt priority feature is disabled and interrupts are compatible with pic ? microcontroller mid-range devices. in compatibility mode, the interrupt priority bits of the iprx registers have no effect. the peie/giel bit of the intcon register is the global interrupt enable for the peripherals. the peie/giel bit disables only the peripheral interrupt sources and enables the peripheral interrupt sources when the gie/gieh bit is also set. the gie/gieh bit of the intcon register is the global interrupt enable which enables all non-peripheral interrupt sources and disables all interrupt sources, including the peripherals. all interrupts branch to address 0008h in compatibility mode. 9.2 interrupt priority the interrupt priority feature is enabled by setting the ipen bit of the rcon register. when interrupt priority is enabled the gie/gieh and peie/giel global inter- rupt enable bits of compatibility mode are replaced by the gieh high priority, and giel low priority, global interrupt enables. when set, the gieh bit of the int- con register enables all interrupts that have their associated iprx register or intconx register priority bit set (high priority). when clear, the gieh bit disables all interrupt sources including those selected as low pri- ority. when clear, the giel bit of the intcon register disables only the interrupts that have their associated priority bit cleared (low priority). when set, the giel bit enables the low priority sources when the gieh bit is also set. when the interrupt flag, enable bit and appropriate global interrupt enable (gie) bit are all set, the interrupt will vector immediately to address 0008h for high priority, or 0018h for low priority, depending on level of the interrupting source?s priority bit. individual interrupts can be disabled through their corresponding interrupt enable bits. 9.3 interrupt response when an interrupt is responded to, the global interrupt enable bit is cleared to disable further interrupts. the gie/gieh bit is the global interrupt enable when the ipen bit is cleared. when the ipen bit is set, enabling interrupt priority levels, the gieh bit is the high priority global interrupt enable and the giel bit is the low priority global interrupt enable. high priority interrupt sources can interrupt a low priority interrupt. low priority interrupts are not processed while high priority interrupts are in progress. the return address is pushed onto the stack and the pc is loaded with the interrupt vector address (0008h or 0018h). once in the interrupt service routine, the source(s) of the interrupt can be determined by polling the interrupt flag bits in the intconx and pirx registers. the interrupt flag bits must be cleared by software before re-enabling interrupts to avoid repeating the same interrupt. the ?return from interrupt? instruction, retfie , exits the interrupt routine and sets the gie/gieh bit (gieh or giel if priority levels are used), which re-enables interrupts. for external interrupt events, such as the int pins or the portb interrupt-on-change, the interrupt latency will be three to four instruction cycles. the exact latency is the same for one-cycle or two-cycle instructions. individual interrupt flag bits are set, regardless of the status of their corresponding enable bits or the global interrupt enable bit. pic18(l)f2x/4xk22 ds41412f-page 114 ? 2010-2012 microchip technology inc. figure 9-1: pic18 interrupt logic note: do not use the movff instruction to modify any of the interrupt control regis- ters while any interrupt is enabled. doing so may cause erratic microcontroller behavior. tmr0ie gieh/gie wake-up if in interrupt to cpu vector to location 0008h int2if int2ie int2ip int1if int1ie int1ip tmr0if tmr0ie tmr0ip rbif rbie rbip tmr0if tmr0ip int1if int1ie int1ip int2if int2ie int2ip rbif rbie rbip int0if int0ie giel/peie interrupt to cpu vector to location ipen ipen 0018h high priority interrupt generation low priority interrupt generation idle or sleep modes gieh/gie note 1: the rbif interrupt also requires the individual pin iocb enables. (1) (1) pir1<6:0> pie1<6:0> ipr1<6:0> pir2<7:0> pie2<7:0> ipr2<7:0> pir3<7:0> pie3<7:0> ipr3<7:0> pir4<2:0> pie4<2:0> ipr4<2:0> pir5<2:0> pie5<2:0> ipr5<2:0> pir1<6:0> pie1<6:0> ipr1<6:0> pir2<7:0> pie2<7:0> ipr2<7:0> pir3<7:0> pie3<7:0> ipr3<7:0> pir4<2:0> pie4<2:0> ipr4<2:0> pir5<2:0> pie5<2:0> ipr5<2:0> ipen giel/peie ? 2010-2012 microchip technology inc. ds41412f-page 115 pic18(l)f2x/4xk22 9.4 intcon registers the intcon registers are readable and writable registers, which contain various enable, priority and flag bits. 9.5 pir registers the pir registers contain the individual flag bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are five peripheral interrupt request flag registers (pir1, pir2, pir3, pir4 and pir5). 9.6 pie registers the pie registers contain the individual enable bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are five peripheral interrupt enable registers (pie1, pie2, pie3, pie4 and pie5). when ipen = 0 , the peie/giel bit must be set to enable any of these peripheral interrupts. 9.7 ipr registers the ipr registers contain the individual priority bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are five peripheral interrupt priority registers (ipr1, ipr2, ipr3, ipr4 and ipr5). using the priority bits requires that the interrupt priority enable (ipen) bit be set. pic18(l)f2x/4xk22 ds41412f-page 116 ? 2010-2012 microchip technology inc. 9.8 register definitions: interrupt control register 9-1: intcon: interrupt control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-x gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 gie/gieh: global interrupt enable bit when ipen = 0 : 1 = enables all unmasked interrupts 0 = disables all interrupts including peripherals when ipen = 1 : 1 = enables all high priority interrupts 0 = disables all interrupts including low priority bit 6 peie/giel: peripheral interrupt enable bit when ipen = 0 : 1 = enables all unmask ed peripheral interrupts 0 = disables all peripheral interrupts when ipen = 1 : 1 = enables all low priority interrupts 0 = disables all low priority interrupts bit 5 tmr0ie: tmr0 overflow interrupt enable bit 1 = enables the tmr0 overflow interrupt 0 = disables the tmr0 overflow interrupt bit 4 int0ie: int0 external interrupt enable bit 1 = enables the int0 external interrupt 0 = disables the int0 external interrupt bit 3 rbie: port b interrupt-on-change (iocx) interrupt enable bit (2) 1 = enables the iocx port change interrupt 0 = disables the iocx port change interrupt bit 2 tmr0if: tmr0 overflow interrupt flag bit 1 = tmr0 register has overflowed (must be cleared by software) 0 = tmr0 register did not overflow bit 1 int0if: int0 external interrupt flag bit 1 = the int0 external interrupt oc curred (must be cleared by software) 0 = the int0 external interrupt did not occur bit 0 rbif: port b interrupt-on-change (iocx) interrupt flag bit (1) 1 = at least one of the ioc<3:0> (rb<7:4>) pins changed state (must be cleared by software) 0 = none of the ioc<3:0> (rb<7:4>) pins have changed state note 1: a mismatch condition will continue to set the rbif bit. reading portb will end the mismatch condition and allow the bit to be cleared. 2: rb port change interrupts also require the individual pin iocb enables. note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. ? 2010-2012 microchip technology inc. ds41412f-page 117 pic18(l)f2x/4xk22 register 9-2: intcon2: in terrupt control 2 register r/w-1 r/w-1 r/w-1 r/w-1 u-0 r/w-1 u-0 r/w-1 rbpu intedg0 intedg1 intedg2 ?tmr0ip ?rbip bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 rbpu : portb pull-up enable bit 1 = all portb pull-ups are disabled 0 = portb pull-ups are enabled provided that the pin is an input and the corresponding wpub bit is set. bit 6 intedg0: external interrupt 0 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 5 intedg1: external interrupt 1 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 4 intedg2: external interrupt 2 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 3 unimplemented: read as ? 0 ? bit 2 tmr0ip: tmr0 overflow interrupt priority bit 1 =high priority 0 = low priority bit 1 unimplemented: read as ? 0 ? bit 0 rbip: rb port change interrupt priority bit 1 =high priority 0 = low priority note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. pic18(l)f2x/4xk22 ds41412f-page 118 ? 2010-2012 microchip technology inc. register 9-3: intcon3: in terrupt control 3 register r/w-1 r/w-1 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 int2ip int1ip ? int2ie int1ie ? int2if int1if bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 int2ip: int2 external interrupt priority bit 1 = high priority 0 = low priority bit 6 int1ip: int1 external interrupt priority bit 1 = high priority 0 = low priority bit 5 unimplemented: read as ? 0 ? bit 4 int2ie: int2 external interrupt enable bit 1 = enables the int2 external interrupt 0 = disables the int2 external interrupt bit 3 int1ie: int1 external interrupt enable bit 1 = enables the int1 external interrupt 0 = disables the int1 external interrupt bit 2 unimplemented: read as ? 0 ? bit 1 int2if: int2 external interrupt flag bit 1 = the int2 external interrupt occurred (must be cleared by software) 0 = the int2 external interrupt did not occur bit 0 int1if: int1 external interrupt flag bit 1 = the int1 external interrupt occurred (must be cleared by software) 0 = the int1 external interrupt did not occur note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. ? 2010-2012 microchip technology inc. ds41412f-page 119 pic18(l)f2x/4xk22 register 9-4: pir1: peripheral interrupt request (flag) register 1 u-0 r/w-0 r-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 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 unimplemented: read as ? 0 ?. bit 6 adif: a/d converter interrupt flag bit 1 = an a/d conversion completed (must be cleared by software) 0 = the a/d conversion is not complete or has not been started bit 5 rc1if: eusart1 receive interrupt flag bit 1 = the eusart1 receive buffer, rcreg1, is full (cleared when rcreg1 is read) 0 = the eusart1 receive buffer is empty bit 4 tx1if: eusart1 transmit interrupt flag bit 1 = the eusart1 transmit buffer, txreg1, is empty (cleared when txreg1 is written) 0 = the eusart1 transmit buffer is full bit 3 ssp1if: master synchronous serial port 1 interrupt flag bit 1 = the transmission/reception is complete (must be cleared by software) 0 = waiting to transmit/receive bit 2 ccp1if: ccp1 interrupt flag bit capture mode: 1 = a tmr1 register capture occurred (must be cleared by software) 0 = no tmr1 register capture occurred compare mode: 1 = a tmr1 register compare match occurred (must be cleared by software) 0 = no tmr1 register compare match occurred pwm mode: unused in this mode bit 1 tmr2if: tmr2 to pr2 match interrupt flag bit 1 = tmr2 to pr2 match occurred (must be cleared by software) 0 = no tmr2 to pr2 match occurred bit 0 tmr1if: tmr1 overflow interrupt flag bit 1 = tmr1 register overflowed (must be cleared by software) 0 = tmr1 register did not overflow note 1: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global interrupt enable bit, gie/ gieh of the intcon register. note: user software should ensure the appro- priate interrupt flag bits are cleared prior to enabling an interrupt and after servic- ing that interrupt. pic18(l)f2x/4xk22 ds41412f-page 120 ? 2010-2012 microchip technology inc. register 9-5: pir2: peripheral interrupt request (flag) register 2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 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 oscfif: oscillator fail interrupt flag bit 1 = device oscillator failed, clock input has changed to hfintosc (must be cleared by software) 0 = device clock operating bit 6 c1if: comparator c1 interrupt flag bit 1 = comparator c1 output has changed (must be cleared by software) 0 = comparator c1 output has not changed bit 5 c2if: comparator c2 interrupt flag bit 1 = comparator c2 output has changed (must be cleared by software) 0 = comparator c2 output has not changed bit 4 eeif: data eeprom/flash write operation interrupt flag bit 1 = the write operation is complete (must be cleared by software) 0 = the write operation is not complete or has not been started bit 3 bcl1if: mssp1 bus collision interrupt flag bit 1 = a bus collision occurred (must be cleared by software) 0 = no bus collision occurred bit 2 hlvdif: low-voltage detect interrupt flag bit 1 = a low-voltage condition occurred (direction determined by the vdirmag bit of the hlvdcon register) 0 = a low-voltage condition has not occurred bit 1 tmr3if: tmr3 overflow interrupt flag bit 1 = tmr3 register overflowed (must be cleared by software) 0 = tmr3 register did not overflow bit 0 ccp2if: ccp2 interrupt flag bit capture mode: 1 = a tmr1 register capture occurred (must be cleared by software) 0 = no tmr1 register capture occurred compare mode: 1 = a tmr1 register compare match occurred (must be cleared by software) 0 = no tmr1 register compare match occurred pwm mode: unused in this mode. ? 2010-2012 microchip technology inc. ds41412f-page 121 pic18(l)f2x/4xk22 register 9-6: pir3: peripheral interrupt (flag) register 3 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 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 ssp2if: synchronous serial port interrupt flag bit 1 = the transmission/reception is complete (must be cleared in software) 0 = waiting to transmit/receive bit 6 bcl2if: mssp2 bus collision interrupt flag bit 1 = a bus collision has occurred while the ssp2 module configured in i 2 c master was transmitting (must be cleared in software) 0 = no bus collision occurred bit 5 rc2if: eusart2 receive interrupt flag bit 1 = the eusart2 receive buffer, rcreg2, is full (cleared by reading rcreg2) 0 = the eusart2 receive buffer is empty bit 4 tx2if: eusart2 transmit interrupt flag bit 1 = the eusart2 transmit buffer, txreg2, is empty (cleared by writing txreg2) 0 = the eusart2 transmit buffer is full bit 3 ctmuif: ctmu interrupt flag bit 1 = ctmu interrupt occurred (must be cleared in software) 0 = no ctmu interrupt occurred bit 2 tmr5gif: tmr5 gate interrupt flag bits 1 = tmr gate interrupt occurred (must be cleared in software) 0 = no tmr gate occurred bit 1 tmr3gif: tmr3 gate interrupt flag bits 1 = tmr gate interrupt occurred (must be cleared in software) 0 = no tmr gate occurred bit 0 tmr1gif: tmr1 gate interrupt flag bits 1 = tmr gate interrupt occurred (must be cleared in software) 0 = no tmr gate occurred pic18(l)f2x/4xk22 ds41412f-page 122 ? 2010-2012 microchip technology inc. register 9-7: pir4: peripheral interrupt (flag) register 4 u-0 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 ? ? ? ? ? ccp5if ccp4if ccp3if 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-3 unimplemented: read as ? 0 ? bit 2 ccp5if: ccp5 interrupt flag bits capture mode: 1 = a tmr register capture occurred (must be cleared in software) 0 = no tmr register capture occurred compare mode: 1 = a tmr register compare match occurred (must be cleared in software) 0 = no tmr register compare match occurred pwm mode: unused in pwm mode. bit 1 ccp4if: ccp4 interrupt flag bits capture mode: 1 = a tmr register capture occurred (must be cleared in software) 0 = no tmr register capture occurred compare mode: 1 = a tmr register compare match occurred (must be cleared in software) 0 = no tmr register compare match occurred pwm mode: unused in pwm mode. bit 0 ccp3if: eccp3 interrupt flag bits capture mode: 1 = a tmr register capture occurred (must be cleared in software) 0 = no tmr register capture occurred compare mode: 1 = a tmr register compare match occurred (must be cleared in software) 0 = no tmr register compare match occurred pwm mode: unused in pwm mode. ? 2010-2012 microchip technology inc. ds41412f-page 123 pic18(l)f2x/4xk22 register 9-8: pir5: peripheral interrupt (flag) register 5 u-0 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 ? ? ? ? ? tmr6if tmr5if tmr4if 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-3 unimplemented: read as ? 0 ? bit 2 tmr6if: tmr6 to pr6 match interrupt flag bit 1 = tmr6 to pr6 match occurred (must be cleared in software) 0 = no tmr6 to pr6 match occurred bit 1 tmr5if: tmr5 overflow interrupt flag bit 1 = tmr5 register overflowed (must be cleared in software) 0 = tmr5 register did not overflow bit 0 tmr4if: tmr4 to pr4 match interrupt flag bit 1 = tmr4 to pr4 match occurred (must be cleared in software) 0 = no tmr4 to pr4 match occurred pic18(l)f2x/4xk22 ds41412f-page 124 ? 2010-2012 microchip technology inc. register 9-9: pie1: peripheral in terrupt enable (flag) register 1 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 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 unimplemented: read as ? 0 ?. bit 6 adie: a/d converter interrupt enable bit 1 = enables the a/d interrupt 0 = disables the a/d interrupt bit 5 rc1ie: eusart1 receive interrupt enable bit 1 = enables the eusart1 receive interrupt 0 = disables the eusart1 receive interrupt bit 4 tx1ie: eusart1 transmit interrupt enable bit 1 = enables the eusart1 transmit interrupt 0 = disables the eusart1 transmit interrupt bit 3 ssp1ie: master synchronous serial port 1 interrupt enable bit 1 = enables the mssp1 interrupt 0 = disables the mssp1 interrupt bit 2 ccp1ie: ccp1 interrupt enable bit 1 = enables the ccp1 interrupt 0 = disables the ccp1 interrupt bit 1 tmr2ie: tmr2 to pr2 match interrupt enable bit 1 = enables the tmr2 to pr2 match interrupt 0 = disables the tmr2 to pr2 match interrupt bit 0 tmr1ie: tmr1 overflow interrupt enable bit 1 = enables the tmr1 overflow interrupt 0 = disables the tmr1 overflow interrupt ? 2010-2012 microchip technology inc. ds41412f-page 125 pic18(l)f2x/4xk22 register 9-10: pie2: peripheral in terrupt enable (flag) register 2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 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 oscfie: oscillator fail interrupt enable bit 1 = enabled 0 =disabled bit 6 c1ie: comparator c1 interrupt enable bit 1 = enabled 0 = disabled bit 5 c2ie: comparator c2 interrupt enable bit 1 = enabled 0 = disabled bit 4 eeie: data eeprom/flash write operation interrupt enable bit 1 = enabled 0 =disabled bit 3 bcl1ie: mssp1 bus collision interrupt enable bit 1 = enabled 0 =disabled bit 2 hlvdie: low-voltage detect interrupt enable bit 1 = enabled 0 =disabled bit 1 tmr3ie: tmr3 overflow interrupt enable bit 1 = enabled 0 =disabled bit 0 ccp2ie: ccp2 interrupt enable bit 1 = enabled 0 =disabled pic18(l)f2x/4xk22 ds41412f-page 126 ? 2010-2012 microchip technology inc. register 9-11: pie3: peripheral interrupt enable (flag) register 3 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 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 ssp2ie: tmr5 gate interrupt enable bit 1 = enabled 0 =disabled bit 6 bcl2ie: bus collision interrupt enable bit 1 = enabled 0 = disabled bit 5 rc2ie: eusart2 receive interrupt enable bit 1 = enabled 0 = disabled bit 4 tx2ie: eusart2 transmit interrupt enable bit 1 = enabled 0 =disabled bit 3 ctmuie: ctmu interrupt enable bit 1 = enabled 0 =disabled bit 2 tmr5gie: tmr5 gate interrupt enable bit 1 = enabled 0 =disabled bit 1 tmr3gie: tmr3 gate interrupt enable bit 1 = enabled 0 =disabled bit 0 tmr1gie: tmr1 gate interrupt enable bit 1 = enabled 0 =disabled ? 2010-2012 microchip technology inc. ds41412f-page 127 pic18(l)f2x/4xk22 register 9-12: pie4: peripheral in terrupt enable (flag) register 4 u-0 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 ? ? ? ? ? ccp5ie ccp4ie ccp3ie 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-3 unimplemented: read as ? 0 ? bit 2 ccp5ie: ccp5 interrupt enable bit 1 = enabled 0 =disabled bit 1 ccp4ie: ccp4 interrupt enable bit 1 = enabled 0 =disabled bit 0 ccp3ie: ccp3 interrupt enable bit 1 = enabled 0 =disabled register 9-13: pie5: peripheral in terrupt enable (flag) register 5 u-0 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 ? ? ? ? ? tmr6ie tmr5ie tmr4ie 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-3 unimplemented: read as ? 0 ? bit 2 tmr6ie: tmr6 to pr6 match interrupt enable bit 1 = enables the tmr6 to pr6 match interrupt 0 = disables the tmr6 to pr6 match interrupt bit 1 tmr5ie: tmr5 overflow interrupt enable bit 1 = enables the tmr5 overflow interrupt 0 = disables the tmr5 overflow interrupt bit 0 tmr4ie: tmr4 to pr4 match interrupt enable bit 1 = enables the tmr4 to pr4 match interrupt 0 = disables the tmr4 to pr4 match interrupt pic18(l)f2x/4xk22 ds41412f-page 128 ? 2010-2012 microchip technology inc. register 9-14: ipr1: peripheral interrupt priority register 1 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 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 unimplemented: read as ? 0 ? bit 6 adip: a/d converter interrupt priority bit 1 = high priority 0 = low priority bit 5 rc1ip: eusart1 receive interrupt priority bit 1 = high priority 0 = low priority bit 4 tx1ip: eusart1 transmit interrupt priority bit 1 = high priority 0 = low priority bit 3 ssp1ip: master synchronous serial port 1 interrupt priority bit 1 = high priority 0 = low priority bit 2 ccp1ip: ccp1 interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr2ip: tmr2 to pr2 match interrupt priority bit 1 = high priority 0 = low priority bit 0 tmr1ip: tmr1 overflow interrupt priority bit 1 = high priority 0 = low priority ? 2010-2012 microchip technology inc. ds41412f-page 129 pic18(l)f2x/4xk22 register 9-15: ipr2: peripheral interrupt priority register 2 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 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 oscfip: oscillator fail interrupt priority bit 1 = high priority 0 = low priority bit 6 c1ip: comparator c1 interrupt priority bit 1 = high priority 0 = low priority bit 5 c2ip: comparator c2 interrupt priority bit 1 = high priority 0 = low priority bit 4 eeip: data eeprom/flash write operation interrupt priority bit 1 = high priority 0 = low priority bit 3 bcl1ip: mssp1 bus collision interrupt priority bit 1 = high priority 0 = low priority bit 2 hlvdip: low-voltage detect interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr3ip: tmr3 overflow interrupt priority bit 1 = high priority 0 = low priority bit 0 ccp2ip: ccp2 interrupt priority bit 1 = high priority 0 = low priority pic18(l)f2x/4xk22 ds41412f-page 130 ? 2010-2012 microchip technology inc. register 9-16: ipr3: peripheral interrupt priority register 3 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 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 ssp2ip: synchronous serial port 2 interrupt priority bit 1 = high priority 0 = low priority bit 6 bcl2ip: bus collision 2 interrupt priority bit 1 = high priority 0 = low priority bit 5 rc2ip: eusart2 receive interrupt priority bit 1 = high priority 0 = low priority bit 4 tx2ip: eusart2 transmit interrupt priority bit 1 = high priority 0 = low priority bit 3 ctmuip: ctmu interrupt priority bit 1 = high priority 0 = low priority bit 2 tmr5gip: tmr5 gate interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr3gip: tmr3 gate interrupt priority bit 1 = high priority 0 = low priority bit 0 tmr1gip: tmr1 gate interrupt priority bit 1 = high priority 0 = low priority ? 2010-2012 microchip technology inc. ds41412f-page 131 pic18(l)f2x/4xk22 register 9-17: ipr4: peripheral interrupt priority register 4 u-0 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 ? ? ? ? ? ccp5ip ccp4ip ccp3ip 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-3 unimplemented: read as ? 0 ? bit 2 ccp5ip: ccp5 interrupt priority bit 1 = high priority 0 = low priority bit 1 ccp4ip: ccp4 interrupt priority bit 1 = high priority 0 = low priority bit 0 ccp3ip: ccp3 interrupt priority bit 1 = high priority 0 = low priority register 9-18: ipr5: peripheral interrupt priority register 5 u-0 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 ? ? ? ? ? tmr6ip tmr5ip tmr4ip 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-3 unimplemented: read as ? 0 ? bit 2 tmr6ip: tmr6 to pr6 match interrupt priority bit 1 = high priority 0 = low priority bit 1 tmr5ip: tmr5 overflow interrupt priority bit 1 = high priority 0 = low priority bit 0 tmr4ip: tmr4 to pr4 match interrupt priority bit 1 = high priority 0 = low priority pic18(l)f2x/4xk22 ds41412f-page 132 ? 2010-2012 microchip technology inc. 9.9 intn pin interrupts external interrupts on the rb0/int0, rb1/int1 and rb2/int2 pins are edge-triggered. if the corresponding intedgx bit in the intcon2 register is set (= 1 ), the interrupt is triggered by a rising edge; if the bit is clear, the trigger is on the falling edge. when a valid edge appears on the rbx/intx pin, the corresponding flag bit, intxf, is set. this interrupt can be disabled by clearing the corresponding enable bit, intxe. flag bit, intxf, must be cleared by software in the interrupt service routine before re-enabling the interrupt. all external interrupts (int0, int1 and int2) can wake- up the processor from idle or sleep modes if bit intxe was set prior to going into those modes. if the global interrupt enable bit, gie/gieh, is set, the processor will branch to the interrupt vector following wake-up. interrupt priority for int1 and int2 is determined by the value contained in the interrupt priority bits, int1ip and int2ip of the intcon3 register. there is no prior- ity bit associated with int0. it is always a high priority interrupt source. 9.10 tmr0 interrupt in 8-bit mode (which is the default), an overflow in the tmr0 register (ffh ? 00h) will set flag bit, tmr0if. in 16-bit mode, an overflow in the tmr0h:tmr0l regis- ter pair (ffffh ?? 0000h) will set tmr0if. the interrupt can be enabled/disabled by setting/clearing enable bit, tmr0ie of the intcon register. interrupt priority for timer0 is determined by the value contained in the interrupt priority bit, tmr0ip of the intcon2 register. see section 11.0 ?timer0 module? for further details on the timer0 module. 9.11 portb interrupt-on-change an input change on portb<7:4> sets flag bit, rbif of the intcon register. the interrupt can be enabled/ disabled by setting/clearing enable bit, rbie of the intcon register. pins must also be individually enabled with the iocb register. interrupt priority for portb interrupt-on-change is determined by the value contained in the interrupt priority bit, rbip of the intcon2 register. 9.12 context saving during interrupts during interrupts, the return pc address is saved on the stack. additionally, the wreg, status and bsr registers are saved on the fast return stack. if a fast return from interrupt is not used (see section 5.2.1 ?fast register stack? ), the user may need to save the wreg, status and bsr registers on entry to the interrupt service routine. depending on the user?s application, other registers may also need to be saved. example 9-1 saves and restores the wreg, status and bsr registers during an interrupt service routine. example 9-1: saving status, wr eg and bsr registers in ram movwf w_temp ; w_temp is in virtual bank movff status, status_temp ; status_temp located anywhere movff bsr, bsr_temp ; bsr_tmep located anywhere ; ; user isr code ; movff bsr_temp, bsr ; restore bsr movf w_temp, w ; restore wreg movff status_temp, status ; restore status ? 2010-2012 microchip technology inc. ds41412f-page 133 pic18(l)f2x/4xk22 table 9-1: registers associated with interrupts name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page anselb ? ? ansb5 ansb4 ansb3 ansb2 ansb1 ansb0 155 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 intcon2 r bpu intedg0 intedg1 intedg2 ?tmr0ip ?rbip 117 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 118 iocb iocb7 iocb6 iocb5 iocb4 ? ? ? ? 158 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 ipr4 ? ? ? ? ? ccp5ip ccp4ip ccp3ip 131 ipr5 ? ? ? ? ? tmr6ip tmr5ip tmr4ip 131 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pie4 ? ? ? ? ? ccp5ie ccp4ie ccp3ie 127 pie5 ? ? ? ? ? tmr6ie tmr5ie tmr4ie 127 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pir4 ? ? ? ? ? ccp5if ccp4if ccp3if 122 pir5 ? ? ? ? ? tmr6if tmr5if tmr4if 123 portb rb7 rb6 rb5 rb4 rb3 rb2 rb1 rb0 153 rcon ipen sboren ? r i t o p d p or b or 60 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for interrupts. table 9-2: configuratio n registers associated with interrupts name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 config4l debug xinst ? ? ? lvp ? strven 361 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for interrupts. pic18(l)f2x/4xk22 ds41412f-page 134 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 135 pic18(l)f2x/4xk22 10.0 i/o ports depending on the device selected and features enabled, there are up to five ports available. all pins of the i/o ports are multiplexed with one or more alternate functions from the peripheral features on the device. in general, when a peripheral is enabled, that pin may not be used as a general purpose i/o pin. each port has five registers for its operation. these registers are: ? tris register (data direction register) ? port register (reads the levels on the pins of the device) ? lat register (output latch) ? ansel register (analog input control) ? slrcon register (port slew rate control) the data latch (lat register) is useful for read-modify- write operations on the value that the i/o pins are driving. a simplified model of a generic i/o port, without the interfaces to other peripherals, is shown in figure 10-1 . figure 10-1: generic i/o port operation 10.1 porta registers porta is an 8-bit wide, bidirectional port. the corresponding data direction register is trisa. setting a trisa bit (= 1 ) will make the corresponding porta pin an input (i.e., disable the output driver). clearing a trisa bit (= 0 ) will make the corresponding porta pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). reading the porta register reads the status of the pins, whereas writing to it, will write to the port latch. the data latch (lata) register is also memory mapped. 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 and one of the comparator outputs to become the ra4/t0cki/c1out pin. pins ra6 and ra7 are multiplexed with the main oscillator pins; they are enabled as oscillator or i/o pins by the selection of the main oscillator in the configuration register (see section 24.1 ?configuration bits? for details). when they are not used as port pins, ra6 and ra7 and their associated tris and lat bits are read as ? 0 ?. the other porta pins are multiplexed with analog inputs, the analog v ref + and v ref - inputs, and the comparator voltage reference output. the operation of pins ra<3:0> and ra5 as analog is selected by setting the ansela<5, 3:0> bits in the ansela register which is the default setting after a power-on reset. pins ra0 through ra5 may also be used as comparator inputs or outputs by setting the appropriate bits in the cm1con0 and cm2con0 registers. the ra4/t0cki/c1out pin is a schmitt trigger input. all other porta pins have ttl input levels and full cmos output drivers. the trisa register controls the drivers of the porta pins, even when they are being used as analog inputs. the user should ensure the bits in the trisa register are maintained set when using them as analog inputs. example 10-1: initiali zing porta data bus wr lat wr tris rd port data latch tris latch rd tris input buffer i/o pin (1) q d ck q d ck en qd en rd lat or port note 1: i/o pins have diode protection to v dd and v ss . trisx anselx note: on a power-on reset, ra5 and ra<3:0> are configured as analog inputs and read as ? 0 ?. ra4 is configured as a digital input. movlb 0xf ; set bsr for banked sfrs clrf porta ; initialize porta by ; clearing output ; data latches clrf lata ; alternate method ; to clear output ; data latches movlw e0h ; configure i/o movwf ansela ; for digital inputs movlw 0cfh ; value used to ; initialize data ; direction movwf trisa ; set ra<3:0> as inputs ; ra<5:4> as outputs pic18(l)f2x/4xk22 ds41412f-page 136 ? 2010-2012 microchip technology inc. table 10-1: porta i/o summary pin name function tris setting ansel setting pin ty pe buffer type description ra0/c12in0-/an0 ra0 00 o dig lata<0> data output; not affected by analog input. 10 i ttl porta<0> data input; disabled when analog input enabled. c12in0- 11 i an comparators c1 and c2 inverting input. an0 11 i an analog input 0. ra1/c12in1-/an1 ra1 00 o dig lata<1> data output; not affected by analog input. 10 i ttl porta<1> data input; disabled when analog input enabled. c12in1- 11 i an comparators c1 and c2 inverting input. an1 11 i an analog input 1. ra2/c2in+/an2/ dacout/v ref - ra2 00 o dig lata<2> data output; not affected by analog input; disabled when dacout enabled. 10 i ttl porta<2> data input; disabled when analog input enabled; disabled when dacout enabled. c2in+ 11 i an comparator c2 non-inverting input. an2 11 i an analog output 2. dacout x1 o an dac reference output. v ref - 11 i an a/d reference voltage (low) input. ra3/c1in+/an3/ v ref + ra3 0 o dig lata<3> data output; not affected by analog input. 10 i ttl porta<3> data input; disabled when analog input enabled. c1in+ 11 i an comparator c1 non-inverting input. an3 11 i an analog input 3. v ref + 11 i an a/d reference voltage (high) input. ra4/ccp5/ c1out/srq/ t0cki ra4 0 ? o dig lata<4> data output. 1 ? i st porta<4> data input; default configuration on por. ccp5 0 ? o dig ccp5 compare output/pwm output, takes priority over ra4 output. 1 ? i st capture 5 input/compare 5 output/ pwm 5 output. c1out 0 ? o dig comparator c1 output. srq 0 ? o dig sr latch q output; take priority over ccp 5 output. t0cki 1 ? i st timer0 external clock input. ra5/c2out/ srnq/ss1 / hlvdin/an4 ra5 00 o dig lata<5> data output; not affected by analog input. 10 i ttl porta<5> data input; disabled when analog input enabled. c2out 00 o dig comparator c2 output. srnq 00 o dig sr latch q output. ss1 10 i ttl spi slave select input (mssp1). hlvdin 11 i an high/low-voltage detect input. an4 11 i an a/d input 4. legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. ? 2010-2012 microchip technology inc. ds41412f-page 137 pic18(l)f2x/4xk22 ra6/clko/osc2 ra6 0 ? o dig lata<6> data output; enabled in intosc modes when clko is not enabled. 1 ? i ttl porta<6> data input; enabled in intosc modes when clko is not enabled. clko x ? o dig in rc mode, osc2 pin outputs clkout which has 1/4 the frequency of osc1 and denotes the instruction cycle rate. osc2 x ? o xtal oscillator crystal output; connects to crystal or resonator in crystal oscillator mode. ra7/clki/osc1 ra7 0 ? o dig lata<7> data output; disabled in external oscillator modes. 1 ? i ttl porta<7> data input; disabled in external oscillator modes. clki x ? i an external clock source input; always associated with pin function osc1. osc1 x ? i xtal oscillator crystal input or external clock source input st buffer when configured in rc mode; cmos otherwise. table 10-1: porta i/o summary (continued) pin name function tris setting ansel setting pin ty pe buffer type description legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. table 10-2: registers associated with porta name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ansela ? ? ansa5 ? ansa3 ansa2 ansa1 ansa0 154 cm1con0 c1on c1out c1oe c1pol c1sp c1r c1ch<1:0> 317 cm2con0 c2on c2out c2oe c2pol c2sp c2r c2ch<1:0> 317 lata lata7 lata6 lata5 lata4 lata3 lata2 lata1 lata0 158 vrefcon1 dacen daclps dacoe ? dacpss<1:0> ? dacnss 347 vrefcon2 ? ? ? dacr<4:0> 348 hlvdcon vdirmag bgvst irvst hlvden hlvdl<3:0> 349 porta ra7 ra6 ra5 ra4 ra3 ra2 ra1 ra0 153 slrcon ? ? ? slre slrd slrc slrb slra 158 srcon0 srlen srclk<2:0> srqen srnqen srps srpr 340 ssp1con1 wcol sspov sspen ckp sspm<3:0> 260 t0con tmr0on t08bit t0cs t0se psa t0ps<2:0> 159 trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for porta. table 10-3: configuration regist ers associated with porta name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config1h ieso fcmen priclken pllcfg fosc<3:0> 357 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for porta. pic18(l)f2x/4xk22 ds41412f-page 138 ? 2010-2012 microchip technology inc. 10.1.1 porta output priority each porta pin is multiplexed with other functions. the pins, their combined functions and their output priorities are briefly described here. for additional information, refer to the appropriate section in this data sheet. when multiple outputs are enabled, the actual pin control goes to the peripheral with the higher priority. table 10-4 lists the porta pin functions from the highest to the lowest priority. analog input functions, such as adc and comparator, are not shown in the priority lists. these inputs are active when the i/o pin is set for analog mode using the anselx registers. digital output functions may control the pin when it is in analog mode with the priority shown below. table 10-4: port pin function priority port bit port function priority by port pin porta portb portc portd (2) porte (2) 0 ra0 ccp4 (1) sosco scl2 ccp3 (8) rb0 p2b (6) sck2 p3a (8) rc0 rd0 re0 1 ra1 scl2 (1) sosci sda2 p3b sck2 (1) ccp2 (3) ccp4 re1 p1c (1) p2a (3) rd1 rb1 rc1 2 ra2 sda2 (1) ccp1 p2b ccp5 p1b (1) p1a rd2 (4) re2 rb2 ctpls rc2 3 ra3 sdo2 (1) scl1 p2c mclr ccp2 (6) sck1 rd3 v pp p2a (6) rc3 re3 rb3 4srqp1d (1) sda1 sdo2 c1out rb4 rc4 p2d ccp5 (1) rd4 ra4 note 1: pic18(l)f2xk22 devices. 2: pic18(l)f4xk22 devices. 3: function default pin. 4: function default pin (28-pin devices). 5: function default pin (40/44-pin devices). 6: function alternate pin. 7: function alternate pin (28-pin devices). 8: function alternate pin (40/44-pin devices) ? 2010-2012 microchip technology inc. ds41412f-page 139 pic18(l)f2x/4xk22 5 srnq ccp3 (3) sdo1 p1b c2out p3a (3) rc5 rd5 ra5 p2b (1)(4) rb5 6 osc2 pgc tx1/ck1 tx2/ck2 clko tx2/ck2 (1) ccp3 (1)(7) p1c ra6 rb6 p3a (1)(7) rd6 icdck rc6 7ra7 osc1 pgd rx1/dt1 rx2/dt2 ra7 rx2/dt2 (1) p3b (1) p1d rb7 rc7 rd7 icddt table 10-4: port pin function priority (continued) port bit port function priority by port pin porta portb portc portd (2) porte (2) note 1: pic18(l)f2xk22 devices. 2: pic18(l)f4xk22 devices. 3: function default pin. 4: function default pin (28-pin devices). 5: function default pin (40/44-pin devices). 6: function alternate pin. 7: function alternate pin (28-pin devices). 8: function alternate pin (40/44-pin devices) pic18(l)f2x/4xk22 ds41412f-page 140 ? 2010-2012 microchip technology inc. 10.2 portb registers portb is an 8-bit wide, bidirectional port. the corresponding data direction register is trisb. setting a trisb bit (= 1 ) will make the corresponding portb pin an input (i.e., disable the output driver). clearing a trisb bit (= 0 ) will make the corresponding portb pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). the data latch register (latb) is also memory mapped. read-modify-write operations on the latb register read and write the latched output value for portb. example 10-2: initializing portb 10.2.1 portb output priority each portb pin is multiplexed with other functions. the pins, their combined functions and their output priorities are briefly described here. for additional information, refer to the appropriate section in this data sheet. when multiple outputs are enabled, the actual pin control goes to the peripheral with the higher priority. table 10-4 lists the portb pin functions from the highest to the lowest priority. analog input functions, such as adc, comparator and sr latch inputs, are not shown in the priority lists. these inputs are active when the i/o pin is set for analog mode using the anselx registers. digital output functions may control the pin when it is in analog mode with the priority shown below. 10.3 additional portb pin functions portb pins rb<7:4> have an interrupt-on-change option. all portb pins have a weak pull-up option. 10.3.1 weak pull-ups each of the portb pins has an individually controlled weak internal pull-up. when set, each bit of the wpub register enables the corresponding pin pull-up. when cleared, the rbpu bit of the intcon2 register enables pull-ups on all pins which also have their corresponding wpub bit set. when set, the rbpu bit disables all weak pull-ups. the weak pull-up is automatically turned off when the port pin is configured as an output. the pull-ups are disabled on a power-on reset. 10.3.2 interrupt-on-change four of the portb pins (rb<7:4>) are individually configurable as interrupt-on-change pins. control bits in the iocb register enable (when set) or disable (when clear) the interrupt function for each pin. when set, the rbie bit of the intcon register enables interrupts on all pins which also have their corresponding iocb bit set. when clear, the rbie bit disables all interrupt-on-changes. only pins configured as inputs can cause this interrupt to occur (i.e., any rb<7:4> pin configured as an output is excluded from the interrupt-on-change comparison). for enabled interrupt-on-change pins, the values are compared with the old value latched on the last read of portb. the ?mismatch? outputs of the last read are or?d together to set the portb change interrupt flag bit (rbif) in the intcon register. this interrupt can wake the device from the sleep mode, or any of the idle modes. the user, in the interrupt service routine, can clear the interrupt in the following manner: a) any read or write of portb to clear the mis- match condition (except when portb is the source or destination of a movff instruction). b) execute at least one instruction after reading or writing portb, then clear the flag bit, rbif. movlb 0xf ; set bsr for banked sfrs clrf portb ; initialize portb by ; clearing output ; data latches clrf latb ; alternate method ; to clear output ; data latches movlw 0f0h ; value for init movwf anselb ; enable rb<3:0> for ; digital input pins ; (not required if config bit ; pbaden is clear) movlw 0cfh ; value used to ; initialize data ; direction movwf trisb ; set rb<3:0> as inputs ; rb<5:4> as outputs ; rb<7:6> as inputs note: on a power-on reset, rb<5:0> are configured as analog inputs by default and read as ? 0 ?; rb<7:6> are configured as digital inputs. when the pbaden configuration bit is set to ? 1 ?, rb<5:0> will alternatively be configured as digital inputs on por. ? 2010-2012 microchip technology inc. ds41412f-page 141 pic18(l)f2x/4xk22 a mismatch condition will continue to set the rbif flag bit. reading or writing portb will end the mismatch condition and allow the rbif bit to be cleared. the latch holding the last read value is not affected by a mclr nor brown-out reset. after either one of these resets, the rbif flag will continue to be set if a mismatch is present. 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. 10.3.3 alternate functions portb is multiplexed with several peripheral functions ( table 10-5 ). the pins have ttl input buffers. some of these pin functions can be relocated to alternate pins using the control fuse bits in config3h. rb5 is the default pin for p2b (28-pin devices). clearing the p2bmx bit moves the pin function to rc0. rb5 is also the default pin for the ccp3/p3a peripheral pin. clear- ing the ccp3mx bit moves the pin function to the rc6 pin (28-pin devices) or re0 (40/44-pin devices). two other pin functions, t3cki and ccp2/p2a, can be relocated from their default pins to portb pins by clearing the control fuses in config3h. clearing t3cmx and ccp2mx moves the pin functions to rb5 and rb3, respectively. note: if a change on the i/o pin should occur when the read operation is being executed (start of the q2 cycle), then the rbif interrupt flag may not get set. furthermore, since a read or write on a port affects all bits of that port, care must be taken when using multiple pins in interrupt-on-change mode. changes on one pin may not be seen while servicing changes on another pin. table 10-5: portb i/o summary pin function tris setting ansel setting pin type buffer type description rb0/int0/ccp4/ flt0/sri/ss2 / an12 rb0 00 o dig latb<0> data output; not affected by analog input. 10 i ttl portb<0> data input; disabled when analog input enabled. int0 10 i st external interrupt 0. ccp4 (3) 00 o dig compare 4 output/pwm 4 output. 10 i st capture 4 input. flt0 10 i st pwm fault input for eccp auto-shutdown. sri 10 i st sr latch input. ss2 (3) 10 i ttl spi slave select input (mssp2). an12 11 i an analog input 12. rb1/int1/p1c/ sck2/scl2/ c12in3-/an10 rb1 00 o dig latb<1> data output; not affected by analog input. 10 i ttl portb<1> data input; disabled when analog input enabled. int1 10 i st external interrupt 1. p1c (3) 00 o dig enhanced ccp1 pwm output 3. sck2 (3) 00 o dig mssp2 spi clock output. 10 i st mssp2 spi clock input. scl2 (3) 00 odigmssp2 i 2 c tm clock output. 10 ii 2 c mssp2 i 2 c tm clock input. c12in3- 11 i an comparators c1 and c2 inverting input. an10 11 i an analog input 10. legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. note 1: default pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. 3: function on portd and porte for pic18(l)f4xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 142 ? 2010-2012 microchip technology inc. rb2/int2/cted1/ p1b/sdi2/sda2/ an8 rb2 00 o dig latb<2> data output; not affected by analog input. 10 i ttl portb<2> data input; disabled when analog input enabled. int2 10 i st external interrupt 2. cted1 10 i st ctmu edge 1 input. p1b (3) 00 o dig enhanced ccp1 pwm output 2. sdi2 (3) 10 i st mssp2 spi data input. sda2 (3) 00 odigmssp2 i 2 c tm data output. 10 ii 2 c mssp2 i 2 c tm data input. an8 11 i an analog input 8. rb3/cted2/p2a/ ccp2/sdo2/ c12in2-/an9 rb3 00 o dig latb<3> data output; not affected by analog input. 10 i ttl portb<3> data input; disabled when analog input enabled. cted2 10 i st ctmu edge 2 input. p2a 00 o dig enhanced ccp1 pwm output 1. ccp2 (2) 00 o dig compare 2 output/pwm 2 output. 10 i st capture 2 input. sdo2 (2) 00 o dig mssp2 spi data output. c12in2- 11 i an comparators c1 and c2 inverting input. an9 11 i an analog input 9. rb4/ioc0/p1d/ t5g/an11 rb4 00 o dig latb<4> data output; not affected by analog input. 10 i ttl portb<4> data input; disabled when analog input enabled. ioc0 10 i ttl interrupt-on-change pin. p1d 00 o dig enhanced ccp1 pwm output 4. t5g 10 i st timer5 external clock gate input. an11 11 i an analog input 11. rb5/ioc1/p2b/ p3a/ccp3/t3cki/ t1g/an13 rb5 00 o dig latb<5> data output; not affected by analog input. 10 i ttl portb<5> data input; disabled when analog input enabled. ioc1 10 i ttl interrupt-on-change pin 1. p2b (1)(3) 00 o dig enhanced ccp2 pwm output 2. p3a (1) 00 o dig enhanced ccp3 pwm output 1. ccp3 (1) 00 o dig compare 3 output/pwm 3 output. 10 i st capture 3 input. t3cki (2) 10 i st timer3 clock input. t1g 10 i st timer1 external clock gate input. an13 11 i an analog input 13. table 10-5: portb i/o summary (continued) pin function tris setting ansel setting pin type buffer type description legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. note 1: default pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp 2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. 3: function on portd and porte for pic18(l)f4xk22 devices. ? 2010-2012 microchip technology inc. ds41412f-page 143 pic18(l)f2x/4xk22 rb6/kbi2/pgc rb6 0 ? o dig latb<6> data output; not affected by analog input. 1 ? i ttl portb<6> data input; disabled when analog input enabled. ioc2 1 ? i ttl interrupt-on-change pin. tx2 (3) 1 ? o dig eusart asynchronous transmit data output. ck2 (3) 1 ? o dig eusart synchronous serial clock output. 1 ? i st eusart synchronous serial clock input. pgc x ? i st in-circuit debugger and icsp tm programming clock input. rb7/kbi3/pgd rb7 0 ? o dig latb<7> data output; not affected by analog input. 1 ? i ttl portb<7> data input; disabled when analog input enabled. ioc3 1 ? i ttl interrupt-on-change pin. rx2 (2), (3) 1 ? i st eusart asynchronous receive data input. dt2 (2), (3) 1 ? o dig eusart synchronous serial data output. 1 ? i st eusart synchronous serial data input. pgd x ? o dig in-circuit debugger and icsp tm programming data output. x ? i st in-circuit debugger and icsp tm programming data input. table 10-5: portb i/o summary (continued) pin function tris setting ansel setting pin type buffer type description legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. note 1: default pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. 3: function on portd and porte for pic18(l)f4xk22 devices. table 10-6: registers associated with portb name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page anselb ? ? ansb5 ansb4 ansb3 ansb2 ansb1 ansb0 155 eccp2as ccp2ase ccp2as<2:0> pss2ac<1:0> pss2bd<1:0> 209 ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 205 eccp3as ccp3ase ccp3as<2:0> pss3ac<1:0> pss3bd<1:0> 209 ccp3con p3m<1:0> dc3b<1:0> ccp3m<3:0> 205 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 intcon2 r bpu intedg0 intedg1 intedg2 ? tmr0ip ?rbip 117 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 118 iocb iocb7 iocb6 iocb5 iocb4 ? ? ? ? 158 latb latb7 latb6 latb5 latb4 latb3 latb2 latb1 latb0 157 portb rb7 rb6 rb5 rb4 rb3 rb2 rb1 rb0 153 slrcon ? ? ? slre (1) slrd (1) slrc slrb slra 158 t1gcon tmr1ge t1gpol t1gtm t1gspm t1ggo/d one t1gval t1gss<1:0> 173 t3con tmr3cs<1:0> t3ckps<1:0> t3soscen t 3sync t3rd16 tmr3on 172 t5gcon tmr5ge t5gpol t5gtm t5gspm t5ggo/done t5gval t5gss<1:0> 173 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 wpub wpub7 wpub6 wpub5 wpub4 wpub3 wpub2 wpub1 wpub0 157 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for portb. note 1: available on pic18(l)f4xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 144 ? 2010-2012 microchip technology inc. 10.4 portc registers portc is an 8-bit wide, bidirectional port. the corresponding data direction register is trisc. setting a trisc bit (= 1 ) will make the corresponding portc pin an input (i.e., disable the output driver). clearing a trisc bit (= 0 ) will make the corresponding portc pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). the data latch register (latc) is also memory mapped. read-modify-write operations on the latc register read and write the latched output value for portc. portc is multiplexed with several peripheral functions ( ta b l e 1 0 - 8 ). the pins have schmitt trigger input buf- fers. some of these pin functions can be relocated to alter- nate pins using the control fuse bits in config3h. rc0 is the default pin for t3cki. clearing the t3cmx bit moves the pin function to rb5. rc1 is the default pin for the ccp2 peripheral pin. clearing the ccp2mx bit moves the pin function to the rb3 pin. two other pin functions, p2b and ccp3, can be relo- cated from their default pins to portc pins by clearing the control fuses in config3h. clearing p2bmx and ccp3mx moves the pin functions to rc0 and rc6 (1) / re0 (2) , respectively. when enabling peripheral functions, care should be taken in defining tris bits for each portc pin. the eusart and mssp peripherals override the tris bit to make a pin an output or an input, depending on the peripheral configuration. refer to the corresponding peripheral section for additional information. the contents of the trisc register are affected by peripheral overrides. reading trisc always returns the current contents, even though a peripheral device may be overriding one or more of the pins. example 10-3: initializing portc 10.4.1 portc output priority each portc pin is multiplexed with other functions. the pins, their combined functions and their output priorities are briefly described here. for additional information, refer to the appropriate section in this data sheet. when multiple outputs are enabled, the actual pin control goes to the peripheral with the higher priority. table 10-4 lists the portc pin functions from the highest to the lowest priority. analog input functions, such as adc, comparator and sr latch inputs, are not shown in the priority lists. these inputs are active when the i/o pin is set for analog mode using the anselx registers. digital output functions may control the pin when it is in analog mode with the priority shown below. table 10-7: configuration regist ers associated with portb name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 config4l debug xinst ? ? ?lvp (1) ? strven 361 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for portb. note 1: can only be changed when in high voltage programming mode. note: on a power-on reset, these pins are con- figured as analog inputs. movlb 0xf ; set bsr for banked sfrs clrf portc ; initialize portc by ; clearing output ; data latches clrf latc ; alternate method ; to clear output ; data latches movlw 0cfh ; value used to ; initialize data ; direction movwf trisc ; set rc<3:0> as inputs ; rc<5:4> as outputs ; rc<7:6> as inputs movlw 30h ; value used to ; enable digital inputs movwf anselc ; rc<3:2> dig input enable ; no ansel bits for rc<1:0> ; rc<7:6> dig input enable ? 2010-2012 microchip technology inc. ds41412f-page 145 pic18(l)f2x/4xk22 table 10-8: portc i/o summary pin name function tris setting ansel setting pin type buffer type description rc0/p2b/t3cki/t3g/ t1cki/sosco rc0 0 ? o dig latc<0> data output; not affected by analog input. 1 ? i st portc<0> data input; disabled when analog input enabled. p2b (2) 0 ? o dig enhanced ccp2 pwm output 2. t3cki (1) 1 ? i st timer3 clock input. t3g 1 ? i st timer3 external clock gate input. t1cki 1 ? i st timer1 clock input. sosco x ? o xtal secondary oscillator output. rc1/p2a/ccp2/sosci rc1 0 ? o dig latc<1> data output; not affected by analog input. 1 ? i st portc<1> data input; disabled when analog input enabled. p2a 0 ? o dig enhanced ccp2 pwm output 1. ccp2 (1) 0 ? o dig compare 2 output/pwm 2 output. 1 ? i st capture 2 input. sosci x ? i xtal secondary oscillator input. rc2/ctpls/p1a/ ccp1/t5cki/an14 rc2 00 o dig latc<2> data output; not affected by analog input. 10 i st portc<2> data input; disabled when analog input enabled. ctpls 00 o dig ctmu pulse generator output. p1a 00 o dig enhanced ccp1 pwm output 1. ccp1 00 o dig compare 1 output/pwm 1 output. 10 i st capture 1 input. t5cki 10 i st timer5 clock input. an14 11 i an analog input 14. rc3/sck1/scl1/an15 rc3 00 o dig latc<3> data output; not affected by analog input. 10 i st portc<3> data input; disabled when analog input enabled. sck1 00 o dig mssp1 spi clock output. 10 i st mssp1 spi clock input. scl1 00 odigmssp1 i 2 c? clock output. 10 i i2c mssp1 i 2 c? clock input. an15 11 i an analog input 15. rc4/sdi1/sda1/an16 rc4 00 o dig latc<4> data output; not affected by analog input. 10 i st portc<4> data input; disabled when analog input enabled. sdi1 10 i st mssp1 spi data input. sda1 00 odigmssp1 i 2 c? data output. 10 i i2c mssp1 i 2 c? data input. an16 11 i an analog input 16. legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. note 1: default pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. 3: function on portd and porte for pic18(l)f4xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 146 ? 2010-2012 microchip technology inc. pin name function tris setting ansel setting pin type buffer type description rc5/sdo1/an17 rc5 00 o dig latc<5> data output; not affected by analog input. 10 i st portc<5> data input; disabled when analog input enabled. sdo1 00 o dig mssp1 spi data output. an17 11 i an analog input 17. rc6/p3a/ccp3/tx1/ ck1/an18 rc6 00 o dig latc<6> data output; not affected by analog input. 10 i st portc<6> data input; disabled when analog input enabled. p3a (2), (3) 00 o cmos enhanced ccp3 pwm output 1. ccp3 (2), (3) 00 o dig compare 3 output/pwm 3 output. 10 i st capture 3 input. tx1 10 o dig eusart asynchronous transmit data output. ck1 10 o dig eusart synchronous serial clock output. 10 i st eusart synchronous serial clock input. an18 11 i an analog input 18. rc7/p3b/rx1/dt1/ an19 rc7 00 o dig latc<7> data output; not affected by analog input. 10 i st portc<7> data input; disabled when analog input enabled. p3b 00 o cmos enhanced ccp3 pwm output 2. rx1 10 i st eusart asynchronous receive data in. dt1 10 o dig eusart synchronous serial data output. 10 i st eusart synchronous serial data input. an19 11 i an analog input 19. legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. note 1: default pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. 2: alternate pin assignment for p2b, t3cki, ccp3 and ccp 2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are clear. 3: function on portd and porte for pic18(l)f4xk22 devices. table 10-8: portc i/o summary (continued) ? 2010-2012 microchip technology inc. ds41412f-page 147 pic18(l)f2x/4xk22 table 10-9: registers associated with portc name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page anselc ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ? ? 155 eccp1as ccp1ase ccp1as<2:0> pss1ac<1:0> pss1bd<1:0> 209 ccp1con p1m<1:0> dc1b<1:0> ccp1m<3:0> 205 eccp2as ccp2ase ccp2as<2:0> pss2ac<1:0> pss2bd<1:0> 209 ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 205 ctmuconh ctmuen ? ctmusidl tgen edgen edgseqen idissen cttrig 333 latc latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 157 portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 153 rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 slrcon ? ? ? slre (1) slrd (1) slrc slrb slra 158 ssp1con1 wcol sspov sspen ckp sspm<3:0> 260 t1con tmr1cs<1:0> t1ckps<1:0> t1soscen t 1sync t1rd16 tmr1on 172 t3con tmr3cs<1:0> t3ckps<1:0> t3soscen t 3sync t3rd16 tmr3on 172 t3gcon tmr3ge t3gpol t3gtm t3gspm t3ggo/d one t3gval t3gss<1:0> 173 t5con tmr5cs<1:0> t5ckps<1:0> t5soscen t 5sync t5rd16 tmr5on 172 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for portc. note 1: available on pic18(l)f4xk22 devices. table 10-10: configurat ion registers associated with portc name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for portc. pic18(l)f2x/4xk22 ds41412f-page 148 ? 2010-2012 microchip technology inc. 10.5 portd registers portd is an 8-bit wide, bidirectional port. the corresponding data direction register is trisd. setting a trisd bit (= 1 ) will make the corresponding portd pin an input (i.e., disable the output driver). clearing a trisd bit (= 0 ) will make the corresponding portd pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). the data latch register (latd) is also memory mapped. read-modify-write operations on the latd register read and write the latched output value for portd. all pins on portd are implemented with schmitt trigger input buffers. each pin is individually configurable as an input or output. all of the portd pins are multiplexed with analog and digital peripheral modules. see ta b l e 1 0 - 11 . example 10-4: initializing portd 10.5.1 portd output priority each portd pin is multiplexed with other functions. the pins, their combined functions and their output priorities are briefly described here. for additional information, refer to the appropriate section in this data sheet. when multiple outputs are enabled, the actual pin control goes to the peripheral with the higher priority. table 10-4 lists the portd pin functions from the highest to the lowest priority. analog input functions, such as adc, comparator and sr latch inputs, are not shown in the priority lists. these inputs are active when the i/o pin is set for analog mode using the anselx registers. digital output functions may control the pin when it is in analog mode with the priority shown below. note: portd is only available on 40-pin and 44-pin devices. note: on a power-on reset, these pins are configured as analog inputs. movlb 0xf ; set bsr for banked sfrs clrf portd ; initialize portd by ; clearing output ; data latches clrf latd ; alternate method ; to clear output ; data latches movlw 0cfh ; value used to ; initialize data ; direction movwf trisd ; set rd<3:0> as inputs ; rd<5:4> as outputs ; rd<7:6> as inputs movlw 30h ; value used to ; enable digital inputs movwf anseld ; rd<3:0> dig input enable ; rc<7:6> dig input enable ? 2010-2012 microchip technology inc. ds41412f-page 149 pic18(l)f2x/4xk22 table 10-11: portd i/o summary pin name function tris setting ansel setting pin type buffer type description rd0/sck2/scl2/an20 rd0 00 o dig latd<0> data output; not affected by analog input. 10 i st portd<0> data input; disabled when analog input enabled. sck2 00 o dig mssp2 spi clock output. 10 i st mssp2 spi clock input. scl2 00 odigmssp2 i 2 c? clock output. 10 ii 2 c mssp2 i 2 c? clock input. an20 11 i an analog input 20. rd1/ccp4/sdi2/sda2/ an21 rd1 00 o dig latd<1> data output; not affected by analog input. 10 i st portd<1> data input; disabled when analog input enabled. ccp4 00 o dig compare 4 output/pwm 4 output. 10 i st capture 4 input. sdi2 10 i st mssp2 spi data input. sda2 00 odigmssp2 i 2 c? data output. 10 ii2cmssp2 i 2 c? data input. an21 11 i an analog input 21. rd2/p2b/an22 rd2 00 o dig latd<2> data output; not affected by analog input. 10 i st portd<2> data input; disabled when analog input enabled. p2b (1) 00 o dig enhanced ccp2 pwm output 2. an22 11 i an analog input 22. rd3/p2c/ss2 /an23 rd3 00 o dig latd<3> data output; not affected by analog input. 10 i st portd<3> data input; disabled when analog input enabled. p2c 00 o dig enhanced ccp2 pwm output 4. s s2 10 i ttl mssp2 spi slave select input. an23 11 i an analog input 23. rd4/p2d/sdo2/an24 rd4 00 o dig latd<4> data output; not affected by analog input. 10 i st portd<4> data input; disabled when analog input enabled. p2d 00 o dig enhanced ccp2 pwm output 3. sdo2 00 o dig mssp2 spi data output. an24 11 i an analog input 24. rd5/p1b/an25 rd5 00 o dig latd<5> data output; not affected by analog input. 10 i st portd<5> data input; disabled when analog input enabled. p1b 00 o dig enhanced ccp1 pwm output 2. an25 11 i an analog input 25. legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = s chmitt trigger input with i 2 c. note 1: default pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. pic18(l)f2x/4xk22 ds41412f-page 150 ? 2010-2012 microchip technology inc. table 10-13: configurat ion registers associated with portd pin name function tris setting ansel setting pin type buffer type description rd6/p1c/tx2/ck2/ an26 rd6 00 o dig latd<6> data output; not affected by analog input. 10 i st portd<6> data input; disabled when analog input enabled. p1c 00 o dig enhanced ccp1 pwm output 3. tx2 10 o dig eusart asynchronous transmit data output. ck2 10 o dig eusart synchronous serial clock output. 10 i st eusart synchronous serial clock input. an26 11 i an analog input 26. rd7/p1d/rx2/dt2/ an27 rd7 00 o dig latd<7> data output; not affected by analog input. 10 i st portd<7> data input; disabled when analog input enabled. p1d 00 o dig enhanced ccp1 pwm output 4. rx2 10 i st eusart asynchronous receive data in. dt2 10 o dig eusart synchronous serial data output. 10 i st eusart synchronous serial data input. an27 11 i an analog input 27. legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. note 1: default pin assignment for p2b, t3cki, ccp3 and ccp2 when configuration bits pb2mx, t3cmx, ccp3mx and ccp2mx are set. table 10-11: portd i/o summary (continued) table 10-12: registers associated with portd name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page anseld (1) ansd7 ansd6 ansd5 ansd4 ansd3 ansd2 ansd1 ansd0 155 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 ccp1con p1m<1:0> dc1b<1:0> ccp1m<3:0> 205 ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 205 ccp4con ? ? dc4b<1:0> ccp4m<3:0> 205 latd (1) latd7 latd6 latd5 latd4 latd3 latd2 latd1 latd0 157 portd (1) rd7 rd6 rd5 rd4 rd3 rd2 rd1 rd0 153 rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 slrcon (1) ? ? ? slre slrd slrc slrb slra 158 ssp2con1 wcol sspov sspen ckp sspm<3:0> 260 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for portd. note 1: available on pic18(l)f4xk22 devices. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for portd. ? 2010-2012 microchip technology inc. ds41412f-page 151 pic18(l)f2x/4xk22 10.6 porte registers depending on the particular pic18(l)f2x/4xk22 device selected, porte is implemented in two different ways. 10.6.1 porte on 40/44-pin devices for pic18(l)f2x/4xk22 devices, porte is a 4-bit wide port. three pins (re0/p3a/ccp3/an5, re1/p3b/ an6 and re2/ccp5/an7) are individually configurable as inputs or outputs. these pins have schmitt trigger input buffers. when selected as an analog input, these pins will read as ? 0 ?s. the corresponding data direction register is trise. setting a trise bit (= 1 ) will make the corresponding porte pin an input (i.e., disable the output driver). clearing a trise bit (= 0 ) will make the corresponding porte pin an output (i.e., enable the output driver and put the contents of the output latch on the selected pin). trise controls the direction of the rex pins, even when they are being used as analog inputs. the user must make sure to keep the pins configured as inputs when using them as analog inputs. the data latch register (late) is also memory mapped. read-modify-write operations on the late register read and write the latched output value for porte. the fourth pin of porte (mclr /v pp /re3) is an input only pin. its operation is controlled by the mclre configuration bit. when selected as a port pin (mclre = 0 ), it functions as a digital input only pin; as such, it does not have tris or lat bits associated with its operation. otherwise, it functions as the device?s master clear input. in either configuration, re3 also functions as the programming voltage input during programming. example 10-5: initializing porte 10.6.2 porte on 28-pin devices for pic18f2xk22 devices, porte is only available when master clear functionality is disabled (mclr = 0 ). in these cases, porte is a single bit, input only port comprised of re3 only. the pin operates as previously described. 10.6.3 re3 weak pull-up the port re3 pin has an individually controlled weak internal pull-up. when set, the wpue3 (trise<7>) bit enables the re3 pin pull-up. the rbpu bit of the intcon2 register controls pull-ups on both portb and porte. when rbpu = 0 , the weak pull-ups become active on all pins which have the wpue3 or wpubx bits set. when set, the rbpu bit disables all weak pull-ups. the pull-ups are disabled on a power- on reset. when the re3 port pin is configured as mclr , (config3h<7>, mclre= 1 and config4l<2>, lvp= 0 ), or configured for low voltage programming, (mclre=x and lvp= 1 ), the pull-up is always enabled and the wpue3 bit has no effect. 10.6.4 porte output priority each porte pin is multiplexed with other functions. the pins, their combined functions and their output priorities are briefly described here. for additional information, refer to the appropriate section in this data sheet. when multiple outputs are enabled, the actual pin control goes to the peripheral with the higher priority. table 10-4 lists the porte pin functions from the highest to the lowest priority. analog input functions, such as adc, comparator and sr latch inputs, are not shown in the priority lists. these inputs are active when the i/o pin is set for analog mode using the anselx registers. digital output functions may control the pin when it is in analog mode with the priority shown below. note: on a power-on reset, re<2:0> are configured as analog inputs. note: on a power-on reset, re3 is enabled as a digital input only if master clear functionality is disabled. clrf porte ; initialize porte by ; clearing output ; data latches clrf late ; alternate method ; to clear output ; data latches clrf ansele ; configure analog pins ; for digital only movlw 05h ; value used to ; initialize data ; direction movwf trise ; set re<0> as input ; re<1> as output ; re<2> as input pic18(l)f2x/4xk22 ds41412f-page 152 ? 2010-2012 microchip technology inc. table 10-14: po rte i/o summary pin function tris setting ansel setting pin type buffer type description re0/p3a/ccp3/an5 re0 00 o dig late<0> data output; not affected by analog input. 10 i st porte<0> data input; disabled when analog input enabled. p3a (1) 00 o dig enhanced ccp3 pwm output. ccp3 (1) 00 odig compare 3 output/pwm 3 output. 10 ist capture 3 input. an5 11 i an analog input 5. re1/p3b/an6 re1 00 o dig late<1> data output; not affected by analog input. 10 i st porte<1> data input; disabled when analog input enabled. p3b 00 o dig enhanced ccp3 pwm output. an6 11 i an analog input 6. re2/ccp5/an7 re2 00 o dig late<2> data output; not affected by analog input. 10 i st porte<2> data input; disabled when analog input enabled. ccp5 00 o dig compare 5 output/pwm 5 output. 10 i st capture 5 input. an7 11 i an analog input 7. re3/v pp /mclr re3 ? ? i st porte<3> data input; enabled when configuration bit mclre = 0 . v pp ? ? p an programming voltage input; always available mclr ?? i st active-low master clear (device reset) input; enabled when configuration bit mclre = 1 . legend: an = analog input or output; ttl = ttl compatible input; hv = high voltage; od = open drain; xtal = crystal; cmos = cmos compatible input or output; st = schmitt trigger input with cmos levels; i 2 c tm = schmitt trigger input with i 2 c. note 1: alternate pin assignment for p3a/ccp3 when configuration bit ccp3mx is clear. table 10-15: registers associated with porte name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ansele (1) ? ? ? ? ? anse2 anse1 anse0 156 intcon2 rbpu intedg0 intedg1 intedg2 ? tmr0ip ? rbip 117 late (1) ? ? ? ? ? late2 late1 late0 157 porte ? ? ? ?re3 re2 (1) re1 (1) re0 (1) 154 slrcon ? ? ?slre (1) slrd (1) slrc slrb slra 158 trise wpue3 ? ? ? ? trise2 (1) trise1 (1) trise0 (1) 156 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for porte. note 1: available on pic18(l)f4xk22 devices. ? 2010-2012 microchip technology inc. ds41412f-page 153 pic18(l)f2x/4xk22 10.7 port analog control most port pins are multiplexed with analog functions such as the analog-to-digital converter and comparators. when these i/o pins are to be used as analog inputs it is necessary to disable the digital input buffer to avoid excessive current caused by improper biasing of the digital input. individual control of the digital input buffers on pins which share analog functions is provided by the ansela, anselb, anselc, anseld and ansele registers. setting an ansx bit high will disable the associated digital input buffer and cause all reads of that pin to return ? 0 ? while allowing analog functions of that pin to operate correctly. the state of the ansx bits has no affect on digital output functions. a pin with the associated trisx bit clear and ansx bit set will still operate as a digital output but the input mode will be analog. this can cause unexpected behavior when performing read- modify-write operations on the affected port. all ansel register bits default to ? 1 ? upon por and bor, disabling digital inputs for their associated port pins. all tris register bits default to ? 1 ? upon por or bor, disabling digital outputs for their associated port pins. as a result, all port pins that have an ansel register will default to analog inputs upon por or bor. 10.8 port slew rate control the output slew rate of each port is programmable to select either the standard transition rate or a reduced transition rate of approximately 0.1 times the standard to minimize emi. the reduced transition time is the default slew rate for all ports. 10.9 register definitions ? port control table 10-16: configurat ion registers associated with porte name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 config4l debug xinst ? ? ? lvp (1) ? strven 361 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for interrupts. note 1: can only be changed when in high voltage programming mode. register 10-1: portx (1) : portx register r/w-u/x r/w-u/x r/w-u/x r/w-u/x r/w-u/x r/w-u/x r/w-u/x r/w-u/x rx7 rx6 rx5 rx4 rx3 rx2 rx1 rx0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? ?1? = bit is set ?0? = bit is cleared x = bit is unknown -n/n = value at por and bor/value at all other resets bit 7-0 rx<7:0>: portx i/o bit values (2) note 1: register description for port a, portb, portc and portd. 2: writes to portx are written to corresponding latx register. reads from portx register is return of i/o pin values. pic18(l)f2x/4xk22 ds41412f-page 154 ? 2010-2012 microchip technology inc. register 10-2: port e: porte register u-0 u-0 u-0 u-0 r/w-u/x r/w-u/x r/w-u/x r/w-u/x ? ? ? ?re3 (1) re2 (2), (3) re1 (2), (3) re0 (2), (3) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? ?1? = bit is set ?0? = bit is cleared x = bit is unknown -n/n = value at por and bor/value at all other resets bit 7-4 unimplemented: read as ? 0 ? bit 3 re3: porte input bit value (1) bit 2-0 re<2:0>: porte i/o bit values (2), (3) note 1: port is available as input only when mclre = 0 . 2: writes to portx are written to corresponding latx register. reads from portx register is return of i/o pin values. 3: available on pic18(l)f4xk22 devices. register 10-3: ansela ? porta analog select register u-0 u-0 r/w-1 u-0 r/w-1 r/w-1 r/w-1 r/w-1 ? ? ansa5 ? ansa3 ansa2 ansa1 ansa0 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-6 unimplemented: read as ? 0 ? bit 5 ansa5: ra5 analog select bit 1 = digital input buffer disabled 0 = digital input buffer enabled bit 4 unimplemented: read as ? 0 ? bit 3-0 ansa<3:0>: ra<3:0> analog select bit 1 = digital input buffer disabled 0 = digital input buffer enabled ? 2010-2012 microchip technology inc. ds41412f-page 155 pic18(l)f2x/4xk22 register 10-4: anselb ? port b analog select register u-0 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ? ? ansb5 ansb4 ansb3 ansb2 ansb1 ansb0 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-6 unimplemented: read as ? 0 ? bit 5-0 ansb<5:0>: rb<5:0> analog select bit 1 = digital input buffer disabled 0 = digital input buffer enabled register 10-5: anselc ? port c analog select register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 u-0 u-0 ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ? ? 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-2 ansc<7:2>: rc<7:2> analog select bit 1 = digital input buffer disabled 0 = digital input buffer enabled bit 1-0 unimplemented: read as ? 0 ? register 10-6: anseld ? port d analog select register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ansd7 ansd6 ansd5 ansd4 ansd3 ansd2 ansd1 ansd0 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-0 ansd<7:0>: rd<7:0> analog select bit 1 = digital input buffer disabled 0 = digital input buffer enabled pic18(l)f2x/4xk22 ds41412f-page 156 ? 2010-2012 microchip technology inc. register 10-7: ansele ? porte analog select register u-0 u-0 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 ? ? ? ? ? anse2 (1) anse1 (1) anse0 (1) 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-3 unimplemented: read as ? 0 ? bit 2-0 anse<2:0>: re<2:0> analog select bit (1) 1 = digital input buffer disabled 0 = digital input buffer enabled note 1: available on pic18(l)f4xk22 devices only. register 10-8: trisx: po rtx tri-state register (1) r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 trisx7 trisx6 trisx5 trisx4 trisx3 trisx2 trisx1 trisx0 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-0 trisx<7:0>: portx tri-state control bit 1 = portx pin configured as an input (tri-stated) 0 = portx pin configured as an output note 1: register description for trisa, trisb, trisc and trisd. register 10-9: trise: porte tri-state register r/w-1 u-0 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 wpue3 ? ? ? ?trise2 (1) trise1 (1) trise0 (1) 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 wpue3: weak pull-up register bits 1 = pull-up enabled on port pin 0 = pull-up disabled on port pin bit 6-3 unimplemented: read as ? 0 ? bit 2-0 trise<7:0>: porte tri-state control bit (1) 1 = porte pin configured as an input (tri-stated) 0 = porte pin configured as an output note 1: available on pic18(l)f4xk22 devices only. ? 2010-2012 microchip technology inc. ds41412f-page 157 pic18(l)f2x/4xk22 register 10-10: latx: port x output latch register (1) r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u latx7 latx6 latx5 latx4 latx3 latx2 latx1 latx0 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-0 latx<7:0>: portx output latch bit value (2) note 1: register description for lata, latb, latc and latd. 2: writes to portx are written to corresponding latx register. reads from portx register is return of i/o pin values. register 10-11: late: porte output latch register (1) u-0 u-0 u-0 u-0 u-0 r/w-x/u r/w-x/u r/w-x/u ? ? ? ? ? late2 late1 late0 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-3 unimplemented: read as ? 0 ? bit 2-0 late<2:0>: porte output latch bit value (2) note 1: available on pic18(l)f4xk22 devices only. 2: writes to porte are written to corresponding late register. reads from porte register is return of i/o pin values. register 10-12: wpub: weak pull-up portb register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 wpub7 wpub6 wpub5 wpub4 wpub3 wpub2 wpub1 wpub0 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-0 wpub<7:0>: weak pull-up register bits 1 = pull-up enabled on port pin 0 = pull-up disabled on port pin pic18(l)f2x/4xk22 ds41412f-page 158 ? 2010-2012 microchip technology inc. register 10-13: iocb: interrupt- on-change portb control register r/w-1 r/w-1 r/w-1 r/w-1 u-0 u-0 u-0 u-0 iocb7 iocb6 iocb5 iocb4 ? ? ? ? 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-4 iocb<7:4>: interrupt-on-change portb control bits 1 = interrupt-on-change enabled (1) 0 = interrupt-on-change disabled note 1: interrupt-on-change requires that the rbie bit (intcon<3>) is set. register 10-14: slrcon: sl ew rate control register u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ? ? ?slre ( 1 ) slrd ( 1 ) slrc slrb slra bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4 slre: porte slew rate control bit (1) 1 = all outputs on porte slew at a limited rate 0 = all outputs on porte slew at the standard rate bit 3 slrd: portd slew rate control bit (1) 1 = all outputs on portd slew at a limited rate 0 = all outputs on portd slew at the standard rate bit 2 slrc: portc slew rate control bit 1 = all outputs on portc slew at a limited rate 0 = all outputs on portc slew at the standard rate bit 1 slrb: portb slew rate control bit 1 = all outputs on portb slew at a limited rate 0 = all outputs on portb slew at the standard rate bit 0 slra: porta slew rate control bit 1 = all outputs on porta slew at a limited rate (2) 0 = all outputs on porta slew at the standard rate note 1: these bits are available on pic18(l)f4xk22 devices. 2: the slew rate of ra6 defaults to standard rate when the pin is used as clkout. ? 2010-2012 microchip technology inc. ds41412f-page 159 pic18(l)f2x/4xk22 11.0 timer0 module the timer0 module incorporates the following features: ? software selectable operation as a timer or counter in both 8-bit or 16-bit modes ? readable and writable registers ? dedicated 8-bit, software programmable prescaler ? selectable clock source (internal or external) ? edge select for external clock ? interrupt-on-overflow the t0con register ( register 11-1 ) controls all aspects of the module?s operation, including the prescale selection. it is both readable and writable. a simplified block diagram of the timer0 module in 8-bit mode is shown in figure 11-1 . figure 11-2 shows a simplified block diagram of the timer0 module in 16-bit mode. 11.1 register definitions: timer0 control register 11-1: t0con: timer0 control register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 tmr0on t08bit t0cs t0se psa tops<2:0> 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 tmr0on: timer0 on/off control bit 1 = enables timer0 0 = stops timer0 bit 6 t08bit : timer0 8-bit/16-bit control bit 1 = timer0 is configured as an 8-bit timer/counter 0 = timer0 is configured as a 16-bit timer/counter bit 5 t0cs : timer0 clock source select bit 1 = transition on t0cki pin 0 = internal instruction cycle clock (clkout) bit 4 t0se : timer0 source edge select bit 1 = increment on high-to-low transition on t0cki pin 0 = increment on low-to-high transition on t0cki pin bit 3 psa : timer0 prescaler assignment bit 1 = timer0 prescaler is not assigned. timer0 clock input bypasses prescaler. 0 = timer0 prescaler is assigned. timer0 clock input comes from prescaler output. bit 2-0 t0ps<2:0> : timer0 prescaler select bits 111 = 1:256 prescale value 110 = 1:128 prescale value 101 = 1:64 prescale value 100 = 1:32 prescale value 011 = 1:16 prescale value 010 = 1:8 prescale value 001 = 1:4 prescale value 000 = 1:2 prescale value pic18(l)f2x/4xk22 ds41412f-page 160 ? 2010-2012 microchip technology inc. 11.2 timer0 operation timer0 can operate as either a timer or a counter; the mode is selected with the t0cs bit of the t0con register. in timer mode (t0cs = 0 ), the module increments on every clock by default unless a different prescaler value is selected (see section 11.4 ?prescaler? ). timer0 incrementing is inhibited for two instruction cycles following a tmr0 register write. the user can work around this by adjusting the value written to the tmr0 register to compensate for the anticipated missing increments. the counter mode is selected by setting the t0cs bit (= 1 ). in this mode, timer0 increments either on every rising or falling edge of pin ra4/t0cki. the increment- ing edge is determined by the timer0 source edge select bit, t0se of the t0con register; clearing this bit selects the rising edge. restrictions on the external clock input are discussed below. an external clock source can be used to drive timer0; however, it must meet certain requirements (see table 27-12 ) to ensure that the external clock can be synchronized with the internal phase clock (t osc ). there is a delay between synchronization and the onset of incrementing the timer/counter. 11.3 timer0 reads and writes in 16-bit mode tmr0h is not the actual high byte of timer0 in 16-bit mode; it is actually a buffered version of the real high byte of timer0 which is neither directly readable nor writable (refer to figure 11-2 ). tmr0h is updated with the contents of the high byte of timer0 during a read of tmr0l. this provides the ability to read all 16 bits of timer0 without the need to verify that the read of the high and low byte were valid. invalid reads could otherwise occur due to a rollover between successive reads of the high and low byte. similarly, a write to the high byte of timer0 must also take place through the tmr0h buffer register. writing to tmr0h does not directly affect timer0. instead, the high byte of timer0 is updated with the contents of tmr0h when a write occurs to tmr0l. this allows all 16 bits of timer0 to be updated at once. figure 11-1: timer0 block diagram (8-bit mode) note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki max. prescale. t0cki pin t0se 0 1 1 0 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) internal data bus psa t0ps<2:0> set tmr0if on overflow 3 8 8 ? 2010-2012 microchip technology inc. ds41412f-page 161 pic18(l)f2x/4xk22 figure 11-2: timer0 block diagram (16-bit mode) 11.4 prescaler an 8-bit counter is available as a prescaler for the timer0 module. the prescaler is not directly readable or writable; its value is set by the psa and t0ps<2:0> bits of the t0con register which determine the prescaler assignment and prescale ratio. clearing the psa bit assigns the prescaler to the timer0 module. when the prescaler is assigned, prescale values from 1:2 through 1:256 in integer power-of-2 increments are selectable. when assigned to the timer0 module, all instructions writing to the tmr0 register (e.g., clrf tmr0 , movwf tmr0 , bsf tmr0 , etc.) clear the prescaler count. 11.4.1 switching prescaler assignment the prescaler assignment is fully under software control and can be changed ?on-the-fly? during program execution. 11.5 timer0 interrupt the tmr0 interrupt is generated when the tmr0 reg- ister overflows from ffh to 00h in 8-bit mode, or from ffffh to 0000h in 16-bit mode. this overflow sets the tmr0if flag bit. the interrupt can be masked by clear- ing the tmr0ie bit of the intcon register. before re-enabling the interrupt, the tmr0if bit must be cleared by software in the interrupt service routine. since timer0 is shut down in sleep mode, the tmr0 interrupt cannot awaken the processor from sleep. table 11-1: registers associated with timer0 note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki max. prescale. t0cki pin t0se 0 1 1 0 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) internal data bus 8 psa t0ps<2:0> set tmr0if on overflow 3 tmr0 tmr0h high byte 8 8 8 read tmr0l write tmr0l 8 note: writing to tmr0 when the prescaler is assigned to timer0 will clear the prescaler count but will not change the prescaler assignment. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 intcon2 rbpu intedg0 intedg1 intedg2 ? tmr0ip ? rbip 117 t0con tmr0on t08bit t0cs t0se psa t0ps<2:0> 159 tmr0h timer0 register, high byte ? tmr0l timer0 register, low byte ? trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by timer0. pic18(l)f2x/4xk22 ds41412f-page 162 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 163 pic18(l)f2x/4xk22 12.0 timer1/3/5 module with gate control the timer1/3/5 module is a 16-bit timer/counter with the following features: ? 16-bit timer/counter register pair (tmrxh:tmrxl) ? programmable internal or external clock source ? 2-bit prescaler ? dedicated secondary 32 khz oscillator circuit ? optionally synchronized comparator out ? multiple timer1/3/5 gate (count enable) sources ? interrupt on overflow ? wake-up on overflow (external clock, asynchronous mode only) ? 16-bit read/write operation ? time base for the capture/compare function ? special event trigger (with ccp/eccp) ? selectable gate source polarity ? gate toggle mode ? gate single-pulse mode ? gate value status ? gate event interrupt figure 12-1 is a block diagram of the timer1/3/5 module. figure 12-1: timer1/3/5 block diagram tmrxh tmrxl txsync txckps<1:0> prescaler 1, 2, 4, 8 0 1 synchronized clock input 2 set flag bit tmrxif on overflow tmrx (2),(4) tmrxon note 1: st buffer is high speed type when using txcki. 2: timer1/3/5 register increments on rising edge. 3: synchronize does not operate while in sleep. 4: see figure 12-2 for 16-bit read/write mode block diagram. 5: t1cki is not available when the secondary oscillator is enabled. (soscgo = 1 or txsoscen = 1 ) 6: t3cki is not available when the secondary oscillator is enabled, unless t3cmx = 1 . 7: synchronized comparator output should not be used in conjunction with synchronized txcki. txg f osc /4 internal clock soscout 1 0 txcki tmrxcs<1:0> (5) synchronize (3),(7) det sleep input tmrxge 0 1 00 01 10 11 txgpol d q ck q 0 1 txgval txgtm single pulse acq. control txgspm txggo/done txgss<1:0> 10 11 00 01 f osc internal clock reserved r d en q q1 rd txgcon data bus det interrupt tmrxgif set txclk f osc /2 internal clock d en q txg_in tmrxon timer2/4/6 match pr2/4/6 sync_c2out (7) sync_c1out (7) to comparator module ,(6) txsoscen secondary oscillator module see figure 2-4 txclk_ext_src (1) pic18(l)f2x/4xk22 ds41412f-page 164 ? 2010-2012 microchip technology inc. 12.1 timer1/3/5 operation the timer1/3/5 module is a 16-bit incrementing counter which is accessed through the tmrxh:tmrxl register pair. writes to tmrxh or tmrxl directly update the counter. when used with an internal clock source, the module is a timer and increments on every instruction cycle. when used with an external clock source, the module can be used as either a timer or counter and increments on every selected edge of the external source. timer1/3/5 is enabled by configuring the tmrxon and tmrxge bits in the txcon and txgcon registers, respectively. table 12-1 displays the timer1/3/5 enable selections. 12.2 clock source selection the tmrxcs<1:0> and txsoscen bits of the txcon register are used to select the clock source for timer1/3/5. the dedicated secondary oscillator circuit can be used as the clock source for timer1, timer3 and timer5, simultaneously. any of the txsoscen bits will enable the secondary oscillator circuit and select it as the clock source for that particular timer. table 12-2 displays the clock source selections. 12.2.1 internal clock source when the internal clock source is selected the tmrxh:tmrxl register pair will increment on multiples of f osc as determined by the timer1/3/5 prescaler. when the f osc internal clock source is selected, the timer1/3/5 register value will increment by four counts every instruction clock cycle. due to this condition, a 2 lsb error in resolution will occur when reading the timer1/3/5 value. to utilize the full resolution of timer1/3/5, an asynchronous input signal must be used to gate the timer1/3/5 clock input. the following asynchronous sources may be used: ? asynchronous event on the txg pin to timer1/3/5 gate ? c1 or c2 comparator input to timer1/3/5 gate 12.2.2 external clock source when the external clock source is selected, the timer1/3/5 module may work as a timer or a counter. when enabled to count, timer1/3/5 is incremented on the rising edge of the external clock input of the txcki pin. this external clock source can be synchronized to the microcontroller system clock or it can run asynchronously. when used as a timer with a clock oscillator, an external 32.768 khz crystal can be used in conjunction with the dedicated secondary internal oscillator circuit. table 12-1: timer1/3/5 enable selections tmrxon tmrxge timer1/3/5 operation 00 off 01 off 10 always on 11 count enabled note: in counter mode, a falling edge must be registered by the counter prior to the first incrementing rising edge after any one or more of the following conditions: ? timer1/3/5 enabled after por ? write to tmrxh or tmrxl ? timer1/3/5 is disabled ? timer1/3/5 is disabled (tmrxon = 0 ) when txcki is high then timer1/3/5 is enabled (tmrxon= 1 ) when txcki is low. table 12-2: clock source selections tmrxcs1 tmrxcs0 txsoscen clock source 01x system clock (f osc ) 00x instruction clock (f osc /4) 100 external clocking on txcki pin 101 osc.circuit on sosci/sosco pins ? 2010-2012 microchip technology inc. ds41412f-page 165 pic18(l)f2x/4xk22 12.3 timer1/3/5 prescaler timer1/3/5 has four prescaler options allowing 1, 2, 4 or 8 divisions of the clock input. the txckps bits of the txcon register control the prescale counter. the prescale counter is not directly readable or writable; however, the prescaler counter is cleared upon a write to tmrxh or tmrxl. 12.4 secondary oscillator a dedicated secondary low-power 32.768 khz oscillator circuit is built-in between pins sosci (input) and sosco (amplifier output). this internal circuit is to be used in conjunction with an external 32.768 khz crystal. the oscillator circuit is enabled by setting the txsoscen bit of the txcon register, the soscgo bit of the osccon2 register or by selecting the secondary oscillator as the system clock by setting scs<1:0> = 01 in the osccon register. the oscillator will continue to run during sleep. 12.5 timer1/3/5 operation in asynchronous counter mode if control bit txsync of the txcon register is set, the external clock input is not synchronized. the timer increments asynchronously to the internal phase clocks. if external clock source is selected then the timer will continue to run during sleep and can generate an interrupt on overflow, which will wake-up the processor. however, special precautions in software are needed to read/write the timer (see section 12.5.1 ?reading and writing timer1/3/5 in asynchronous counter mode? ). 12.5.1 reading and writing timer1/3/5 in asynchronous counter mode reading tmrxh or tmrxl while the timer is running from an external asynchronous clock will ensure a valid read (taken care of in hardware). however, the user should keep in mind that reading the 16-bit timer in two 8-bit values itself, poses certain problems, since the timer may overflow between the reads. for writes, it is recommended that the user simply stop the timer and write the desired values. a write contention may occur by writing to the timer registers, while the register is incrementing. this may produce an unpredictable value in the tmrxh:tmrxl register pair. 12.6 timer1/3/5 16-bit read/write mode timer1/3/5 can be configured to read and write all 16 bits of data, to and from, the 8-bit tmrxl and tmrxh registers, simultaneously. the 16-bit read and write operations are enabled by setting the rd16 bit of the txcon register. to accomplish this function, the tmrxh register value is mapped to a buffer register called the tmrxh buffer register. while in 16-bit mode, the tmrxh register is not directly readable or writable and all read and write operations take place through the use of this tmrxh buffer register. when a read from the tmrxl register is requested, the value of the tmrxh register is simultaneously loaded into the tmrxh buffer register. when a read from the tmrxh register is requested, the value is provided from the tmrxh buffer register instead. this provides the user with the ability to accurately read all 16 bits of the timer1/3/5 value from a single instance in time. in contrast, when not in 16-bit mode, the user must read each register separately and determine if the values have become invalid due to a rollover that may have occurred between the read operations. when a write request of the tmrxl register is requested, the tmrxh buffer register is simultaneously updated with the contents of the tmrxh register. the value of tmrxh must be preloaded into the tmrxh buffer register prior to the write request for the tmrxl register. this provides the user with the ability to write all 16 bits to the tmrxl:tmrxh register pair at the same time. any requests to write to the tmrxh directly does not clear the timer1/3/5 prescaler value. the prescaler value is only cleared through write requests to the tmrxl register. note: the oscillator requires a start-up and stabilization time before use. thus, txsoscen should be set and a suitable delay observed prior to enabling timer1/3/5. note: when switching from synchronous to asynchronous operation, it is possible to skip an increment. when switching from asynchronous to synchronous operation, it is possible to produce an additional increment. pic18(l)f2x/4xk22 ds41412f-page 166 ? 2010-2012 microchip technology inc. figure 12-2: timer1/3/5 16-bit read/write mode block diagram 12.7 timer1/3/5 gate timer1/3/5 can be configured to count freely or the count can be enabled and disabled using timer1/3/5 gate circuitry. this is also referred to as timer1/3/5 gate enable. timer1/3/5 gate can also be driven by multiple selectable sources. 12.7.1 timer1/3/5 gate enable the timer1/3/5 gate enable mode is enabled by setting the tmrxge bit of the txgcon register. the polarity of the timer1/3/5 gate enable mode is configured using the txgpol bit of the txgcon register. when timer1/3/5 gate enable mode is enabled, timer1/3/5 will increment on the rising edge of the timer1/3/5 clock source. when timer1/3/5 gate enable mode is disabled, no incrementing will occur and timer1/3/5 will hold the current count. see figure 12-4 for timing details. 12.7.2 timer1/3/5 gate source selection the timer1/3/5 gate source can be selected from one of four different sources. source selection is controlled by the txgss bits of the txgcon register. the polarity for each available source is also selectable. polarity selection is controlled by the txgpol bit of the txgcon register. the gate resource, timer2 match to pr2, changes between timer2, timer4 and timer6 depending on which of the three 16-bit timers, timer1, timer3 or timer5, is selected. see table 12-5 to determine which timer2/4/6 match to pr2/4/6 combination is available for the 16-bit timer being used. 12.7.2.1 txg pin gate operation the txg pin is one source for timer1/3/5 gate control. it can be used to supply an external source to the timer1/3/5 gate circuitry. 12.7.2.2 timer2/4/6 match gate operation the tmr2/4/6 register will increment until it matches the value in the pr2/4/6 register. on the very next increment cycle, tmr2/4/6 will be reset to 00h. when this reset occurs, a low-to-high pulse will automatically be generated and internally supplied to the timer1/3/5 gate circuitry. see section 12.7.2 ?timer1/3/5 gate source selection? for more information. table 12-3: timer1/3/5 gate enable selections txclk txgpol txg timer1/3/5 operation ? 00 counts ? 01 holds count ? 10 holds count ? 11 counts tmr1l internal data bus 8 set tmr1if on overflow tmr1 tmr1h high byte 8 8 8 read tmr1l write tmr1l 8 from timer1 circuitry block diagram of timer1 example of timer1/3/5 table 12-4: timer1/3/5 gate sources txgss timer1/3/5 gate source 00 timer1/3/5 gate pin 01 timer2/4/6 match to pr2/4/6 (tmr2/4/6 increments to match pr2/4/6) 10 comparator 1 output sync_c1out (optionally timer1/3/5 synchronized out- put) 11 comparator 2 output sync_c2out (optionally timer1/3/5 synchronized out- put) table 12-5: gate resources for timer2/4/6 match to pr2/4/6 timer1/3/5 resource timer1/3/5 gate match selection timer1 tmr2 match to pr2 timer3 tmr4 match to pr4 timer5 tmr6 match to pr6 ? 2010-2012 microchip technology inc. ds41412f-page 167 pic18(l)f2x/4xk22 12.7.2.3 comparator c1 gate operation the output resulting from a comparator 1 operation can be selected as a source for timer1/3/5 gate control. the comparator 1 output (sync_c1out) can be synchronized to the timer1/3/5 clock or left asynchronous. for more information see section 18.8.4 ?synchronizing comparator output to timer1? . 12.7.2.4 comparator c2 gate operation the output resulting from a comparator 2 operation can be selected as a source for timer1/3/5 gate control. the comparator 2 output (sync_c2out) can be synchronized to the timer1/3/5 clock or left asynchronous. for more information see section 18.8.4 ?synchronizing comparator output to timer1? . 12.7.3 timer1/3/5 gate toggle mode when timer1/3/5 gate toggle mode is enabled, it is possible to measure the full-cycle length of a timer1/3/5 gate signal, as opposed to the duration of a single level pulse. the timer1/3/5 gate source is routed through a flip-flop that changes state on every incrementing edge of the signal. see figure 12-5 for timing details. timer1/3/5 gate toggle mode is enabled by setting the txgtm bit of the txgcon register. when the txgtm bit is cleared, the flip-flop is cleared and held clear. this is necessary in order to control which edge is measured. 12.7.4 timer1/3/5 gate single-pulse mode when timer1/3/5 gate single-pulse mode is enabled, it is possible to capture a single-pulse gate event. timer1/3/5 gate single-pulse mode is first enabled by setting the txgspm bit in the txgcon register. next, the txggo/done bit in the txgcon register must be set. the timer1/3/5 will be fully enabled on the next incrementing edge. on the next trailing edge of the pulse, the txggo/done bit will automatically be cleared. no other gate events will be allowed to increment timer1/3/5 until the txggo/done bit is once again set in software. clearing the txgspm bit of the txgcon register will also clear the txggo/done bit. see figure 12-6 for timing details. enabling the toggle mode and the single-pulse mode simultaneously will permit both sections to work together. this allows the cycle times on the timer1/3/5 gate source to be measured. see figure 12-7 for timing details. 12.7.5 timer1/3/5 gate value status when timer1/3/5 gate value status is utilized, it is possible to read the most current level of the gate control value. the value is stored in the txgval bit in the txgcon register. the txgval bit is valid even when the timer1/3/5 gate is not enabled (tmrxge bit is cleared). 12.7.6 timer1/3/5 gate event interrupt when timer1/3/5 gate event interrupt is enabled, it is possible to generate an interrupt upon the completion of a gate event. when the falling edge of txgval occurs, the tmrxgif flag bit in the pir3 register will be set. if the tmrxgie bit in the pie3 register is set, then an interrupt will be recognized. the tmrxgif flag bit operates even when the timer1/3/5 gate is not enabled (tmrxge bit is cleared). for more information on selecting high or low priority status for the timer1/3/5 gate event interrupt see section 9.0 ?interrupts? . note: enabling toggle mode at the same time as changing the gate polarity may result in indeterminate operation. pic18(l)f2x/4xk22 ds41412f-page 168 ? 2010-2012 microchip technology inc. 12.8 timer1/3/5 interrupt the timer1/3/5 register pair (tmrxh:tmrxl) increments to ffffh and rolls over to 0000h. when timer1/3/5 rolls over, the timer1/3/5 interrupt flag bit of the pir1/2/5 register is set. to enable the interrupt on rollover, you must set these bits: ? tmrxon bit of the txcon register ? tmrxie bits of the pie1, pie2 or pie5 registers ? peie/giel bit of the intcon register ? gie/gieh bit of the intcon register the interrupt is cleared by clearing the tmrxif bit in the interrupt service routine. for more information on selecting high or low priority status for the timer1/3/5 overflow interrupt, see section 9.0 ?interrupts? . 12.9 timer1/3/5 operation during sleep timer1/3/5 can only operate during sleep when set up in asynchronous counter mode. in this mode, an external crystal or clock source can be used to increment the counter. to set up the timer to wake the device: ? tmrxon bit of the txcon register must be set ? tmrxie bit of the pie1/2/5 register must be set ? peie/giel bit of the intcon register must be set ? txsync bit of the txcon register must be set ? tmrxcs bits of the txcon register must be configured ? txsoscen bit of the txcon register must be configured the device will wake-up on an overflow and execute the next instruction. if the gie/gieh bit of the intcon register is set, the device will call the interrupt service routine. the secondary oscillator will continue to operate in sleep regardless of the txsync bit setting. 12.10 eccp/ccp capture/compare time base the ccp modules use the tmrxh:tmrxl register pair as the time base when operating in capture or compare mode. in capture mode, the value in the tmrxh:tmrxl register pair is copied into the ccprxh:ccprxl register pair on a configured event. in compare mode, an event is triggered when the value ccprxh:ccprxl register pair matches the value in the tmrxh:tmrxl register pair. this event can be a special event trigger. for more information, see section 14.0 ?capture/compare/pwm modules? . 12.11 eccp/ccp special event trigger when any of the ccp?s are configured to trigger a special event, the trigger will clear the tmrxh:tmrxl register pair. this special event does not cause a timer1/3/5 interrupt. the ccp module may still be configured to generate a ccp interrupt. in this mode of operation, the ccprxh:ccprxl register pair becomes the period register for timer1/3/5. timer1/3/5 should be synchronized and f osc /4 should be selected as the clock source in order to utilize the special event trigger. asynchronous operation of timer1/3/5 can cause a special event trigger to be missed. in the event that a write to tmrxh or tmrxl coincides with a special event trigger from the ccp, the write will take precedence. for more information, see section 17.2.8 ?special event trigger? . note: the tmrxh:tmrxl register pair and the tmrxif bit should be cleared before enabling interrupts. ? 2010-2012 microchip technology inc. ds41412f-page 169 pic18(l)f2x/4xk22 figure 12-3: timer1/3/5 incrementing edge figure 12-4: timer1/3/5 gate enable mode txcki = 1 when tmrx enabled txcki = 0 when tmrx enabled note 1: arrows indicate counter increments. 2: in counter mode, a falling edge must be registered by the count er prior to the first incrementing rising edge of the clock. tmrxge txgpol txg_in txcki txgval timer1/3/5 n n + 1 n + 2 n + 3 n + 4 pic18(l)f2x/4xk22 ds41412f-page 170 ? 2010-2012 microchip technology inc. figure 12-5: timer1/3/5 gate toggle mode figure 12-6: timer1/3/5 gate single-pulse mode tmrxge txgpol txgtm txtxg_in txcki txgval timer1/3/5 n n + 1 n + 2 n + 3 n + 4 n + 5 n + 6 n + 7 n + 8 tmrxge txgpol txg_in txcki txgval timer1/3/5 n n + 1 n + 2 txgspm txggo/ done set by software cleared by hardware on falling edge of txgval set by hardware on falling edge of txgval cleared by software cleared by software tmrxgif counting enabled on rising edge of txg ? 2010-2012 microchip technology inc. ds41412f-page 171 pic18(l)f2x/4xk22 figure 12-7: timer1/3/5 gate single -pulse and toggle combined mode 12.12 peripheral module disable when a peripheral module is not used or inactive, the module can be disabled by setting the module disable bit in the pmd registers. this will reduce power con- sumption to an absolute minimum. setting the pmd bits holds the module in reset and disconnects the module?s clock source. the module disable bits for timer1 (tmr1md), timer3 (tmr3md) and timer5 (tmr5md) are in the pmd0 register. see section 3.0 ?power-managed modes? for more information. tmrxge txgpol txg_in txcki txgval timer1/3/5 nn + 1 n + 2 txgspm txggo/ done set by software cleared by hardware on falling edge of txgval set by hardware on falling edge of txgval cleared by software cleared by software tmrxgif txgtm counting enabled on rising edge of txg n + 4 n + 3 pic18(l)f2x/4xk22 ds41412f-page 172 ? 2010-2012 microchip technology inc. 12.13 register definitions: timer1/3/5 control register 12-1: txcon: time r1/3/5 control register r/w-0/u r/w-0/u r/w-0/u r/w-0/u r/w-0/u r/w-0/u r/w-0/0 r/w-0/u tmrxcs<1:0> txckps<1:0> txsoscen txs ync txrd16 tmrxon bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7-6 tmrxcs<1:0>: timer1/3/5 clock source select bits 11 = reserved. do not use. 10 = timer1/3/5 clock source is pin or oscillator: if txsoscen = 0 : external clock from txcki pin (on the rising edge) if txsoscen = 1 : crystal oscillator on sosci/sosco pins 01 = timer1/3/5 clock source is system clock (f osc ) 00 = timer1/3/5 clock source is instruction clock (f osc /4) bit 5-4 txckps<1:0>: timer1/3/5 input clock prescale select bits 11 = 1:8 prescale value 10 = 1:4 prescale value 01 = 1:2 prescale value 00 = 1:1 prescale value bit 3 txsoscen: secondary oscillator enable control bit 1 = dedicated secondary oscillator circuit enabled 0 = dedicated secondary oscillator circuit disabled bit 2 tx s ync : timer1/3/5 external clock input synchronization control bit tmrxc s<1:0> = 1x 1 = do not synchronize external clock input 0 = synchronize external clock input with system clock (f osc ) tmrxc s<1:0> = 0x this bit is ignored. timer1/3/5 uses the internal clock when tmrxcs<1:0> = 1x . bit 1 txrd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer1/3/5 in one 16-bit operation 0 = enables register read/write of timer1/3/5 in two 8-bit operation bit 0 tmrxon: timer1/3/5 on bit 1 = enables timer1/3/5 0 = stops timer1/3/5 clears timer1/3/5 gate flip-flop ? 2010-2012 microchip technology inc. ds41412f-page 173 pic18(l)f2x/4xk22 register 12-2: txgcon: timer1 /3/5 gate control register r/w-0/u r/w-0/u r/w-0/u r/w-0/u r/w/hc-0/u r-x/x r/w-0/u r/w-0/u tmrxge txgpol txgtm txgspm txggo/done txgval txgss<1:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared hc = bit is cleared by hardware bit 7 tmrxge: timer1/3/5 gate enable bit if tmrxon = 0 : this bit is ignored if tmrxon = 1 : 1 = timer1/3/5 counting is controlled by the timer1/3/5 gate function 0 = timer1/3/5 counts regardless of timer1/3/5 gate function bit 6 txgpol: timer1/3/5 gate polarity bit 1 = timer1/3/5 gate is active-high (timer1/3/5 counts when gate is high) 0 = timer1/3/5 gate is active-low (timer1/3/5 counts when gate is low) bit 5 txgtm: timer1/3/5 gate toggle mode bit 1 = timer1/3/5 gate toggle mode is enabled 0 = timer1/3/5 gate toggle mode is disabled and toggle flip-flop is cleared timer1/3/5 gate flip-flop toggles on every rising edge. bit 4 txgspm: timer1/3/5 gate single-pulse mode bit 1 = timer1/3/5 gate single-pulse mode is enabled and is controlling timer1/3/5 gate 0 = timer1/3/5 gate single-pulse mode is disabled bit 3 txggo/done : timer1/3/5 gate single-pulse acquisition status bit 1 = timer1/3/5 gate single-pulse acquisition is ready, waiting for an edge 0 = timer1/3/5 gate single-pulse acquisition has completed or has not been started this bit is automatically cleared when txgspm is cleared. bit 2 txgval: timer1/3/5 gate current state bit indicates the current state of the timer1/3/5 gate that could be provided to tmrxh:tmrxl. unaffected by timer1/3/5 gate enable (tmrxge). bit 1-0 txgss<1:0>: timer1/3/5 gate source select bits 00 = timer1/3/5 gate pin 01 = timer2/4/6 match pr2/4/6 output (see table 12-6 for proper timer match selection) 10 = comparator 1 optionally synchronized output (sync_c1out) 11 = comparator 2 optionally synchronized output (sync_c2out) pic18(l)f2x/4xk22 ds41412f-page 174 ? 2010-2012 microchip technology inc. table 12-6: registers associated with timer1/3/5 as a timer/counter table 12-7: configuration register s associated with timer1/3/5 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page anselb ? ? ansb5 ansb4 ansb3 ansb2 ansb1 ansb0 155 anselc ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ? ? 155 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 ipr5 ? ? ? ? ? tmr6ip tmr5ip tmr4ip 131 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pie5 ? ? ? ? ? tmr6ie tmr5ie tmr4ie 127 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pir5 ? ? ? ? ? tmr6if tmr5if tmr4if 123 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 t1con tmr1cs<1:0> t1ckps<1:0> t1soscen t1sync t1rd16 tmr1on 172 t1gcon tmr1ge t1gpol t1gtm t1gspm t1ggo/d one t1gval t1gss<1:0> 173 t3con tmr3cs<1:0> t3ckps<1:0> t3soscen t3sync t3rd16 tmr3on 172 t3gcon tmr3ge t3gpol t3gtm t3gspm t3ggo/d one t3gval t3gss<1:0> 173 t5con tmr5cs<1:0> t5ckps<1:0> t5soscen t5sync t5rd16 tmr5on 172 t5gcon tmr5ge t5gpol t5gtm t5gspm t5ggo/d one t5gval t5gss<1:0> 173 tmr1h holding register for the most significant byte of the 16-bit tmr1 register ? tmr1l least significant byte of the 16-bit tmr1 register ? tmr3h holding register for the most significant byte of the 16-bit tmr3 register ? tmr3l least significant byte of the 16-bit tmr3 register ? tmr5h holding register for the most significant byte of the 16-bit tmr5 register ? tmr5l least significant byte of the 16-bit tmr5 register ? trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 ? 2010-2012 microchip technology inc. ds41412f-page 175 pic18(l)f2x/4xk22 13.0 timer2/4/6 module there are three identical 8-bit timer2-type modules available. to maintain pre-existing naming conventions, the timers are called timer2, timer4 and timer6 (also timer2/4/6). the timer2/4/6 module incorporates the following features: ? 8-bit timer and period registers (tmrx and prx, respectively) ? readable and writable (both registers) ? software programmable prescaler (1:1, 1:4, 1:16) ? software programmable postscaler (1:1 to 1:16) ? interrupt on tmrx match with prx, respectively ? optional use as the shift clock for the msspx modules (timer2 only) see figure 13-1 for a block diagram of timer2/4/6. figure 13-1: timer2/4/6 block diagram note: the ?x? variable used in this section is used to designate timer2, timer4, or timer6. for example, txcon references t2con, t4con, or t6con. prx refer- ences pr2, pr4, or pr6. comparator tmrx sets flag tmrx output reset postscaler prescaler prx 2 f osc /4 1:1 to 1:16 1:1, 1:4, 1:16 eq 4 bit tmrxif txoutps<3:0> txckps<1:0> pic18(l)f2x/4xk22 ds41412f-page 176 ? 2010-2012 microchip technology inc. 13.1 timer2/4/6 operation the clock input to the timer2/4/6 module is the system instruction clock (f osc /4). tmrx increments from 00h on each clock edge. a 4-bit counter/prescaler on the clock input allows direct input, divide-by-4 and divide-by-16 prescale options. these options are selected by the prescaler control bits, txckps<1:0> of the txcon register. the value of tmrx is compared to that of the period register, prx, on each clock cycle. when the two values match, the comparator generates a match signal as the timer output. this signal also resets the value of tmrx to 00h on the next cycle and drives the output counter/postscaler (see section 13.2 ?timer2/4/6 interrupt? ). the tmrx and prx registers are both directly readable and writable. the tmrx register is cleared on any device reset, whereas the prx register initializes to ffh. both the prescaler and postscaler counters are cleared on the following events: ? a write to the tmrx register ? a write to the txcon register ? power-on reset (por) ? brown-out reset (bor) ?mclr reset ? watchdog timer (wdt) reset ? stack overflow reset ? stack underflow reset ? reset instruction 13.2 timer2/4/6 interrupt timer2/4/6 can also generate an optional device interrupt. the timer2/4/6 output signal (tmrx-to-prx match) provides the input for the 4-bit counter/postscaler. this counter generates the tmrx match interrupt flag which is latched in tmrxif of the pir1/pir5 registers. the interrupt is enabled by setting the tmrx match interrupt enable bit, tmrxie of the pie1/pie5 registers. interrupt priority is selected with the tmrxip bit in the ipr1/ipr5 registers. a range of 16 postscale options (from 1:1 through 1:16 inclusive) can be selected with the postscaler control bits, txoutps<3:0>, of the txcon register. 13.3 timer2/4/6 output the unscaled output of tmrx is available primarily to the ccp modules, where it is used as a time base for operations in pwm mode. the timer to be used with a specific ccp module is selected using the cxtsel<1:0> bits in the ccptmrs0 and ccptmrs1 registers. timer2 can be optionally used as the shift clock source for the msspx modules operating in spi mode by setting sspm<3:0> = 0011 in the sspxcon1 register. additional information is provided in section 15.0 ?master synchronous serial port (mssp1 and mssp2) module? . 13.4 timer2/4/6 operation during sleep the timer2/4/6 timers cannot be operated while the processor is in sleep mode. the contents of the tmrx and prx registers will remain unchanged while the processor is in sleep mode. 13.5 peripheral module disable when a peripheral module is not used or inactive, the module can be disabled by setting the module disable bit in the pmd registers. this will reduce power con- sumption to an absolute minimum. setting the pmd bits holds the module in reset and disconnects the module?s clock source. the module disable bits for timer2 (tmr2md), timer4 (tmr4md) and timer6 (tmr6md) are in the pmd0 register. see section 3.0 ?power-managed modes? for more information. note: tmrx is not cleared when txcon is written. ? 2010-2012 microchip technology inc. ds41412f-page 177 pic18(l)f2x/4xk22 13.6 register definitions: timer2/4/6 control register 13-1: txcon: timer2/t imer4/timer6 control register u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? txoutps<3:0> tmrxon txckps<1:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7 unimplemented: read as ? 0 ? bit 6-3 txoutps<3:0>: timerx output postscaler select bits 0000 = 1:1 postscaler 0001 = 1:2 postscaler 0010 = 1:3 postscaler 0011 = 1:4 postscaler 0100 = 1:5 postscaler 0101 = 1:6 postscaler 0110 = 1:7 postscaler 0111 = 1:8 postscaler 1000 = 1:9 postscaler 1001 = 1:10 postscaler 1010 = 1:11 postscaler 1011 = 1:12 postscaler 1100 = 1:13 postscaler 1101 = 1:14 postscaler 1110 = 1:15 postscaler 1111 = 1:16 postscaler bit 2 tmrxon: timerx on bit 1 = timerx is on 0 = timerx is off bit 1-0 txckps<1:0>: timer2-type clock prescale select bits 00 = prescaler is 1 01 = prescaler is 4 1x = prescaler is 16 pic18(l)f2x/4xk22 ds41412f-page 178 ? 2010-2012 microchip technology inc. table 13-1: summary of registers associated with timer2/4/6 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ccptmrs0 c3tsel<1:0> ? c2tsel<1:0> ? c1tsel<1:0> 208 ccptmrs1 ? ? ? ? c5tsel<1:0> c4tsel<1:0> 208 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr5 ? ? ? ? ?tmr6ip tmr5ip tmr4ip 131 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie5 ? ? ? ? ?tmr6ie tmr5ie tmr4ie 127 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir5 ? ? ? ? ?tmr6if tmr5if tmr4if 123 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 pr2 timer2 period register ? pr4 timer4 period register ? pr6 timer6 period register ? t2con ? t2outps<3:0> tmr2on t2ckps<1:0> 172 t4con ? t4outps<3:0> tmr4on t4ckps<1:0> 172 t6con ? t6outps<3:0> tmr6on t6ckps<1:0> 172 tmr2 timer2 register ? tmr4 timer4 register ? tmr6 timer6 register ? legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by timer2/4/6. ? 2010-2012 microchip technology inc. ds41412f-page 179 pic18(l)f2x/4xk22 14.0 capture/compare/pwm modules the capture/compare/pwm module is a peripheral which allows the user to time and control different events, and to generate pulse-width modulation (pwm) signals. in capture mode, the peripheral allows the timing of the duration of an event. the compare mode allows the user to trigger an external event when a predetermined amount of time has expired. the pwm mode can generate pulse-width modulated signals of varying frequency and duty cycle. this family of devices contains three enhanced capture/compare/pwm modules (eccp1, eccp2, and eccp3) and two standard capture/compare/pwm modules (ccp4 and ccp5). the capture and compare functions are identical for all ccp/eccp modules. the difference between ccp and eccp modules are in the pulse-width modulation (pwm) function. in ccp modules, the standard pwm function is identical. in eccp modules, the enhanced pwm function has either full-bridge or half-bridge pwm output. full-bridge eccp modules have four available i/o pins while half-bridge eccp modules only have two available i/o pins. eccp pwm modules are backward compatible with ccp pwm modules and can be configured as standard pwm modules. see tab l e 1 4- 1 to determine the ccp/eccp functionality available on each device in this family. note 1: in devices with more than one ccp module, it is very important to pay close attention to the register names used. a number placed after the module acronym is used to distinguish between separate modules. for example, the ccp1con and ccp2con control the same operational aspects of two completely different ccp modules. 2: throughout this section, generic references to a ccp module in any of its operating modes may be interpreted as being equally applicable to eccp1, eccp2, eccp3, ccp4 and ccp5. register names, module signals, i/o pins, and bit names may use the generic designator ?x? to indicate the use of a numeral to distinguish a particular module, when required. table 14-1: pwm resources device name eccp1 eccp2 eccp3 ccp4 ccp5 pic18(l)f23k22 pic18(l)f24k22 pic18(l)f25k22 pic18(l)f26k22 enhanced pwm full-bridge enhanced pwm half-bridge enhanced pwm half-bridge standard pwm standard pwm (special event trigger) pic18(l)f43k22 pic18(l)f44k22 pic18(l)f45k22 pic18(l)f46k22 enhanced pwm full-bridge enhanced pwm full-bridge enhanced pwm half-bridge standard pwm standard pwm (special event trigger) pic18(l)f2x/4xk22 ds41412f-page 180 ? 2010-2012 microchip technology inc. 14.1 capture mode the capture mode function described in this section is identical for all ccp and eccp modules available on this device family. capture mode makes use of the 16-bit timer resources, timer1, timer3 and timer5. the timer resources for each ccp capture function are independent and are selected using the ccptmrs0 and ccptmrs1 registers. when an event occurs on the ccpx pin, the 16-bit ccprxh:ccprxl register pair captures and stores the 16-bit value of the tmrxh:tmrxl register pair, respectively. an event is defined as one of the following and is configured by the ccpxm<3:0> bits of the ccpxcon register: ? every falling edge ? every rising edge ? every 4th rising edge ? every 16th rising edge when a capture is made, the corresponding interrupt request flag bit ccpxif of the pir1, pir2 or pir4 register is set. the interrupt flag must be cleared in software. if another capture occurs before the value in the ccprxh:ccprxl register pair is read, the old captured value is overwritten by the new captured value. figure 14-1 shows a simplified diagram of the capture operation. figure 14-1: capture mode operation block diagram 14.1.1 ccp pin configuration in capture mode, the ccpx pin should be configured as an input by setting the associated tris control bit. some ccpx outputs are multiplexed on a couple of pins. table 14-2 shows the ccp output pin multiplexing. selection of the output pin is determined by the ccpxmx bits in configuration register 3h (config3h). refer to register 24-4 for more details. 14.1.2 timer1 mode resource the 16-bit timer resource must be running in timer mode or synchronized counter mode for the ccp module to use the capture feature. in asynchronous counter mode, the capture operation may not work. see section 12.0 ?timer1/3/5 module with gate control? for more information on configuring the 16-bit timers. 14.1.3 software interrupt mode when the capture mode is changed, a false capture interrupt may be generated. the user should keep the ccpxie interrupt enable bit of the pie1, pie2 or pie4 register clear to avoid false interrupts. additionally, the user should clear the ccpxif interrupt flag bit of the pir1, pir2 or pir4 register following any change in operating mode. note: if the ccpx pin is configured as an output, a write to the port can cause a capture condition. ccprxh ccprxl tmr1/3/5h tmr1/3/5l set flag bit ccpxif (pir1/2/4 register) capture enable ccpxm<3:0> prescaler ? 1, 4, 16 and edge detect pin ccpx system clock (f osc ) table 14-2: ccp pin multiplexing ccp output config 3h control bit bit value pic18(l)f2xk22 i/o pin pic18(l)f4xk22 i/o pin ccp2 ccp2mx 0 rb3 rb3 1 (*) rc1 rc1 ccp3 ccp3mx 0 (*) rc6 re0 1 rb5 rb5 legend: * = default note: clocking the 16-bit timer resource from the system clock (f osc ) should not be used in capture mode. in order for capture mode to recognize the trigger event on the ccpx pin, the timer resource must be clocked from the instruction clock (f osc /4) or from an external clock source. ? 2010-2012 microchip technology inc. ds41412f-page 181 pic18(l)f2x/4xk22 14.1.4 ccp prescaler there are four prescaler settings specified by the ccpxm<3:0> bits of the ccpxcon register. whenever the ccp module is turned off, or the ccp module is not in capture mode, the prescaler counter is cleared. any reset will clear the prescaler counter. switching from one capture prescaler to another does not clear the prescaler and may generate a false interrupt. to avoid this unexpected operation, turn the module off by clearing the ccpxcon register before changing the prescaler. example 14-1 demonstrates the code to perform this function. example 14-1: changing between capture prescalers 14.1.5 capture during sleep capture mode requires a 16-bit timerx module for use as a time base. there are four options for driving the 16-bit timerx module in capture mode. it can be driven by the system clock (f osc ), the instruction clock (f osc / 4), or by the external clock sources, the secondary oscillator (s osc ), or the txcki clock input. when the 16-bit timerx resource is clocked by f osc or f osc /4, timerx will not increment during sleep. when the device wakes from sleep, timerx will continue from its previous state. capture mode will operate during sleep when the 16-bit timerx resource is clocked by one of the external clock sources (s osc or the txcki pin). #define new_capt_ps 0x06 //capture // prescale 4th ... // rising edge ccpxcon = 0; // turn the ccp // module off ccpxcon = new_capt_ps; // turn ccp module // on with new // prescale value table 14-3: registers associated with capture name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ccp1con p1m<1:0> dc1b<1:0> ccp1m<3:0> 205 ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 205 ccp3con p3m<1:0> dc3b<1:0> ccp3m<3:0> 205 ccp4con ? ? dc4b<1:0> ccp4m<3:0> 205 ccp5con ? ? dc5b<1:0> ccp5m<3:0> 205 ccpr1h capture/compare/pwm register 1 high byte (msb) ? ccpr1l capture/compare/pwm register 1 low byte (lsb) ? ccpr2h capture/compare/pwm register 2 high byte (msb) ? ccpr2l capture/compare/pwm register 2 low byte (lsb) ? ccpr3h capture/compare/pwm register 3 high byte (msb) ? ccpr3l capture/compare/pwm register 3 low byte (lsb) ? ccpr4h capture/compare/pwm register 4 high byte (msb) ? ccpr4l capture/compare/pwm register 4 low byte (lsb) ? ccpr5h capture/compare/pwm register 5 high byte (msb) ? ccpr5l capture/compare/pwm register 5 low byte (lsb) ? ccptmrs0 c3tsel<1:0> ? c2tsel<1:0> ? c1tsel<1:0> 208 ccptmrs1 ? ? ? ? c5tsel<1:0> c4tsel<1:0> 208 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 ipr4 ? ? ? ? ?ccp5ip ccp4ip ccp3ip 131 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by capture mode. note 1: these registers/bits are available on pic18(l)f4xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 182 ? 2010-2012 microchip technology inc. pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pie4 ? ? ? ? ? ccp5ie ccp4ie ccp3ie 127 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pir4 ? ? ? ? ? ccp5if ccp4if ccp3if 122 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 56 t1con tmr1cs<1:0> t1ckps<1:0> t1soscen t1sync t1rd16 tmr1on 172 t1gcon tmr1ge t1gpol t1gtm t1gspm t1ggo/d one t1gval t1gss<1:0> 173 t3con tmr3cs<1:0> t3ckps<1:0> t3soscen t 3s ync t3rd16 tmr3on 172 t3gcon tmr3ge t3gpol t3gtm t3gspm t3ggo/do ne t3gval t3gss<1:0> 173 t5con tmr5cs<1:0> t5ckps<1:0> t5soscen t 5s ync t5rd16 tmr5on 172 t5gcon tmr5ge t5gpol t5gtm t5gspm t5ggo/d one t5gval t5gss<1:0> 173 tmr1h holding register for the most significant byte of the 16-bit tmr1 register ? tmr1l least significant byte of the 16-bit tmr1 register ? tmr3h holding register for the most significant byte of the 16-bit tmr3 register ? tmr3l least significant byte of the 16-bit tmr3 register ? tmr5h holding register for the most significant byte of the 16-bit tmr5 register ? tmr5l least significant byte of the 16-bit tmr5 register ? trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 trise wpue3 ? ? ? ?trise2 (1) trise1 (1) trise0 (1) 156 table 14-3: registers associated with capture (continued) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by capture mode. note 1: these registers/bits are available on pic18(l)f4xk22 devices. table 14-4: configuration regist ers associated with capture name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by capture mode. ? 2010-2012 microchip technology inc. ds41412f-page 183 pic18(l)f2x/4xk22 14.2 compare mode the compare mode function described in this section is identical for all ccp and eccp modules available on this device family. compare mode makes use of the 16-bit timerx resources, timer1, timer3 and timer5. the 16-bit value of the ccprxh:ccprxl register pair is constantly compared against the 16-bit value of the tmrxh:tmrxl register pair. when a match occurs, one of the following events can occur: ? toggle the ccpx output ? set the ccpx output ? clear the ccpx output ? generate a special event trigger ? generate a software interrupt the action on the pin is based on the value of the ccpxm<3:0> control bits of the ccpxcon register. at the same time, the interrupt flag ccpxif bit is set. all compare modes can generate an interrupt. figure 14-2 shows a simplified diagram of the compare operation. figure 14-2: compare mode operation block diagram 14.2.1 ccp pin configuration the user must configure the ccpx pin as an output by clearing the associated tris bit. some ccpx outputs are multiplexed on a couple of pins. table 14-2 shows the ccp output pin multiplexing. selection of the output pin is determined by the ccpxmx bits in configuration register 3h (config3h). refer to register 24-4 for more details. 14.2.2 timerx mode resource in compare mode, 16-bit timerx resource must be running in either timer mode or synchronized counter mode. the compare operation may not work in asynchronous counter mode. see section 12.0 ?timer1/3/5 module with gate control? for more information on configuring the 16-bit timerx resources. 14.2.3 software interrupt mode when generate software interrupt mode is chosen (ccpxm<3:0> = 1010 ), the ccpx module does not assert control of the ccpx pin (see the ccpxcon register). ccprxh ccprxl tmrxh tmrxl comparator qs r output logic special event trigger set ccpxif interrupt flag (pir1/2/4) match tris ccpxm<3:0> mode select output enable pin special event trigger function on ? eccp1, eccp2, eccp3, ccp4 and ccp5 will: - reset timerx ? tmrxh:tmrxl = 0x0000 - timerx interrupt flag, (tmrxif) is not set additional function on ? ccp5 will - set adcon0<1>, go/done bit to start an adc conversion if adcon<0>, adon = 1 . ccpx 4 note: clearing the ccpxcon register will force the ccpx compare output latch to the default low level. this is not the port i/o data latch. note: clocking timerx from the system clock (f osc ) should not be used in compare mode. in order for compare mode to recognize the trigger event on the ccpx pin, timerx must be clocked from the instruction clock (f osc /4) or from an external clock source. pic18(l)f2x/4xk22 ds41412f-page 184 ? 2010-2012 microchip technology inc. 14.2.4 special event trigger when special event trigger mode is selected (ccpxm<3:0> = 1011 ), and a match of the tmrxh:tmrxl and the ccprxh:ccprxl registers occurs, all ccpx and eccpx modules will immediately: ? set the ccp interrupt flag bit ? ccpxif ? ccp5 will start an adc conversion, if the adc is enabled on the next timerx rising clock edge: ? a reset of timerx register pair occurs ? tmrxh:tmrxl = 0x0000, this special event trigger mode does not: ? assert control over the ccpx or eccpx pins. ? set the tmrxif interrupt bit when the tmrxh:tmrxl register pair is reset. (tmrxif gets set on a timerx overflow.) if the value of the ccprxh:ccprxl registers are modified when a match occurs, the user should be aware that the automatic reset of timerx occurs on the next rising edge of the clock. therefore, modifying the ccprxh:ccprxl registers before this reset occurs will allow the timerx to continue without being reset, inadvertently resulting in the next event being advanced or delayed. the special event trigger mode allows the ccprxh:ccprxl register pair to effectively provide a 16-bit programmable period register for timerx. 14.2.5 compare during sleep the compare mode is dependent upon the system clock (f osc ) for proper operation. since f osc is shut down during sleep mode, the compare mode will not function properly during sleep. table 14-5: registers associated with compare name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ccp1con p1m<1:0> dc1b<1:0> ccp1m<3:0> 205 ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 205 ccp3con p3m<1:0> dc3b<1:0> ccp3m<3:0> 205 ccp4con ? ? dc4b<1:0> ccp4m<3:0> 205 ccp5con ? ? dc5b<1:0> ccp5m<3:0> 205 ccpr1h capture/compare/pwm register 1 high byte (msb) ? ccpr1l capture/compare/pwm register 1 low byte (lsb) ? ccpr2h capture/compare/pwm register 2 high byte (msb) ? ccpr2l capture/compare/pwm register 2 low byte (lsb) ? ccpr3h capture/compare/pwm register 3 high byte (msb) ? ccpr3l capture/compare/pwm register 3 low byte (lsb) ? ccpr4h capture/compare/pwm register 4 high byte (msb) ? ccpr4l capture/compare/pwm register 4 low byte (lsb) ? ccpr5h capture/compare/pwm register 5 high byte (msb) ? ccpr5l capture/compare/pwm register 5 low byte (lsb) ? ccptmrs0 c3tsel<1:0> ? c2tsel<1:0> ? c1tsel<1:0> 208 ccptmrs1 ? ? ? ? c5tsel<1:0> c4tsel<1:0> 208 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by compare mode. note 1: these registers/bits are available on pic18(l)f4xk22 devices. ? 2010-2012 microchip technology inc. ds41412f-page 185 pic18(l)f2x/4xk22 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 ipr4 ? ? ? ? ? ccp5ip ccp4ip ccp3ip 131 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pie4 ? ? ? ? ? ccp5ie ccp4ie ccp3ie 127 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pir4 ? ? ? ? ? ccp5if ccp4if ccp3if 122 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 56 t1con tmr1cs<1:0> t1ckps<1:0> t1soscen t1sync t1rd16 tmr1on 172 t1gcon tmr1ge t1gpol t1gtm t1gspm t1ggo/d one t1gval t1gss<1:0> 173 t3con tmr3cs<1:0> t3ckps<1:0> t3soscen t 3s ync t3rd16 tmr3on 172 t3gcon tmr3ge t3gpol t3gtm t3gspm t3ggo/d one t3gval t3gss<1:0> 173 t5con tmr5cs<1:0> t5ckps<1:0> t5soscen t 5s ync t5rd16 tmr5on 172 t5gcon tmr5ge t5gpol t5gtm t5gspm t5ggo/d one t5gval t5gss<1:0> 173 tmr1h holding register for the most significant byte of the 16-bit tmr1 register ? tmr1l least significant byte of the 16-bit tmr1 register ? tmr3h holding register for the most significant byte of the 16-bit tmr3 register ? tmr3l least significant byte of the 16-bit tmr3 register ? tmr5h holding register for the most significant byte of the 16-bit tmr5 register ? tmr5l least significant byte of the 16-bit tmr5 register ? trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 trise wpue3 ? ? ? ? trise2 (1) trise1 (1) trise0 (1) 156 table 14-5: registers associated with compare (continued) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by compare mode. note 1: these registers/bits are available on pic18(l)f4xk22 devices. table 14-6: configuration regist ers associated with compare name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by compare mode. pic18(l)f2x/4xk22 ds41412f-page 186 ? 2010-2012 microchip technology inc. 14.3 pwm overview pulse-width modulation (pwm) is a scheme that provides power to a load by switching quickly between fully on and fully off states. the pwm signal resembles a square wave where the high portion of the signal is considered the on state and the low portion of the signal is considered the off state. the high portion, also known as the pulse width, can vary in time and is defined in steps. a larger number of steps applied, which lengthens the pulse width, also supplies more power to the load. lowering the number of steps applied, which shortens the pulse width, supplies less power. the pwm period is defined as the duration of one complete cycle or the total amount of on and off time combined. pwm resolution defines the maximum number of steps that can be present in a single pwm period. a higher resolution allows for more precise control of the pulse width time and in turn the power that is applied to the load. the term duty cycle describes the proportion of the on time to the off time and is expressed in percentages, where 0% is fully off and 100% is fully on. a lower duty cycle corresponds to less power applied and a higher duty cycle corresponds to more power applied. figure 14-3 shows a typical waveform of the pwm signal. 14.3.1 standard pwm operation the standard pwm function described in this section is available and identical for ccp and eccp modules. the standard pwm mode generates a pulse-width modulation (pwm) signal on the ccpx pin with up to 10 bits of resolution. the period, duty cycle, and resolution are controlled by the following registers: ?prx registers ?txcon registers ? ccprxl registers ? ccpxcon registers figure 14-4 shows a simplified block diagram of pwm operation. figure 14-3: ccp pwm output signal figure 14-4: simplified pwm block diagram 14.3.2 setup for pwm operation the following steps should be taken when configuring the ccp module for standard pwm operation: 1. disable the ccpx pin output driver by setting the associated tris bit. 2. select the 8-bit timerx resource, (timer2, timer4 or timer6) to be used for pwm genera- tion by setting the cxtsel<1:0> bits in the ccptmrsx register. (1) 3. load the prx register for the selected timerx with the pwm period value. 4. configure the ccp module for the pwm mode by loading the ccpxcon register with the appropriate values. 5. load the ccprxl register and the dcxb<1:0> bits of the ccpxcon register, with the pwm duty cycle value. note 1: the corresponding tris bit must be cleared to enable the pwm output on the ccpx pin. 2: clearing the ccpxcon register will relinquish control of the ccpx pin. period pulse width tmrx = 0 tmrx = ccprxh:ccpxcon<5:4> tmrx = prx ccprxl ccprxh (2) (slave) comparator tmrx prx (1) rq s duty cycle registers ccpxcon<5:4> clear timer, toggle ccpx pin and latch duty cycle note 1: the 8-bit timer tmrx register is concatenated with the 2-bit internal system clock (f osc ), or 2 bits of the prescaler, to create the 10-bit time base. 2: in pwm mode, ccprxh is a read-only register. tris ccpx comparator ? 2010-2012 microchip technology inc. ds41412f-page 187 pic18(l)f2x/4xk22 6. configure and start the 8-bit timerx resource: ? clear the tmrxif interrupt flag bit of the pir2 or pir4 register. see note 1 below. ? configure the txckps bits of the txcon register with the timer prescale value. ? enable the timer by setting the tmrxon bit of the txcon register. 7. enable pwm output pin: ? wait until the timer overflows and the tmrxif bit of the pir2 or pir4 register is set. see note 1 below. ? enable the ccpx pin output driver by clearing the associated tris bit. 14.3.3 pwm timer resource the pwm standard mode makes use of one of the 8-bit timer2/4/6 timer resources to specify the pwm period. configuring the cxtsel<1:0> bits in the ccptmrs0 or ccptmrs1 register selects which timer2/4/6 timer is used. 14.3.4 pwm period the pwm period is specified by the prx register of 8-bit timerx. the pwm period can be calculated using the formula of equation 14-1 . equation 14-1: pwm period when tmrx is equal to prx, the following three events occur on the next increment cycle: ? tmrx is cleared ? the ccpx pin is set. (exception: if the pwm duty cycle = 0%, the pin will not be set.) ? the pwm duty cycle is latched from ccprxl into ccprxh. 14.3.5 pwm duty cycle the pwm duty cycle is specified by writing a 10-bit value to multiple registers: ccprxl register and dcxb<1:0> bits of the ccpxcon register. the ccprxl contains the eight msbs and the dcxb<1:0> bits of the ccpxcon register contain the two lsbs. ccprxl and dcxb<1:0> bits of the ccpxcon register can be written to at any time. the duty cycle value is not latched into ccprxh until after the period completes (i.e., a match between prx and tmrx registers occurs). while using the pwm, the ccprxh register is read-only. equation 14-2 is used to calculate the pwm pulse width. equation 14-3 is used to calculate the pwm duty cycle ratio. equation 14-2: pulse width equation 14-3: duty cycle ratio the ccprxh register and a 2-bit internal latch are used to double buffer the pwm duty cycle. this double buffering is essential for glitchless pwm operation. the 8-bit timer tmrx register is concatenated with either the 2-bit internal system clock (f osc ), or two bits of the prescaler, to create the 10-bit time base. the system clock is used if the timerx prescaler is set to 1:1. when the 10-bit time base matches the ccprxh and 2-bit latch, then the ccpx pin is cleared (see figure 14-4 ). note 1: in order to send a complete duty cycle and period on the first pwm output, the above steps must be included in the setup sequence. if it is not critical to start with a complete pwm signal on the first output, then step 6 may be ignored. note: the timer postscaler (see section 13.0 ?timer2/4/6 module? ) is not used in the determination of the pwm frequency. pwm period prx ?? 1 + ?? 4t osc ? ? ? = (tmrx prescale value) note 1: t osc = 1/f osc pulse width ccprxl:ccpxcon<5:4> ?? ? = t osc ? (tmrx prescale value) duty cycle ratio ccprxl:ccpxcon<5:4> ?? 4 prx 1 + ?? ---------------------------------------------------------------------- - = pic18(l)f2x/4xk22 ds41412f-page 188 ? 2010-2012 microchip technology inc. 14.3.6 pwm resolution the resolution determines the number of available duty cycles for a given period. for example, a 10-bit resolution will result in 1024 discrete duty cycles, whereas an 8-bit resolution will result in 256 discrete duty cycles. the maximum pwm resolution is ten bits when prx is 255. the resolution is a function of the prx register value as shown by equation 14-4 . equation 14-4: pwm resolution 14.3.7 operation in sleep mode in sleep mode, the tmrx register will not increment and the state of the module will not change. if the ccpx pin is driving a value, it will continue to drive that value. when the device wakes up, tmrx will continue from its previous state. 14.3.8 changes in system clock frequency the pwm frequency is derived from the system clock frequency. any changes in the system clock frequency will result in changes to the pwm frequency. see section 2.0 ?oscillator module (with fail-safe clock monitor)? for additional details. 14.3.9 effects of reset any reset will force all ports to input mode and the ccp registers to their reset states. note: if the pulse width value is greater than the period the assigned pwm pin(s) will remain unchanged. resolution 4prx 1 + ?? ?? log 2 ?? log ----------------------------------------- - bits = table 14-7: example pwm frequencies and resolutions (f osc = 32 mhz) pwm frequency 1.95 khz 7.81 khz 31.25 khz 125 khz 250 khz 333.3 khz timer prescale (1, 4, 16) 16 4 1 1 1 1 prx value 0xff 0xff 0xff 0x3f 0x1f 0x17 maximum resolution (bits) 10 10 10 8 7 6.6 table 14-8: example pwm frequencies and resolutions (f osc = 20 mhz) pwm frequency 1.22 khz 4.88 khz 19.53 khz 78.12 khz 156.3 khz 208.3 khz timer prescale (1, 4, 16) 16 4 1 1 1 1 prx value 0xff 0xff 0xff 0x3f 0x1f 0x17 maximum resolution (bits) 10 10 10 8 7 6.6 table 14-9: example pwm frequencies and resolutions (f osc = 8 mhz) pwm frequency 1.22 khz 4.90 khz 19.61 khz 76.92 khz 153.85 khz 200.0 khz timer prescale (1, 4, 16) 16 4 1 1 1 1 prx value 0x65 0x65 0x65 0x19 0x0c 0x09 maximum resolution (bits) 8 8 8 6 5 5 ? 2010-2012 microchip technology inc. ds41412f-page 189 pic18(l)f2x/4xk22 table 14-11: configuratio n registers associated with standard pwm table 14-10: registers associated with standard pwm name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ccp1con p1m<1:0> dc1b<1:0> ccp1m<3:0> 205 ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 205 ccp3con p3m<1:0> dc3b<1:0> ccp3m<3:0> 205 ccp4con ? ? dc4b<1:0> ccp4m<3:0> 205 ccp5con ? ? dc5b<1:0> ccp5m<3:0> 205 ccptmrs0 c3tsel<1:0> ? c2tsel<1:0> ? c1tsel<1:0> 208 ccptmrs1 ? ? ? ? c5tsel<1:0> c4tsel<1:0> 208 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 ipr4 ? ? ? ? ?ccp5ip ccp4ip ccp3ip 131 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pie4 ? ? ? ? ? ccp5ie ccp4ie ccp3ie 127 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pir4 ? ? ? ? ? ccp5if ccp4if ccp3if 122 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 56 pr2 timer2 period register ? pr4 timer4 period register ? pr6 timer6 period register ? t2con ?t2outps<3:0>tmr2on t2ckps<1:0> 172 t4con ?t4outps<3:0>tmr4on t4ckps<1:0> 172 t6con ?t6outps<3:0>tmr6on t6ckps<1:0> 172 tmr2 timer2 register ? tmr4 timer4 register ? tmr6 timer6 register ? trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 trise wpue3 ? ? ? ?trise2 (1) trise1 (1) trise0 (1) 156 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by standard pwm mode. note 1: these registers/bits are available on pic18(l)f4xk22 devices. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by standard pwm mode. pic18(l)f2x/4xk22 ds41412f-page 190 ? 2010-2012 microchip technology inc. 14.4 pwm (enhanced mode) the enhanced pwm function described in this section is available for ccp modules eccp1, eccp2 and eccp3, with any differences between modules noted. the enhanced pwm mode generates a pulse-width modulation (pwm) signal on up to four different output pins with up to ten bits of resolution. the period, duty cycle, and resolution are controlled by the following registers: ?prx registers ?txcon registers ? ccprxl registers ? ccpxcon registers the eccp modules have the following additional pwm registers which control auto-shutdown, auto-restart, dead-band delay and pwm steering modes: ? eccpxas registers ? pstrxcon registers ? pwmxcon registers the enhanced pwm module can generate the following five pwm output modes: ? single pwm ? half-bridge pwm ? full-bridge pwm, forward mode ? full-bridge pwm, reverse mode ? single pwm with pwm steering mode to select an enhanced pwm output mode, the pxm<1:0> bits of the ccpxcon register must be configured appropriately. the pwm outputs are multiplexed with i/o pins and are designated pxa, pxb, pxc and pxd. the polarity of the pwm pins is configurable and is selected by setting the ccpxm bits in the ccpxcon register appropriately. figure 14-5 shows an example of a simplified block diagram of the enhanced pwm module. table 14-12 shows the pin assignments for various enhanced pwm modes. figure 14-5: example simplified block diagram of the enhanced pwm mode note 1: the corresponding tris bit must be cleared to enable the pwm output on the ccpx pin. 2: clearing the ccpxcon register will relinquish control of the ccpx pin. 3: any pin not used in the enhanced pwm mode is available for alternate pin functions, if applicable. 4: to prevent the generation of an incomplete waveform when the pwm is first enabled, the eccp module waits until the start of a new pwm period before generating a pwm signal. ccprxl ccprxh (slave) comparator tmrx comparator prx (1) rq s duty cycle registers dcxb<1:0> clear timer, toggle pwm pin and latch duty cycle note 1: the 8-bit timer tmrx register is concatenated with the 2-bit internal q clock, or 2 bits of the prescaler to create the 10-bit time base. 2: pxc and pxd are not available on half-bridge eccp modules. trisx ccpx/pxa trisx pxb trisx pxc (2) trisx pxd (2) output controller pxm<1:0> 2 ccpxm<3:0> 4 pwmxcon ccpx/pxa pxb pxc pxd ? 2010-2012 microchip technology inc. ds41412f-page 191 pic18(l)f2x/4xk22 figure 14-6: example pwm (enhanced mode) output relationships (active-high state) table 14-12: example pin assignme nts for various pwm enhanced modes eccp mode pxm<1:0> ccpx/pxa pxb pxc pxd single 00 yes (1) yes (1) yes (1) yes (1) half-bridge 10 yes yes no no full-bridge, forward 01 yes yes yes yes full-bridge, reverse 11 yes yes yes yes note 1: pwm steering enables outputs in single mode. 0 period 00 10 01 11 signal prx+1 pxm<1:0> pxa modulated pxa modulated pxb modulated pxa active pxb inactive pxc inactive pxd modulated pxa inactive pxb modulated pxc active pxd inactive pulse width (single output) (half-bridge) (full-bridge, forward) (full-bridge, reverse) delay (1) delay (1) relationships: ? period = 4 * t osc * (prx + 1) * (tmrx prescale value) ? pulse width = t osc * (ccprxl<7:0>:ccpxcon<5:4>) * (tmrx prescale value) ? delay = 4 * t osc * (pwmxcon<6:0>) note 1: dead-band delay is programmed using the pwmxcon register ( section 14.4.5 ?programmable dead-band delay mode? ). pic18(l)f2x/4xk22 ds41412f-page 192 ? 2010-2012 microchip technology inc. figure 14-7: example enhanced pwm outp ut relationships (active-low state) 0 period 00 10 01 11 signal prx+1 pxm<1:0> pxa modulated pxa modulated pxb modulated pxa active pxb inactive pxc inactive pxd modulated pxa inactive pxb modulated pxc active pxd inactive pulse width (single output) (half-bridge) (full-bridge, forward) (full-bridge, reverse) delay (1) delay (1) relationships: ? period = 4 * t osc * (prx + 1) * (tmrx prescale value) ? pulse width = t osc * (ccprxl<7:0>:ccpxcon<5:4>) * (tmrx prescale value) ? delay = 4 * t osc * (pwmxcon<6:0>) note 1: dead-band delay is programmed using the pwmxcon register ( section 14.4.5 ?programmable dead-band delay mode? ). ? 2010-2012 microchip technology inc. ds41412f-page 193 pic18(l)f2x/4xk22 14.4.1 half-bridge mode in half-bridge mode, two pins are used as outputs to drive push-pull loads. the pwm output signal is output on the ccpx/pxa pin, while the complementary pwm output signal is output on the pxb pin (see figure 14-9 ). this mode can be used for half-bridge applications, as shown in figure 14-9 , or for full-bridge applications, where four power switches are being modulated with two pwm signals. in half-bridge mode, the programmable dead-band delay can be used to prevent shoot-through current in half- bridge power devices. the value of the pdc<6:0> bits of the pwmxcon register sets the number of instruction cycles before the output is driven active. if the value is greater than the duty cycle, the corresponding output remains inactive during the entire cycle. see section 14.4.5 ?programmable dead-band delay mode? for more details of the dead-band delay operations. since the pxa and pxb outputs are multiplexed with the port data latches, the associated tris bits must be cleared to configure pxa and pxb as outputs. figure 14-8: example of half- bridge pwm output figure 14-9: example of half-bridge applications period pulse width td td (1) pxa (2) pxb (2) td = dead-band delay period (1) (1) note 1: at this time, the tmrx register is equal to the prx register. 2: output signals are shown as active-high. pxa pxb fet driver fet driver load + - + - fet driver fet driver v+ load fet driver fet driver pxa pxb standard half-bridge circuit (?push-pull?) half-bridge output driving a full-bridge circuit pic18(l)f2x/4xk22 ds41412f-page 194 ? 2010-2012 microchip technology inc. 14.4.2 full-bridge mode in full-bridge mode, all four pins are used as outputs. an example of full-bridge application is shown in figure 14-10 . in the forward mode, pin ccpx/pxa is driven to its active state, pin pxd is modulated, while pxb and pxc will be driven to their inactive state as shown in figure 14-11 . in the reverse mode, pxc is driven to its active state, pin pxb is modulated, while pxa and pxd will be driven to their inactive state as shown figure 14-11 . pxa, pxb, pxc and pxd outputs are multiplexed with the port data latches. the associated tris bits must be cleared to configure the pxa, pxb, pxc and pxd pins as outputs. figure 14-10: example of full-bridge application pxa pxc fet driver fet driver v+ v- load fet driver fet driver pxb pxd qa qb qd qc ? 2010-2012 microchip technology inc. ds41412f-page 195 pic18(l)f2x/4xk22 figure 14-11: example of full-bridge pwm output period pulse width pxa (2) pxb (2) pxc (2) pxd (2) forward mode (1) period pulse width pxa (2) pxc (2) pxd (2) pxb (2) reverse mode (1) (1) (1) note 1: at this time, the tmrx register is equal to the prx register. 2: output signal is shown as active-high. pic18(l)f2x/4xk22 ds41412f-page 196 ? 2010-2012 microchip technology inc. 14.4.2.1 direction change in full-bridge mode in the full-bridge mode, the pxm1 bit in the ccpxcon register allows users to control the forward/reverse direction. when the application firmware changes this direction control bit, the module will change to the new direction on the next pwm cycle. a direction change is initiated in software by changing the pxm1 bit of the ccpxcon register. the following sequence occurs four timer cycles prior to the end of the current pwm period: ? the modulated outputs (pxb and pxd) are placed in their inactive state. ? the associated unmodulated outputs (pxa and pxc) are switched to drive in the opposite direction. ? pwm modulation resumes at the beginning of the next period. see figure 14-12 for an illustration of this sequence. the full-bridge mode does not provide dead-band delay. as one output is modulated at a time, dead-band delay is generally not required. there is a situation where dead-band delay is required. this situation occurs when both of the following conditions are true: 1. the direction of the pwm output changes when the duty cycle of the output is at or near 100%. 2. the turn off time of the power switch, including the power device and driver circuit, is greater than the turn on time. figure 14-13 shows an example of the pwm direction changing from forward to reverse, at a near 100% duty cycle. in this example, at time t1, the output pxa and pxd become inactive, while output pxc becomes active. since the turn off time of the power devices is longer than the turn on time, a shoot-through current will flow through power devices qc and qd (see figure 14-10 ) for the duration of ?t?. the same phenomenon will occur to power devices qa and qb for pwm direction change from reverse to forward. if changing pwm direction at high duty cycle is required for an application, two possible solutions for eliminating the shoot-through current are: 1. reduce pwm duty cycle for one pwm period before changing directions. 2. use switch drivers that can drive the switches off faster than they can drive them on. other options to prevent shoot-through current may exist. figure 14-12: example of pwm direction change pulse width period (1) signal note 1: the direction bit pxm1 of the ccpxcon register is written any time during the pwm cycle. 2: when changing directions, the pxa and pxc signals switch before the end of the current pwm cycle. the modulated pxb and pxd signals are inactive at this time. the length of this time is (timerx prescale)/f osc , where timerx is timer2, timer4 or timer6. period (2) pxa (active-high) pxb (active-high) pxc (active-high) pxd (active-high) pulse width ? 2010-2012 microchip technology inc. ds41412f-page 197 pic18(l)f2x/4xk22 figure 14-13: example of pwm direct ion change at near 100% duty cycle 14.4.3 enhanced pwm auto- shutdown mode the pwm mode supports an auto-shutdown mode that will disable the pwm outputs when an external shutdown event occurs. auto-shutdown mode places the pwm output pins into a predetermined state. this mode is used to help prevent the pwm from damaging the application. the auto-shutdown sources are selected using the ccpxas<2:0> bits of the eccpxas register. a shutdown event may be generated by: ?a logic ? 0 ? on the int pin ? comparator cx (async_cxout) ? setting the ccpxase bit in firmware a shutdown condition is indicated by the ccpxase (auto-shutdown event status) bit of the eccpxas register. if the bit is a ? 0 ?, the pwm pins are operating normally. if the bit is a ? 1 ?, the pwm outputs are in the shutdown state. when a shutdown event occurs, two things happen: the ccpxase bit is set to ? 1 ?. the ccpxase will remain set until cleared in firmware or an auto-restart occurs (see section 14.4.4 ?auto-restart mode? ). the enabled pwm pins are asynchronously placed in their shutdown states. the pwm output pins are grouped into pairs [pxa/pxc ] and [pxb/pxd]. the state of each pin pair is determined by the pssxac<1:0> and pssxbd<1:0> bits of the eccpxas register. each pin pair may be placed into one of three states: ? drive logic ? 1 ? ? drive logic ? 0 ? ? tri-state (high-impedance) forward period reverse period pxa t on t off t = t off ? t on pxb pxc pxd external switch d potential shoot-through current note 1: all signals are shown as active-high. 2: t on is the turn-on delay of pow er switch qc and its driver. 3: t off is the turn-off delay of power switch qd and its driver. external switch c t1 pw pw note 1: the auto-shutdown condition is a level- based signal, not an edge-based signal. as long as the level is present, the auto- shutdown will persist. 2: writing to the ccpxase bit is disabled while an auto-shutdown condition persists. 3: once the auto-shutdown condition has been removed and the pwm restarted (either through firmware or auto-restart), the pwm signal will always restart at the beginning of the next pwm period. pic18(l)f2x/4xk22 ds41412f-page 198 ? 2010-2012 microchip technology inc. figure 14-14: pwm auto-shutdown wi th firmware restart (pxrsen = 0 ) 14.4.4 auto-restart mode the enhanced pwm can be configured to automatically restart the pwm signal once the auto- shutdown condition has been removed. auto-restart is enabled by setting the pxrsen bit in the pwmxcon register. if auto-restart is enabled, the ccpxase bit will remain set as long as the auto-shutdown condition is active. when the auto-shutdown condition is removed, the ccpxase bit will be cleared via hardware and normal operation will resume. figure 14-15: pwm auto-shutdown with auto-restart (pxrsen = 1 ) shutdown pwm ccpxase bit activity event shutdown event occurs shutdown event clears pwm resumes pwm period start of pwm period ccpxase cleared by firmware timer overflow timer overflow timer overflow timer overflow missing pulse (auto-shutdown) missing pulse (ccpxase not clear) timer overflow shutdown pwm ccpxase bit activity event shutdown event occurs shutdown event clears pwm period start of pwm period ccpxase cleared by hardware timer overflow timer overflow timer overflow timer overflow missing pulse (auto-shutdown) missing pulse (ccpxase not clear) timer overflow pwm resumes ? 2010-2012 microchip technology inc. ds41412f-page 199 pic18(l)f2x/4xk22 14.4.5 programmable dead-band delay mode in half-bridge applications where all power switches are modulated at the pwm frequency, the power switches normally require more time to turn off than to turn on. if both the upper and lower power switches are switched at the same time (one turned on, and the other turned off), both switches may be on for a short period of time until one switch completely turns off. during this brief interval, a very high current ( shoot-through current ) will flow through both power switches, shorting the bridge supply. to avoid this potentially destructive shoot- through current from flowing during switching, turning on either of the power switches is normally delayed to allow the other switch to completely turn off. in half-bridge mode, a digitally programmable dead- band delay is available to avoid shoot-through current from destroying the bridge power switches. the delay occurs at the signal transition from the non-active state to the active state. see figure 14-16 for illustration. the lower seven bits of the associated pwmxcon register ( register 14-6 ) sets the delay period in terms of microcontroller instruction cycles (t cy or 4 t osc ). figure 14-16: example of half- bridge pwm output figure 14-17: example of half-bridge applications period pulse width td td (1) pxa (2) pxb (2) td = dead-band delay period (1) (1) note 1: at this time, the tmrx register is equal to the prx register. 2: output signals are shown as active-high. pxa pxb fet driver fet driver v+ v- load + v - + v - standard half-bridge circuit (?push-pull?) pic18(l)f2x/4xk22 ds41412f-page 200 ? 2010-2012 microchip technology inc. 14.4.6 pwm steering mode in single output mode, pwm steering allows any of the pwm pins to be the modulated signal. additionally, the same pwm signal can be simultaneously available on multiple pins. once the single output mode is selected (ccpxm<3:2> = 11 and pxm<1:0> = 00 of the ccpxcon register), the user firmware can bring out the same pwm signal to one, two, three or four output pins by setting the appropriate steering enable bits (strxa, strxb, strxc and/or strxd) of the pstrxcon register, as shown in table 14-13 . while the pwm steering mode is active, ccpxm<1:0> bits of the ccpxcon register select the pwm output polarity for the pxd, pxc, pxb and pxa pins. the pwm auto-shutdown operation also applies to pwm steering mode as described in section 14.4.3 ?enhanced pwm auto-shutdown mode? . an auto- shutdown event will only affect pins that have pwm outputs enabled. figure 14-18: simplified steering block diagram 14.4.6.1 steering synchronization the strxsync bit of the pstrxcon register gives the user two selections of when the steering event will happen. when the strxsync bit is ? 0 ?, the steering event will happen at the end of the instruction that writes to the pstrxcon register. in this case, the output signal at the pxa, pxb, pxc and pxd pins may be an incomplete pwm waveform. this operation is useful when the user firmware needs to immediately remove a pwm signal from the pin. when the strxsync bit is ? 1 ?, the effective steering update will happen at the beginning of the next pwm period. in this case, steering on/off the pwm output will always produce a complete pwm waveform. figures 14-19 and 14-20 illustrate the timing diagrams of the pwm steering depending on the strxsync setting. note: the associated tris bits must be set to output (? 0 ?) to enable the pin output driver in order to see the pwm signal on the pin. 1 0 tris pxa pin port data pxa signal strxa 1 0 tris pxb pin port data strxb 1 0 tris pxc pin port data strxc 1 0 tris pxd pin port data strxd note 1: port outputs are configured as shown when the ccpxcon register bits pxm<1:0> = 00 and ccpxm<3:2> = 11 . 2: single pwm output requires setting at least one of the strx bits. ccpxm1 ccpxm0 ccpxm1 ccpxm0 ? 2010-2012 microchip technology inc. ds41412f-page 201 pic18(l)f2x/4xk22 14.4.7 start-up considerations when any pwm mode is used, the application hardware must use the proper external pull-up and/or pull-down resistors on the pwm output pins. the ccpxm<1:0> bits of the ccpxcon register allow the user to choose whether the pwm output signals are active-high or active-low for each pair of pwm output pins (pxa/pxc and pxb/pxd). the pwm output polarities must be selected before the pwm pin output drivers are enabled. changing the polarity configuration while the pwm pin output drivers are enable is not recommended since it may result in damage to the application circuits. the pxa, pxb, pxc and pxd output latches may not be in the proper states when the pwm module is initialized. enabling the pwm pin output drivers at the same time as the enhanced pwm modes may cause damage to the application circuit. the enhanced pwm modes must be enabled in the proper output mode and complete a full pwm cycle before enabling the pwm pin output drivers. the completion of a full pwm cycle is indicated by the tmrxif bit of the pir1, pir2 or pir5 register being set as the second pwm period begins. figure 14-19: example of steering even t at end of instruction (strxsync = 0) figure 14-20: example of steering event at beginning of instruction (strxsync = 1) note: when the microcontroller is released from reset, all of the i/o pins are in the high- impedance state. the external circuits must keep the power switch devices in the off state until the microcontroller drives the i/o pins with the proper signal levels or activates the pwm output(s). pwm p1n = pwm strx p1 pic18(l)f2x/4xk22 ds41412f-page 202 ? 2010-2012 microchip technology inc. 14.4.8 setup for eccp pwm operation using eccp1 and timer2 the following steps should be taken when configuring the eccp1 module for pwm operation using timer2: 1. configure the pwm pins to be used (p1a, p1b, p1c, and p1d): ? configure pwm outputs to be used as inputs by setting the corresponding tris bits. this prevents spurious outputs during setup. ? set the pstr1con bits for each pwm output to be used. 2. select timer2 as the period timer by configuring ccptmr0 register bits c1tsel<1:0> = ? 00 ?. 3. set the pwm period by loading the pr2 register. 4. configure auto-shutdown as off or select the source with the ccp1as<2:0> bits of the eccp1as register. 5. configure the auto-shutdown sources as needed: ? configure each comparator used. ? configure the comparator inputs as analog. ? configure the flt0 input pin and clear ansb0. 6. force a shutdown condition (off included): ? configure safe starting output levels by setting the default shutdown drive states with the pss1ac<1:0> and pss1bd<1:0> bits of the eccp1as register. ? clear the p1rsen bit of the pwm1con register. ? set the ccp1as bit of the eccp1as register. 7. configure the eccp1 module for the desired pwm mode and configuration by loading the ccp1con register with the appropriate values: ? select one of the available output configura- tions and direction with the p1m<1:0> bits. ? select the polarities of the pwm output signals with the ccp1m<3:0> bits. 8. set the 10-bit pwm duty cycle: ? load the eight msbs into the ccpr1l register. ? load the two lsbs into the dc<1:0> bits of the ccp1con register. 9. for half-bridge output mode, set the dead- band delay by loading p1dc<6:0> bits of the pwm1con register with the appropriate value. 10. configure and start tmr2: ? set the tmr2 prescale value by loading the t2ckps bits of the t2con register. ? start timer2 by setting the tmr2on bit. 11. enable the eccp1/p1a, p1b, p1c and/or p1d pin outputs by clearing the respective tris bits. 12. start the pwm: ? if shutdown auto-restart is used, then set the p1rsen bit of the pwm1con register. ? if shutdown auto-restart is not used, then clear the ccp1ase bit of the eccp1as register. ? 2010-2012 microchip technology inc. ds41412f-page 203 pic18(l)f2x/4xk22 table 14-13: registers associated with enhanced pwm name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page eccp1as ccp1ase ccp1as<2:0> pss1ac<1:0> pss1bd<1:0> 209 ccp1con p1m<1:0> dc1b<1:0> ccp1m<3:0> 205 eccp2as ccp2ase ccp2as<2:0> pss2ac<1:0> pss2bd<1:0> 209 ccp2con p2m<1:0> dc2b<1:0> ccp2m<3:0> 205 eccp3as ccp3ase ccp3as<2:0> pss3ac<1:0> pss3bd<1:0> 209 ccp3con p3m<1:0> dc3b<1:0> ccp3m<3:0> 205 ccptmrs0 c3tsel<1:0> ? c2tsel<1:0> ? c1tsel<1:0> 208 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 ipr4 ? ? ? ? ? ccp5ip ccp4ip ccp3ip 131 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pie4 ? ? ? ? ? ccp5ie ccp4ie ccp3ie 127 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pir4 ? ? ? ? ? ccp5if ccp4if ccp3if 122 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 56 pr2 timer2 period register ? pr4 timer4 period register ? pr6 timer6 period register ? pstr1con ? ? ? str1sync str1d str1c str1b str1a 210 pstr2con ? ? ? str2sync str2d str2c str2b str2a 210 pstr3con ? ? ? str3sync str3d str3c str3b str3a 210 pwm1con p1rsen p1dc<6:0> 210 pwm2con p2rsen p2dc<6:0> 210 pwm3con p3rsen p3dc<6:0> 210 t2con ? t2outps<3:0> tmr2on t2ckps<1:0> 172 t4con ? t4outps<3:0> tmr4on t4ckps<1:0> 172 t6con ? t6outps<3:0> tmr6on t6ckps<1:0> 172 tmr2 timer2 register ? tmr4 timer4 register ? tmr6 timer6 register ? trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 trise wpue3 ? ? ? ? trise2 (1) trise1 (1) trise0 (1) 156 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by enhanced pwm mode. note 1: these registers/bits are available on pic18(l)f4xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 204 ? 2010-2012 microchip technology inc. table 14-14: conf iguration registers associated with enhanced pwm name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ?p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented location, read as ? 0 ?. shaded bits are not used by enhanced pwm mode. ? 2010-2012 microchip technology inc. ds41412f-page 205 pic18(l)f2x/4xk22 14.5 register definitions: eccp control register 14-1: ccpxcon: standard ccpx control register u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? dcxb<1:0> ccpxm<3:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other reset ?1? = bit is set ?0? = bit is cleared bit 7-6 unused bit 5-4 dcxb<1:0>: pwm duty cycle least significant bits capture mode: unused compare mode: unused pwm mode: these bits are the two lsbs of the pwm duty cycle. the eight msbs are found in ccprxl. bit 3-0 ccpxm<3:0>: eccpx mode select bits 0000 = capture/compare/pwm off (resets the module) 0001 = reserved 0010 = compare mode: toggle output on match 0011 = reserved 0100 = capture mode: every falling edge 0101 = capture mode: every rising edge 0110 = capture mode: every 4th rising edge 0111 = capture mode: every 16th rising edge 1000 = compare mode: set output on compare match (ccpx pin is set, ccpxif is set) 1001 = compare mode: clear output on compare match (ccpx pin is cleared, ccpxif is set) 1010 = compare mode: generate software interrupt on compare match (ccpx pin is unaffected, ccpxif is set) 1011 = compare mode: special event trigger (ccpx pin is unaffected, ccpxif is set) timerx (selected by cxtsel bits) is reset adon is set, starting a/d conversion if a/d module is enabled (1) 11xx =: pwm mode note 1: this feature is available on ccp5 only. pic18(l)f2x/4xk22 ds41412f-page 206 ? 2010-2012 microchip technology inc. register 14-2: ccpxcon: enhanced ccpx control register r/x-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 pxm<1:0> dcxb<1:0> ccpxm<3:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other reset ?1? = bit is set ?0? = bit is cleared bit 7-6 pxm<1:0> : enhanced pwm output configuration bits if ccpxm<3:2> = 00, 01, 10 : (capture/compare modes) xx = pxa assigned as capture/compare input; pxb, pxc, pxd assigned as port pins half-bridge eccp modules (1) : if ccpxm<3:2> = 11 : (pwm modes) 0x = single output; pxa modulated; pxb assigned as port pin 1x = half-bridge output; pxa, pxb modulated with dead-band control full-bridge eccp modules (1) : if ccpxm<3:2> = 11 : (pwm modes) 00 = single output; pxa modulated; pxb, pxc, pxd assigned as port pins 01 = full-bridge output forward; pxd modulated; pxa active; pxb, pxc inactive 10 = half-bridge output; pxa, pxb modulated with dead-band control; pxc, pxd assigned as port pins 11 = full-bridge output reverse; pxb modulated; pxc active; pxa, pxd inactive bit 5-4 dcxb<1:0>: pwm duty cycle least significant bits capture mode: unused compare mode: unused pwm mode: these bits are the two lsbs of the pwm duty cycle. the eight msbs are found in ccprxl. note 1: see table 14-1 to determine full-bridge and half-bridge eccps for the device being used. ? 2010-2012 microchip technology inc. ds41412f-page 207 pic18(l)f2x/4xk22 bit 3-0 ccpxm<3:0>: eccpx mode select bits 0000 = capture/compare/pwm off (resets the module) 0001 = reserved 0010 = compare mode: toggle output on match 0011 = reserved 0100 = capture mode: every falling edge 0101 = capture mode: every rising edge 0110 = capture mode: every 4th rising edge 0111 = capture mode: every 16th rising edge 1000 = compare mode: set output on compare match (ccpx pin is set, ccpxif is set) 1001 = compare mode: clear output on compare match (ccpx pin is cleared, ccpxif is set) 1010 = compare mode: generate software interrupt on compare match (ccpx pin is unaffected, ccpxif is set) 1011 = compare mode: special event trigger (ccpx pin is unaffected, ccpxif is set) timerx is reset half-bridge eccp modules (1) : 1100 = pwm mode: pxa active-high; pxb active-high 1101 = pwm mode: pxa active-high; pxb active-low 1110 = pwm mode: pxa active-low; pxb active-high 1111 = pwm mode: pxa active-low; pxb active-low full-bridge eccp modules (1) : 1100 = pwm mode: pxa, pxc active-high; pxb, pxd active-high 1101 = pwm mode: pxa, pxc active-high; pxb, pxd active-low 1110 = pwm mode: pxa, pxc active-low; pxb, pxd active-high 1111 = pwm mode: pxa, pxc active-low; pxb, pxd active-low register 14-2: ccpxcon: enhanced ccpx control register (continued) note 1: see table 14-1 to determine full-bridge and half-bridge eccps for the device being used. pic18(l)f2x/4xk22 ds41412f-page 208 ? 2010-2012 microchip technology inc. register 14-3: ccptmrs0: pwm time r selection control register 0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 c3tsel<1:0> ? c2tsel<1:0> ? c1tsel<1:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7-6 c3tsel<1:0>: ccp3 timer selection bits 00 = ccp3 ? capture/compare modes use timer1, pwm modes use timer2 01 = ccp3 ? capture/compare modes use timer3, pwm modes use timer4 10 = ccp3 ? capture/compare modes use timer5, pwm modes use timer6 11 =reserved bit 5 unused bit 4-3 c2tsel<1:0>: ccp2 timer selection bits 00 = ccp2 ? capture/compare modes use timer1, pwm modes use timer2 01 = ccp2 ? capture/compare modes use timer3, pwm modes use timer4 10 = ccp2 ? capture/compare modes use timer5, pwm modes use timer6 11 =reserved bit 2 unused bit 1-0 c1tsel<1:0>: ccp1 timer selection bits 00 = ccp1 ? capture/compare modes use timer1, pwm modes use timer2 01 = ccp1 ? capture/compare modes use timer3, pwm modes use timer4 10 = ccp1 ? capture/compare modes use timer5, pwm modes use timer6 11 =reserved register 14-4: ccptmrs1: pwm time r selection control register 1 u-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? ? ? c5tsel<1:0> c4tsel<1:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7-4 unimplemented: read as ? 0 ? bit 3-2 c5tsel<1:0>: ccp5 timer selection bits 00 = ccp5 ? capture/compare modes use timer1, pwm modes use timer2 01 = ccp5 ? capture/compare modes use timer3, pwm modes use timer4 10 = ccp5 ? capture/compare modes use timer5, pwm modes use timer6 11 =reserved bit 1-0 c4tsel<1:0>: ccp4 timer selection bits 00 = ccp4 ? capture/compare modes use timer1, pwm modes use timer2 01 = ccp4 ? capture/compare modes use timer3, pwm modes use timer4 10 = ccp4 ? capture/compare modes use timer5, pwm modes use timer6 11 =reserved ? 2010-2012 microchip technology inc. ds41412f-page 209 pic18(l)f2x/4xk22 register 14-5: eccpxas: ccpx auto-shutdown control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ccpxase ccpxas<2:0> pssxac<1:0> pssxbd<1:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7 ccpxase: ccpx auto-shutdown event status bit if pxrsen = 1 ; 1 = an auto-shutdown event occurred; ccpxase bit will automatically clear when event goes away; ccpx outputs in shutdown state 0 = ccpx outputs are operating if pxrsen = 0 ; 1 = an auto-shutdown event occurred; bit must be cleared in software to restart pwm; ccpx outputs in shutdown state 0 = ccpx outputs are operating bit 6-4 ccpxas<2:0>: ccpx auto-shutdown source select bits (1) 000 = auto-shutdown is disabled 001 = comparator c1 (async_c1out) ? output high will cause shutdown event 010 = comparator c2 (async_c2out) ? output high will cause shutdown event 011 = either comparator c1 or c2 ? output high will cause shutdown event 100 = flt0 pin - low level will cause shutdown event 101 = flt0 pin or comparator c1 (async_c1out) ? low level will cause shutdown event 110 = flt0 pin or comparator c2 (async_c2out) ? low level will cause shutdown event 111 = flt0 pin or comparators c1 or c2 ? low level will cause shutdown event bit 3-2 pssxac<1:0>: pins pxa and pxc shutdown state control bits 00 = drive pins pxa and pxc to ? 0 ? 01 = drive pins pxa and pxc to ? 1 ? 1x = pins pxa and pxc tri-state bit 1-0 pssxbd<1:0>: pins pxb and pxd shutdown state control bits 00 = drive pins pxb and pxd to ? 0 ? 01 = drive pins pxb and pxd to ? 1 ? 1x = pins pxb and pxd tri-state note 1: if c1sync or c2sync bits in the cm2con1 register are enabled, the shutdown will be delayed by timer1. pic18(l)f2x/4xk22 ds41412f-page 210 ? 2010-2012 microchip technology inc. register 14-6: pwmxcon: enhanced pwm control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 pxrsen pxdc<6:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7 pxrsen: pwm restart enable bit 1 = upon auto-shutdown, the ccpxase bit clears automatically once the shutdown event goes away; the pwm restarts automatically 0 = upon auto-shutdown, ccpxase must be cleared in software to restart the pwm bit 6-0 pxdc<6:0>: pwm delay count bits pxdcx = number of f osc /4 (4 * t osc ) cycles between the scheduled time when a pwm signal should transition active and the actual time it transitions active register 14-7: pstrxcon: pwm steering control register (1) u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-1 ? ? ? strxsync strxd strxc strxb strxa bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7-5 unimplemented: read as ? 0 ? bit 4 strxsync: steering sync bit 1 = output steering update occurs on next pwm period 0 = output steering update occurs at the beginning of the instruction cycle boundary bit 3 strxd: steering enable bit d 1 = pxd pin has the pwm waveform with polarity control from ccpxm<1:0> 0 = pxd pin is assigned to port pin bit 2 strxc: steering enable bit c 1 = pxc pin has the pwm waveform with polarity control from ccpxm<1:0> 0 = pxc pin is assigned to port pin bit 1 strxb: steering enable bit b 1 = pxb pin has the pwm waveform with polarity control from ccpxm<1:0> 0 = pxb pin is assigned to port pin bit 0 strxa: steering enable bit a 1 = pxa pin has the pwm waveform with polarity control from ccpxm<1:0> 0 = pxa pin is assigned to port pin note 1: the pwm steering mode is available only when the ccpxcon register bits ccpxm<3:2> = 11 and pxm<1:0> = 00 . ? 2010-2012 microchip technology inc. ds41412f-page 211 pic18(l)f2x/4xk22 15.0 master synchronous serial port (mssp1 and mssp2) module 15.1 master sspx (msspx) module overview the master synchronous serial port (msspx) module is a serial interface useful for communicating with other peripheral or microcontroller devices. these peripheral devices may be serial eeproms, shift registers, display drivers, a/d converters, etc. the msspx module can operate in one of two modes: ? serial peripheral interface (spi) ? inter-integrated circuit (i 2 c?) the spi interface supports the following modes and features: ?master mode ? slave mode ? clock parity ? slave select synchronization (slave mode only) ? daisy chain connection of slave devices figure 15-1 is a block diagram of the spi interface module. figure 15-1: msspx blo ck diagram (spi mode) ( ) read write data bus sspxsr reg sspxm<3:0> bit 0 shift clock ssx control enable edge select clock select tmr2 output t osc prescaler 4, 16, 64 2 edge select 2 (ckp, cke) 4 tris bit sdox sspxbuf reg sdix ssx sckx baud rate generator (sspxadd) pic18(l)f2x/4xk22 ds41412f-page 212 ? 2010-2012 microchip technology inc. the i 2 c interface supports the following modes and features: ?master mode ? slave mode ? byte nacking (slave mode) ? limited multi-master support ? 7-bit and 10-bit addressing ? start and stop interrupts ? interrupt masking ? clock stretching ? bus collision detection ? general call address matching ?address masking ? address hold and data hold modes ? selectable sdax hold times figure 15-2 is a block diagram of the i 2 c interface module in master mode. figure 15-3 is a diagram of the i 2 c interface module in slave mode. the pic18(l)f2x/4xk22 has two mssp modules, mssp1 and mssp2, each module operating indepen- dently from the other. figure 15-2: msspx block diagram (i 2 c? master mode) note 1: in devices with more than one mssp module, it is very important to pay close attention to sspxconx register names. ssp1con1 and ssp1con2 registers control different operational aspects of the same module, while ssp1con1 and ssp2con1 control the same features for two different modules. 2: throughout this section, generic references to an mssp module in any of its operating modes may be interpreted as being equally applicable to mssp1 or mssp2. register names, module i/o signals, and bit names may use the generic designator ?x? to indicate the use of a numeral to distinguish a particular module when required. read write sspxsr start bit, stop bit, start bit detect, sspxbuf internal data bus set/reset: s, p, sspxstat, wcol, sspxov shift clock msb lsb sdax acknowledge generate (sspxcon2) stop bit detect write collision detect clock arbitration state counter for end of xmit/rcv sclx sclx in bus collision sdax in receive enable (rcen) clock cntl clock arbitrate/bcol detect (hold off clock source) [sspxm 3:0] baud rate reset sen, pen (sspxcon2) generator (sspxadd) address match detect set sspxif, bclxif ? 2010-2012 microchip technology inc. ds41412f-page 213 pic18(l)f2x/4xk22 figure 15-3: msspx block diagram (i 2 c? slave mode) read write sspxsr reg match detect sspxadd reg start and stop bit detect sspxbuf reg internal data bus addr match set, reset s, p bits (sspxstat reg) sclx sdax shift clock msb lsb sspxmsk reg pic18(l)f2x/4xk22 ds41412f-page 214 ? 2010-2012 microchip technology inc. 15.2 spi mode overview the serial peripheral interface (spi) bus is a synchronous serial data communication bus that operates in full-duplex mode. devices communicate in a master/slave environment where the master device initiates the communication. a slave device is controlled through a chip select known as slave select. the spi bus specifies four signal connections: ? serial clock (sckx) ? serial data out (sdox) ? serial data in (sdix) ? slave select (ssx ) figure 15-1 shows the block diagram of the msspx module when operating in spi mode. the spi bus operates with a single master device and one or more slave devices. when multiple slave devices are used, an independent slave select connection is required from the master device to each slave device. figure 15-4 shows a typical connection between a master device and multiple slave devices. the master selects only one slave at a time. most slave devices have tri-state outputs so their output signal appears disconnected from the bus when they are not selected. transmissions involve two shift registers, eight bits in size, one in the master and one in the slave. with either the master or the slave device, data is always shifted out one bit at a time, with the most significant bit (msb) shifted out first. at the same time, a new least significant bit (lsb) is shifted into the same register. figure 15-5 shows a typical connection between two processors configured as master and slave devices. data is shifted out of both shift registers on the programmed clock edge and latched on the opposite edge of the clock. the master device transmits information out on its sdox output pin which is connected to, and received by, the slave?s sdix input pin. the slave device trans- mits information out on its sdox output pin, which is connected to, and received by, the master?s sdix input pin. to begin communication, the master device first sends out the clock signal. both the master and the slave devices should be configured for the same clock polarity. the master device starts a transmission by sending out the msb from its shift register. the slave device reads this bit from that same line and saves it into the lsb position of its shift register. during each spi clock cycle, a full-duplex data transmission occurs. this means that at the same time, the slave device is sending out the msb from its shift register and the master device is reading this bit from that same line and saving it as the lsb of its shift register. after 8 bits have been shifted out, the master and slave have exchanged register values. if there is more data to exchange, the shift registers are loaded with new data and the process repeats itself. whether the data is meaningful or not (dummy data), depends on the application software. this leads to three scenarios for data transmission: ? master sends useful data and slave sends dummy data. ? master sends useful data and slave sends useful data. ? master sends dummy data and slave sends useful data. transmissions may involve any number of clock cycles. when there is no more data to be transmitted, the master stops sending the clock signal and it deselects the slave. every slave device connected to the bus that has not been selected through its slave select line must disre- gard the clock and transmission signals and must not transmit out any data of its own. ? 2010-2012 microchip technology inc. ds41412f-page 215 pic18(l)f2x/4xk22 figure 15-4: spi master and multiple slave connection 15.2.1 spi mode registers the msspx module has five registers for spi mode operation. these are: ? msspx status register (sspxstat) ? msspx control register 1 (sspxcon1) ? msspx control register 3 (sspxcon3) ? msspx data buffer register (sspxbuf) ? msspx address register (sspxadd) ? msspx shift register (sspxsr) (not directly accessible) sspxcon1 and sspxstat are the control and status registers in spi mode operation. the sspxcon1 register is readable and writable. the lower 6 bits of the sspxstat are read-only. the upper two bits of the sspxstat are read/write. in one spi master mode, sspxadd can be loaded with a value used in the baud rate generator. more information on the baud rate generator is available in section 15.7 ?baud rate generator? . sspxsr is the shift register used for shifting data in and out. sspxbuf provides indirect access to the sspxsr register. sspxbuf is the buffer register to which data bytes are written, and from which data bytes are read. in receive operations, sspxsr and sspxbuf together create a buffered receiver. when sspxsr receives a complete byte, it is transferred to sspxbuf and the sspxif interrupt is set. during transmission, the sspxbuf is not buffered. a write to sspxbuf will write to both sspxbuf and sspxsr. 15.2.2 spi mode operation when initializing the spi, several options need to be specified. this is done by programming the appropriate control bits (sspxcon1<5:0> and sspxstat<7:6>). these control bits allow the following to be specified: ? master mode (sckx is the clock output) ? slave mode (sckx is the clock input) ? clock polarity (idle state of sckx) ? data input sample phase (middle or end of data output time) ? clock edge (output data on rising/falling edge of sckx) ? clock rate (master mode only) ? slave select mode (slave mode only) to enable the serial port, sspx enable bit, sspxen of the sspxcon1 register, must be set. to reset or recon- figure spi mode, clear the sspxen bit, re-initialize the sspxconx registers and then set the sspxen bit. this configures the sdix, sdox, sckx and ssx pins as serial port pins. for the pins to behave as the serial port function, some must have their data direction bits (in the tris register) appropriately programmed as fol- lows: ? sdix must have corresponding tris bit set ? sdox must have corresponding tris bit cleared ? sckx (master mode) must have corresponding tris bit cleared ? sckx (slave mode) must have corresponding tris bit set ? ssx must have corresponding tris bit set spi master sclk sdox sdix general i/o general i/o general i/o sclk sdix sdox ssx spi slave #1 sclk sdix sdox ssx spi slave #2 sclk sdix sdox ssx spi slave #3 pic18(l)f2x/4xk22 ds41412f-page 216 ? 2010-2012 microchip technology inc. any serial port function that is not desired may be overridden by programming the corresponding data direction (tris) register to the opposite value. the msspx consists of a transmit/receive shift register (sspxsr) and a buffer register (sspxbuf). the sspxsr shifts the data in and out of the device, msb first. the sspxbuf holds the data that was written to the sspxsr until the received data is ready. once the 8 bits of data have been received, that byte is moved to the sspxbuf register. then, the buffer full detect bit, bf of the sspxstat register, and the interrupt flag bit, sspxif, are set. this double-buffering of the received data (sspxbuf) allows the next byte to start reception before reading the data that was just received. any write to the sspxbuf register during transmission/ reception of data will be ignored and the write collision detect bit, wcol of the sspxcon1 register, will be set. user software must clear the wcol bit to allow the following write(s) to the sspxbuf register to complete successfully. when the application software is expecting to receive valid data, the sspxbuf should be read before the next byte of data to transfer is written to the sspxbuf. the buffer full bit, bf of the sspxstat register, indicates when sspxbuf has been loaded with the received data (transmission is complete). when the sspxbuf is read, the bf bit is cleared. this data may be irrelevant if the spi is only a transmitter. generally, the msspx interrupt is used to determine when the transmission/reception has completed. if the interrupt method is not going to be used, then software polling can be done to ensure that a write collision does not occur. figure 15-5: spi mast er/slave connection serial input buffer (buf) shift register (sspxsr) msb lsb sdox sdix processor 1 sckx spi master sspxm<3:0> = 00xx serial input buffer (sspxbuf) shift register (sspxsr) lsb msb sdix sdox processor 2 sckx spi slave sspxm<3:0> = 010x serial clock ssx slave select general i/o (optional) = 1010 ? 2010-2012 microchip technology inc. ds41412f-page 217 pic18(l)f2x/4xk22 15.2.3 spi master mode the master can initiate the data transfer at any time because it controls the sckx line. the master determines when the slave (processor 2, figure 15-5 ) is to broadcast data by the software protocol. in master mode, the data is transmitted/received as soon as the sspxbuf register is written to. if the spi is only going to receive, the sdox output could be dis- abled (programmed as an input). the sspxsr register will continue to shift in the signal present on the sdix pin at the programmed clock rate. as each byte is received, it will be loaded into the sspxbuf register as if a normal received byte (interrupts and status bits appropriately set). the clock polarity is selected by appropriately programming the ckp bit of the sspxcon1 register and the cke bit of the sspxstat register. this then, would give waveforms for spi communication as shown in figure 15-6 , figure 15-8 , figure 15-9 and figure 15-10 , where the msb is transmitted first. in master mode, the spi clock rate (bit rate) is user programmable to be one of the following: ?f osc /4 (or t cy ) ?f osc /16 (or 4 * t cy ) ?f osc /64 (or 16 * t cy ) ? timer2 output/2 ?f osc /(4 * (sspxadd + 1)) figure 15-6 shows the waveforms for master mode. when the cke bit is set, the sdox data is valid before there is a clock edge on sckx. the change of the input sample is shown based on the state of the smp bit. the time when the sspxbuf is loaded with the received data is shown. figure 15-6: spi mode waveform (master mode) sckx (ckp = 0 sckx (ckp = 1 sckx (ckp = 0 sckx (ckp = 1 4 clock modes input sample input sample sdix bit 7 bit 0 sdox bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 bit 7 sdix sspxif (smp = 1 ) (smp = 0 ) (smp = 1 ) cke = 1 ) cke = 0 ) cke = 1 ) cke = 0 ) (smp = 0 ) write to sspxbuf sspxsr to sspxbuf sdox bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 (cke = 0 ) (cke = 1 ) bit 0 pic18(l)f2x/4xk22 ds41412f-page 218 ? 2010-2012 microchip technology inc. 15.2.4 spi slave mode in slave mode, the data is transmitted and received as external clock pulses appear on sckx. when the last bit is latched, the sspxif interrupt flag bit is set. before enabling the module in spi slave mode, the clock line must match the proper idle state. the clock line can be observed by reading the sckx pin. the idle state is determined by the ckp bit of the sspxcon1 register. while in slave mode, the external clock is supplied by the external clock source on the sckx pin. this exter- nal clock must meet the minimum high and low times as specified in the electrical specifications. while in sleep mode, the slave can transmit/receive data. the shift register is clocked from the sckx pin input and when a byte is received, the device will gen- erate an interrupt. if enabled, the device will wake-up from sleep. 15.2.4.1 daisy-chain configuration the spi bus can sometimes be connected in a daisy- chain configuration. the first slave output is connected to the second slave input, the second slave output is connected to the third slave input, and so on. the final slave output is connected to the master input. each slave sends out, during a second group of clock pulses, an exact copy of what was received during the first group of clock pulses. the whole chain acts as one large communication shift register. the daisy- chain feature only requires a single slave select line from the master device. figure 15-7 shows the block diagram of a typical daisy-chain connection when operating in spi mode. in a daisy-chain configuration, only the most recent byte on the bus is required by the slave. setting the boen bit of the sspxcon3 register will enable writes to the sspxbuf register, even if the previous byte has not been read. this allows the software to ignore data that may not apply to it. 15.2.5 slave select synchronization the slave select can also be used to synchronize communication. the slave select line is held high until the master device is ready to communicate. when the slave select line is pulled low, the slave knows that a new transmission is starting. if the slave fails to receive the communication properly, it will be reset at the end of the transmission, when the slave select line returns to a high state. the slave is then ready to receive a new transmission when the slave select line is pulled low again. if the slave select line is not used, there is a risk that the slave will even- tually become out of sync with the master. if the slave misses a bit, it will always be one bit off in future trans- missions. use of the slave select line allows the slave and master to align themselves at the beginning of each transmission ( figure 15-8 ). the ssx pin allows a synchronous slave mode. the spi must be in slave mode with ssx pin control enabled (sspxcon1<3:0> = 0100 ). when the ssx pin is low, transmission and reception are enabled and the sdox pin is driven. when the ssx pin goes high, the sdox pin is no longer driven, even if in the middle of a transmitted byte and becomes a floating output. external pull-up/pull-down resistors may be desirable depending on the applica- tion. when the spi module resets, the bit counter is forced to ? 0 ?. this can be done by either forcing the ssx pin to a high level or clearing the sspxen bit. note 1: when the spi is in slave mode with ssx pin control enabled (sspxcon1<3:0> = 0100 ), the spi module will reset if the ssx pin is set to v dd . 2: when the spi is used in slave mode with cke set; the user must enable ssx pin control. 3: while operated in spi slave mode the smp bit of the sspxstat register must remain clear. ? 2010-2012 microchip technology inc. ds41412f-page 219 pic18(l)f2x/4xk22 figure 15-7: spi daisy-chain connection figure 15-8: slave sele ct synchronous waveform spi master sclk sdox sdix general i/o sclk sdix sdox ssx spi slave #1 sclk sdix sdox ssx spi slave #2 sclk sdix sdox ssx spi slave #3 sckx (ckp = 1 sckx (ckp = 0 input sample sdix bit 7 sdox bit 7 bit 6 bit 7 sspxif interrupt cke = 0 ) cke = 0 ) write to sspxbuf sspxsr to sspxbuf ssx flag bit 0 bit 7 bit 0 bit 6 sspxbuf to sspxsr shift register sspxsr and bit count are reset pic18(l)f2x/4xk22 ds41412f-page 220 ? 2010-2012 microchip technology inc. figure 15-9: spi mode wavefo rm (slave mode with cke = 0 ) figure 15-10: spi mode waveform (slave mode with cke = 1 ) sckx (ckp = 1 sckx (ckp = 0 input sample sdix bit 7 sdox bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 sspxif interrupt cke = 0 ) cke = 0 ) write to sspxbuf sspxsr to sspxbuf ssx flag optional bit 0 detection active write collision valid sckx (ckp = 1 sckx (ckp = 0 input sample sdix bit 7 bit 0 sdox bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 sspxif interrupt cke = 1 ) cke = 1 ) write to sspxbuf sspxsr to sspxbuf ssx flag not optional write collision detection active valid ? 2010-2012 microchip technology inc. ds41412f-page 221 pic18(l)f2x/4xk22 15.2.6 spi operation in sleep mode in spi master mode, module clocks may be operating at a different speed than when in full-power mode; in the case of the sleep mode, all clocks are halted. special care must be taken by the user when the msspx clock is much faster than the system clock. in slave mode, when msspx interrupts are enabled, after the master completes sending data, an msspx interrupt will wake the controller from sleep. if an exit from sleep mode is not desired, msspx interrupts should be disabled. in spi master mode, when the sleep mode is selected, all module clocks are halted and the transmission/ reception will remain in that state until the device wakes. after the device returns to run mode, the module will resume transmitting and receiving data. in spi slave mode, the spi transmit/receive shift register operates asynchronously to the device. this allows the device to be placed in sleep mode and data to be shifted into the spi transmit/receive shift register. when all eight bits have been received, the msspx interrupt flag bit will be set and if enabled, will wake the device. table 15-1: registers associ ated with spi operation name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ansela ? ? ansa5 ? ansa3 ansa2 ansa1 ansa0 154 anselb ? ? ansb5 ansb4 ansb3 (1) ansb2 (1) ansb1 (1) ansb0 (1) 155 anselc ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ? ? 155 anseld ansd7 ansd6 ansd5 ansd4 (2) ansd3 (2) ansd2 ansd1 (2) ansd0 (2) 155 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 56 ssp1buf ssp1 receive buffer/transmit register ? ssp1con1 wcol sspov sspen ckp sspm<3:0> 260 ssp1con3 acktim pcie scie boen sdaht sbcde ahen dhen 263 ssp1stat smp cke d/a p s r/w ua bf 259 ssp2buf ssp2 receive buffer/transmit register ? ssp2con1 wcol sspov sspen ckp sspm<3:0> 260 ssp2con3 acktim pcie scie boen sdaht sbcde ahen dhen 263 ssp2stat smp cke d/a p s r/w ua bf 259 trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 trisb trisb7 trisb6 trisb5 trisb4 trisb3 (1) trisb2 (1) trisb1 (1) trisb0 (1) 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd trisd7 trisd6 trisd5 trisd4 (2) trisd3 (2) trisd2 trisd1 (2) trisd0 (2) 156 legend: shaded bits are not used by the msspx in spi mode. note 1: pic18(l)f2xk22 devices. 2: pic18(l)f4xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 222 ? 2010-2012 microchip technology inc. 15.3 i 2 c mode overview the inter-integrated circuit bus (i 2 c) is a multi-master serial data communication bus. devices communicate in a master/slave environment where the master devices initiate the communication. a slave device is controlled through addressing. the i 2 c bus specifies two signal connections: ? serial clock (sclx) ? serial data (sdax) figure 15-2 shows the block diagram of the msspx module when operating in i 2 c mode. both the sclx and sdax connections are bidirectional open-drain lines, each requiring pull-up resistors for the supply voltage. pulling the line to ground is considered a logical zero and letting the line float is considered a logical one. figure 15-11 shows a typical connection between two processors configured as master and slave devices. the i 2 c bus can operate with one or more master devices and one or more slave devices. there are four potential modes of operation for a given device: ? master transmit mode (master is transmitting data to a slave) ? master receive mode (master is receiving data from a slave) ?slave transmit mode (slave is transmitting data to a master) ? slave receive mode (slave is receiving data from the master) to begin communication, a master device starts out in master transmit mode. the master device sends out a start bit followed by the address byte of the slave it intends to communicate with. this is followed by a sin- gle read/write bit, which determines whether the mas- ter intends to transmit to or receive data from the slave device. if the requested slave exists on the bus, it will respond with an acknowledge bit, otherwise known as an ack . the master then continues in either transmit mode or receive mode and the slave continues in the comple- ment, either in receive mode or transmit mode, respectively. a start bit is indicated by a high-to-low transition of the sdax line while the sclx line is held high. address and data bytes are sent out, most significant bit (msb) first. the read/write bit is sent out as a logical one when the master intends to read data from the slave, and is sent out as a logical zero when it intends to write data to the slave. figure 15-11: i 2 c? master/ slave connection the acknowledge bit (ack ) is an active-low signal, which holds the sdax line low to indicate to the transmitter that the slave device has received the transmitted data and is ready to receive more. the transition of data bits is always performed while the sclx line is held low. transitions that occur while the sclx line is held high are used to indicate start and stop bits. if the master intends to write to the slave, then it repeatedly sends out a byte of data, with the slave responding after each byte with an ack bit. in this example, the master device is in master transmit mode and the slave is in slave receive mode. if the master intends to read from the slave, then it repeatedly receives a byte of data from the slave, and responds after each byte with an ack bit. in this example, the master device is in master receive mode and the slave is slave transmit mode. on the last byte of data communicated, the master device may end the transmission by sending a stop bit. if the master device is in receive mode, it sends the stop bit in place of the last ack bit. a stop bit is indicated by a low-to-high transition of the sdax line while the sclx line is held high. in some cases, the master may want to maintain con- trol of the bus and re-initiate another transmission. if so, the master device may send another start bit in place of the stop bit or last ack bit when it is in receive mode. the i 2 c bus specifies three message protocols; ? single message where a master writes data to a slave. ? single message where a master reads data from a slave. ? combined message where a master initiates a minimum of two writes, or two reads, or a combination of writes and reads, to one or more slaves. master sclk sdix sclk sdox slave v dd v dd ? 2010-2012 microchip technology inc. ds41412f-page 223 pic18(l)f2x/4xk22 when one device is transmitting a logical one, or letting the line float, and a second device is transmitting a logical zero, or holding the line low, the first device can detect that the line is not a logical one. this detection, when used on the sclx line, is called clock stretching. clock stretching give slave devices a mechanism to control the flow of data. when this detection is used on the sdax line, it is called arbitration. arbitration ensures that there is only one master device communicating at any single time. 15.3.1 clock stretching when a slave device has not completed processing data, it can delay the transfer of more data through the process of clock stretching. an addressed slave device may hold the sclx clock line low after receiving or sending a bit, indicating that it is not yet ready to continue. the master that is communicating with the slave will attempt to raise the sclx line in order to transfer the next bit, but will detect that the clock line has not yet been released. because the sclx connection is open-drain, the slave has the ability to hold that line low until it is ready to continue communicating. clock stretching allows receivers that cannot keep up with a transmitter to control the flow of incoming data. 15.3.2 arbitration each master device must monitor the bus for start and stop bits. if the device detects that the bus is busy, it cannot begin a new message until the bus returns to an idle state. however, two master devices may try to initiate a transmission on or about the same time. when this occurs, the process of arbitration begins. each transmitter checks the level of the sdax data line and compares it to the level that it expects to find. the first transmitter to observe that the two levels don?t match, loses arbitration, and must stop transmitting on the sdax line. for example, if one transmitter holds the sdax line to a logical one (lets it float) and a second transmitter holds it to a logical zero (pulls it low), the result is that the sdax line will be low. the first transmitter then observes that the level of the line is different than expected and concludes that another transmitter is communicating. the first transmitter to notice this difference is the one that loses arbitration and must stop driving the sdax line. if this transmitter is also a master device, it also must stop driving the sclx line. it then can monitor the lines for a stop condition before trying to reissue its transmission. in the meantime, the other device that has not noticed any difference between the expected and actual levels on the sdax line continues with its original transmission. it can do so without any compli- cations, because so far, the transmission appears exactly as expected with no other transmitter disturbing the message. slave transmit mode can also be arbitrated, when a master addresses multiple slaves, but this is less common. if two master devices are sending a message to two different slave devices at the address stage, the master sending the lower slave address always wins arbitration. when two master devices send messages to the same slave address, and addresses can sometimes refer to multiple slaves, the arbitration process must continue into the data stage. arbitration usually occurs very rarely, but it is a necessary process for proper multi-master support. pic18(l)f2x/4xk22 ds41412f-page 224 ? 2010-2012 microchip technology inc. 15.4 i 2 c mode operation all msspx i 2 c communication is byte oriented and shifted out msb first. six sfr registers and 2 interrupt flags interface the module with the pic ? microcon- troller and user software. two pins, sdax and sclx, are exercised by the module to communicate with other external i 2 c devices. 15.4.1 byte format all communication in i 2 c is done in 9-bit segments. a byte is sent from a master to a slave or vice-versa, followed by an acknowledge bit sent back. after the 8th falling edge of the sclx line, the device outputting data on the sdax changes that pin to an input and reads in an acknowledge value on the next clock pulse. the clock signal, sclx, is provided by the master. data is valid to change while the sclx signal is low, and sampled on the rising edge of the clock. changes on the sdax line while the sclx line is high define special conditions on the bus, explained below. 15.4.2 definition of i 2 c terminology there is language and terminology in the description of i 2 c communication that have definitions specific to i 2 c. that word usage is defined below and may be used in the rest of this document without explana- tion. this table was adapted from the phillips i 2 c specification. 15.4.3 sdax and sclx pins selection of any i 2 c mode with the sspxen bit set, forces the sclx and sdax pins to be open-drain. these pins should be set by the user to inputs by setting the appropriate tris bits. 15.4.4 sdax hold time the hold time of the sdax pin is selected by the sdaht bit of the sspxcon3 register. hold time is the time sdax is held valid after the falling edge of sclx. setting the sdaht bit selects a longer 300 ns mini- mum hold time and may help on buses with large capacitance. table 15-2: i 2 c? bus terms note: data is tied to output zero when an i 2 c mode is enabled. term description transmitter the device which shifts data out onto the bus. receiver the device which shifts data in from the bus. master the device that initiates a transfer, generates clock signals and termi- nates a transfer. slave the device addressed by the mas- ter. multi-master a bus with more than one device that can initiate data transfers. arbitration procedure to ensure that only one master at a time controls the bus. winning arbitration ensures that the message is not corrupted. synchronization procedure to synchronize the clocks of two or more devices on the bus. idle no master is controlling the bus, and both sdax and sclx lines are high. active any time one or more master devices are controlling the bus. addressed slave slave device that has received a matching address and is actively being clocked by a master. matching address address byte that is clocked into a slave that matches the value stored in sspxadd. write request slave receives a matching address with r/w bit clear, and is ready to clock in data. read request master sends an address byte with the r/w bit set, indicating that it wishes to clock data out of the slave. this data is the next and all following bytes until a restart or stop. clock stretching when a device on the bus holds sclx low to stall communication. bus collision any time the sdax line is sampled low by the module while it is out- putting and expected high state. ? 2010-2012 microchip technology inc. ds41412f-page 225 pic18(l)f2x/4xk22 15.4.5 start condition the i 2 c specification defines a start condition as a transition of sdax from a high-to -low state while sclx line is high. a start condition is always generated by the master and signifies the transition of the bus from an idle to an active state. figure 15-12 shows wave forms for start and stop conditions. a bus collision can occur on a start condition if the module samples the sdax line low before asserting it low. this does not conform to the i 2 c specification that states no bus collision can occur on a start. 15.4.6 stop condition a stop condition is a transition of the sdax line from a low-to-high state while the sclx line is high. 15.4.7 restart condition a restart is valid any time that a stop would be valid. a master can issue a restart if it wishes to hold the bus after terminating the current transfer. a restart has the same effect on the slave that a start would, resetting all slave logic and preparing it to clock in an address. the master may want to address the same or another slave. figure 15-13 shows the wave form for a restart condition. in 10-bit addressing slave mode a restart is required for the master to clock data out of the addressed slave. once a slave has been fully addressed, matching both high and low address bytes, the master can issue a restart and the high address byte with the r/w bit set. the slave logic will then hold the clock and prepare to clock out data. after a full match with r/w clear in 10-bit mode, a prior match flag is set and maintained. until a stop condition, a high address with r/w clear, or high address match fails. 15.4.8 start/stop condition interrupt masking the scie and pcie bits of the sspxcon3 register can enable the generation of an interrupt in slave modes that do not typically support this function. slave modes where interrupt on start and stop detect are already enabled, these bits will have no effect. figure 15-12: i 2 c? start and stop conditions figure 15-13: i 2 c? restart condition note: at least one sclx low time must appear before a stop is valid, therefore, if the sdax line goes low then high again while the sclx line stays high, only the start condition is detected. sdax sclx p stop condition s start condition change of data allowed change of data allowed restart condition sr change of data allowed change of data allowed pic18(l)f2x/4xk22 ds41412f-page 226 ? 2010-2012 microchip technology inc. 15.4.9 acknowledge sequence the 9th sclx pulse for any transferred byte in i 2 c is dedicated as an acknowledge. it allows receiving devices to respond back to the transmitter by pulling the sdax line low. the transmitter must release con- trol of the line during this time to shift in the response. the acknowledge (ack ) is an active-low signal, pull- ing the sdax line low indicated to the transmitter that the device has received the transmitted data and is ready to receive more. the result of an ack is placed in the ackstat bit of the sspxcon2 register. slave software, when the ahen and dhen bits are set, allow the user to set the ack value sent back to the transmitter. the ackdt bit of the sspxcon2 register is set/cleared to determine the response. slave hardware will generate an ack response if the ahen and dhen bits of the sspxcon3 register are clear. there are certain conditions where an ack will not be sent by the slave. if the bf bit of the sspxstat register or the sspxov bit of the sspxcon1 register are set when a byte is received. when the module is addressed, after the 8th falling edge of sclx on the bus, the acktim bit of the sspxcon3 register is set. the acktim bit indicates the acknowledge time of the active bus. the acktim status bit is only active when the ahen bit or dhen bit is enabled. 15.5 i 2 c slave mode operation the msspx slave mode operates in one of four modes selected in the sspxm bits of sspxcon1 register. the modes can be divided into 7-bit and 10-bit addressing mode. 10-bit addressing modes operate the same as 7-bit with some additional overhead for handling the larger addresses. modes with start and stop bit interrupts operated the same as the other modes with sspxif additionally getting set upon detection of a start, restart, or stop condition. 15.5.1 slave mode addresses the sspxadd register ( register 15-6 ) contains the slave mode address. the first byte received after a start or restart condition is compared against the value stored in this register. if the byte matches, the value is loaded into the sspxbuf register and an interrupt is generated. if the value does not match, the module goes idle and no indication is given to the software that anything happened. the sspx mask register ( register 15-5 ) affects the address matching process. see section 15.5.9 ?sspx mask register? for more information. 15.5.1.1 i 2 c slave 7-bit addressing mode in 7-bit addressing mode, the lsb of the received data byte is ignored when determining if there is an address match. 15.5.1.2 i 2 c slave 10-bit addressing mode in 10-bit addressing mode, the first received byte is compared to the binary value of ?1 1 1 1 0 a9 a8 0?. a9 and a8 are the two msb of the 10-bit address and stored in bits 2 and 1 of the sspxadd register. after the acknowledge of the high byte the ua bit is set and sclx is held low until the user updates sspxadd with the low address. the low address byte is clocked in and all eight bits are compared to the low address value in sspxadd. even if there is not an address match; sspxif and ua are set, and sclx is held low until sspxadd is updated to receive a high byte again. when sspxadd is updated the ua bit is cleared. this ensures the module is ready to receive the high address byte on the next communication. a high and low address match as a write request is required at the start of all 10-bit addressing communication. a transmission can be initiated by issuing a restart once the slave is addressed, and clocking in the high address with the r/w bit set. the slave hardware will then acknowledge the read request and prepare to clock out data. this is only valid for a slave after it has received a complete high and low address byte match. ? 2010-2012 microchip technology inc. ds41412f-page 227 pic18(l)f2x/4xk22 15.5.2 slave reception when the r/w bit of a matching received address byte is clear, the r/w bit of the sspxstat register is cleared. the received address is loaded into the sspxbuf register and acknowledged. when the overflow condition exists for a received address, then not acknowledge is given. an overflow condition is defined as either bit bf of the sspxstat register is set, or bit sspxov of the sspxcon1 regis- ter is set. the boen bit of the sspxcon3 register modifies this operation. for more information see register 15-4 . an msspx interrupt is generated for each transferred data byte. flag bit, sspxif, must be cleared by software. when the sen bit of the sspxcon2 register is set, sclx will be held low (clock stretch) following each received byte. the clock must be released by setting the ckp bit of the sspxcon1 register, except sometimes in 10-bit mode. see section 15.2.3 ?spi master mode? for more detail. 15.5.2.1 7-bit addressing reception this section describes a standard sequence of events for the msspx module configured as an i 2 c slave in 7-bit addressing mode. all decisions made by hard- ware or software and their effect on reception. figure 15-14 and figure 15-15 are used as a visual reference for this description. this is a step by step process of what typically must be done to accomplish i 2 c communication. 1. start bit detected. 2. s bit of sspxstat is set; sspxif is set if interrupt on start detect is enabled. 3. matching address with r/w bit clear is received. 4. the slave pulls sdax low sending an ack to the master, and sets sspxif bit. 5. software clears the sspxif bit. 6. software reads received address from sspxbuf clearing the bf flag. 7. if sen = 1 ; slave software sets ckp bit to release the sclx line. 8. the master clocks out a data byte. 9. slave drives sdax low sending an ack to the master, and sets sspxif bit. 10. software clears sspxif. 11. software reads the received byte from sspxbuf clearing bf. 12. steps 8-12 are repeated for all received bytes from the master. 13. master sends stop condition, setting p bit of sspxstat, and the bus goes idle. 15.5.2.2 7-bit reception with ahen and dhen slave device reception with ahen and dhen set operate the same as without these options with extra interrupts and clock stretching added after the 8th fall- ing edge of sclx. these additional interrupts allow the slave software to decide whether it wants to ack the receive address or data byte, rather than the hard- ware. this functionality adds support for pmbus? that was not present on previous versions of this module. this list describes the steps that need to be taken by slave software to use these options for i 2 c communication. figure 15-16 displays a module using both address and data holding. figure 15-17 includes the operation with the sen bit of the sspxcon2 register set. 1. s bit of sspxstat is set; sspxif is set if interrupt on start detect is enabled. 2. matching address with r/w bit clear is clocked in. sspxif is set and ckp cleared after the 8th falling edge of sclx. 3. slave clears the sspxif. 4. slave can look at the acktim bit of the sspxcon3 register to determine if the sspxif was after or before the ack. 5. slave reads the address value from sspxbuf, clearing the bf flag. 6. slave sets ack value clocked out to the master by setting ackdt. 7. slave releases the clock by setting ckp. 8. sspxif is set after an ack , not after a nack. 9. if sen = 1 the slave hardware will stretch the clock after the ack. 10. slave clears sspxif. 11. sspxif set and ckp cleared after 8th falling edge of sclx for a received data byte. 12. slave looks at acktim bit of sspxcon3 to determine the source of the interrupt. 13. slave reads the received data from sspxbuf clearing bf. 14. steps 7-14 are the same for each received data byte. 15. communication is ended by either the slave sending an ack = 1 , or the master sending a stop condition. if a stop is sent and interrupt on stop detect is disabled, the slave will only know by polling the p bit of the sststat register. note: sspxif is still set after the 9th falling edge of sclx even if there is no clock stretching and bf has been cleared. only if nack is sent to master is sspxif not set. pic18(l)f2x/4xk22 ds41412f-page 228 ? 2010-2012 microchip technology inc. figure 15-14: i 2 c slave, 7-bit address, reception (sen = 0 , ahen = 0 , dhen = 0 ) receiving address ack receiving data ack receiving data ack = 1 a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d2 d1 d0 sdax sclx sspxif bf sspxov 12345678 12345678 12345678 9 9 9 ack is not sent. sspxov set because sspxbuf is still full. cleared by software first byte of data is available in sspxbuf sspxbuf is read sspxif set on 9th falling edge of sclx cleared by software p bus master sends stop condition s from slave to master ? 2010-2012 microchip technology inc. ds41412f-page 229 pic18(l)f2x/4xk22 figure 15-15: i 2 c slave, 7-bit address, reception (sen = 1 , ahen = 0 , dhen = 0 ) sen sen a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d2 d1 d0 sdax sclx 123456789 123456789 123456789 p sspxif set on 9th sclx is not held ckp is written to 1 in software, ckp is written to ? 1 ? in software, ack low because falling edge of sclx releasing sclx ack is not sent. bus master sends ckp sspxov bf sspxif sspxov set because sspxbuf is still full. cleared by software first byte of data is available in sspxbuf ack = 1 cleared by software sspxbuf is read clock is held low until ckp is set to ? 1 ? releasing sclx stop condition s ack ack receive address receive data receive data r/w= 0 pic18(l)f2x/4xk22 ds41412f-page 230 ? 2010-2012 microchip technology inc. figure 15-16: i 2 c slave, 7-bit address, reception (sen = 0 , ahen = 1 , dhen = 1 ) receiving address receiving data received data p a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d2 d1 d0 sdax sclx bf ckp s p 12 3 4 56 7 8 9 12345678 9 12345678 master sends stop condition s data is read from sspxbuf cleared by software sspxif is set on 9th falling edge of sclx, after ack ckp set by software, sclx is released slave software 9 acktim cleared by hardware in 9th rising edge of sclx sets ackdt to not ack when dhen= 1 : ckp is cleared by hardware on 8th falling edge of sclx slave software clears ackdt to ack the received byte acktim set by hardware on 8th falling edge of sclx when ahen= 1 : ckp is cleared by hardware and sclx is stretched address is read from ssbuf acktim set by hardware on 8th falling edge of sclx ack master releases sdax to slave for ack sequence no interrupt after not ack from slave ack = 1 ack ackdt acktim sspxif if ahen = 1 : sspxif is set ? 2010-2012 microchip technology inc. ds41412f-page 231 pic18(l)f2x/4xk22 figure 15-17: i 2 c slave, 7-bit address, reception (sen = 1 , ahen = 1 , dhen = 1 ) receiving address receive data receive data a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d2 d1 d0 sdax sclx sspxif bf ackdt ckp s p ack s 12 34 5678 9 12 3 4567 8 9 12 345 67 8 9 ack ack cleared by software acktim is cleared by hardware sspxbuf can be set by software, read any time before next byte is loaded release sclx on 9th rising edge of sclx received address is loaded into sspxbuf slave software clears ackdt to ack r/w = 0 master releases sdax to slave for ack sequence the received byte when ahen = 1 ; on the 8th falling edge of sclx of an address byte, ckp is cleared acktim is set by hardware on 8th falling edge of sclx when dhen = 1 ; on the 8th falling edge of sclx of a received data byte, ckp is cleared received data is available on sspxbuf slave sends not ack ckp is not cleared if not ack p master sends stop condition no interrupt after if not ack from slave acktim pic18(l)f2x/4xk22 ds41412f-page 232 ? 2010-2012 microchip technology inc. 15.5.3 slave transmission when the r/w bit of the incoming address byte is set and an address match occurs, the r/w bit of the sspxstat register is set. the received address is loaded into the sspxbuf register, and an ack pulse is sent by the slave on the ninth bit. following the ack , slave hardware clears the ckp bit and the sclx pin is held low (see section 15.5.6 ?clock stretching? for more detail). by stretching the clock, the master will be unable to assert another clock pulse until the slave is done preparing the transmit data. the transmit data must be loaded into the sspxbuf register which also loads the sspxsr register. then the sclx pin should be released by setting the ckp bit of the sspxcon1 register. the eight data bits are shifted out on the falling edge of the sclx input. this ensures that the sdax signal is valid during the sclx high time. the ack pulse from the master-receiver is latched on the rising edge of the ninth sclx input pulse. this ack value is copied to the ackstat bit of the sspxcon2 register. if ackstat is set (not ack ), then the data transfer is complete. in this case, when the not ack is latched by the slave, the slave goes idle and waits for another occurrence of the start bit. if the sdax line was low (ack ), the next transmit data must be loaded into the sspxbuf register. again, the sclx pin must be released by setting bit ckp. an msspx interrupt is generated for each data transfer byte. the sspxif bit must be cleared by software and the sspxstat register is used to determine the status of the byte. the sspxif bit is set on the falling edge of the ninth clock pulse. 15.5.3.1 slave mode bus collision a slave receives a read request and begins shifting data out on the sdax line. if a bus collision is detected and the sbcde bit of the sspxcon3 register is set, the bclxif bit of the pirx register is set. once a bus collision is detected, the slave goes idle and waits to be addressed again. user software can use the bclxif bit to handle a slave bus collision. 15.5.3.2 7-bit transmission a master device can transmit a read request to a slave, and then clock data out of the slave. the list below outlines what software for a slave will need to do to accomplish a standard transmission. figure 15-18 can be used as a reference to this list. 1. master sends a start condition on sdax and sclx. 2. s bit of sspxstat is set; sspxif is set if inter- rupt on start detect is enabled. 3. matching address with r/w bit set is received by the slave setting sspxif bit. 4. slave hardware generates an ack and sets sspxif. 5. sspxif bit is cleared by user. 6. software reads the received address from sspxbuf, clearing bf. 7. r/w is set so ckp was automatically cleared after the ack. 8. the slave software loads the transmit data into sspxbuf. 9. ckp bit is set releasing sclx, allowing the mas- ter to clock the data out of the slave. 10. sspxif is set after the ack response from the master is loaded into the ackstat register. 11. sspxif bit is cleared. 12. the slave software checks the ackstat bit to see if the master wants to clock out more data. 13. steps 9-13 are repeated for each transmitted byte. 14. if the master sends a not ack ; the clock is not held, but sspxif is still set. 15. the master sends a restart condition or a stop. 16. the slave is no longer addressed. note 1: if the master ack s the clock will be stretched. 2: ackstat is the only bit updated on the rising edge of sclx (9th) rather than the falling. ? 2010-2012 microchip technology inc. ds41412f-page 233 pic18(l)f2x/4xk22 figure 15-18: i 2 c slave, 7-bit address, transmission (ahen = 0 ) receiving address automatic transmitting data automatic transmitting data a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d2 d1 d0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 sdax sclx sspxif bf ckp ackstat r/w d/a s p received address when r/w is set r/w is copied from the indicates an address is read from sspxbuf sclx is always held low after 9th sclx falling edge matching address byte has been received masters not ack is copied to ackstat ckp is not held for not ack bf is automatically cleared after 8th falling edge of sclx data to transmit is loaded into sspxbuf set by software cleared by software ack ack ack r/w = 1 s p master sends stop condition pic18(l)f2x/4xk22 ds41412f-page 234 ? 2010-2012 microchip technology inc. 15.5.3.3 7-bit transmission with address hold enabled setting the ahen bit of the sspxcon3 register enables additional clock stretching and interrupt generation after the 8th falling edge of a received matching address. once a matching address has been clocked in, ckp is cleared and the sspxif interrupt is set. figure 15-19 displays a standard waveform of a 7-bit address slave transmission with ahen enabled. 1. bus starts idle. 2. master sends start condition; the s bit of sspxstat is set; sspxif is set if interrupt on start detect is enabled. 3. master sends matching address with r/w bit set. after the 8th falling edge of the sclx line the ckp bit is cleared and sspxif interrupt is generated. 4. slave software clears sspxif. 5. slave software reads acktim bit of sspxcon3 register, and r/w and d/a of the sspxstat register to determine the source of the interrupt. 6. slave reads the address value from the sspxbuf register clearing the bf bit. 7. slave software decides from this information if it wishes to ack or not ack and sets ackdt bit of the sspxcon2 register accordingly. 8. slave sets the ckp bit releasing sclx. 9. master clocks in the ack value from the slave. 10. slave hardware automatically clears the ckp bit and sets sspxif after the ack if the r/w bit is set. 11. slave software clears sspxif. 12. slave loads value to transmit to the master into sspxbuf setting the bf bit. 13. slave sets ckp bit releasing the clock. 14. master clocks out the data from the slave and sends an ack value on the 9th sclx pulse. 15. slave hardware copies the ack value into the ackstat bit of the sspxcon2 register. 16. steps 10-15 are repeated for each byte transmitted to the master from the slave. 17. if the master sends a not ack the slave releases the bus allowing the master to send a stop and end the communication. note: sspxbuf cannot be loaded until after the ack. note: master must send a not ack on the last byte to ensure that the slave releases the sclx line to receive a stop. ? 2010-2012 microchip technology inc. ds41412f-page 235 pic18(l)f2x/4xk22 figure 15-19: i 2 c slave, 7-bit address, transmission (ahen = 1 ) receiving address automatic transmitting data automatic transmitting data a7 a6 a5 a4 a3 a2 a1 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d2 d1 d0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 sdax sclx sspxif bf ackdt ackstat ckp r/w d/a received address is read from sspxbuf bf is automatically cleared after 8th falling edge of sclx data to transmit is loaded into sspxbuf cleared by software slave clears ackdt to ack address master?s ack response is copied to sspxstat ckp not cleared after not ack set by software, releases sclx acktim is cleared on 9th rising edge of sclx acktim is set on 8th falling edge of sclx when ahen = 1 ; ckp is cleared by hardware after receiving matching address. when r/w = 1 ; ckp is always cleared after ack s p master sends stop condition ack r/w = 1 master releases sdax to slave for ack sequence ack ack acktim pic18(l)f2x/4xk22 ds41412f-page 236 ? 2010-2012 microchip technology inc. 15.5.4 slave mode 10-bit address reception this section describes a standard sequence of events for the msspx module configured as an i 2 c slave in 10-bit addressing mode ( figure 15-20 ) and is used as a visual reference for this description. this is a step by step process of what must be done by slave software to accomplish i 2 c communication. 1. bus starts idle. 2. master sends start condition; s bit of sspxstat is set; sspxif is set if interrupt on start detect is enabled. 3. master sends matching high address with r/w bit clear; ua bit of the sspxstat register is set. 4. slave sends ack and sspxif is set. 5. software clears the sspxif bit. 6. software reads received address from sspxbuf clearing the bf flag. 7. slave loads low address into sspxadd, releasing sclx. 8. master sends matching low address byte to the slave; ua bit is set. 9. slave sends ack and sspxif is set. 10. slave clears sspxif. 11. slave reads the received matching address from sspxbuf clearing bf. 12. slave loads high address into sspxadd. 13. master clocks a data byte to the slave and clocks out the slaves ack on the 9th sclx pulse; sspxif is set. 14. if sen bit of sspxcon2 is set, ckp is cleared by hardware and the clock is stretched. 15. slave clears sspxif. 16. slave reads the received byte from sspxbuf clearing bf. 17. if sen is set the slave sets ckp to release the sclx. 18. steps 13-17 repeat for each received byte. 19. master sends stop to end the transmission. 15.5.5 10-bit addressing with address or data hold reception using 10-bit addressing with ahen or dhen set is the same as with 7-bit modes. the only difference is the need to update the sspxadd register using the ua bit. all functionality, specifically when the ckp bit is cleared and sclx line is held low are the same. figure 15-21 can be used as a reference of a slave in 10-bit addressing with ahen set. figure 15-22 shows a standard waveform for a slave transmitter in 10-bit addressing mode. note: updates to the sspxadd register are not allowed until after the ack sequence. note: if the low address does not match, sspxif and ua are still set so that the slave soft- ware can set sspxadd back to the high address. bf is not set because there is no match. ckp is unaffected. ? 2010-2012 microchip technology inc. ds41412f-page 237 pic18(l)f2x/4xk22 figure 15-20: i 2 c slave, 10-bit address, reception (sen = 1 , ahen = 0 , dhen = 0 ) sspxif receive first address byte ack receive second address byte ack receive data ack receive data ack 1 1 1 1 0 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 d4 d3 d2 d1 d0 sdax sclx ua ckp 1 2345678 912345678 912345678 9 12345678 9 p master sends stop condition cleared by software receive address is software updates sspxadd data is read sclx is held low set by software, while ckp = 0 from sspxbuf releasing sclx when sen = 1 ; ckp is cleared after 9th falling edge of received byte read from sspxbuf and releases sclx when ua = 1 ; if address matches set by hardware on 9th falling edge sspxadd it is loaded into sspxbuf sclx is held low s bf pic18(l)f2x/4xk22 ds41412f-page 238 ? 2010-2012 microchip technology inc. figure 15-21: i 2 c slave, 10-bit address, reception (sen = 0 , ahen = 1 , dhen = 0 ) receive first address byte ua receive second address byte ua receive data ack receive data 1 1 1 1 0 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 d7 d6 d5 d4 d3 d2 d1 d0 d7 d6 d5 sdax sclx sspxif bf ackdt ua ckp acktim 12345678 9 s ack ack 12 345678 9 12345678 91 2 sspxbuf is read from received data sspxbuf can be read anytime before the next received byte cleared by software falling edge of sclx not allowed until 9th update to sspxadd is set ckp with software releases sclx sclx clears ua and releases update of sspxadd, set by hardware on 9th falling edge slave software clears ackdt to ack the received byte if when ahen = 1 ; on the 8th falling edge of sclx of an address byte, ckp is cleared acktim is set by hardware on 8th falling edge of sclx cleared by software r/w = 0 ? 2010-2012 microchip technology inc. ds41412f-page 239 pic18(l)f2x/4xk22 figure 15-22: i 2 c slave, 10-bit address, transmission (sen = 0 , ahen = 0 , dhen = 0 ) receiving address ack receiving second address byte sr receive first address byte ack transmitting data byte 1 1 1 1 0 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 1 1 1 1 0 a9 a8 d7 d6 d5 d4 d3 d2 d1 d0 sdax sclx sspxif bf ua ckp r/w d/a 1 2345 6789 1 2345 6789 1 23 4 5 6789 1 23456 789 ack = 1 p master sends stop condition master sends not ack master sends restart event ack r/w = 0 s cleared by software after sspxadd is updated, ua is cleared and sclx is released high address is loaded received address is data to transmit is set by software indicates an address when r/w = 1 ; r/w is copied from the set by hardware ua indicates sspxadd sspxbuf loaded with received address must be updated has been received loaded into sspxbuf releases sclx masters not ack is copied matching address byte ckp is cleared on 9th falling edge of sclx read from sspxbuf back into sspxadd ackstat set by hardware pic18(l)f2x/4xk22 ds41412f-page 240 ? 2010-2012 microchip technology inc. 15.5.6 clock stretching clock stretching occurs when a device on the bus holds the sclx line low effectively pausing communi- cation. the slave may stretch the clock to allow more time to handle data or prepare a response for the mas- ter device. a master device is not concerned with stretching as anytime it is active on the bus and not transferring data it is stretching. any stretching done by a slave is invisible to the master software and han- dled by the hardware that generates sclx. the ckp bit of the sspxcon1 register is used to control stretching in software. any time the ckp bit is cleared, the module will wait for the sclx line to go low and then hold it. setting ckp will release sclx and allow more communication. 15.5.6.1 normal clock stretching following an ack if the r/w bit of sspxstat is set, a read request, the slave hardware will clear ckp. this allows the slave time to update sspxbuf with data to transfer to the master. if the sen bit of sspxcon2 is set, the slave hardware will always stretch the clock after the ack sequence. once the slave is ready; ckp is set by software and communication resumes. 15.5.6.2 10-bit addressing mode in 10-bit addressing mode, when the ua bit is set, the clock is always stretched. this is the only time the sclx is stretched without ckp being cleared. sclx is released immediately after a write to sspxadd. 15.5.6.3 byte nacking when the ahen bit of sspxcon3 is set; ckp is cleared by hardware after the 8th falling edge of sclx for a received matching address byte. when the dhen bit of sspxcon3 is set; ckp is cleared after the 8th falling edge of sclx for received data. stretching after the 8th falling edge of sclx allows the slave to look at the received address or data and decide if it wants to ack the received data. 15.5.7 clock synchronization and the ckp bit any time the ckp bit is cleared, the module will wait for the sclx line to go low and then hold it. however, clearing the ckp bit will not assert the sclx output low until the sclx output is already sampled low. therefore, the ckp bit will not assert the sclx line until an external i 2 c master device has already asserted the sclx line. the sclx output will remain low until the ckp bit is set and all other devices on the i 2 c bus have released sclx. this ensures that a write to the ckp bit will not violate the minimum high time requirement for sclx (see figure 15-23 ). figure 15-23: clock synchronization timing note 1: the bf bit has no effect on whether the clock will be stretched or not. this is different than previous versions of the module that would not stretch the clock, clear ckp, if sspxbuf was read before the 9th falling edge of sclx. 2: previous versions of the module did not stretch the clock for a transmission if sspxbuf was loaded before the 9th fall- ing edge of sclx. it is now always cleared for read requests. note: previous versions of the module did not stretch the clock if the second address byte did not match. sdax sclx dx ? ? 1 dx wr q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 sspxcon1 ckp master device releases clock master device asserts clock ? 2010-2012 microchip technology inc. ds41412f-page 241 pic18(l)f2x/4xk22 15.5.8 general call address support the addressing procedure for the i 2 c bus is such that the first byte after the start condition usually determines which device will be the slave addressed by the master device. the exception is the general call address which can address all devices. when this address is used, all devices should, in theory, respond with an acknowledge. the general call address is a reserved address in the i 2 c protocol, defined as address 0x00. when the gcen bit of the sspxcon2 register is set, the slave module will automatically ack the reception of this address regardless of the value stored in sspxadd. after the slave clocks in an address of all zeros with the r/w bit clear, an interrupt is generated and slave soft- ware can read sspxbuf and respond. figure 15-24 shows a general call reception sequence. in 10-bit address mode, the ua bit will not be set on the reception of the general call address. the slave will prepare to receive the second byte as data, just as it would in 7-bit mode. if the ahen bit of the sspxcon3 register is set, just as with any other address reception, the slave hardware will stretch the clock after the 8th falling edge of sclx. the slave must then set its ackdt value and release the clock with communication progressing as it would normally. figure 15-24: slave mode general call address sequence 15.5.9 sspx mask register an sspx mask (sspxmsk) register ( register 15-5 ) is available in i 2 c slave mode as a mask for the value held in the sspxsr register during an address comparison operation. a zero (? 0 ?) bit in the sspxmsk register has the effect of making the corresponding bit of the received address a ?don?t care?. this register is reset to all ? 1 ?s upon any reset condition and, therefore, has no effect on standard sspx operation until written with a mask value. the sspx mask register is active during: ? 7-bit address mode: address compare of a<7:1>. ? 10-bit address mode: address compare of a<7:0> only. the sspx mask has no effect during the reception of the first (high) byte of the address. sdax sclx s sspxif bf (sspxstat<0>) cleared by software sspxbuf is read r/w = 0 ack general call address address is compared to general call address receiving data ack 123456789123456789 d7 d6 d5 d4 d3 d2 d1 d0 after ack , set interrupt gcen (sspxcon2<7>) ? 1 ? pic18(l)f2x/4xk22 ds41412f-page 242 ? 2010-2012 microchip technology inc. 15.6 i 2 c master mode master mode is enabled by setting and clearing the appropriate sspxm bits in the sspxcon1 register and by setting the sspxen bit. in master mode, the sclx and sdax lines are set as inputs and are manipulated by the msspx hardware. master mode of operation is supported by interrupt generation on the detection of the start and stop con- ditions. the stop (p) and start (s) bits are cleared from a reset or when the msspx module is disabled. con- trol of the i 2 c bus may be taken when the p bit is set, or the bus is idle. in firmware controlled master mode, user code conducts all i 2 c bus operations based on start and stop bit condition detection. start and stop condition detection is the only active circuitry in this mode. all other communication is done by the user software directly manipulating the sdax and sclx lines. the following events will cause the sspx interrupt flag bit, sspxif, to be set (sspx interrupt, if enabled): ? start condition detected ? stop condition detected ? data transfer byte transmitted/received ? acknowledge transmitted/received ? repeated start generated 15.6.1 i 2 c master mode operation the master device generates all of the serial clock pulses and the start and stop conditions. a transfer is ended with a stop condition or with a repeated start condition. since the repeated start condition is also the beginning of the next serial transfer, the i 2 c bus will not be released. in master transmitter mode, serial data is output through sdax, while sclx outputs the serial clock. the first byte transmitted contains the slave address of the receiving device (7 bits) and the read/write (r/w ) bit. in this case, the r/w bit will be logic ? 0 ?. serial data is transmitted eight bits at a time. after each byte is trans- mitted, an acknowledge bit is received. start and stop conditions are output to indicate the beginning and the end of a serial transfer. in master receive mode, the first byte transmitted con- tains the slave address of the transmitting device (7 bits) and the r/w bit. in this case, the r/w bit will be logic ? 1 ?. thus, the first byte transmitted is a 7-bit slave address followed by a ? 1 ? to indicate the receive bit. serial data is received via sdax, while sclx outputs the serial clock. serial data is received eight bits at a time. after each byte is received, an acknowledge bit is transmitted. start and stop conditions indicate the beginning and end of transmission. a baud rate generator is used to set the clock frequency output on sclx. see section 15.7 ?baud rate generator? for more detail. note 1: the msspx module, when configured in i 2 c master mode, does not allow queue- ing of events. for instance, the user is not allowed to initiate a start condition and immediately write the sspxbuf register to initiate transmission before the start condition is complete. in this case, the sspxbuf will not be written to and the wcol bit will be set, indicating that a write to the sspxbuf did not occur 2: when in master mode, start/stop detection is masked and an interrupt is generated when the sen/pen bit is cleared and the generation is complete. ? 2010-2012 microchip technology inc. ds41412f-page 243 pic18(l)f2x/4xk22 15.6.2 clock arbitration clock arbitration occurs when the master, during any receive, transmit or repeated start/stop condition, releases the sclx pin (sclx allowed to float high). when the sclx pin is allowed to float high, the baud rate generator (brg) is suspended from counting until the sclx pin is actually sampled high. when the sclx pin is sampled high, the baud rate generator is reloaded with the contents of sspxadd<7:0> and begins counting. this ensures that the sclx high time will always be at least one brg rollover count in the event that the clock is held low by an external device ( figure 15-25 ). figure 15-25: baud rate generator timing with clock arbitration 15.6.3 wcol status flag if the user writes the sspxbuf when a start, restart, stop, receive or transmit sequence is in progress, the wcol is set and the contents of the buffer are unchanged (the write does not occur). any time the wcol bit is set it indicates that an action on sspxbuf was attempted while the module was not idle. sdax sclx sclx deasserted but slave holds dx ? ? 1 dx brg sclx is sampled high, reload takes place and brg starts its count 03h 02h 01h 00h (hold off) 03h 02h reload brg value sclx low (clock arbitration) sclx allowed to transition high brg decrements on q2 and q4 cycles note: because queueing of events is not allowed, writing to the lower 5 bits of sspxcon2 is disabled until the start condition is complete. pic18(l)f2x/4xk22 ds41412f-page 244 ? 2010-2012 microchip technology inc. 15.6.4 i 2 c master mode start condition timing to initiate a start condition ( figure 15-26 ), the user sets the start enable bit, sen, of the sspxcon2 reg- ister. if the sdax and sclx pins are sampled high, the baud rate generator is reloaded with the contents of sspxadd<7:0> and starts its count. if sclx and sdax are both sampled high when the baud rate generator times out (t brg ), the sdax pin is driven low. the action of the sdax being driven low while sclx is high is the start condition and causes the s bit of the sspxstat1 register to be set. following this, the baud rate generator is reloaded with the contents of sspxadd<7:0> and resumes its count. when the baud rate generator times out (t brg ), the sen bit of the sspxcon2 register will be automatically cleared by hardware; the baud rate generator is suspended, leaving the sdax line held low and the start condition is complete. figure 15-26: first start bit timing note 1: if at the beginning of the start condition, the sdax and sclx pins are already sam- pled low, or if during the start condition, the sclx line is sampled low before the sdax line is driven low, a bus collision occurs, the bus collision interrupt flag, bclxif, is set, the start condition is aborted and the i 2 c module is reset into its idle state. 2: the philips i 2 c specification states that a bus collision cannot occur on a start. sdax sclx s t brg 1st bit 2nd bit t brg sdax = 1 , at completion of start bit, sclx = 1 write to sspxbuf occurs here t brg hardware clears sen bit t brg write to sen bit occurs here set s bit (sspxstat<3>) and sets sspxif bit ? 2010-2012 microchip technology inc. ds41412f-page 245 pic18(l)f2x/4xk22 15.6.5 i 2 c master mode repeated start condition timing a repeated start condition ( figure 15-27 ) occurs when the rsen bit of the sspxcon2 register is pro- grammed high and the master state machine is no lon- ger active. when the rsen bit is set, the sclx pin is asserted low. when the sclx pin is sampled low, the baud rate generator is loaded and begins counting. the sdax pin is released (brought high) for one baud rate generator count (t brg ). when the baud rate generator times out, if sdax is sampled high, the sclx pin will be deasserted (brought high). when sclx is sampled high, the baud rate generator is reloaded and begins counting. sdax and sclx must be sam- pled high for one t brg . this action is then followed by assertion of the sdax pin (sdax = 0 ) for one t brg while sclx is high. sclx is asserted low. following this, the rsen bit of the sspxcon2 register will be automatically cleared and the baud rate generator will not be reloaded, leaving the sdax pin held low. as soon as a start condition is detected on the sdax and sclx pins, the s bit of the sspxstat register will be set. the sspxif bit will not be set until the baud rate generator has timed out. figure 15-27: repeat start condition waveform note 1: if rsen is programmed while any other event is in progress, it will not take effect. 2: a bus collision during the repeated start condition occurs if: ? sdax is sampled low when sclx goes from low-to-high. ? sclx goes low before sdax is asserted low. this may indicate that another master is attempting to transmit a data ? 1 ?. sdax sclx repeated start write to sspxcon2 write to sspxbuf occurs here at completion of start bit, hardware clears rsen bit 1st bit s bit set by hardware t brg t brg sdax = 1 , sdax = 1 , sclx (no change) sclx = 1 occurs here t brg t brg t brg and sets sspxif sr pic18(l)f2x/4xk22 ds41412f-page 246 ? 2010-2012 microchip technology inc. 15.6.6 i 2 c master mode transmission transmission of a data byte, a 7-bit address or the other half of a 10-bit address is accomplished by simply writing a value to the sspxbuf register. this action will set the buffer full flag bit, bf, and allow the baud rate generator to begin counting and start the next trans- mission. each bit of address/data will be shifted out onto the sdax pin after the falling edge of sclx is asserted. sclx is held low for one baud rate genera- tor rollover count (t brg ). data should be valid before sclx is released high. when the sclx pin is released high, it is held that way for t brg . the data on the sdax pin must remain stable for that duration and some hold time after the next falling edge of sclx. after the eighth bit is shifted out (the falling edge of the eighth clock), the bf flag is cleared and the master releases sdax. this allows the slave device being addressed to respond with an ack bit during the ninth bit time if an address match occurred, or if data was received prop- erly. the status of ack is written into the ackstat bit on the rising edge of the ninth clock. if the master receives an acknowledge, the acknowledge status bit, ackstat, is cleared. if not, the bit is set. after the ninth clock, the sspxif bit is set and the master clock (baud rate generator) is suspended until the next data byte is loaded into the sspxbuf, leaving sclx low and sdax unchanged ( figure 15-28 ). after the write to the sspxbuf, each bit of the address will be shifted out on the falling edge of sclx until all seven address bits and the r/w bit are completed. on the falling edge of the eighth clock, the master will release the sdax pin, allowing the slave to respond with an acknowledge. on the falling edge of the ninth clock, the master will sample the sdax pin to see if the address was recognized by a slave. the status of the ack bit is loaded into the ackstat status bit of the sspxcon2 register. following the falling edge of the ninth clock transmission of the address, the sspxif is set, the bf flag is cleared and the baud rate generator is turned off until another write to the sspxbuf takes place, holding sclx low and allowing sdax to float. 15.6.6.1 bf status flag in transmit mode, the bf bit of the sspxstat register is set when the cpu writes to sspxbuf and is cleared when all 8 bits are shifted out. 15.6.6.2 wcol status flag if the user writes the sspxbuf when a transmit is already in progress (i.e., sspxsr is still shifting out a data byte), the wcol is set and the contents of the buffer are unchanged (the write does not occur). wcol must be cleared by software before the next transmission. 15.6.6.3 ackstat status flag in transmit mode, the ackstat bit of the sspxcon2 register is cleared when the slave has sent an acknowledge (ack = 0 ) and is set when the slave does not acknowledge (ack = 1 ). a slave sends an acknowledge when it has recognized its address (including a general call), or when the slave has properly received its data. 15.6.6.4 typical transmit sequence: 1. the user generates a start condition by setting the sen bit of the sspxcon2 register. 2. sspxif is set by hardware on completion of the start. 3. sspxif is cleared by software. 4. the msspx module will wait the required start time before any other operation takes place. 5. the user loads the sspxbuf with the slave address to transmit. 6. address is shifted out the sdax pin until all eight bits are transmitted. transmission begins as soon as sspxbuf is written to. 7. the msspx module shifts in the ack bit from the slave device and writes its value into the ackstat bit of the sspxcon2 register. 8. the msspx module generates an interrupt at the end of the ninth clock cycle by setting the sspxif bit. 9. the user loads the sspxbuf with eight bits of data. 10. data is shifted out the sdax pin until all eight bits are transmitted. 11. the msspx module shifts in the ack bit from the slave device and writes its value into the ackstat bit of the sspxcon2 register. 12. steps 8-11 are repeated for all transmitted data bytes. 13. the user generates a stop or restart condition by setting the pen or rsen bits of the sspxcon2 register. interrupt is generated once the stop/restart condition is complete. ? 2010-2012 microchip technology inc. ds41412f-page 247 pic18(l)f2x/4xk22 figure 15-28: i 2 c master mode waveform (transmission, 7 or 10-bit address) sdax sclx sspxif bf (sspxstat<0>) sen a7 a6 a5 a4 a3 a2 a1 ack = 0 d7 d6 d5 d4 d3 d2 d1 d0 ack transmitting data or second half r/w = 0 transmit address to slave 123456789 123456789 p cleared by software service routine sspxbuf is written by software from sspx interrupt after start condition, sen cleared by hardware s sspxbuf written with 7-bit address and r/w start transmit sclx held low while cpu responds to sspxif sen = 0 of 10-bit address write sspxcon2<0> sen = 1 start condition begins from slave, clear ackstat bit sspxcon2<6> ackstat in sspxcon2 = 1 cleared by software sspxbuf written pen r/w cleared by software pic18(l)f2x/4xk22 ds41412f-page 248 ? 2010-2012 microchip technology inc. 15.6.7 i 2 c master mode reception master mode reception ( figure 15-29 ) is enabled by programming the receive enable bit, rcen, of the sspxcon2 register. the baud rate generator begins counting and on each rollover, the state of the sclx pin changes (high-to-low/ low-to-high) and data is shifted into the sspxsr. after the falling edge of the eighth clock, the receive enable flag is automatically cleared, the contents of the sspxsr are loaded into the sspxbuf, the bf flag bit is set, the sspxif flag bit is set and the baud rate generator is suspended from counting, holding sclx low. the msspx is now in idle state awaiting the next command. when the buffer is read by the cpu, the bf flag bit is automatically cleared. the user can then send an acknowledge bit at the end of reception by set- ting the acknowledge sequence enable bit, acken, of the sspxcon2 register. 15.6.7.1 bf status flag in receive operation, the bf bit is set when an address or data byte is loaded into sspxbuf from sspxsr. it is cleared when the sspxbuf register is read. 15.6.7.2 sspxov status flag in receive operation, the sspxov bit is set when eight bits are received into the sspxsr and the bf flag bit is already set from a previous reception. 15.6.7.3 wcol status flag if the user writes the sspxbuf when a receive is already in progress (i.e., sspxsr is still shifting in a data byte), the wcol bit is set and the contents of the buffer are unchanged (the write does not occur). 15.6.7.4 typical receive sequence: 1. the user generates a start condition by setting the sen bit of the sspxcon2 register. 2. sspxif is set by hardware on completion of the start. 3. sspxif is cleared by software. 4. user writes sspxbuf with the slave address to transmit and the r/w bit set. 5. address is shifted out the sdax pin until all eight bits are transmitted. transmission begins as soon as sspxbuf is written to. 6. the msspx module shifts in the ack bit from the slave device and writes its value into the ackstat bit of the sspxcon2 register. 7. the msspx module generates an interrupt at the end of the ninth clock cycle by setting the sspxif bit. 8. user sets the rcen bit of the sspxcon2 regis- ter and the master clocks in a byte from the slave. 9. after the 8th falling edge of sclx, sspxif and bf are set. 10. master clears sspxif and reads the received byte from sspxuf, clears bf. 11. master sets ack value sent to slave in ackdt bit of the sspxcon2 register and initiates the ack by setting the acken bit. 12. masters ack is clocked out to the slave and sspxif is set. 13. user clears sspxif. 14. steps 8-13 are repeated for each received byte from the slave. 15. master sends a not ack or stop to end communication. note: the msspx module must be in an idle state before the rcen bit is set or the rcen bit will be disregarded. ? 2010-2012 microchip technology inc. ds41412f-page 249 pic18(l)f2x/4xk22 figure 15-29: i 2 c? master mode waveform (reception, 7-bit address) p 9 8 7 6 5 d0 d1 d2 d3 d4 d5 d6 d7 s a7 a6 a5 a4 a3 a2 a1 sdax sclx 12 3 4 5 6 7 8 9 12 3 4 5 678 9 1234 bus master terminates transfer ack receiving data from slave receiving data from slave d0 d1 d2 d3 d4 d5 d6 d7 ack transmit address to slave sspxif bf ack is not sent write to sspxcon2<0>(sen = 1 ), write to sspxbuf occurs here, ack from slave master configured as a receiver by programming sspxcon2<3> (rcen = 1 ) pen bit = 1 written here data shifted in on falling edge of clk cleared by software start xmit sen = 0 sspxov sdax = 0 , sclx = 1 while cpu (sspxstat<0>) ack cleared by software cleared by software set sspxif interrupt at end of receive set p bit (sspxstat<4>) and sspxif cleared in software ack from master set sspxif at end set sspxif interrupt at end of acknowledge sequence set sspxif interrupt at end of acknow- ledge sequence of receive set acken, start acknowledge sequence sspxov is set because sspxbuf is still full sdax = ackdt = 1 rcen cleared automatically rcen = 1 , start next receive write to sspxcon2<4> to start acknowledge sequence sdax = ackdt (sspxcon2<5>) = 0 rcen cleared automatically responds to sspxif acken begin start condition cleared by software sdax = ackdt = 0 last bit is shifted into sspxsr and contents are unloaded into sspxbuf rcen master configured as a receiver by programming sspxcon2<3> (rcen = 1 ) rcen cleared automatically ack from master sdax = ackdt = 0 rcen cleared automatically r/w pic18(l)f2x/4xk22 ds41412f-page 250 ? 2010-2012 microchip technology inc. 15.6.8 acknowledge sequence timing an acknowledge sequence is enabled by setting the acknowledge sequence enable bit, acken, of the sspxcon2 register. when this bit is set, the sclx pin is pulled low and the contents of the acknowledge data bit are presented on the sdax pin. if the user wishes to generate an acknowledge, then the ackdt bit should be cleared. if not, the user should set the ackdt bit before starting an acknowledge sequence. the baud rate generator then counts for one rollover period (t brg ) and the sclx pin is deasserted (pulled high). when the sclx pin is sampled high (clock arbitration), the baud rate generator counts for t brg . the sclx pin is then pulled low. following this, the acken bit is auto- matically cleared, the baud rate generator is turned off and the msspx module then goes into idle mode ( figure 15-30 ). 15.6.8.1 wcol status flag if the user writes the sspxbuf when an acknowledge sequence is in progress, then wcol is set and the contents of the buffer are unchanged (the write does not occur). 15.6.9 stop condition timing a stop bit is asserted on the sdax pin at the end of a receive/transmit by setting the stop sequence enable bit, pen, of the sspxcon2 register. at the end of a receive/transmit, the sclx line is held low after the falling edge of the ninth clock. when the pen bit is set, the master will assert the sdax line low. when the sdax line is sampled low, the baud rate generator is reloaded and counts down to ? 0 ?. when the baud rate generator times out, the sclx pin will be brought high and one t brg (baud rate generator rollover count) later, the sdax pin will be deasserted. when the sdax pin is sampled high while sclx is high, the p bit of the sspxstat register is set. a t brg later, the pen bit is cleared and the sspxif bit is set ( figure 15-31 ). 15.6.9.1 wcol status flag if the user writes the sspxbuf when a stop sequence is in progress, then the wcol bit is set and the contents of the buffer are unchanged (the write does not occur). figure 15-30: acknowledge sequen ce waveform note: t brg = one baud rate generator period. sdax sclx sspxif set at acknowledge sequence starts here, write to sspxcon2 acken automatically cleared cleared in t brg t brg the end of receive 8 acken = 1 , ackdt = 0 d0 9 sspxif software sspxif set at the end of acknowledge sequence cleared in software ack ? 2010-2012 microchip technology inc. ds41412f-page 251 pic18(l)f2x/4xk22 figure 15-31: stop cond ition receive or transmit mode 15.6.10 sleep operation while in sleep mode, the i 2 c slave module can receive addresses or data and when an address match or complete byte transfer occurs, wake the processor from sleep (if the msspx interrupt is enabled). 15.6.11 effects of a reset a reset disables the msspx module and terminates the current transfer. 15.6.12 multi-master mode in multi-master mode, the interrupt generation on the detection of the start and stop conditions allows the determination of when the bus is free. the stop (p) and start (s) bits are cleared from a reset or when the msspx module is disabled. control of the i 2 c bus may be taken when the p bit of the sspxstat register is set, or the bus is idle, with both the s and p bits clear. when the bus is busy, enabling the sspx interrupt will generate the interrupt when the stop condition occurs. in multi-master operation, the sdax line must be monitored for arbitration to see if the signal level is the expected output level. this check is performed by hardware with the result placed in the bclxif bit. the states where arbitration can be lost are: ? address transfer ? data transfer ? a start condition ? a repeated start condition ? an acknowledge condition sclx sdax sdax asserted low before rising edge of clock write to sspxcon2, set pen falling edge of sclx = 1 for t brg , followed by sdax = 1 for t brg 9th clock sclx brought high after t brg note: t brg = one baud rate generator period. t brg t brg after sdax sampled high. p bit (sspxstat<4>) is set. t brg to set up stop condition ack p t brg pen bit (sspxcon2<2>) is cleared by hardware and the sspxif bit is set pic18(l)f2x/4xk22 ds41412f-page 252 ? 2010-2012 microchip technology inc. 15.6.13 multi -master communication, bus collision and bus arbitration multi-master mode support is achieved by bus arbitration. when the master outputs address/data bits onto the sdax pin, arbitration takes place when the master outputs a ? 1 ? on sdax, by letting sdax float high and another master asserts a ? 0 ?. when the sclx pin floats high, data should be stable. if the expected data on sdax is a ? 1 ? and the data sampled on the sdax pin is ? 0 ?, then a bus collision has taken place. the master will set the bus collision interrupt flag, bclxif, and reset the i 2 c port to its idle state ( figure 15-32 ). if a transmit was in progress when the bus collision occurred, the transmission is halted, the bf flag is cleared, the sdax and sclx lines are deasserted and the sspxbuf can be written to. when the user services the bus collision interrupt service routine and if the i 2 c bus is free, the user can resume communication by asserting a start condition. if a start, repeated start, stop or acknowledge condition was in progress when the bus collision occurred, the condition is aborted, the sdax and sclx lines are deasserted and the respective control bits in the sspxcon2 register are cleared. when the user services the bus collision interrupt service routine and if the i 2 c bus is free, the user can resume communication by asserting a start condition. the master will continue to monitor the sdax and sclx pins. if a stop condition occurs, the sspxif bit will be set. a write to the sspxbuf will start the transmission of data at the first data bit, regardless of where the transmitter left off when the bus collision occurred. in multi-master mode, the interrupt generation on the detection of start and stop conditions allows the determination of when the bus is free. control of the i 2 c bus can be taken when the p bit is set in the sspxstat register, or the bus is idle and the s and p bits are cleared. figure 15-32: bus collision timing for transmit and acknowledge sdax sclx bclxif sdax released sdax line pulled low by another source sample sdax. while sclx is high, data does not match what is driven bus collision has occurred. set bus collision interrupt (bclxif) by the master. by master data changes while sclx = 0 ? 2010-2012 microchip technology inc. ds41412f-page 253 pic18(l)f2x/4xk22 15.6.13.1 bus collision during a start condition during a start condition, a bus collision occurs if: a) sdax or sclx are sampled low at the beginning of the start condition ( figure 15-33 ). b) sclx is sampled low before sdax is asserted low ( figure 15-34 ). during a start condition, both the sdax and the sclx pins are monitored. if the sdax pin is already low, or the sclx pin is already low, then all of the following occur: ? the start condition is aborted, ? the bclxif flag is set and ? the msspx module is reset to its idle state ( figure 15-33 ). the start condition begins with the sdax and sclx pins deasserted. when the sdax pin is sampled high, the baud rate generator is loaded and counts down. if the sclx pin is sampled low while sdax is high, a bus collision occurs because it is assumed that another master is attempting to drive a data ? 1 ? during the start condition. if the sdax pin is sampled low during this count, the brg is reset and the sdax line is asserted early ( figure 15-35 ). if, however, a ? 1 ? is sampled on the sdax pin, the sdax pin is asserted low at the end of the brg count. the baud rate generator is then reloaded and counts down to zero; if the sclx pin is sampled as ? 0 ? during this time, a bus collision does not occur. at the end of the brg count, the sclx pin is asserted low. figure 15-33: bus collision during st art condition (sdax only) note: the reason that bus collision is not a fac- tor during a start condition is that no two bus masters can assert a start condition at the exact same time. therefore, one master will always assert sdax before the other. this condition does not cause a bus collision because the two masters must be allowed to arbitrate the first address fol- lowing the start condition. if the address is the same, arbitration must be allowed to continue into the data portion, repeated start or stop conditions. sdax sclx sen sdax sampled low before sdax goes low before the sen bit is set. s bit and sspxif set because sspx module reset into idle state. sen cleared automatically because of bus collision. s bit and sspxif set because set sen, enable start condition if sdax = 1 , sclx = 1 sdax = 0 , sclx = 1 . bclxif s sspxif sdax = 0 , sclx = 1 . sspxif and bclxif are cleared by software sspxif and bclxif are cleared by software set bclxif, start condition. set bclxif. pic18(l)f2x/4xk22 ds41412f-page 254 ? 2010-2012 microchip technology inc. figure 15-34: bus collision d uring start condition (sclx = 0 ) figure 15-35: brg reset due to sda arbitration during start condition sdax sclx sen bus collision occurs. set bclxif. sclx = 0 before sdax = 0 , set sen, enable start sequence if sdax = 1 , sclx = 1 t brg t brg sdax = 0 , sclx = 1 bclxif s sspxif interrupt cleared by software bus collision occurs. set bclxif. sclx = 0 before brg time-out, ? 0 ?? 0 ? ? 0 ? ? 0 ? sdax sclx sen set s less than t brg t brg sdax = 0 , sclx = 1 bclxif s sspxif s interrupts cleared by software set sspxif sdax = 0 , sclx = 1 , sclx pulled low after brg time-out set sspxif ? 0 ? sdax pulled low by other master. reset brg and assert sdax. set sen, enable start sequence if sdax = 1 , sclx = 1 ? 2010-2012 microchip technology inc. ds41412f-page 255 pic18(l)f2x/4xk22 15.6.13.2 bus collision during a repeated start condition during a repeated start condition, a bus collision occurs if: a) a low level is sampled on sdax when sclx goes from low level to high level (case 1). b) sclx goes low before sdax is asserted low, indicating that another master is attempting to transmit a data ? 1 ? (case 2). when the user releases sdax and the pin is allowed to float high, the brg is loaded with sspxadd and counts down to zero. the sclx pin is then deasserted and when sampled high, the sdax pin is sampled. if sdax is low, a bus collision has occurred (i.e., another master is attempting to transmit a data ? 0 ?, figure 15-36 ). if sdax is sampled high, the brg is reloaded and begins counting. if sdax goes from high-to-low before the brg times out, no bus collision occurs because no two masters can assert sdax at exactly the same time. if sclx goes from high-to-low before the brg times out and sdax has not already been asserted, a bus collision occurs. in this case, another master is attempting to transmit a data ? 1 ? during the repeated start condition, see figure 15-37 . if, at the end of the brg time-out, both sclx and sdax are still high, the sdax pin is driven low and the brg is reloaded and begins counting. at the end of the count, regardless of the status of the sclx pin, the sclx pin is driven low and the repeated start condition is complete. figure 15-36: bus collision during a repeat ed start condition (case 1) figure 15-37: bus collision during repeat ed start condition (case 2) sdax sclx rsen bclxif s sspxif sample sdax when sclx goes high. if sdax = 0 , set bclxif and release sdax and sclx. cleared by software ? 0 ? ? 0 ? sdax sclx bclxif rsen s sspxif interrupt cleared by software sclx goes low before sdax, set bclxif. release sdax and sclx. t brg t brg ? 0 ? pic18(l)f2x/4xk22 ds41412f-page 256 ? 2010-2012 microchip technology inc. 15.6.13.3 bus collision during a stop condition bus collision occurs during a stop condition if: a) after the sdax pin has been deasserted and allowed to float high, sdax is sampled low after the brg has timed out (case 1). b) after the sclx pin is deasserted, sclx is sampled low before sdax goes high (case 2). the stop condition begins with sdax asserted low. when sdax is sampled low, the sclx pin is allowed to float. when the pin is sampled high (clock arbitration), the baud rate generator is loaded with sspxadd and counts down to zero. after the brg times out, sdax is sampled. if sdax is sampled low, a bus collision has occurred. this is due to another master attempting to drive a data ? 0 ? ( figure 15-38 ). if the sclx pin is sampled low before sdax is allowed to float high, a bus collision occurs. this is another case of another master attempting to drive a data ? 0 ? ( figure 15-39 ). figure 15-38: bus collision during a stop condition (case 1) figure 15-39: bus collision during a stop condition (case 2) sdax sclx bclxif pen p sspxif t brg t brg t brg sdax asserted low sdax sampled low after t brg , set bclxif ? 0 ? ? 0 ? sdax sclx bclxif pen p sspxif t brg t brg t brg assert sdax sclx goes low before sdax goes high, set bclxif ? 0 ? ? 0 ? ? 2010-2012 microchip technology inc. ds41412f-page 257 pic18(l)f2x/4xk22 table 15-3: registers associated with i 2 c? operation name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ansela ? ? ansa5 ? ansa3 ansa2 ansa1 ansa0 154 anselb ? ? ansb5 ansb4 ansb3 ansb2 ansb1 (1) ansb0 (1) 155 anselc ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ? ? 155 anseld ansd7 ansd6 ansd5 ansd4 ansd3 ansd2 ansd1 (2) ansd0 (2) 155 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 56 ssp1add ssp1 address register in i 2 c slave mode. ssp1 baud rate reload register in i 2 c master mode. 265 ssp1buf ssp1 receive buffer/transmit register ? ssp1con1 wcol sspov sspen ckp sspm<3:0> 260 ssp1con2 gcen ackstat ackdt acken rcen pen rsen sen 262 ssp1con3 acktim pcie scie boen sdaht sbcde ahen dhen 263 ssp1msk ssp1 mask register bits 264 ssp1stat smp cke d/a psr/w ua bf 259 ssp2add ssp2 address register in i 2 c slave mode. ssp2 baud rate reload register in i 2 c master mode. 265 ssp2buf ssp2 receive buffer/transmit register ? ssp2con1 wcol sspov sspen ckp sspm<3:0> 260 ssp2con2 gcen ackstat ackdt acken rcen pen rsen sen 262 ssp2con3 acktim pcie scie boen sdaht sbcde ahen dhen 263 ssp2msk ssp1 mask register bits 264 ssp2stat smp cke d/a psr/w ua bf 259 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 (1) trisb0 (1) 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 (2) trisd0 (2) 156 legend: shaded bits are not used by the msspx in i 2 c mode. note 1: pic18(l)f2xk22 devices. 2: pic18(l)f4xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 258 ? 2010-2012 microchip technology inc. 15.7 baud rate generator the msspx module has a baud rate generator avail- able for clock generation in both i 2 c and spi master modes. the baud rate generator (brg) reload value is placed in the sspxadd register ( register 15-6 ). when a write occurs to sspxbuf, the baud rate gen- erator will automatically begin counting down. once the given operation is complete, the internal clock will automatically stop counting and the clock pin will remain in its last state. an internal signal ?reload? in figure 15-40 triggers the value from sspxadd to be loaded into the brg counter. this occurs twice for each oscillation of the module clock line. the logic dictating when the reload signal is asserted depends on the mode the msspx is being operated in. table 15-4 demonstrates clock rates based on instruction cycles and the brg value loaded into sspxadd. equation 15-1: figure 15-40: baud rate genera tor block diagram f clock f osc sspxadd 1 + ?? 4 ?? ------------------------------------------------- = note: values of 0x00, 0x01 and 0x02 are not valid for sspxadd when used as a baud rate generator for i 2 c. this is an implementation limitation. sspxm<3:0> brg down counter sspxclk f osc /2 sspxadd<7:0> sspxm<3:0> sclx reload control reload table 15-4: msspx clock rate w/brg f osc f cy brg value f clock (2 rollovers of brg) 32 mhz 8 mhz 13h 400 khz (1) 32 mhz 8 mhz 19h 308 khz 32 mhz 8 mhz 4fh 100 khz 16 mhz 4 mhz 09h 400 khz (1) 16 mhz 4 mhz 0ch 308 khz 16 mhz 4 mhz 27h 100 khz 4 mhz 1 mhz 09h 100 khz note 1: the i 2 c interface does not conform to the 400 khz i 2 c specification (which applies to rates greater than 100 khz) in all details, but may be used with care where higher rates are required by the application. ? 2010-2012 microchip technology inc. ds41412f-page 259 pic18(l)f2x/4xk22 15.8 register definitions: mssp control register 15-1: sspxstat: sspx status register r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a psr/w ua bf bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = val ue at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7 smp: spi data input sample bit spi master mode: 1 = input data sampled at end of data output time 0 = input data sampled at middle of data output time spi slave mode: smp must be cleared when spi is used in slave mode in i 2 c master or slave mode: 1 = slew rate control disabled for standard speed mode (100 khz and 1 mhz) 0 = slew rate control enabled for high speed mode (400 khz) bit 6 cke: spi clock edge select bit (spi mode only) in spi master or slave mode: 1 = transmit occurs on transition from active to idle clock state 0 = transmit occurs on transition from idle to active clock state in i 2 c mode only: 1 = enable input logic so that thresholds are compliant with smbus specification 0 = disable smbus specific inputs bit 5 d/a : data/address bit (i 2 c mode only) 1 = indicates that the last byte received or transmitted was data 0 = indicates that the last byte received or transmitted was address bit 4 p: stop bit (i 2 c mode only. this bit is cleared when the msspx module is disabled, sspxen is cleared.) 1 = indicates that a stop bit has been detected last (this bit is ? 0 ? on reset) 0 = stop bit was not detected last bit 3 s: start bit (i 2 c mode only. this bit is cleared when the msspx module is disabled, sspxen is cleared.) 1 = indicates that a start bit has been detected last (this bit is ? 0 ? on reset) 0 = start bit was not detected last bit 2 r/w : read/write bit information (i 2 c mode only) this bit holds the r/w bit information following the last address match. this bit is only valid from the address match to the next start bit, stop bit, or not ack bit. in i 2 c slave mode: 1 = read 0 = write in i 2 c master mode: 1 = transmit is in progress 0 = transmit is not in progress or-ing this bit with sen, rsen, pen, rcen or acken will indicate if the msspx is in idle mode. bit 1 ua: update address bit (10-bit i 2 c mode only) 1 = indicates that the user needs to update the address in the sspxadd register 0 = address does not need to be updated bit 0 bf: buffer full status bit receive (spi and i 2 c modes): 1 = receive complete, sspxbuf is full 0 = receive not complete, sspxbuf is empty transmit (i 2 c mode only): 1 = data transmit in progress (does not include the ack and stop bits), sspxbuf is full 0 = data transmit complete (does not include the ack and stop bits), sspxbuf is empty pic18(l)f2x/4xk22 ds41412f-page 260 ? 2010-2012 microchip technology inc. register 15-2: sspxcon1: ss px control register 1 r/c/hs-0 r/c/hs-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspxov sspxen ckp sspxm<3:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = val ue at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared hs = bit is set by hardware c = user cleared bit 7 wcol: write collision detect bit master mode: 1 = a write to the sspxbuf register was attempted while the i 2 c conditions were not valid for a transmission to be started 0 = no collision slave mode: 1 = the sspxbuf register is written while it is still transmitting the previous word (must be cleared in software) 0 = no collision bit 6 sspxov: receive overflow indicator bit (1) in spi mode: 1 = a new byte is received while the sspxbu f register is still holding the previous data. in case of overflow, the data in sspxsr is lost. overflow can only occur in slave mode. in slave mode, the user must read the sspxbuf, even if only transmitting data, to avoid setting overflow. in mast er mode, the overflow bit is not set since each new recep- tion (and transmission) is initiated by writing to the sspxbuf register (must be cleared in software). 0 = no overflow in i 2 c mode: 1 = a byte is received while the sspxbuf register is st ill holding the previous byte. sspxov is a ?don?t care? in transmit mode (must be cleared in software). 0 = no overflow bit 5 sspxen: synchronous serial port enable bit in both modes, when enabled, these pins must be properly configured as input or output in spi mode: 1 = enables serial port and configures sckx, sdox, sdix and ssx as the source of the serial port pins (2) 0 = disables serial port and configures these pins as i/o port pins in i 2 c mode: 1 = enables the serial port and configures the sdax and sclx pins as the source of the serial port pins (3) 0 = disables serial port and configures these pins as i/o port pins bit 4 ckp: clock polarity select bit in spi mode: 1 = idle state for clock is a high level 0 = idle state for clock is a low level in i 2 c slave mode: sclx release control 1 = enable clock 0 = holds clock low (clock stretch). (used to ensure data setup time.) in i 2 c master mode: unused in this mode ? 2010-2012 microchip technology inc. ds41412f-page 261 pic18(l)f2x/4xk22 bit 3-0 sspxm<3:0>: synchronous serial port mode select bits 0000 = spi master mode, clock = f osc /4 0001 = spi master mode, clock = f osc /16 0010 = spi master mode, clock = f osc /64 0011 = spi master mode, clock = tmr2 output/2 0100 = spi slave mode, clock = sckx pin, ssx pin control enabled 0101 = spi slave mode, clock = sckx pin, ssx pin control disabled, ssx can be used as i/o pin 0110 = i 2 c slave mode, 7-bit address 0111 = i 2 c slave mode, 10-bit address 1000 = i 2 c master mode, clock = f osc / (4 * (sspxadd+1)) (4) 1001 = reserved 1010 = spi master mode, clock = f osc /(4 * (sspxadd+1)) 1011 = i 2 c firmware controlled master mode (slave idle) 1100 = reserved 1101 = reserved 1110 = i 2 c slave mode, 7-bit address with start and stop bit interrupts enabled 1111 = i 2 c slave mode, 10-bit address with start and stop bit interrupts enabled note 1: in master mode, the overflow bit is not set since each new reception (and transmission) is initiated by writing to the sspxbuf register. 2: when enabled, these pins must be proper ly configured as input or output. 3: when enabled, the sdax and sclx pins must be configured as inputs. 4: sspxadd values of 0, 1 or 2 are not supported for i 2 c mode. register 15-2: sspxcon1: sspx co ntrol register 1 (continued) pic18(l)f2x/4xk22 ds41412f-page 262 ? 2010-2012 microchip technology inc. register 15-3: sspxcon2: ss px control register 2 r/w-0 r-0 r/w-0 r/s/hc-0 r/s/hc-0 r/s/hc-0 r/s/hc-0 r/w/hc-0 gcen ackstat ackdt acken (1) rcen (1) pen (1) rsen (1) sen (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared hc = cleared by hardware s = user set bit 7 gcen: general call enable bit (in i 2 c slave mode only) 1 = enable interrupt when a general call address (0x00 or 00h) is received in the sspxsr 0 = general call address disabled bit 6 ackstat: acknowledge status bit (in i 2 c mode only) 1 = acknowledge was not received 0 = acknowledge was received bit 5 ackdt: acknowledge data bit (in i 2 c mode only) in receive mode: value transmitted when the user initiates an acknowledge sequence at the end of a receive 1 = not acknowledge 0 = acknowledge bit 4 acken (1) : acknowledge sequence enable bit (in i 2 c master mode only) in master receive mode: 1 = initiate acknowledge sequence on sdax and sclx pins, and transmit ackdt data bit. automatically cleared by hardware. 0 = acknowledge sequence idle bit 3 rcen (1) : receive enable bit (in i 2 c master mode only) 1 = enables receive mode for i 2 c 0 = receive idle bit 2 pen (1) : stop condition enable bit (in i 2 c master mode only) sckx release control: 1 = initiate stop condition on sdax and sclx pins. automatically cleared by hardware. 0 = stop condition idle bit 1 rsen (1) : repeated start condition enabled bit (in i 2 c master mode only) 1 = initiate repeated start condition on sdax and sclx pins. automatically cleared by hardware. 0 = repeated start condition idle bit 0 sen (1) : start condition enabled bit (in i 2 c master mode only) in master mode: 1 = initiate start condition on sdax and sclx pins. automatically cleared by hardware. 0 = start condition idle in slave mode: 1 = clock stretching is enabled for both slave transmit and slave receive (stretch enabled) 0 = clock stretching is disabled note 1: for bits acken, rcen, pen, rsen, sen: if the i 2 c module is not in the idle mode, this bit may not be set (no spooling) and the sspxbuf may not be written (or writes to the sspxbuf are disabled). ? 2010-2012 microchip technology inc. ds41412f-page 263 pic18(l)f2x/4xk22 register 15-4: sspxcon3: ss px control register 3 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 acktim pcie scie boen sdaht sbcde ahen dhen bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7 acktim: acknowledge time status bit (i 2 c mode only) (3) 1 = indicates the i 2 c bus is in an acknowledge sequence, set on 8 th falling edge of sclx clock 0 = not an acknowledge sequence, cleared on 9 th rising edge of sclx clock bit 6 pcie : stop condition interrupt enable bit (i 2 c mode only) 1 = enable interrupt on detection of stop condition 0 = stop detection interrupts are disabled (2) bit 5 scie : start condition interrupt enable bit (i 2 c mode only) 1 = enable interrupt on detection of start or restart conditions 0 = start detection interrupts are disabled (2) bit 4 boen: buffer overwrite enable bit in spi slave mode: (1) 1 = sspxbuf updates every time that a new data byte is shifted in ignoring the bf bit 0 = if new byte is received with bf bit of the sspxstat register already set, sspxov bit of the sspxcon1 register is set, and the buffer is not updated in i 2 c master mode: this bit is ignored. in i 2 c slave mode: 1 = sspxbuf is updated and ack is generated for a received address/data byte, ignoring the state of the sspxov bit only if the bf bit = 0 . 0 = sspxbuf is only updated when sspxov is clear bit 3 sdaht: sdax hold time selection bit (i 2 c mode only) 1 = minimum of 300 ns hold time on sdax after the falling edge of sclx 0 = minimum of 100 ns hold time on sdax after the falling edge of sclx bit 2 sbcde: slave mode bus collision detect enable bit (i 2 c slave mode only) if on the rising edge of sclx, sdax is sampled low when the module is outputting a high state, the bclxif bit of the pir2 register is set, and bus goes idle 1 = enable slave bus collision interrupts 0 = slave bus collision interrupts are disabled bit 1 ahen: address hold enable bit (i 2 c slave mode only) 1 = following the 8th falling edge of sclx for a matching received address byte; ckp bit of the sspxcon1 register will be cleared and the sclx will be held low. 0 = address holding is disabled note 1: for daisy-chained spi operation; allows the user to ignore all but the last received byte. sspxov is still set when a new byte is received and bf = 1 , but hardware continues to write the most recent byte to sspxbuf. 2: this bit has no effect in slave modes for which start and stop condition detection is explicitly listed as enabled. 3: the acktim status bit is active only when the ahen bit or dhen bit is set. pic18(l)f2x/4xk22 ds41412f-page 264 ? 2010-2012 microchip technology inc. bit 0 dhen: data hold enable bit (i 2 c slave mode only) 1 = following the 8th falling edge of sclx for a received data byte; slave hardware clears the ckp bit of the sspxcon1 register and sclx is held low. 0 = data holding is disabled register 15-4: sspxcon3: sspx co ntrol register 3 (continued) note 1: for daisy-chained spi operation; allows the user to ignore all but the last received byte. sspxov is still set when a new byte is received and bf = 1 , but hardware continues to write the most recent byte to sspxbuf. 2: this bit has no effect in slave modes for which start and stop condition detection is explicitly listed as enabled. 3: the acktim status bit is active only when the ahen bit or dhen bit is set. register 15-5: sspxmsk: sspx mask register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 msk7 msk6 msk5 msk4 msk3 msk2 msk1 msk0 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7-1 msk<7:1>: mask bits 1 = the received address bit n is compared to sspxadd ? 2010-2012 microchip technology inc. ds41412f-page 265 pic18(l)f2x/4xk22 register 15-6: sspxadd: msspx address and baud rate register (i 2 c mode) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 add<7:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared master mode: bit 7-0 add<7:0>: baud rate clock divider bits sclx pin clock period = ((add<7:0> + 1) *4)/f osc 10-bit slave mode ? most significant address byte: bit 7-3 not used: unused for most significant address byte. bit state of this register is a ?don?t care?. bit pattern sent by master is fixed by i 2 c specification and must be equal to ? 11110 ?. however, those bits are compared by hardware and are not affected by the value in this register. bit 2-1 add<2:1>: two most significant bits of 10-bit address bit 0 not used: unused in this mode. bit state is a ?don?t care?. 10-bit slave mode ? least significant address byte: bit 7-0 add<7:0>: eight least significant bits of 10-bit address 7-bit slave mode: bit 7-1 add<7:1>: 7-bit address bit 0 not used: unused in this mode. bit state is a ?don?t care?. pic18(l)f2x/4xk22 ds41412f-page 266 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 267 pic18(l)f2x/4xk22 16.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) the enhanced universal synchronous asynchronous receiver transmitter (eusart) module is a serial i/o communications peripheral. it contains all the clock generators, shift registers and data buffers necessary to perform an input or output serial data transfer independent of device program execution. the eusart, also known as a serial communications interface (sci), can be configured as a full-duplex asynchronous system or half-duplex synchronous system. full-duplex mode is useful for communications with peripheral systems, such as crt terminals and personal computers. half-duplex synchronous mode is intended for communications with peripheral devices, such as a/d or d/a integrated circuits, serial eeproms or other microcontrollers. these devices typically do not have internal clocks for baud rate generation and require the external clock signal provided by a master synchronous device. the eusart module includes the following capabilities: ? full-duplex asynchronous transmit and receive ? two-character input buffer ? one-character output buffer ? programmable 8-bit or 9-bit character length ? address detection in 9-bit mode ? input buffer overrun error detection ? received character framing error detection ? half-duplex synchronous master ? half-duplex synchronous slave ? programmable clock and data polarity the eusart module implements the following additional features, making it ideally suited for use in local interconnect network (lin) bus systems: ? automatic detection and calibration of the baud rate ? wake-up on break reception ? 13-bit break character transmit block diagrams of the eusart transmitter and receiver are shown in figure 16-1 and figure 16-2 . figure 16-1: eusart transmi t block diagram txxif txxie interrupt txen tx9d msb lsb data bus txregx register transmit shift register (tsr) (8) 0 tx9 trmt txx/ckx pin pin buffer and control 8 spbrgx spbrghx brg16 f osc n n + 1 multiplier x4 x16 x64 sync 1x00 0 brgh x110 0 brg16 x101 0 baud rate generator ??? pic18(l)f2x/4xk22 ds41412f-page 268 ? 2010-2012 microchip technology inc. figure 16-2: eusart receiv e block diagram the operation of the eusart module is controlled through three registers: ? transmit status and control (txstax) ? receive status and control (rcstax) ? baud rate control (baudconx) these registers are detailed in register 16-1 , register 16-2 and register 16-3 , respectively. for all modes of eusart operation, the tris control bits corresponding to the rxx/dtx and txx/ckx pins should be set to ? 1 ?. the eusart control will automatically reconfigure the pin from input to output, as needed. when the receiver or transmitter section is not enabled then the corresponding rxx/dtx or txx/ckx pin may be used for general purpose input and output. rxx/dtx pin pin buffer and control data recovery cren oerr ferr rsr register msb lsb rx9d rcregx register fifo interrupt rcxif rcxie data bus 8 stop start (8) 7 1 0 rx9 ? ? ? spbrgx spbrghx brg16 rcidl f osc n n + 1 multiplier x4 x16 x64 sync 1x00 0 brgh x110 0 brg16 x101 0 baud rate generator ? 2010-2012 microchip technology inc. ds41412f-page 269 pic18(l)f2x/4xk22 16.1 eusart asynchronous mode the eusart transmits and receives data using the standard non-return-to-zero (nrz) format. nrz is implemented with two levels: a v oh mark state which represents a ? 1 ? data bit, and a v ol space state which represents a ? 0 ? data bit. nrz refers to the fact that consecutively transmitted data bits of the same value stay at the output level of that bit without returning to a neutral level between each bit transmission. an nrz transmission port idles in the mark state. each character transmission consists of one start bit followed by eight or nine data bits and is always terminated by one or more stop bits. the start bit is always a space and the stop bits are always marks. the most common data format is eight bits. each transmitted bit persists for a period of 1/(baud rate). an on-chip dedicated 8-bit/16- bit baud rate generator is used to derive standard baud rate frequencies from the system oscillator. see table 16-5 for examples of baud rate configurations. the eusart transmits and receives the lsb first. the eusart?s transmitter and receiver are functionally independent, but share the same data format and baud rate. parity is not supported by the hardware, but can be implemented in software and stored as the ninth data bit. 16.1.1 eusart asynchronous transmitter the eusart transmitter block diagram is shown in figure 16-1 . the heart of the transmitter is the serial transmit shift register (tsr), which is not directly accessible by software. the tsr obtains its data from the transmit buffer, which is the txregx register. 16.1.1.1 enabling the transmitter the eusart transmitter is enabled for asynchronous operations by configuring the following three control bits: ?txen = 1 ? sync = 0 ? spen = 1 all other eusart control bits are assumed to be in their default state. setting the txen bit of the txstax register enables the transmitter circuitry of the eusart. clearing the sync bit of the txstax register configures the eusart for asynchronous operation. setting the spen bit of the rcstax register enables the eusart and automatically configures the txx/ckx i/o pin as an output. if the txx/ckx pin is shared with an analog peripheral the analog i/o function must be disabled by clearing the corresponding ansel bit. 16.1.1.2 transmitting data a transmission is initiated by writing a character to the txregx register. if this is the first character, or the previous character has been completely flushed from the tsr, the data in the txregx is immediately transferred to the tsr register. if the tsr still contains all or part of a previous character, the new character data is held in the txregx until the stop bit of the previous character has been transmitted. the pending character in the txregx is then transferred to the tsr in one t cy immediately following the stop bit transmission. the transmission of the start bit, data bits and stop bit sequence commences immediately following the transfer of the data to the tsr from the txregx. 16.1.1.3 transmit data polarity the polarity of the transmit data can be controlled with the cktxp bit of the baudconx register. the default state of this bit is ? 0 ? which selects high true transmit idle and data bits. setting the cktxp bit to ? 1 ? will invert the transmit data resulting in low true idle and data bits. the cktxp bit controls transmit data polarity only in asynchronous mode. in synchronous mode the cktxp bit has a different function. 16.1.1.4 transmit interrupt flag the txxif interrupt flag bit of the pir1/pir3 register is set whenever the eusart transmitter is enabled and no character is being held for transmission in the txregx. in other words, the txxif bit is only clear when the tsr is busy with a character and a new character has been queued for transmission in the txregx. the txxif flag bit is not cleared immediately upon writing txregx. txxif becomes valid in the second instruction cycle following the write execution. polling txxif immediately following the txregx write will return invalid results. the txxif bit is read-only, it cannot be set or cleared by software. the txxif interrupt can be enabled by setting the txxie interrupt enable bit of the pie1/pie3 register. however, the txxif flag bit will be set whenever the txregx is empty, regardless of the state of txxie enable bit. to use interrupts when transmitting data, set the txxie bit only when there is more data to send. clear the txxie interrupt enable bit upon writing the last character of the transmission to the txregx. note: the txxif transmitter interrupt flag is set when the txen enable bit is set. pic18(l)f2x/4xk22 ds41412f-page 270 ? 2010-2012 microchip technology inc. 16.1.1.5 tsr status the trmt bit of the txstax register indicates the status of the tsr register. this is a read-only bit. the trmt bit is set when the tsr register is empty and is cleared when a character is transferred to the tsr register from the txregx. the trmt bit remains clear until all bits have been shifted out of the tsr register. no interrupt logic is tied to this bit, so the user needs to poll this bit to determine the tsr status. 16.1.1.6 transmitting 9-bit characters the eusart supports 9-bit character transmissions. when the tx9 bit of the txstax register is set the eusart will shift nine bits out for each character trans- mitted. the tx9d bit of the txstax register is the ninth, and most significant, data bit. when transmitting 9-bit data, the tx9d data bit must be written before writing the eight least significant bits into the txregx. all nine bits of data will be transferred to the tsr shift register immediately after the txregx is written. a special 9-bit address mode is available for use with multiple receivers. see section 16.1.2.8 ?address detection? for more information on the address mode. 16.1.1.7 asynchronous transmission setup: 1. initialize the spbrghx:spbrgx register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.4 ?eusart baud rate generator (brg)? ). 2. set the rxx/dtx and txx/ckx tris controls to ? 1 ?. 3. enable the asynchronous serial port by clearing the sync bit and setting the spen bit. 4. if 9-bit transmission is desired, set the tx9 con- trol bit. a set ninth data bit will indicate that the eight least significant data bits are an address when the receiver is set for address detection. 5. set the cktxp control bit if inverted transmit data polarity is desired. 6. enable the transmission by setting the txen control bit. this will cause the txxif interrupt bit to be set. 7. if interrupts are desired, set the txxie interrupt enable bit. an interrupt will occur immediately provided that the gie/gieh and peie/giel bits of the intcon register are also set. 8. if 9-bit transmission is selected, the ninth bit should be loaded into the tx9d data bit. 9. load 8-bit data into the txregx register. this will start the transmission. figure 16-3: asynchronous transmission note: the tsr register is not mapped in data memory, so it is not available to the user. word 1 stop bit word 1 transmit shift reg start bit bit 0 bit 1 bit 7/8 write to txregx word 1 brg output (shift clock) txx/ckx txxif bit (transmit buffer reg. empty flag) trmt bit (transmit shift reg. empty flag) 1 t cy pin ? 2010-2012 microchip technology inc. ds41412f-page 271 pic18(l)f2x/4xk22 figure 16-4: asynchronous transmiss ion (back-to-back) transmit shift reg write to txregx brg output (shift clock) txx/ckx txxif bit (interrupt reg. flag) trmt bit (transmit shift reg. empty flag) word 1 word 2 word 1 word 2 start bit stop bit start bit transmit shift reg word 1 word 2 bit 0 bit 1 bit 7/8 bit 0 note: this timing diagram shows two consecutive transmissions. 1 t cy 1 t cy pin table 16-1: registers associated with asynchronous transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 spbrg1 eusart1 baud rate generator, low byte ? spbrgh1 eusart1 baud rate generator, high byte ? spbrg2 eusart2 baud rate generator, low byte ? spbrgh2 eusart2 baud rate generator, high byte ? txreg1 eusart1 transmit register ? txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 txreg2 eusart2 transmit register ? txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for asynchronous transmission. pic18(l)f2x/4xk22 ds41412f-page 272 ? 2010-2012 microchip technology inc. 16.1.2 eusart asynchronous receiver the asynchronous mode would typically be used in rs-232 systems. the receiver block diagram is shown in figure 16-2 . the data is received on the rxx/dtx pin and drives the data recovery block. the data recovery block is actually a high-speed shifter operating at 16 times the baud rate, whereas the serial receive shift register (rsr) operates at the bit rate. when all eight or nine bits of the character have been shifted in, they are immediately transferred to a two character first-in-first-out (fifo) memory. the fifo buffering allows reception of two complete characters and the start of a third character before software must start servicing the eusart receiver. the fifo and rsr registers are not directly accessible by software. access to the received data is via the rcregx register. 16.1.2.1 enabling the receiver the eusart receiver is enabled for asynchronous operation by configuring the following three control bits: ? cren = 1 ? sync = 0 ? spen = 1 all other eusart control bits are assumed to be in their default state. setting the cren bit of the rcstax register enables the receiver circuitry of the eusart. clearing the sync bit of the txstax register configures the eusart for asynchronous operation. setting the spen bit of the rcstax register enables the eusart. the rxx/dtx i/o pin must be configured as an input by setting the corresponding tris control bit. if the rxx/dtx pin is shared with an analog peripheral the analog i/o function must be disabled by clearing the corresponding ansel bit. 16.1.2.2 receiving data the receiver data recovery circuit initiates character reception on the falling edge of the first bit. the first bit, also known as the start bit, is always a zero. the data recovery circuit counts one-half bit time to the center of the start bit and verifies that the bit is still a zero. if it is not a zero then the data recovery circuit aborts character reception, without generating an error, and resumes looking for the falling edge of the start bit. if the start bit zero verification succeeds then the data recovery circuit counts a full bit time to the center of the next bit. the bit is then sampled by a majority detect circuit and the resulting ? 0 ? or ? 1 ? is shifted into the rsr. this repeats until all data bits have been sampled and shifted into the rsr. one final bit time is measured and the level sampled. this is the stop bit, which is always a ? 1 ?. if the data recovery circuit samples a ? 0 ? in the stop bit position then a framing error is set for this character, otherwise the framing error is cleared for this character. see section 16.1.2.5 ?receive framing error? for more information on framing errors. immediately after all data bits and the stop bit have been received, the character in the rsr is transferred to the eusart receive fifo and the rcxif interrupt flag bit of the pir1/pir3 register is set. the top charac- ter in the fifo is transferred out of the fifo by reading the rcregx register. 16.1.2.3 receive data polarity the polarity of the receive data can be controlled with the dtrxp bit of the baudconx register. the default state of this bit is ? 0 ? which selects high true receive idle and data bits. setting the dtrxp bit to ? 1 ? will invert the receive data resulting in low true idle and data bits. the dtrxp bit controls receive data polarity only in asyn- chronous mode. in synchronous mode the dtrxp bit has a different function. note: if the receive fifo is overrun, no additional characters will be received until the overrun condition is cleared. see section 16.1.2.6 ?receive overrun error? for more information on overrun errors. ? 2010-2012 microchip technology inc. ds41412f-page 273 pic18(l)f2x/4xk22 16.1.2.4 receive interrupts the rcxif interrupt flag bit of the pir1/pir3 register is set whenever the eusart receiver is enabled and there is an unread character in the receive fifo. the rcxif interrupt flag bit is read-only, it cannot be set or cleared by software. rcxif interrupts are enabled by setting the following bits: ? rcxie interrupt enable bit of the pie1/pie3 register ? peie/giel peripheral interrupt enable bit of the intcon register ? gie/gieh global interrupt enable bit of the intcon register the rcxif interrupt flag bit will be set when there is an unread character in the fifo, regardless of the state of interrupt enable bits. 16.1.2.5 receive framing error each character in the receive fifo buffer has a corresponding framing error status bit. a framing error indicates that a stop bit was not seen at the expected time. the framing error status is accessed via the ferr bit of the rcstax register. the ferr bit represents the status of the top unread character in the receive fifo. therefore, the ferr bit must be read before reading the rcreg.x the ferr bit is read-only and only applies to the top unread character in the receive fifo. a framing error (ferr = 1 ) does not preclude reception of additional characters. it is not necessary to clear the ferr bit. reading the next character from the fifo buffer will advance the fifo to the next character and the next corresponding framing error. the ferr bit can be forced clear by clearing the spen bit of the rcstax register which resets the eusart. clearing the cren bit of the rcstax register does not affect the ferr bit. a framing error by itself does not generate an interrupt. 16.1.2.6 receive overrun error the receive fifo buffer can hold two characters. an overrun error will be generated if a third character, in its entirety, is received before the fifo is accessed. when this happens the oerr bit of the rcstax register is set. the characters already in the fifo buffer can be read but no additional characters will be received until the error is cleared. the error must be cleared by either clearing the cren bit of the rcstax register or by resetting the eusart by clearing the spen bit of the rcstax register. 16.1.2.7 receiving 9-bit characters the eusart supports 9-bit character reception. when the rx9 bit of the rcstax register is set, the eusart will shift nine bits into the rsr for each character received. the rx9d bit of the rcstax register is the ninth and most significant data bit of the top unread character in the receive fifo. when reading 9-bit data from the receive fifo buffer, the rx9d data bit must be read before reading the eight least significant bits from the rcregx. 16.1.2.8 address detection a special address detection mode is available for use when multiple receivers share the same transmission line, such as in rs-485 systems. address detection is enabled by setting the adden bit of the rcstax register. address detection requires 9-bit character reception. when address detection is enabled, only characters with the ninth data bit set will be transferred to the receive fifo buffer, thereby setting the rcxif interrupt bit. all other characters will be ignored. upon receiving an address character, user software determines if the address matches its own. upon address match, user software must disable address detection by clearing the adden bit before the next stop bit occurs. when user software detects the end of the message, determined by the message protocol used, software places the receiver back into the address detection mode by setting the adden bit. note: if all receive characters in the receive fifo have framing errors, repeated reads of the rcregx will not clear the ferr bit. pic18(l)f2x/4xk22 ds41412f-page 274 ? 2010-2012 microchip technology inc. 16.1.2.9 asynchronous reception setup: 1. initialize the spbrghx:spbrgx register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.4 ?eusart baud rate generator (brg)? ). 2. set the rxx/dtx and txx/ckx tris controls to ? 1 ?. 3. enable the serial port by setting the spen bit and the rxx/dtx pin tris bit. the sync bit must be clear for asynchronous operation. 4. if interrupts are desired, set the rcxie interrupt enable bit and set the gie/gieh and peie/giel bits of the intcon register. 5. if 9-bit reception is desired, set the rx9 bit. 6. set the dtrxp if inverted receive polarity is desired. 7. enable reception by setting the cren bit. 8. the rcxif interrupt flag bit will be set when a character is transferred from the rsr to the receive buffer. an interrupt will be generated if the rcxie interrupt enable bit was also set. 9. read the rcstax register to get the error flags and, if 9-bit data reception is enabled, the ninth data bit. 10. get the received eight least significant data bits from the receive buffer by reading the rcregx register. 11. if an overrun occurred, clear the oerr flag by clearing the cren receiver enable bit. 16.1.2.10 9-bit address detection mode setup this mode would typically be used in rs-485 systems. to set up an asynchronous reception with address detect enable: 1. initialize the spbrghx, spbrgx register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.4 ?eusart baud rate generator (brg)? ). 2. set the rxx/dtx and txx/ckx tris controls to ? 1 ?. 3. enable the serial port by setting the spen bit. the sync bit must be clear for asynchronous operation. 4. if interrupts are desired, set the rcxie interrupt enable bit and set the gie/gieh and peie/giel bits of the intcon register. 5. enable 9-bit reception by setting the rx9 bit. 6. enable address detection by setting the adden bit. 7. set the dtrxp if inverted receive polarity is desired. 8. enable reception by setting the cren bit. 9. the rcxif interrupt flag bit will be set when a character with the ninth bit set is transferred from the rsr to the receive buffer. an interrupt will be generated if the rcxie interrupt enable bit was also set. 10. read the rcstax register to get the error flags. the ninth data bit will always be set. 11. get the received eight least significant data bits from the receive buffer by reading the rcregx register. software determines if this is the device?s address. 12. if an overrun occurred, clear the oerr flag by clearing the cren receiver enable bit. 13. if the device has been addressed, clear the adden bit to allow all received data into the receive buffer and generate interrupts. ? 2010-2012 microchip technology inc. ds41412f-page 275 pic18(l)f2x/4xk22 figure 16-5: asynchronous reception start bit bit 7/8 bit 1 bit 0 bit 7/8 bit 0 stop bit start bit start bit bit 7/8 stop bit rxx/dtx pin reg rcv buffer reg rcv shift read rcv buffer reg rcregx rcxif (interrupt flag) oerr bit cren word 1 rcregx word 2 rcregx stop bit note: this timing diagram shows three words appearing on the rxx/dtx input. the rcregx (receive buffer) is read after the third word, causing the oerr (overrun) bit to be set. rcidl table 16-2: registers associated with asynchronous reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 rcreg1 eusart1 receive register ? rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 rcreg2 eusart2 receive register ? rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 spbrg1 eusart1 baud rate generator, low byte ? spbrgh1 eusart1 baud rate generator, high byte ? spbrg2 eusart2 baud rate generator, low byte ? spbrgh2 eusart2 baud rate generator, high byte ? trisb (2) trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for asynchronous reception. note 1: pic18(l)f4xk22 devices. 2: pic18(l)f2xk22 devices. pic18(l)f2x/4xk22 ds41412f-page 276 ? 2010-2012 microchip technology inc. 16.2 clock accuracy with asynchronous operation the factory calibrates the internal oscillator block output (hfintosc). however, the hfintosc frequency may drift as v dd or temperature changes, and this directly affects the asynchronous baud rate. two methods may be used to adjust the baud rate clock, but both require a reference clock source of some kind. the first (preferred) method uses the osctune register to adjust the hfintosc output. adjusting the value in the osctune register allows for fine resolution changes to the system clock source. see section 2.6 ?internal clock modes? for more information. the other method adjusts the value in the baud rate generator. this can be done automatically with the auto-baud detect feature (see section 16.4.1 ?auto- baud detect? ). there may not be fine enough resolution when adjusting the baud rate generator to compensate for a gradual change in the peripheral clock frequency. ? 2010-2012 microchip technology inc. ds41412f-page 277 pic18(l)f2x/4xk22 16.3 register definitions: eusart control register 16-1: txstax: transmi t status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-1 r/w-0 csrc tx9 txen (1) sync sendb brgh trmt tx9d bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 csrc: clock source select bit asynchronous mode : don?t care synchronous mode : 1 = master mode (clock generated internally from brg) 0 = slave mode (clock from external source) bit 6 tx9: 9-bit transmit enable bit 1 = selects 9-bit transmission 0 = selects 8-bit transmission bit 5 txen: transmit enable bit (1) 1 = transmit enabled 0 = transmit disabled bit 4 sync: eusart mode select bit 1 = synchronous mode 0 = asynchronous mode bit 3 sendb: send break character bit asynchronous mode : 1 = send sync break on next transmission (cleared by hardware upon completion) 0 = sync break transmission completed synchronous mode : don?t care bit 2 brgh: high baud rate select bit asynchronous mode : 1 = high speed 0 = low speed synchronous mode: unused in this mode bit 1 trmt: transmit shift register status bit 1 = tsr empty 0 = tsr full bit 0 tx9d: ninth bit of transmit data can be address/data bit or a parity bit. note 1: sren/cren overrides txen in sync mode. pic18(l)f2x/4xk22 ds41412f-page 278 ? 2010-2012 microchip technology inc. register 16-2: rcstax: receive status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-0 r-0 r-0 spen rx9 sren cren adden ferr oerr rx9d 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 spen: serial port enable bit 1 = serial port enabled (configures rxx/dtx and txx/ckx pins as serial port pins) 0 = serial port disabled (held in reset) bit 6 rx9: 9-bit receive enable bit 1 = selects 9-bit reception 0 = selects 8-bit reception bit 5 sren: single receive enable bit asynchronous mode : don?t care synchronous mode ? master : 1 = enables single receive 0 = disables single receive this bit is cleared after reception is complete. synchronous mode ? slave don?t care bit 4 cren: continuous receive enable bit asynchronous mode : 1 = enables receiver 0 = disables receiver synchronous mode : 1 = enables continuous receive until enable bit cren is cleared (cren overrides sren) 0 = disables continuous receive bit 3 adden: address detect enable bit asynchronous mode 9-bit (rx9 = 1 ) : 1 = enables address detection, enable interrupt and load the receive buffer when rsr<8> is set 0 = disables address detection, all bytes are received and ninth bit can be used as parity bit asynchronous mode 8-bit (rx9 = 0 ) : don?t care bit 2 ferr: framing error bit 1 = framing error (can be updated by reading rcregx register and receive next valid byte) 0 = no framing error bit 1 oerr: overrun error bit 1 = overrun error (can be cleared by clearing bit cren) 0 = no overrun error bit 0 rx9d: ninth bit of received data this can be address/data bit or a parity bit and must be calculated by user firmware. ? 2010-2012 microchip technology inc. ds41412f-page 279 pic18(l)f2x/4xk22 register 16-3: baudconx: baud rate control register r/w-0 r-1 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 abdovf rcidl dtrxp cktxp brg16 ? wue abden bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 abdovf: auto-baud detect overflow bit asynchronous mode : 1 = auto-baud timer overflowed 0 = auto-baud timer did not overflow synchronous mode : don?t care bit 6 rcidl : receive idle flag bit asynchronous mode : 1 = receiver is idle 0 = start bit has been detected and the receiver is active synchronous mode : don?t care bit 5 dtrxp : data/receive polarity select bit asynchronous mode : 1 = receive data (rxx) is inverted (active-low) 0 = receive data (rxx) is not inverted (active-high) synchronous mode : 1 = data (dtx) is inverted (active-low) 0 = data (dtx) is not inverted (active-high) bit 4 cktxp : clock/transmit polarity select bit asynchronous mode : 1 = idle state for transmit (txx) is low 0 = idle state for transmit (txx) is high synchronous mode : 1 = data changes on the falling edge of the clock and is sampled on the rising edge of the clock 0 = data changes on the rising edge of the clock and is sampled on the falling edge of the clock bit 3 brg16: 16-bit baud rate generator bit 1 = 16-bit baud rate generator is used (spbrghx:spbrgx) 0 = 8-bit baud rate generator is used (spbrgx) bit 2 unimplemented: read as ? 0 ? bit 1 wue: wake-up enable bit asynchronous mode : 1 = receiver is waiting for a falling edge. no character will be received but rcxif will be set on the falling edge. wue will automatically clear on the rising edge. 0 = receiver is operating normally synchronous mode : don?t care bit 0 abden : auto-baud detect enable bit asynchronous mode : 1 = auto-baud detect mode is enabled (clears when auto-baud is complete) 0 = auto-baud detect mode is disabled synchronous mode : don?t care pic18(l)f2x/4xk22 ds41412f-page 280 ? 2010-2012 microchip technology inc. 16.4 eusart baud rate generator (brg) the baud rate generator (brg) is an 8-bit or 16-bit timer that is dedicated to the support of both the asynchronous and synchronous eusart operation. by default, the brg operates in 8-bit mode. setting the brg16 bit of the baudconx register selects 16-bit mode. the spbrghx:spbrgx register pair determines the period of the free running baud rate timer. in asynchronous mode the multiplier of the baud rate period is determined by both the brgh bit of the txstax register and the brg16 bit of the baudconx register. in synchronous mode, the brgh bit is ignored. table 16-3 contains the formulas for determining the baud rate. example 16-1 provides a sample calculation for determining the baud rate and baud rate error. typical baud rates and error values for various asynchronous modes have been computed for your convenience and are shown in table 16-5 . it may be advantageous to use the high baud rate (brgh = 1 ), or the 16-bit brg (brg16 = 1 ) to reduce the baud rate error. the 16-bit brg mode is used to achieve slow baud rates for fast oscillator frequencies. writing a new value to the spbrghx, spbrgx register pair causes the brg timer to be reset (or cleared). this ensures that the brg does not wait for a timer overflow before outputting the new baud rate. if the system clock is changed during an active receive operation, a receive error or data loss may result. to avoid this problem, check the status of the rcidl bit to make sure that the receive operation is idle before changing the system clock. example 16-1: calculating baud rate error for a device with f osc of 16 mhz, desired baud rate of 9600, asynchronous mode, 8-bit brg: solving for spbrghx:spbrgx: x f osc desired baud rate --------------------------------------------- 64 --------------------------------------------- 1 ? = desired baud rate f osc 64 [spbrghx:spbrgx] 1 + ?? ------------------------------------------------------------------------- - = 16000000 9600 ----------------------- - 64 ----------------------- -1 ? = 25.042 ?? 25 == calculated baud rate 16000000 64 25 1 + ?? -------------------------- - = 9615 = error calc. baud rate desired baud rate ? desired baud rate -------------------------------------------------------------------------------------------- = 9615 9600 ? ?? 9600 ---------------------------------- 0 . 1 6 % == table 16-3: baud rate formulas configuration bits brg/eusart mode baud rate formula sync brg16 brgh 000 8-bit/asynchronous f osc /[64 (n+1)] 001 8-bit/asynchronous f osc /[16 (n+1)] 010 16-bit/asynchronous 011 16-bit/asynchronous f osc /[4 (n+1)] 10x 8-bit/synchronous 11x 16-bit/synchronous legend: x = don?t care, n = value of spbrghx, spbrgx register pair. ? 2010-2012 microchip technology inc. ds41412f-page 281 pic18(l)f2x/4xk22 table 16-4: registers associated with baud rate generator name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 spbrg1 eusart1 baud rate generator, low byte ? spbrgh1 eusart1 baud rate generator, high byte ? spbrg2 eusart2 baud rate generator, low byte ? spbrgh2 eusart2 baud rate generator, high byte ? pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented, read as ? 0 ?. shaded bits are not used by the brg. table 16-5: baud rates for asynchronous modes baud rate sync = 0 , brgh = 0 , brg16 = 0 f osc = 64.000 mhz f osc = 18.432 mhz f osc = 16.000 mhz f osc = 11.0592 mhz actual rate % error spbrxg value (decimal) actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) 300 ? ? ? ? ? ? ? ? ? ? ? ? 1200 ?? ? 1200 0.00 239 1202 0.16 207 1200 0.00 143 2400 ?? ? 2400 0.00 119 2404 0.16 103 2400 0.00 71 9600 9615 0.16 103 9600 0.00 29 9615 0.16 25 9600 0.00 17 10417 10417 0.00 95 10286 -1.26 27 10417 0.00 23 10165 -2.42 16 19.2k 19.23k 0.16 51 19.20k 0.00 14 19.23k 0.16 12 19.20k 0.00 8 57.6k 58.82k 2.12 16 57.60k 0.00 7 ?? ? 57.60k 0.00 2 115.2k 111.11k -3.55 8 ? ? ? ? ? ? ? ? ? baud rate sync = 0 , brgh = 0 , brg16 = 0 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) 300 ? ? ? 300 0.16 207 300 0.00 191 300 0.16 51 1200 1202 0.16 103 1202 0.16 51 1200 0.00 47 1202 0.16 12 2400 2404 0.16 51 2404 0.16 25 2400 0.00 23 ? ? ? 9600 9615 0.16 12 ? ? ? 9600 0.00 5 ? ? ? 10417 10417 0.00 11 10417 0.00 5 ? ? ? ? ? ? 19.2k ? ? ? ? ? ? 19.20k 0.00 2 ? ? ? 57.6k ? ? ? ? ? ? 57.60k 0.00 0 ? ? ? 115.2k ? ? ? ? ? ? ? ? ? ? ? ? pic18(l)f2x/4xk22 ds41412f-page 282 ? 2010-2012 microchip technology inc. baud rate sync = 0 , brgh = 1 , brg16 = 0 f osc = 64.000 mhz f osc = 18.432 mhz f osc = 16.000 mhz f osc = 11.0592 mhz actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) 300 ?? ? ?? ? ? ? ? ? ? ? 1200 ? ? ? ? ? ? ? ? ? ? ? ? 2400 ? ? ? ? ? ? ?? ? ?? ? 9600 ? ? ? 9600 0.00 119 9615 0.16 103 9600 0.00 71 10417 ? ? ? 10378 -0.37 110 10417 0.00 95 10473 0.53 65 19.2k 19.23k 0.16 207 19.20k 0.00 59 19.23k 0.16 51 19.20k 0.00 35 57.6k 57.97k 0.64 68 57.60k 0.00 19 58.82k 2.12 16 57.60k 0.00 11 115.2k 114.29k -0.79 34 115.2k 0.00 9 111.1k -3.55 8 115.2k 0.00 5 baud rate sync = 0 , brgh = 1 , brg16 = 0 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrgx value (decimal) actual rate % error spbrgx value (decimal) actual rate % error sxbrgx value (decimal) actual rate % error spbrgx value (decimal) 300 ?? ? ? ? ? ? ? ? 300 0.16 207 1200 ? ? ? 1202 0.16 207 1200 0.00 191 1202 0.16 51 2400 2404 0.16 207 2404 0.16 103 2400 0.00 95 2404 0.16 25 9600 9615 0.16 51 9615 0.16 25 9600 0.00 23 ? ? ? 10417 10417 0.00 47 10417 0.00 23 10473 0.53 21 10417 0.00 5 19.2k 19231 0.16 25 19.23k 0.16 12 19.2k 0.00 11 ? ? ? 57.6k 55556 -3.55 8 ? ? ? 57.60k 0.00 3 ? ? ? 115.2k ? ? ? ? ? ? 115.2k 0.00 1 ? ? ? baud rate sync = 0 , brgh = 0 , brg16 = 1 f osc = 64.000 mhz f osc = 18.432 mhz f osc = 16.000 mhz f osc = 11.0592 mhz actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx :spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) 300 300.0 0.00 13332 300.0 0.00 3839 300.03 0.01 3332 300.0 0.00 2303 1200 1200.1 0.01 3332 1200 0.00 959 1200.5 0.04 832 1200 0.00 575 2400 2399 -0.02 1666 2400 0.00 479 2398 -0.08 416 2400 0.00 287 9600 9592 -0.08 416 9600 0.00 119 9615 0.16 103 9600 0.00 71 10417 10417 0.00 383 10378 -0.37 110 10417 0.00 95 10473 0.53 65 19.2k 19.23k 0.16 207 19.20k 0.00 59 19.23k 0.16 51 19.20k 0.00 35 57.6k 57.97k 0.64 68 57.60k 0.00 19 58.82k 2.12 16 57.60k 0.00 11 115.2k 114.29k -0.79 34 115.2k 0.00 9 111.11k -3.55 8 115.2k 0.00 5 table 16-5: baud rates for asynchronous modes (continued) ? 2010-2012 microchip technology inc. ds41412f-page 283 pic18(l)f2x/4xk22 baud rate sync = 0 , brgh = 0 , brg16 = 1 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx :spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) 300 299.9 -0.02 1666 300.1 0.04 832 300.0 0.00 767 300.5 0.16 207 1200 1199 -0.08 416 1202 0.16 207 1200 0.00 191 1202 0.16 51 2400 2404 0.16 207 2404 0.16 103 2400 0.00 95 2404 0.16 25 9600 9615 0.16 51 9615 0.16 25 9600 0.00 23 ? ? ? 10417 10417 0.00 47 10417 0.00 23 10473 0.53 21 10417 0.00 5 19.2k 19.23k 0.16 25 19.23k 0.16 12 19.20k 0.00 11 ? ? ? 57.6k 55556 -3.55 8 ? ? ? 57.60k 0.00 3 ? ? ? 115.2k ? ? ? ? ? ? 115.2k 0.00 1 ? ? ? baud rate sync = 0 , brgh = 1 , brg16 = 1 or sync = 1 , brg16 = 1 f osc = 64.000 mhz f osc = 18.432 mhz f osc = 16.000 mhz f osc = 11.0592 mhz actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx :spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) 300 300 0.00 53332 300.0 0.00 15359 300.0 0.00 13332 300.0 0.00 9215 1200 1200 0.00 13332 1200 0.00 3839 1200.1 0.01 3332 1200 0.00 2303 2400 2400 0.00 6666 2400 0.00 1919 2399.5 -0.02 1666 2400 0.00 1151 9600 9598.1 -0.02 1666 9600 0.00 479 9592 -0.08 416 9600 0.00 287 10417 10417 0.00 1535 10425 0.08 441 10417 0.00 383 10433 0.16 264 19.2k 19.21k 0.04 832 19.20k 0.00 239 19.23k 0.16 207 19.20k 0.00 143 57.6k 57.55k -0.08 277 57.60k 0.00 79 57.97k 0.64 68 57.60k 0.00 47 115.2k 115.11k -0.08 138 115.2k 0.00 39 114.29k -0.79 34 115.2k 0.00 23 baud rate sync = 0 , brgh = 1 , brg16 = 1 or sync = 1 , brg16 = 1 f osc = 8.000 mhz f osc = 4.000 mhz f osc = 3.6864 mhz f osc = 1.000 mhz actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) actual rate % error spbrghx :spbrgx (decimal) actual rate % error spbrghx: spbrgx (decimal) 300 300.0 0.00 6666 300.0 0.01 3332 300.0 0.00 3071 300.1 0.04 832 1200 1200 -0.02 1666 1200 0.04 832 1200 0.00 767 1202 0.16 207 2400 2401 0.04 832 2398 0.08 416 2400 0.00 383 2404 0.16 103 9600 9615 0.16 207 9615 0.16 103 9600 0.00 95 9615 0.16 25 10417 10417 0.00 191 10417 0.00 95 10473 0.53 87 10417 0.00 23 19.2k 19.23k 0.16 103 19.23k 0.16 51 19.20k 0.00 47 19.23k 0.16 12 57.6k 57.14k -0.79 34 58.82k 2.12 16 57.60k 0.00 15 ? ? ? 115.2k 117.6k 2.12 16 111.1k -3.55 8 115.2k 0.00 7 ? ? ? table 16-5: baud rates for asynchronous modes (continued) pic18(l)f2x/4xk22 ds41412f-page 284 ? 2010-2012 microchip technology inc. 16.4.1 auto-baud detect the eusart module supports automatic detection and calibration of the baud rate. in the auto-baud detect (abd) mode, the clock to the brg is reversed. rather than the brg clocking the incoming rxx signal, the rxx signal is timing the brg. the baud rate generator is used to time the period of a received 55h (ascii ?u?) which is the sync character for the lin bus. the unique feature of this character is that it has five rising edges including the stop bit edge. setting the abden bit of the baudconx register starts the auto-baud calibration sequence ( section 16.4.2 ?auto-baud overflow? ). while the abd sequence takes place, the eusart state machine is held in idle. on the first rising edge of the receive line, after the start bit, the spbrgx begins counting up using the brg counter clock as shown in table 16-6 . the fifth rising edge will occur on the rxx/ dtx pin at the end of the eighth bit period. at that time, an accumulated value totaling the proper brg period is left in the spbrghx:spbrgx register pair, the abden bit is automatically cleared, and the rcxif interrupt flag is set. a read operation on the rcregx needs to be performed to clear the rcxif interrupt. rcregx content should be discarded. when calibrating for modes that do not use the spbrghx register the user can verify that the spbrgx register did not overflow by checking for 00h in the spbrghx register. the brg auto-baud clock is determined by the brg16 and brgh bits as shown in table 16-6 . during abd, both the spbrghx and spbrgx registers are used as a 16-bit counter, independent of the brg16 bit setting. while calibrating the baud rate period, the spbrghx and spbrgx registers are clocked at 1/8th the brg base clock rate. the resulting byte measurement is the average bit time when clocked at full speed. figure 16-6: automatic baud rate calibration note 1: if the wue bit is set with the abden bit, auto-baud detection will occur on the byte following the break character (see section 16.4.3 ?auto-wake-up on break? ). 2: it is up to the user to determine that the incoming character baud rate is within the range of the selected brg clock source. some combinations of oscillator frequency and eusart baud rates are not possible. 3: during the auto-baud process, the auto- baud counter starts counting at one. upon completion of the auto-baud sequence, to achieve maximum accuracy, subtract one from the spbrghx:spbrgx register pair. table 16-6: brg counter clock rates brg16 brgh brg base clock brg abd clock 00 f osc /64 f osc /512 01 f osc /16 f osc /128 10 f osc /16 f osc /128 11 f osc /4 f osc /32 note: during the abd sequence, spbrgx and spbrghx registers are both used as a 16-bit counter, independent of brg16 setting. brg value rxx/dtx pin abden bit rcxif bit bit 0 bit 1 (interrupt) read rcregx brg clock start auto cleared set by user xxxxh 0000h edge #1 bit 2 bit 3 edge #2 bit 4 bit 5 edge #3 bit 6 bit 7 edge #4 stop bit edge #5 001ch note 1: the abd sequence requires the eusart module to be configured in asynchronous mode. spbrgx xxh 1ch spbrghx xxh 00h rcidl ? 2010-2012 microchip technology inc. ds41412f-page 285 pic18(l)f2x/4xk22 16.4.2 auto-baud overflow during the course of automatic baud detection, the abdovf bit of the baudconx register will be set if the baud rate counter overflows before the fifth rising edge is detected on the rx pin. the abdovf bit indicates that the counter has exceeded the maximum count that can fit in the 16 bits of the spbrghx:spbrgx register pair. after the abdovf has been set, the counter con- tinues to count until the fifth rising edge is detected on the rxx/dtx pin. upon detecting the fifth rxx/dtx edge, the hardware will set the rcxif interrupt flag and clear the abden bit of the baudconx register. the rcxif flag can be subsequently cleared by reading the rcregx. the abdovf flag can be cleared by soft- ware directly. to terminate the auto-baud process before the rcxif flag is set, clear the abden bit then clear the abdovf bit. the abdovf bit will remain set if the abden bit is not cleared first. 16.4.3 auto-wake-up on break during sleep mode, all clocks to the eusart are suspended. because of this, the baud rate generator is inactive and a proper character reception cannot be performed. the auto-wake-up feature allows the controller to wake-up due to activity on the rxx/dtx line. this feature is available only in asynchronous mode. the auto-wake-up feature is enabled by setting the wue bit of the baudconx register. once set, the normal receive sequence on rxx/dtx is disabled, and the eusart remains in an idle state, monitoring for a wake-up event independent of the cpu mode. a wake- up event consists of a high-to-low transition on the rxx/dtx line. (this coincides with the start of a sync break or a wake-up signal character for the lin protocol.) the eusart module generates an rcxif interrupt coincident with the wake-up event. the interrupt is generated synchronously to the q clocks in normal cpu operating modes ( figure 16-7 ), and asynchronously if the device is in sleep mode ( figure 16-8 ). the interrupt condition is cleared by reading the rcregx register. the wue bit is automatically cleared by the low-to-high transition on the rxx line at the end of the break. this signals to the user that the break event is over. at this point, the eusart module is in idle mode waiting to receive the next character. 16.4.3.1 special considerations break character to avoid character errors or character fragments during a wake-up event, the wake-up character must be all zeros. when the wake-up is enabled the function works independent of the low time on the data stream. if the wue bit is set and a valid non-zero character is received, the low time from the start bit to the first rising edge will be interpreted as the wake-up event. the remaining bits in the character will be received as a fragmented character and subsequent characters can result in framing or overrun errors. therefore, the initial character in the transmission must be all ? 0 ?s. this must be 10 or more bit times, 13-bit times recommended for lin bus, or any number of bit times for standard rs-232 devices. oscillator start-up time oscillator start-up time must be considered, especially in applications using oscillators with longer start-up intervals (i.e., lp, xt or hs/pll mode). the sync break (or wake-up signal) character must be of sufficient length, and be followed by a sufficient interval, to allow enough time for the selected oscillator to start and provide proper initialization of the eusart. wue bit the wake-up event causes a receive interrupt by setting the rcxif bit. the wue bit is cleared by hardware by a rising edge on rxx/dtx. the interrupt condition is then cleared by software by reading the rcregx register and discarding its contents. to ensure that no actual data is lost, check the rcidl bit to verify that a receive operation is not in process before setting the wue bit. if a receive operation is not occurring, the wue bit may then be set just prior to entering the sleep mode. pic18(l)f2x/4xk22 ds41412f-page 286 ? 2010-2012 microchip technology inc. figure 16-7: auto-wake-up bit (wue) timing during normal operation figure 16-8: auto-wake-up bit (wue) timings during sleep q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rxx/dtx line rcxif bit set by user auto cleared cleared due to user read of rcregx note 1: the eusart remains in idle while the wue bit is set. q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rxx/dtx line rcxif bit set by user auto cleared cleared due to user read of rcregx sleep command executed note 1 note 1: if the wake-up event requires long oscillator warm-up time, the automatic clearing of the wue bit can occur while the stposc signal is still active. this sequence should not depend on the presence of q clocks. 2: the eusart remains in idle while the wue bit is set. sleep ends ? 2010-2012 microchip technology inc. ds41412f-page 287 pic18(l)f2x/4xk22 16.4.4 break character sequence the eusart module has the capability of sending the special break character sequences that are required by the lin bus standard. a break character consists of a start bit, followed by 12 ? 0 ? bits and a stop bit. to send a break character, set the sendb and txen bits of the txstax register. the break character trans- mission is then initiated by a write to the txregx. the value of data written to txregx will be ignored and all ? 0 ?s will be transmitted. the sendb bit is automatically reset by hardware after the corresponding stop bit is sent. this allows the user to preload the transmit fifo with the next transmit byte following the break character (typically, the sync character in the lin specification). the trmt bit of the txstax register indicates when the transmit operation is active or idle, just as it does during normal transmission. see figure 16-9 for the timing of the break character sequence. 16.4.4.1 break and sync transmit sequence the following sequence will start a message frame header made up of a break, followed by an auto-baud sync byte. this sequence is typical of a lin bus master. 1. configure the eusart for the desired mode. 2. set the txen and sendb bits to enable the break sequence. 3. load the txregx with a dummy character to initiate transmission (the value is ignored). 4. write ?55h? to txregx to load the sync charac- ter into the transmit fifo buffer. 5. after the break has been sent, the sendb bit is reset by hardware and the sync character is then transmitted. when the txregx becomes empty, as indicated by the txxif, the next data byte can be written to txregx. 16.4.5 receiving a break character the enhanced eusart module can receive a break character in two ways. the first method to detect a break character uses the ferr bit of the rcstax register and the received data as indicated by rcregx. the baud rate generator is assumed to have been initialized to the expected baud rate. a break character has been received when; ? rcxif bit is set ? ferr bit is set ? rcregx = 00h the second method uses the auto-wake-up feature described in section 16.4.3 ?auto-wake-up on break? . by enabling this feature, the eusart will sample the next two transitions on rxx/dtx, cause an rcxif interrupt, and receive the next data byte followed by another interrupt. note that following a break character, the user will typically want to enable the auto-baud detect feature. for both methods, the user can set the abden bit of the baudconx register before placing the eusart in sleep mode. figure 16-9: send break character sequence write to txregx dummy write brg output (shift clock) start bit bit 0 bit 1 bit 11 stop bit break txxif bit (transmit interrupt flag) txx/ckx (pin) trmt bit (transmit shift reg. empty flag) sendb (send break control bit) sendb sampled here auto cleared pic18(l)f2x/4xk22 ds41412f-page 288 ? 2010-2012 microchip technology inc. 16.5 eusart synchronous mode synchronous serial communications are typically used in systems with a single master and one or more slaves. the master device contains the necessary circuitry for baud rate generation and supplies the clock for all devices in the system. slave devices can take advantage of the master clock by eliminating the internal clock generation circuitry. there are two signal lines in synchronous mode: a bidirectional data line and a clock line. slaves use the external clock supplied by the master to shift the serial data into and out of their respective receive and transmit shift registers. since the data line is bidirectional, synchronous operation is half-duplex only. half-duplex refers to the fact that master and slave devices can receive and transmit data but not both simultaneously. the eusart can operate as either a master or slave device. start and stop bits are not used in synchronous transmissions. 16.5.1 synchronous master mode the following bits are used to configure the eusart for synchronous master operation: ? sync = 1 ? csrc = 1 ? sren = 0 (for transmit); sren = 1 (for receive) ? cren = 0 (for transmit); cren = 1 (for receive) ? spen = 1 setting the sync bit of the txstax register configures the device for synchronous operation. setting the csrc bit of the txstax register configures the device as a master. clearing the sren and cren bits of the rcstax register ensures that the device is in the transmit mode, otherwise the device will be configured to receive. setting the spen bit of the rcstax register enables the eusart. if the rxx/dtx or txx/ckx pins are shared with an analog peripheral the analog i/o functions must be disabled by clearing the corresponding ansel bits. the tris bits corresponding to the rxx/dtx and txx/ckx pins should be set. 16.5.1.1 master clock synchronous data transfers use a separate clock line, which is synchronous with the data. a device configured as a master transmits the clock on the txx/ckx line. the txx/ckx pin output driver is automatically enabled when the eusart is configured for synchronous transmit or receive operation. serial data bits change on the leading edge to ensure they are valid at the trailing edge of each clock. one clock cycle is generated for each data bit. only as many clock cycles are generated as there are data bits. 16.5.1.2 clock polarity a clock polarity option is provided for microwire compatibility. clock polarity is selected with the cktxp bit of the baudconx register. setting the cktxp bit sets the clock idle state as high. when the cktxp bit is set, the data changes on the falling edge of each clock and is sampled on the rising edge of each clock. clearing the cktxp bit sets the idle state as low. when the cktxp bit is cleared, the data changes on the rising edge of each clock and is sampled on the falling edge of each clock. 16.5.1.3 synchronous master transmission data is transferred out of the device on the rxx/dtx pin. the rxx/dtx and txx/ckx pin output drivers are automatically enabled when the eusart is configured for synchronous master transmit operation. a transmission is initiated by writing a character to the txregx register. if the tsr still contains all or part of a previous character the new character data is held in the txregx until the last bit of the previous character has been transmitted. if this is the first character, or the previous character has been completely flushed from the tsr, the data in the txregx is immediately trans- ferred to the tsr. the transmission of the character commences immediately following the transfer of the data to the tsr from the txregx. each data bit changes on the leading edge of the master clock and remains valid until the subsequent leading clock edge. 16.5.1.4 data polarity the polarity of the transmit and receive data can be controlled with the dtrxp bit of the baudconx register. the default state of this bit is ? 0 ? which selects high true transmit and receive data. setting the dtrxp bit to ? 1 ? will invert the data resulting in low true transmit and receive data. note: the tsr register is not mapped in data memory, so it is not available to the user. ? 2010-2012 microchip technology inc. ds41412f-page 289 pic18(l)f2x/4xk22 16.5.1.5 synchronous master transmission setup: 1. initialize the spbrghx, spbrgx register pair and the brgh and brg16 bits to achieve the desired baud rate (see section 16.4 ?eusart baud rate generator (brg)? ). 2. set the rxx/dtx and txx/ckx tris controls to ? 1 ?. 3. enable the synchronous master serial port by setting bits sync, spen and csrc. set the tris bits corresponding to the rxx/dtx and txx/ckx i/o pins. 4. disable receive mode by clearing bits sren and cren. 5. enable transmit mode by setting the txen bit. 6. if 9-bit transmission is desired, set the tx9 bit. 7. if interrupts are desired, set the txxie, gie/ gieh and peie/giel interrupt enable bits. 8. if 9-bit transmission is selected, the ninth bit should be loaded in the tx9d bit. 9. start transmission by loading data to the txregx register. figure 16-10: synchronous transmission figure 16-11: synchronous transmis sion (through txen) bit 0 bit 1 bit 7 word 1 bit 2 bit 0 bit 1 bit 7 rxx/dtx write to txregx reg txxif bit (interrupt flag) txen bit ? 1 ? ? 1 ? word 2 trmt bit write word 1 write word 2 note: sync master mode, spbrgx = 0 , continuous transmission of two 8-bit words. pin txx/ckx pin txx/ckx pin (sckp = 0 ) (sckp = 1 ) rxx/dtx pin txx/ckx pin write to txregx reg txxif bit trmt bit bit 0 bit 1 bit 2 bit 6 bit 7 txen bit pic18(l)f2x/4xk22 ds41412f-page 290 ? 2010-2012 microchip technology inc. table 16-7: registers associated wi th synchronous master transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 spbrg1 eusart1 baud rate generator, low byte ? spbrgh1 eusart1 baud rate generator, high byte ? spbrg2 eusart2 baud rate generator, low byte ? spbrgh2 eusart2 baud rate generator, high byte ? trisb (2) trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 txreg1 eusart1 transmit register ? txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 txreg2 eusart2 transmit register ? txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for synchronous master transmission. note 1: pic18(l)f4xk22 devices. 2: pic18(l)f2xk22 devices. ? 2010-2012 microchip technology inc. ds41412f-page 291 pic18(l)f2x/4xk22 16.5.1.6 synchronous master reception data is received at the rxx/dtx pin. the rxx/dtx pin output driver must be disabled by setting the corresponding tris bits when the eusart is configured for synchronous master receive operation. in synchronous mode, reception is enabled by setting either the single receive enable bit (sren of the rcstax register) or the continuous receive enable bit (cren of the rcstax register). when sren is set and cren is clear, only as many clock cycles are generated as there are data bits in a single character. the sren bit is automatically cleared at the completion of one character. when cren is set, clocks are continuously generated until cren is cleared. if cren is cleared in the middle of a character the ck clock stops immediately and the partial charac- ter is discarded. if sren and cren are both set, then sren is cleared at the completion of the first character and cren takes precedence. to initiate reception, set either sren or cren. data is sampled at the rxx/dtx pin on the trailing edge of the txx/ckx clock pin and is shifted into the receive shift register (rsr). when a complete character is received into the rsr, the rcxif bit is set and the character is automatically transferred to the two character receive fifo. the least significant eight bits of the top character in the receive fifo are available in rcregx. the rcxif bit remains set as long as there are un-read characters in the receive fifo. 16.5.1.7 slave clock synchronous data transfers use a separate clock line, which is synchronous with the data. a device configured as a slave receives the clock on the txx/ckx line. the txx/ckx pin output driver must be disabled by setting the associated tris bit when the device is configured for synchronous slave transmit or receive operation. serial data bits change on the leading edge to ensure they are valid at the trailing edge of each clock. one data bit is transferred for each clock cycle. only as many clock cycles should be received as there are data bits. 16.5.1.8 receive overrun error the receive fifo buffer can hold two characters. an overrun error will be generated if a third character, in its entirety, is received before rcregx is read to access the fifo. when this happens the oerr bit of the rcstax register is set. previous data in the fifo will not be overwritten. the two characters in the fifo buffer can be read, however, no additional characters will be received until the error is cleared. the oerr bit can only be cleared by clearing the overrun condition. if the overrun error occurred when the sren bit is set and cren is clear then the error is cleared by reading rcregx. if the overrun occurred when the cren bit is set then the error condition is cleared by either clearing the cren bit of the rcstax register or by clearing the spen bit which resets the eusart. 16.5.1.9 receiving 9-bit characters the eusart supports 9-bit character reception. when the rx9 bit of the rcstax register is set the eusart will shift 9-bits into the rsr for each character received. the rx9d bit of the rcstax register is the ninth, and most significant, data bit of the top unread character in the receive fifo. when reading 9-bit data from the receive fifo buffer, the rx9d data bit must be read before reading the eight least significant bits from the rcregx. 16.5.1.10 synchronous master reception setup: 1. initialize the spbrghx, spbrgx register pair for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. set the rxx/dtx and txx/ckx tris controls to ? 1 ?. 3. enable the synchronous master serial port by setting bits sync, spen and csrc. disable rxx/dtx and txx/ckx output drivers by setting the corresponding tris bits. 4. ensure bits cren and sren are clear. 5. if using interrupts, set the gie/gieh and peie/ giel bits of the intcon register and set rcxie. 6. if 9-bit reception is desired, set bit rx9. 7. start reception by setting the sren bit or for continuous reception, set the cren bit. 8. interrupt flag bit rcxif will be set when recep- tion of a character is complete. an interrupt will be generated if the enable bit rcxie was set. 9. read the rcstax register to get the ninth bit (if enabled) and determine if any error occurred during reception. 10. read the 8-bit received data by reading the rcregx register. 11. if an overrun error occurs, clear the error by either clearing the cren bit of the rcstax register or by clearing the spen bit which resets the eusart. pic18(l)f2x/4xk22 ds41412f-page 292 ? 2010-2012 microchip technology inc. figure 16-12: synchronous reception (master mode, sren) cren bit rxx/dtx write to bit sren sren bit rcxif bit (interrupt) read rcregx ? 0 ? bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6 bit 7 ? 0 ? note: timing diagram demonstrates sync master mode with bit sren = 1 and bit brgh = 0 . txx/ckx pin txx/ckx pin pin (sckp = 0 ) (sckp = 1 ) table 16-8: registers associated with synchronous master reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 rcreg1 eusart1 receive register ? rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 rcreg2 eusart2 receive register ? rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 spbrg1 eusart1 baud rate generator, low byte ? spbrgh1 eusart1 baud rate generator, high byte ? spbrg2 eusart2 baud rate generator, low byte ? spbrgh2 eusart2 baud rate generator, high byte ? txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for synchronous master reception. ? 2010-2012 microchip technology inc. ds41412f-page 293 pic18(l)f2x/4xk22 16.5.2 synchronous slave mode the following bits are used to configure the eusart for synchronous slave operation: ? sync = 1 ? csrc = 0 ? sren = 0 (for transmit); sren = 1 (for receive) ? cren = 0 (for transmit); cren = 1 (for receive) ? spen = 1 setting the sync bit of the txstax register configures the device for synchronous operation. clearing the csrc bit of the txstax register configures the device as a slave. clearing the sren and cren bits of the rcstax register ensures that the device is in the transmit mode, otherwise the device will be configured to receive. setting the spen bit of the rcstax register enables the eusart. if the rxx/dtx or txx/ckx pins are shared with an analog peripheral the analog i/o functions must be disabled by clearing the corresponding ansel bits. rxx/dtx and txx/ckx pin output drivers must be disabled by setting the corresponding tris bits. 16.5.2.1 eusart synchronous slave transmit the operation of the synchronous master and slave modes are identical (see section 16.5.1.3 ?synchronous master transmission? ) , except in the case of the sleep mode. if two words are written to the txregx and then the sleep instruction is executed, the following will occur: 1. the first character will immediately transfer to the tsr register and transmit. 2. the second word will remain in txregx register. 3. the txxif bit will not be set. 4. after the first character has been shifted out of tsr, the txregx register will transfer the second character to the tsr and the txxif bit will now be set. 5. if the peie/giel and txxie bits are set, the interrupt will wake the device from sleep and execute the next instruction. if the gie/gieh bit is also set, the program will call the interrupt service routine. 16.5.2.2 synchronous slave transmission setup: 1. set the sync and spen bits and clear the csrc bit. 2. set the rxx/dtx and txx/ckx tris controls to ? 1 ?. 3. clear the cren and sren bits. 4. if using interrupts, ensure that the gie/gieh and peie/giel bits of the intcon register are set and set the txxie bit. 5. if 9-bit transmission is desired, set the tx9 bit. 6. enable transmission by setting the txen bit. 7. if 9-bit transmission is selected, insert the most significant bit into the tx9d bit. 8. start transmission by writing the least significant eight bits to the txregx register. pic18(l)f2x/4xk22 ds41412f-page 294 ? 2010-2012 microchip technology inc. table 16-9: registers associated with synchronous slave transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 spbrg1 eusart1 baud rate generator, low byte ? spbrgh1 eusart1 baud rate generator, high byte ? spbrg2 eusart2 baud rate generator, low byte ? spbrgh2 eusart2 baud rate generator, high byte ? trisb (2) trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 txreg1 eusart1 transmit register ? txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 txreg2 eusart2 transmit register ? txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for synchronous slave transmission. note 1: pic18(l)f4xk22 devices. 2: pic18(l)f2xk22 devices. ? 2010-2012 microchip technology inc. ds41412f-page 295 pic18(l)f2x/4xk22 16.5.2.3 eusart synchronous slave reception the operation of the synchronous master and slave modes is identical ( section 16.5.1.6 ?synchronous master reception? ), with the following exceptions: ? sleep ? cren bit is always set, therefore the receiver is never idle ? sren bit, which is a ?don't care? in slave mode a character may be received while in sleep mode by setting the cren bit prior to entering sleep. once the word is received, the rsr register will transfer the data to the rcregx register. if the rcxie enable bit is set, the interrupt generated will wake the device from sleep and execute the next instruction. if the gie/gieh bit is also set, the program will branch to the interrupt vector. 16.5.2.4 synchronous slave reception setup: 1. set the sync and spen bits and clear the csrc bit. 2. set the rxx/dtx and txx/ckx tris controls to ? 1 ?. 3. if using interrupts, ensure that the gie/gieh and peie/giel bits of the intcon register are set and set the rcxie bit. 4. if 9-bit reception is desired, set the rx9 bit. 5. set the cren bit to enable reception. 6. the rcxif bit will be set when reception is complete. an interrupt will be generated if the rcxie bit was set. 7. if 9-bit mode is enabled, retrieve the most significant bit from the rx9d bit of the rcstax register. 8. retrieve the eight least significant bits from the receive fifo by reading the rcregx register. 9. if an overrun error occurs, clear the error by either clearing the cren bit of the rcstax register or by clearing the spen bit which resets the eusart. table 16-10: registers associated with synchronous slave reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page baudcon1 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 baudcon2 abdovf rcidl dtrxp cktxp brg16 ? wue abden 279 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ? adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie1 ? adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir1 ? adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd0 uart2md uart1md tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md 55 rcreg1 eusart1 receive register ? rcsta1 spen rx9 sren cren adden ferr oerr rx9d 278 rcreg2 eusart2 receive register ? rcsta2 spen rx9 sren cren adden ferr oerr rx9d 278 spbrg1 eusart1 baud rate generator, low byte ? spbrgh1 eusart1 baud rate generator, high byte ? spbrg2 eusart2 baud rate generator, low byte ? spbrgh2 eusart2 baud rate generator, high byte ? txsta1 csrc tx9 txen sync sendb brgh trmt tx9d 277 txsta2 csrc tx9 txen sync sendb brgh trmt tx9d 277 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used for synchronous slave reception. pic18(l)f2x/4xk22 ds41412f-page 296 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 297 pic18(l)f2x/4xk22 17.0 analog-to-digital converter (adc) module the analog-to-digital converter (adc) allows conversion of an analog input signal to a 10-bit binary representation of that signal. this device uses analog inputs, which are multiplexed into a single sample and hold circuit. the output of the sample and hold is connected to the input of the converter. the converter generates a 10-bit binary result via successive approximation and stores the conversion result into the adc result registers (adresl and adresh). the adc voltage reference is software selectable to either v dd or a voltage applied to the external reference pins. the adc can generate an interrupt upon completion of a conversion. this interrupt can be used to wake-up the device from sleep. figure 17-1 shows the block diagram of the adc. figure 17-1: adc block diagram note: additional adc channels an5-an7 and an20-an27 are only available on pic18(l)f4xk22 devices. 11111 11110 11101 11100 11011 fvr buf2 dac ctmu an28 (1) an27 (1) 00101 00100 an5 (1) an4 00011 00010 an3 an2 00001 00000 an1 an0 5 chs<4:0> 10-bit adc adcmd adon go/done 10 0 = left justify 1 = right justify adfm 10 adresh adresl 00 01 a vdd 10 11 fvr buf2 reserved v ref +/an3 2 pvcfg<1:0> 00 01 a vss 10 11 reserved reserved v ref -/an2 2 nvcfg<1:0> pic18(l)f2x/4xk22 ds41412f-page 298 ? 2010-2012 microchip technology inc. 17.1 adc configuration when configuring and using the adc the following functions must be considered: ? port configuration ? channel selection ? adc voltage reference selection ? adc conversion clock source ? interrupt control ? results formatting 17.1.1 port configuration the anselx and trisx registers configure the a/d port pins. any port pin needed as an analog input should have its corresponding ansx bit set to disable the digital input buffer and trisx bit set to disable the digital output driver. if the trisx bit is cleared, the digital output level (v oh or v ol ) will be converted. the a/d operation is independent of the state of the ansx bits and the tris bits. 17.1.2 channel selection the chs bits of the adcon0 register determine which channel is connected to the sample and hold circuit. when changing channels, a delay is required before starting the next conversion. refer to section 17.2 ?adc operation? for more information. 17.1.3 adc v oltage reference the pvcfg<1:0> and nvcfg<1:0> bits of the adcon1 register provide independent control of the positive and negative voltage references. the positive voltage reference can be: ?v dd ? the fixed voltage reference (fvr buf2) ? an external voltage source (v ref +) the negative voltage reference can be: ?v ss ? an external voltage source (v ref -) 17.1.4 selecting and configuring acquisition time the adcon2 register allows the user to select an acquisition time that occurs each time the go/done bit is set. acquisition time is set with the acqt<2:0> bits of the adcon2 register. acquisition delays cover a range of 2 to 20 t ad . when the go/done bit is set, the a/d module continues to sample the input for the selected acquisition time, then automatically begins a conversion. since the acquisition time is programmed, there is no need to wait for an acquisition time between selecting a channel and setting the go/done bit. manual acquisition is selected when acqt<2:0> = 000 . when the go/done bit is set, sampling is stopped and a conversion begins. the user is responsible for ensuring the required acquisition time has passed between selecting the desired input channel and setting the go/done bit. this option is also the default reset state of the acqt<2:0> bits and is compatible with devices that do not offer programmable acquisition times. in either case, when the conversion is completed, the go/done bit is cleared, the adif flag is set and the a/d begins sampling the currently selected channel again. when an acquisition time is programmed, there is no indication of when the acquisition time ends and the conversion begins. note 1: when reading the port register, all pins with their corresponding ansx bit set read as cleared (a low level). however, analog conversion of pins configured as digital inputs (ansx bit cleared and trisx bit set) will be accurately converted. 2: analog levels on any pin with the corre- sponding ansx bit cleared may cause the digital input buffer to consume current out of the device?s specification limits. 3: the pbaden bit in configuration register 3h configures portb pins to reset as analog or digital pins by controlling how the bits in anselb are reset. ? 2010-2012 microchip technology inc. ds41412f-page 299 pic18(l)f2x/4xk22 17.1.5 conversion clock the source of the conversion clock is software selectable via the adcs bits of the adcon2 register. there are seven possible clock options: ?f osc /2 ?f osc /4 ?f osc /8 ?f osc /16 ?f osc /32 ?f osc /64 ?f rc (dedicated internal oscillator) the time to complete one bit conversion is defined as t ad . one full 10-bit conversion requires 11 t ad periods as shown in figure 17-3 . for correct conversion, the appropriate t ad specification must be met. see a/d conversion requirements in table 27-23 for more information. ta b l e 1 7 - 1 gives examples of appropriate adc clock selections. 17.1.6 interrupts the adc module allows for the ability to generate an interrupt upon completion of an analog-to-digital conversion. the adc interrupt enable is the adie bit in the pie1 register and the interrupt priority is the adip bit in the ipr1 register. the adc interrupt flag is the adif bit in the pir1 register. the adif bit must be cleared by software. this interrupt can be generated while the device is operating or while in sleep. if the device is in sleep, the interrupt will wake-up the device. upon waking from sleep, the next instruction following the sleep instruction is always executed. if the user is attempting to wake-up from sleep and resume in-line code execution, the global interrupt must be disabled. if the global interrupt is enabled, execution will switch to the interrupt service routine. note: unless using the f rc , any changes in the system clock frequency will change the adc clock frequency, which may adversely affect the adc result. note: the adif bit is set at the completion of every conversion, regardless of whether or not the adc interrupt is enabled. table 17-1: adc clock period (t ad ) v s . device operating frequencies adc clock period (t ad ) device frequency (f osc ) adc clock source adcs<2:0> 64 mhz 16 mhz 4 mhz 1 mhz f osc /2 000 31.25 ns (2) 125 ns (2) 500 ns (2) 2.0 ? s f osc /4 100 62.5 ns (2) 250 ns (2) 1.0 ? s 4.0 ? s (3) f osc /8 001 400 ns (2) 500 ns (2) 2.0 ? s 8.0 ? s (3) f osc /16 101 250 ns (2) 1.0 ? s 4.0 ? s (3) 16.0 ? s (3) f osc /32 010 500 ns (2) 2.0 ? s 8.0 ? s (3) 32.0 ? s (3) f osc /64 110 1.0 ? s 4.0 ? s (3) 16.0 ? s (3) 64.0 ? s (3) f rc x11 1-4 ? s (1,4) 1-4 ? s (1,4) 1-4 ? s (1,4) 1-4 ? s (1,4) legend: shaded cells are outside of recommended range. note 1: the f rc source has a typical t ad time of 1.7 ? s. 2: these values violate the minimum required t ad time. 3: for faster conversion times, the selection of another clock source is recommended. 4: when the device frequency is greater than 1 mhz, the f rc clock source is only recommended if the conversion will be performed during sleep. pic18(l)f2x/4xk22 ds41412f-page 300 ? 2010-2012 microchip technology inc. 17.1.7 result formatting the 10-bit a/d conversion result can be supplied in two formats, left justified or right justified. the adfm bit of the adcon2 register controls the output format. figure 17-2 shows the two output formats. figure 17-2: 10-bit a/d conv ersion result format adresh adresl (adfm = 0 )msb lsb bit 7 bit 0 bit 7 bit 0 10-bit a/d result unimplemented: read as ? 0 ? (adfm = 1 ) msb lsb bit 7 bit 0 bit 7 bit 0 unimplemented: read as ? 0 ? 10-bit a/d result ? 2010-2012 microchip technology inc. ds41412f-page 301 pic18(l)f2x/4xk22 17.2 adc operation 17.2.1 starting a conversion to enable the adc module, the adon bit of the adcon0 register must be set to a ? 1 ?. setting the go/ done bit of the adcon0 register to a ? 1 ? will, depend- ing on the acqt bits of the adcon2 register, either immediately start the analog-to-digital conversion or start an acquisition delay followed by the analog-to- digital conversion. figure 17-3 shows the operation of the a/d converter after the go bit has been set and the acqt<2:0> bits are cleared. a conversion is started after the following instruction to allow entry into sleep mode before the conversion begins. figure 17-4 shows the operation of the a/d converter after the go bit has been set and the acqt<2:0> bits are set to ? 010 ? which selects a 4 t ad acquisition time before the conversion starts. figure 17-3: a/d conversion t ad cycles (a cqt <2:0> = 000 , t acq = 0 ) figure 17-4: a/d conversion t ad cycles (a cqt <2:0> = 010 , t acq = 4 t ad ) note: the go/done bit should not be set in the same instruction that turns on the adc. refer to section 17.2.10 ?a/d conver- sion procedure? . t ad 1 t ad 2 t ad 3 t ad 4 t ad 5 t ad 6 t ad 7 t ad 8 t ad 11 set go bit holding capacitor is disconnected from analog input (typically 100 ns) t ad 9 t ad 10 t cy - t ad adresh:adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is connected to analog input. conversion starts b0 b9 b6 b5 b4 b3 b2 b1 b8 b7 on the following cycle: 2 t ad discharge 1 2 3 4 5 6 7 8 11 set go bit (holding capacitor is disconnected from analog input) 9 10 conversion starts 1 2 3 4 (holding capacitor continues acquiring input) t acqt cycles t ad cycles automatic acquisition time b0 b9 b6 b5 b4 b3 b2 b1 b8 b7 adresh:adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is connected to analog input. on the following cycle: 2 t ad discharge pic18(l)f2x/4xk22 ds41412f-page 302 ? 2010-2012 microchip technology inc. 17.2.2 completion of a conversion when the conversion is complete, the adc module will: ? clear the go/done bit ? set the adif flag bit ? update the adresh:adresl registers with new conversion result 17.2.3 discharge the discharge phase is used to initialize the value of the capacitor array. the array is discharged after every sample. this feature helps to optimize the unity-gain amplifier, as the circuit always needs to charge the capacitor array, rather than charge/discharge based on previous measure values. 17.2.4 terminating a conversion if a conversion must be terminated before completion, the go/done bit can be cleared by software. the adresh:adresl registers will not be updated with the partially complete analog-to-digital conversion sample. instead, the adresh:adresl register pair will retain the value of the previous conversion. 17.2.5 delay between conversions after the a/d conversion is completed or aborted, a 2t ad wait is required before the next acquisition can be started. after this wait, the currently selected channel is reconnected to the charge holding capacitor commencing the next acquisition. 17.2.6 adc operation in power- managed modes the selection of the automatic acquisition time and a/d conversion clock is determined in part by the clock source and frequency while in a power-managed mode. if the a/d is expected to operate while the device is in a power-managed mode, the acqt<2:0> and adcs<2:0> bits in adcon2 should be updated in accordance with the clock source to be used in that mode. after entering the mode, an a/d acquisition or conversion may be started. once started, the device should continue to be clocked by the same clock source until the conversion has been completed. if desired, the device may be placed into the corresponding idle mode during the conversion. if the device clock frequency is less than 1 mhz, the a/d f rc clock source should be selected. 17.2.7 adc operation during sleep the adc module can operate during sleep. this requires the adc clock source to be set to the f rc option. when the f rc clock source is selected, the adc waits one additional instruction before starting the conversion. this allows the sleep instruction to be executed, which can reduce system noise during the conversion. if the adc interrupt is enabled, the device will wake-up from sleep when the conversion completes. if the adc interrupt is disabled, the adc module is turned off after the conversion completes, although the adon bit remains set. when the adc clock source is something other than f rc , a sleep instruction causes the present conversion to be aborted and the adc module is turned off, although the adon bit remains set. 17.2.8 special event trigger two special event triggers are available to start an a/d conversion: ctmu and ccp5. the special event trigger source is selected using the trigsel bit in adcon1. when trigsel = 0 , the ccp5 module is selected as the special event trigger source. to enable the special event trigger in the ccp module, set ccp5m<3:0> = 1011 , in the ccp5con register. when trigsel = 1 , the ctmu module is selected. the ctmu module requires that the cttrig bit in ctmuconh is set to enable the special event trigger. in addition to trigsel bit, the following steps are required to start an a/d conversion: ? the a/d module must be enabled (adon = 1 ) ? the appropriate analog input channel selected ? the minimum acquisition period set one of these ways: - timing provided by the user - selection made of an appropriate t acq time with these conditions met, the trigger sets the go/done bit and the a/d acquisition starts. if the a/d module is not enabled (adon = 0 ), the module ignores the special event trigger. 17.2.9 peripheral module disable when a peripheral module is not used or inactive, the module can be disabled by setting the module disable bit in the pmd registers. this will reduce power consumption to an absolute minimum. setting the pmd bits holds the module in reset and disconnects the module?s clock source. the module disable bit for the adc module is adcmd in the pmd2 register. see section 3.0 ?power-managed modes? for more information. note: a device reset forces all registers to their reset state. thus, the adc module is turned off and any pending conversion is terminated. ? 2010-2012 microchip technology inc. ds41412f-page 303 pic18(l)f2x/4xk22 17.2.10 a/d conversion procedure this is an example procedure for using the adc to perform an analog-to-digital conversion: 1. configure port: ? disable pin output driver (see tris register) ? configure pin as analog 2. configure the adc module: ? select adc conversion clock ? configure voltage reference ? select adc input channel ? select result format ? select acquisition delay ? turn on adc module 3. configure adc interrupt (optional): ? clear adc interrupt flag ? enable adc interrupt ? enable peripheral interrupt ? enable global interrupt (1) 4. wait the required acquisition time (2) . 5. start conversion by setting the go/done bit. 6. wait for adc conversion to complete by one of the following: ? polling the go/done bit ? waiting for the adc interrupt (interrupts enabled) 7. read adc result 8. clear the adc interrupt flag (required if interrupt is enabled). example 17-1: a/d conversion note 1: the global interrupt can be disabled if the user is attempting to wake-up from sleep and resume in-line code execution. 2: software delay required if acqt bits are set to zero delay. see section 17.4 ?a/d acquisition requirements? . ;this code block configures the adc ;for polling, vdd and vss as reference, frc clock and an0 input. ; ;conversion start & polling for completion ; are included. ; movlw b?10101111? ;right justify, frc, movwf adcon2 ; & 12 tad acq time movlw b?00000000? ;adc ref = vdd,vss movwf adcon1 ; bsf trisa,0 ;set ra0 to input bsf ansel,0 ;set ra0 to analog movlw b?00000001? ;an0, adc on movwf adcon0 ; bsf adcon0,go ;start conversion adcpoll: btfsc adcon0,go ;is conversion done? bra adcpoll ;no, test again ; result is complete - store 2 msbits in ; resulthi and 8 lsbits in resultlo movff adresh,resulthi movff adresl,resultlo pic18(l)f2x/4xk22 ds41412f-page 304 ? 2010-2012 microchip technology inc. 17.3 register definitions: adc control note: analog pin control is determined by the anselx registers (see register 10-2 ) register 17-1: adcon0: a/ d control register 0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? chs<4:0> go/done adon 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 unimplemented: read as ? 0 ? bit 6-2 chs<4:0>: analog channel select bits 00000 = an0 00001 = an1 00010 = an2 00011 = an3 00100 = an4 00101 = an5 (1) 00110 = an6 (1) 00111 = an7 (1) 01000 = an8 01001 = an9 01010 = an10 01011 = an11 01100 = an12 01101 = an13 01110 = an14 01111 = an15 10000 = an16 10001 = an17 10010 = an18 10011 = an19 10100 = an20 (1) 10101 = an21 (1) 10110 = an22 (1) 10111 = an23 (1) 11000 = an24 (1) 11001 = an25 (1) 11010 = an26 (1) 11011 = an27 (1) 11100 = reserved 11101 = ctmu 11110 = dac 11111 = fvr buf2 (1.024v/2.048v/2.096v volt fixed voltage reference) (2) bit 1 go/done : a/d conversion status bit 1 = a/d conversion cycle in progress. setti ng this bit starts an a/d conversion cycle. this bit is automatically cleared by hardware when the a/d conversion has completed. 0 = a/d conversion completed/not in progress bit 0 adon: adc enable bit 1 = adc is enabled 0 = adc is disabled and consumes no operating current note 1: available on pic18(l)f4xk22 devices only. 2: allow greater than 15 ? s acquisition time when measuring the fixed voltage reference. ? 2010-2012 microchip technology inc. ds41412f-page 305 pic18(l)f2x/4xk22 register 17-2: adcon1: a/ d control register 1 r/w-0 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 trigsel ? ? ? pvcfg<1:0> nvcfg<1:0> 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 trigsel : special trigger select bit 1 = selects the special trigger from ctmu 0 = selects the special trigger from ccp5 bit 6-4 unimplemented: read as ? 0 ? bit 3-2 pvcfg<1:0>: positive voltage reference configuration bits 00 = a/d v ref + connected to internal signal, av dd 01 = a/d v ref + connected to external pin, v ref + 10 = a/d v ref + connected to internal signal, fvr buf2 11 = reserved (by default, a/d v ref + connected to internal signal, av dd ) bit 1-0 nvcfg<1:0>: negative voltage reference configuration bits 00 = a/d v ref - connected to internal signal, av ss 01 = a/d v ref - connected to external pin, v ref - 10 = reserved (by default, a/d v ref - connected to internal signal, av ss ) 11 = reserved (by default, a/d v ref - connected to internal signal, av ss ) pic18(l)f2x/4xk22 ds41412f-page 306 ? 2010-2012 microchip technology inc. register 17-3: adcon2: a/ d control register 2 r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 adfm ? acqt<2:0> adcs<2:0> 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 adfm: a/d conversion result format select bit 1 = right justified 0 = left justified bit 6 unimplemented: read as ? 0 ? bit 5-3 acqt<2:0>: a/d acquisition time select bits. acquisition time is the duration that the a/d charge holding capacitor remains connected to a/d channel from the instant the go/done bit is set until conversions begins. 000 = 0 (1) 001 = 2 t ad 010 = 4 t ad 011 = 6 t ad 100 = 8 t ad 101 = 12 t ad 110 = 16 t ad 111 = 20 t ad bit 2-0 adcs<2:0>: a/d conversion clock select bits 000 = f osc /2 001 = f osc /8 010 = f osc /32 011 = f rc (1) (clock derived from a dedicated internal oscillator = 600 khz nominal) 100 = f osc /4 101 = f osc /16 110 = f osc /64 111 = f rc (1) (clock derived from a dedicated internal oscillator = 600 khz nominal) note 1: when the a/d clock source is selected as f rc then the start of conversion is delayed by one instruction cycle after the go/done bit is set to allow the sleep instruction to be executed. ? 2010-2012 microchip technology inc. ds41412f-page 307 pic18(l)f2x/4xk22 register 17-4: adresh: adc result register high (adresh) adfm = 0 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres<9:2> 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-0 adres<9:2> : adc result register bits upper eight bits of 10-bit conversion result register 17-5: adresl: adc result register low (adresl) adfm = 0 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres<1:0> ? ? ? ? ? ? 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-6 adres<1:0> : adc result register bits lower two bits of 10-bit conversion result bit 5-0 reserved : do not use. register 17-6: adresh: adc result register high (adresh) adfm = 1 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x ? ? ? ? ? ? adres<9:8> 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-2 reserved : do not use. bit 1-0 adres<9:8> : adc result register bits upper two bits of 10-bit conversion result register 17-7: adresl: adc result register low (adresl) adfm = 1 r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x r/w-x adres<7:0> 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-0 adres<7:0> : adc result register bits lower eight bits of 10-bit conversion result pic18(l)f2x/4xk22 ds41412f-page 308 ? 2010-2012 microchip technology inc. 17.4 a/d acquisition requirements for the adc to meet its specified accuracy, the charge holding capacitor (c hold ) must be allowed to fully charge to the input channel voltage level. the analog input model is shown in figure 17-5 . the source impedance (r s ) and the internal sampling switch (r ss ) impedance directly affect the time required to charge the capacitor c hold . the sampling switch (r ss ) impedance varies over the device voltage (v dd ), see figure 17-5 . the maximum recommended impedance for analog sources is 10 k ? . as the source impedance is decreased, the acquisition time may be decreased. after the analog input channel is selected (or changed), an a/d acquisition must be done before the conversion can be started. to calculate the minimum acquisition time, equation 17-1 may be used. this equation assumes that 1/2 lsb error is used (1024 steps for the adc). the 1/2 lsb error is the maximum error allowed for the adc to meet its specified resolution. equation 17-1: acquisition time example t acq amplifier settling time hold capacitor charging time temperature coefficient ++ = t amp t c t coff ++ = 5 s t c temperature - 25c ?? 0.05s/c ?? ?? ++ = t c c hold r ic r ss r s ++ ?? ln(1/2047) ? = 13.5pf 1k ? 700 ? 10k ? ++ ?? ? ln(0.0004885) = 1.20 = s t acq 5 s 1.20s 50c- 25c ?? 0.05 ? s/ c ?? ?? ++ = 7.45 s = v applied 1e tc ? rc --------- ? ?? ?? ?? v applied 1 1 2047 ----------- - ? ?? ?? = v applied 1 1 2047 ----------- - ? ?? ?? v chold = v applied 1e t c ? rc --------- - ? ?? ?? ?? v chold = ;[1] v chold charged to within 1/2 lsb ;[2] v chold charge response to v applied ;combining [1] and [2] the value for t c can be approximated with the following equations: solving for t c : therefore: temperature 50c and external impedance of 10k ? 3.0 v v dd = assumptions: note 1: the reference voltage (v ref ) has no effect on the equation, since it cancels itself out. 2: the charge holding capacitor (c hold ) is discharged after each conversion. 3: the maximum recommended impedance for analog sources is 10 k ? . this is required to meet the pin leakage specification. ? 2010-2012 microchip technology inc. ds41412f-page 309 pic18(l)f2x/4xk22 figure 17-5: analog input model figure 17-6: adc transfer function c pin va rs anx 5 pf v dd i leakage (1) r ic ? 1k sampling switch ss rss c hold = 13.5 pf v ss /v ref - 2.5v rss (k ? ) 2.0v 1.5v .1 1 10 v dd legend: c pin i leakage r ic ss c hold = input capacitance = leakage current at the pin due to = interconnect resistance = sampling switch = sample/hold capacitance various junctions discharge switch 3.0v 3.5v 100 note 1: see section 27.0 ?electrical characteristics? . 3ffh 3feh adc output code 3fdh 3fch 004h 003h 002h 001h 000h full-scale 3fbh 1/2 lsb ideal v ss /v ref - zero-scale transition v dd /v ref + transition 1/2 lsb ideal full-scale range analog input voltage pic18(l)f2x/4xk22 ds41412f-page 310 ? 2010-2012 microchip technology inc. table 17-3: configuration registers associated with the adc module table 17-2: registers associ ated with a/d operation name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page adcon0 ? chs<4:0> go/d one adon 304 adcon1 trigsel ? ? ? pvcfg<1:0> nvcfg<1:0> 305 adcon2 adfm ? acqt<2:0> adcs<2:0> 306 adresh a/d result, high byte 307 adresl a/d result, low byte 307 ansela ? ?ansa5 ? ansa3 ansa2 ansa1 ansa0 154 anselb ? ? ansb5 ansb4 ansb3 ansb2 ansb1 ansb0 155 anselc ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ? ? 155 anseld (1) ansd7 ansd6 ansd5 ansd4 ansd3 ansd2 ansd1 ansd0 155 ansele (1) ? ? ? ? ? anse2 anse1 anse0 156 ccp5con ? ? dc5b<1:0> ccp5m<3:0> 205 ctmuconh ctmuen ? ctmusidl tgen edgen edgseqen idissen cttrig 333 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr1 ?adip rc1ip tx1ip ssp1ip ccp1ip tmr2ip tmr1ip 128 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 ipr4 ? ? ? ? ? ccp5ip ccp4ip ccp3ip 131 pie1 ?adie rc1ie tx1ie ssp1ie ccp1ie tmr2ie tmr1ie 124 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pie4 ? ? ? ? ? ccp5ie ccp4ie ccp3ie 127 pir1 ?adif rc1if tx1if ssp1if ccp1if tmr2if tmr1if 119 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pir4 ? ? ? ? ?ccp5if ccp4if ccp3if 122 pmd1 mssp2md mssp1md ? ccp5md ccp4md ccp3md ccp2md ccp1md 56 pmd2 ? ? ? ?ctmumd cmp2md cmp1md adcmd 57 trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 trisc trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 156 trisd (1) trisd7 trisd6 trisd5 trisd4 trisd3 trisd2 trisd1 trisd0 156 trise wpue3 ? ? ? ? trise2 (1) trise1 (1) trise0 (1) 156 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by this module. note 1: available on pic18(l)f4xk22 devices. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 360 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by the adc module. ? 2010-2012 microchip technology inc. ds41412f-page 311 pic18(l)f2x/4xk22 18.0 comparator module comparators are used to interface analog circuits to a digital circuit by comparing two analog voltages and providing a digital indication of their relative magnitudes. the comparators are very useful mixed signal building blocks because they provide analog functionality independent of the program execution. the analog comparator module includes the following features: ? independent comparator control ? programmable input selection ? comparator output is available internally/externally ? programmable output polarity ? interrupt-on-change ? wake-up from sleep ? programmable speed/power optimization ?pwm shutdown ? programmable and fixed voltage reference ? selectable hysteresis 18.1 comparator overview a single comparator is shown in figure 18-1 along with the relationship between the analog input levels and the digital output. when the analog voltage at v in + is less than the analog voltage at v in -, the output of the comparator is a digital low level. when the analog voltage at v in + is greater than the analog voltage at v in -, the output of the comparator is a digital high level. figure 18-1: single comparator ? + v in + v in - output output v in + v in - note: the black areas of the output of the comparator represents the uncertainty due to input offsets and response time. pic18(l)f2x/4xk22 ds41412f-page 312 ? 2010-2012 microchip technology inc. figure 18-2: comparator c1/c2 simplified block diagram note 1: when c1on = 0 , the c1 comparator will produce a ? 0 ? output to the xor gate. 2: q1 and q3 are phases of the four-phase system clock (f osc ). 3: q1 is held high during sleep mode. 4: synchronized comparator output should not be used to gate timer1 in conjunction with synchronized t1cki. cx cxpol to pwm logic 0 1 2 3 cxon (1) cxch<1:0> 2 0 1 cxr cm2con1 (mcxout) to interrupts cxv in - cxv in + c12in0- c12in1- c12in2- c12in3- cxin+ dq en q1 (2),(3) dq en cl read or write reset + - 0 1 dac output c x rsel fvr buf1 cxsp c x v ref cxoe c xout timer1 clock dq sync_c xout to cmxcon0 (cxout) (cxif) of cmxcon0 q3 (2) 0 1 cxsync async_c x out tris bit - to sr latch - to txg mux (4) ? 2010-2012 microchip technology inc. ds41412f-page 313 pic18(l)f2x/4xk22 18.2 comparator control each comparator has a separate control and configuration register: cm1con0 for comparator c1 and cm2con0 for comparator c2. in addition, comparator c2 has a second control register, cm2con1, for controlling the interaction with timer1 and simultaneous reading of both comparator outputs. the cm1con0 and cm2con0 registers (see register 18-1 ) contain the control and status bits for the following: ? enable ? input selection ? reference selection ?output selection ? output polarity ? speed selection 18.2.1 comparator enable setting the cxon bit of the cmxcon0 register enables the comparator for operation. clearing the cxon bit disables the comparator resulting in minimum current consumption. 18.2.2 comparator input selection the cxch<1:0> bits of the cmxcon0 register direct one of four analog input pins to the comparator inverting input. 18.2.3 comparator reference selection setting the cxr bit of the cmxcon0 register directs an internal voltage reference or an analog input pin to the non-inverting input of the comparator. see section 21.0 ?fixed voltage reference (fvr)? for more information on the internal voltage reference module. 18.2.4 comparator output selection the output of the comparator can be monitored by reading either the cxout bit of the cmxcon0 register or the mcxout bit of the cm2con1 register. in order to make the output available for an external connection, the following conditions must be true: ? cxoe bit of the cmxcon0 register must be set ? corresponding tris bit must be cleared ? cxon bit of the cmxcon0 register must be set 18.2.5 comparator output polarity inverting the output of the comparator is functionally equivalent to swapping the comparator inputs. the polarity of the comparator output can be inverted by setting the cxpol bit of the cmxcon0 register. clearing the cxpol bit results in a non-inverted output. table 18-1 shows the output state versus input conditions, including polarity control. 18.2.6 comparator speed selection the trade-off between speed or power can be optimized during program execution with the cxsp control bit. the default state for this bit is ? 1 ? which selects the normal speed mode. device power consumption can be optimized at the cost of slower comparator propagation delay by clearing the cxsp bit to ? 0 ?. 18.3 comparator response time the comparator output is indeterminate for a period of time after the change of an input source or the selection of a new reference voltage. this period is referred to as the response time. the response time of the comparator differs from the settling time of the voltage reference. therefore, both of these times must be considered when determining the total response time to a comparator input change. see the comparator and voltage reference specifications in section 27.0 ?electrical characteristics? for more details. note: to use cxin+ and c12inx- pins as analog inputs, the appropriate bits must be set in the ansel register and the corresponding tris bits must also be set to disable the output drivers. note 1: the cxoe bit overrides the port data latch. setting the cxon has no impact on the port override. 2: the internal output of the comparator is latched with each instruction cycle. unless otherwise specified, external outputs are not latched. table 18-1: comparator output state vs. input conditions input condition cxpol cxout cxv in - > cxv in + 00 cxv in - < cxv in + 01 cxv in - > cxv in + 11 cxv in - < cxv in + 10 pic18(l)f2x/4xk22 ds41412f-page 314 ? 2010-2012 microchip technology inc. 18.4 comparator interrupt operation the comparator interrupt flag will be set whenever there is a change in the output value of the comparator. changes are recognized by means of a mismatch circuit which consists of two latches and an exclusive- or gate (see figure 18-2 ). the first latch is updated with the comparator output value, when the cmxcon0 register is read or written. the value is latched on the third cycle of the system clock, also known as q3. this first latch retains the comparator value until another read or write of the cmxcon0 register occurs or a reset takes place. the second latch is updated with the comparator output value on every first cycle of the system clock, also known as q1. when the output value of the comparator changes, the second latch is updated and the output values of both latches no longer match one another, resulting in a mismatch condition. the latch outputs are fed directly into the inputs of an exclusive-or gate. this mismatch condition is detected by the exclusive-or gate and sent to the interrupt circuitry. the mismatch condition will persist until the first latch value is updated by performing a read of the cmxcon0 register or the comparator output returns to the previous state. when the mismatch condition occurs, the comparator interrupt flag is set. the interrupt flag is triggered by the edge of the changing value coming from the exclusive- or gate. this means that the interrupt flag can be reset once it is triggered without the additional step of read- ing or writing the cmxcon0 register to clear the mis- match latches. when the mismatch registers are cleared, an interrupt will occur upon the comparator?s return to the previous state, otherwise no interrupt will be generated. software will need to maintain information about the status of the comparator output, as read from the cmxcon0 register, or cm2con1 register, to determine the actual change that has occurred. see figures 18-3 and 18-4 . the cxif bit of the pir2 register is the comparator interrupt flag. this bit must be reset by software by clearing it to ? 0 ?. since it is also possible to write a ? 1 ? to this register, an interrupt can be generated. in mid-range compatibility mode the cxie bit of the pie2 register and the peie/giel and gie/gieh bits of the intcon register must all be set to enable compar- ator interrupts. if any of these bits are cleared, the inter- rupt is not enabled, although the cxif bit of the pir2 register will still be set if an interrupt condition occurs. 18.4.1 presetting the mismatch latches the comparator mismatch latches can be preset to the desired state before the comparators are enabled. when the comparator is off the cxpol bit controls the cxout level. set the cxpol bit to the desired cxout non-interrupt level while the cxon bit is cleared. then, configure the desired cxpol level in the same instruc- tion that the cxon bit is set. since all register writes are performed as a read-modify-write, the mismatch latches will be cleared during the instruction read phase and the actual configuration of the cxon and cxpol bits will be occur in the final write phase. figure 18-3: comparator interrupt timing w/o cmxcon0 read figure 18-4: comparator interrupt timing with cmxcon0 read note 1: a write operation to the cmxcon0 register will also clear the mismatch condition because all writes include a read operation at the beginning of the write cycle. 2: comparator interrupts will operate correctly regardless of the state of cxoe. note 1: if a change in the cmxcon0 register (cxout) should occur when a read oper- ation is being executed (start of the q2 cycle), then the cxif interrupt flag of the pir2 register may not get set. 2: when either comparator is first enabled, bias circuitry in the comparator module may cause an invalid output from the comparator until the bias circuitry is stable. allow about 1 ? s for bias settling then clear the mismatch condition and interrupt flags before enabling comparator interrupts. q1 q3 cxin+ cxin set cxif (edge) cxif t rt reset by software q1 q3 cxin+ cxout set cxif (edge) cxif t rt reset by software cleared by cmxcon0 read ? 2010-2012 microchip technology inc. ds41412f-page 315 pic18(l)f2x/4xk22 18.5 operation during sleep the comparator, if enabled before entering sleep mode, remains active during sleep. the additional current consumed by the comparator is shown separately in section 27.0 ?electrical characteristics? . if the comparator is not used to wake the device, power consumption can be minimized while in sleep mode by turning off the comparator. each comparator is turned off by clearing the cxon bit of the cmxcon0 register. a change to the comparator output can wake-up the device from sleep. to enable the comparator to wake the device from sleep, the cxie bit of the pie2 register and the peie/giel bit of the intcon register must be set. the instruction following the sleep instruction always executes following a wake from sleep. if the gie/gieh bit of the intcon register is also set, the device will then execute the interrupt service routine. 18.6 effects of a reset a device reset forces the cmxcon0 and cm2con1 registers to their reset states. this forces both comparators and the voltage references to their off states.comparator control registers. 18.7 analog input connection considerations a simplified circuit for an analog input is shown in figure 18-5 . since the analog input pins share their connection with a digital input, they have reverse biased esd protection diodes to v dd and v ss . the analog input, therefore, must be between v ss and v dd . if the input voltage deviates from this range by more than 0.6v in either direction, one of the diodes is forward biased and a latch-up may occur. a maximum source impedance of 10 k ? is recommended for the analog sources. also, any external component connected to an analog input pin, such as a capacitor or a zener diode, should have very little leakage current to minimize inaccuracies introduced. figure 18-5: analog input model note 1: when reading a port register, all pins configured as analog inputs will read as a ? 0 ?. pins configured as digital inputs will convert as an analog input, according to the input specification. 2: analog levels on any pin defined as a digital input, may cause the input buffer to consume more current than is specified. v a rs < 10k c pin 5 pf v dd v t ? 0.6v v t ? 0.6v r ic i leakage (1) vss a in legend: c pin = input capacitance i leakage = leakage current at the pin due to various junctions r ic = interconnect resistance r s = source impedance v a = analog voltage v t = threshold voltage note 1: see section 27.0 ?electrical characteristics? . to comparator pic18(l)f2x/4xk22 ds41412f-page 316 ? 2010-2012 microchip technology inc. 18.8 additional comparator features there are four additional comparator features: ? simultaneous read of comparator outputs ? internal reference selection ? hysteresis selection ? output synchronization 18.8.1 simultaneous comparator output read the mc1out and mc2out bits of the cm2con1 register are mirror copies of both comparator outputs. the ability to read both outputs simultaneously from a single register eliminates the timing skew of reading separate registers. 18.8.2 internal reference selection there are two internal voltage references available to the non-inverting input of each comparator. one of these is the fixed voltage reference (fvr) and the other is the variable digital-to-analog converter (dac). the cxrsel bit of the cm2con1 register determines which of these references is routed to the comparator voltage reference output (c x v ref ). further routing to the comparator is accomplished by the cxr bit of the cmxcon0 register. see section 21.0 ?fixed voltage reference (fvr)? and figure 18-2 for more detail. 18.8.3 comparator hysteresis each comparator has a selectable hysteresis feature. the hysteresis can be enabled by setting the cxhys bit of the cm2con1 register. see section 27.0 ?elec- trical characteristics? for more details. 18.8.4 synchronizing comparator output to timer1 the comparator cx output can be synchronized with timer1 by setting the cxsync bit of the cm2con1 register. when enabled, the cx output is latched on the falling edge of the timer1 source clock. to prevent a race condition when gating timer1 clock with the comparator output, timer1 increments on the rising edge of its clock source, and the falling edge latches the comparator output. see the comparator block diagram ( figure 18-2 ) and the timer1 block diagram ( figure 12-1 ) for more information. note 1: obtaining the status of c1out or c2out by reading cm2con1 does not affect the comparator interrupt mismatch registers. note 1: the comparator synchronized output should not be used to gate the external timer1 clock when the timer1 synchronizer is enabled. 2: the timer1 prescale should be set to 1:1 when synchronizing the comparator output as unexpected results may occur with other prescale values. ? 2010-2012 microchip technology inc. ds41412f-page 317 pic18(l)f2x/4xk22 18.9 register definitions: comparator control register 18-1: cmxcon0: comp arator x control register r/w-0 r-0 r/w-0 r/w-0 r/w-1 r/w-0 r/w-0 r/w-0 cxon cxout cxoe cxpol cxsp cxr cxch<1:0> 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 cxon: comparator cx enable bit 1 = comparator cx is enabled 0 = comparator cx is disabled bit 6 cxout: comparator cx output bit if cxpol = 1 (inverted polarity): cxout = 0 when cxv in + > cxv in - cxout = 1 when cxv in + < cxv in - if cxpol = 0 (non-inverted polarity): cxout = 1 when cxv in + > cxv in - cxout = 0 when cxv in + < cxv in - bit 5 cxoe: comparator cx output enable bit 1 = cxout is present on the cxout pin (1) 0 = cxout is internal only bit 4 cxpol: comparator cx output polarity select bit 1 = cxout logic is inverted 0 = cxout logic is not inverted bit 3 cxsp: comparator cx speed/power select bit 1 = cx operates in normal-power, higher speed mode 0 = cx operates in low-power, low-speed mode bit 2 cxr: comparator cx reference select bit (non-inverting input) 1 = cxv in + connects to c x v ref output 0 = cxv in + connects to c12in+ pin bit 1-0 cxch<1:0>: comparator cx channel select bit 00 = c12in0- pin of cx connects to cxv in - 01 = c12in1- pin of cx connects to c x v in - 10 = c12in2- pin of cx connects to cxv in - 11 = c12in3- pin of cx connects to cxv in - note 1: comparator output requires the following three conditions: cxoe = 1 , cxon = 1 and corresponding port tris bit = 0 . pic18(l)f2x/4xk22 ds41412f-page 318 ? 2010-2012 microchip technology inc. register 18-2: cm2con1: compa rator 1 and 2 control register r-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 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 mc1out: mirror copy of c1out bit bit 6 mc2out: mirror copy of c2out bit bit 5 c1rsel: comparator c1 reference select bit 1 = fvr buf1 routed to c1v ref input 0 = dac routed to c1v ref input bit 4 c2rsel: comparator c2 reference select bit 1 = fvr buf1 routed to c2v ref input 0 = dac routed to c2v ref input bit 3 c1hys: comparator c1 hysteresis enable bit 1 = comparator c1 hysteresis enabled 0 = comparator c1 hysteresis disabled bit 2 c2hys: comparator c2 hysteresis enable bit 1 = comparator c2 hysteresis enabled 0 = comparator c2 hysteresis disabled bit 1 c1sync: c1 output synchronous mode bit 1 = c1 output is synchronized to rising edge of tmr1 clock (t1clk) 0 = c1 output is asynchronous bit 0 c2sync: c2 output synchronous mode bit 1 = c2 output is synchronized to rising edge of tmr1 clock (t1clk) 0 = c2 output is asynchronous ? 2010-2012 microchip technology inc. ds41412f-page 319 pic18(l)f2x/4xk22 table 18-2: registers associated with comparator module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ansela ? ? ansa5 ? ansa3 ansa2 ansa1 ansa0 154 anselb ? ? ansb5 ansb4 ansb3 ansb2 ansb1 ansb0 155 cm2con1 mc1out mc2out c1rsel c2rsel c1hys c2hys c1sync c2sync 318 cm1con0 c1on c1out c1oe c1pol c1sp c1r c1ch<1:0> 317 cm2con0 c2on c2out c2oe c2pol c2sp c2r c2ch<1:0> 317 vrefcon1 dacen daclps dacoe ? dacpss<1:0> ? dacnss 347 vrefcon2 ? ? ? dacr<4:0> 348 vrefcon0 fvren fvrst fvrs<1:0> ? ? ? ? 344 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 pmd2 ? ? ? ? ctmumd cmp2md cmp1md adcmd 57 trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by the comparator module. pic18(l)f2x/4xk22 ds41412f-page 320 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 321 pic18(l)f2x/4xk22 19.0 charge time measurement unit (ctmu) the charge time measurement unit (ctmu) is a flexible analog module that provides accurate differential time measurement between pulse sources, as well as asynchronous pulse generation. by working with other on-chip analog modules, the ctmu can be used to precisely measure time, measure capacitance, measure relative changes in capacitance or generate output pulses with a specific time delay. the ctmu is ideal for interfacing with capacitive-based sensors. the module includes the following key features: ? up to 28 (1) channels available for capacitive or time measurement input ? on-chip precision current source ? four-edge input trigger sources ? polarity control for each edge source ? control of edge sequence ? control of response to edges ? high precision time measurement ? time delay of external or internal signal asynchronous to system clock ? accurate current source suitable for capacitive measurement the ctmu works in conjunction with the a/d converter to provide up to 28 (1) channels for time or charge measurement, depending on the specific device and the number of a/d channels available. when config- ured for time delay, the ctmu is connected to the c12in1- input of comparator 2. the level-sensitive input edge sources can be selected from four sources: two external input pins (cted1/cted2) or the eccp1/ (e)ccp2 special event triggers. figure 19-1 provides a block diagram of the ctmu. figure 19-1: ctmu block diagram note 1: pic18(l)f2xk22 devices have up to 17 channels available. cted1 cted2 current source edge control logic ctmuconh/ctmuconl pulse generator eccp2 eccp1 current control itrim<5:0> irng<1:0> ctmuicon ctmu control logic edgen edgseqen edg1selx edg1pol edg2selx edg2pol edg1stat edg2stat tgen idissen ctpls comparator 2 output cttrig comparator c1/c2 input a/d converter pic18(l)f2x/4xk22 ds41412f-page 322 ? 2010-2012 microchip technology inc. 19.1 ctmu operation the ctmu works by using a fixed current source to charge a circuit. the type of circuit depends on the type of measurement being made. in the case of charge measurement, the current is fixed, and the amount of time the current is applied to the circuit is fixed. the amount of voltage read by the a/d is then a measure- ment of the capacitance of the circuit. in the case of time measurement, the current, as well as the capaci- tance of the circuit, is fixed. in this case, the voltage read by the a/d is then representative of the amount of time elapsed from the time the current source starts and stops charging the circuit. if the ctmu is being used as a time delay, both capacitance and current source are fixed, as well as the voltage supplied to the comparator circuit. the delay of a signal is determined by the amount of time it takes the voltage to charge to the comparator threshold voltage. 19.1.1 theory of operation the operation of the ctmu is based on the equation for charge: more simply, the amount of charge measured in coulombs in a circuit is defined as current in amperes ( i ) multiplied by the amount of time in seconds that the current flows ( t ). charge is also defined as the capacitance in farads ( c ) multiplied by the voltage of the circuit ( v ). it follows that: the ctmu module provides a constant, known current source. the a/d converter is used to measure ( v ) in the equation, leaving two unknowns: capacitance ( c ) and time ( t ). the above equation can be used to calcu- late capacitance or time, by either the relationship using the known fixed capacitance of the circuit: or by: using a fixed time that the current source is applied to the circuit. 19.1.2 current source at the heart of the ctmu is a precision current source, designed to provide a constant reference for measure- ments. the level of current is user-selectable across three ranges, with the ability to trim the output. the cur- rent range is selected by the irng<1:0> bits (ctmui- con<1:0>), with a value of ? 00 ? representing the lowest range. current trim is provided by the itrim<5:0> bits (ctmuicon<7:2>). note that half of the range adjusts the current source positively and the other half reduces the current source. a value of ? 000000 ? is the neutral position (no change). a value of ? 100000 ? is the maxi- mum negative adjustment, and ? 011111 ? is the maxi- mum positive adjustment. 19.1.3 edge selection and control ctmu measurements are controlled by edge events occurring on the module?s two input channels. each channel, referred to as edge 1 and edge 2, can be con- figured to receive input pulses from one of the edge input pins (cted1 and cted2) or eccpx special event triggers. the input channels are level-sensitive, responding to the instantaneous level on the channel rather than a transition between levels. the inputs are selected using the edg1sel and edg2sel bit pairs (ctmuconl<3:2 and 6:5>). in addition to source, each channel can be configured for event polarity using the edge2pol and edge1pol bits (ctmuconl<7,4>). the input channels can also be filtered for an edge event sequence (edge 1 occur- ring before edge 2) by setting the edgseqen bit (ctmuconh<2>). 19.1.4 edge status the ctmuconl register also contains two status bits: edg2stat and edg1stat (ctmuconl<1:0>). their primary function is to show if an edge response has occurred on the corresponding channel. the ctmu automatically sets a particular bit when an edge response is detected on its channel. the level-sensitive nature of the input channels also means that the status bits become set immediately if the channel?s configura- tion is changed and is the same as the channel?s current state. ci dv dt ------ - ? = it ? cv . ? = tcv ? ?? i ? = cit ? ?? v ? = ? 2010-2012 microchip technology inc. ds41412f-page 323 pic18(l)f2x/4xk22 the module uses the edge status bits to control the current source output to external analog modules (such as the a/d converter). current is only supplied to external modules when only one (but not both) of the status bits is set, and shuts current off when both bits are either set or cleared. this allows the ctmu to measure current only during the interval between edges. after both status bits are set, it is necessary to clear them before another measurement is taken. both bits should be cleared simultaneously, if possible, to avoid re-enabling the ctmu current source. in addition to being set by the ctmu hardware, the edge status bits can also be set by software. this is also the user?s application to manually enable or disable the current source. setting either one (but not both) of the bits enables the current source. setting or clearing both bits at once disables the source. 19.1.5 interrupts the ctmu sets its interrupt flag (pir3<2>) whenever the current source is enabled, then disabled. an interrupt is generated only if the corresponding interrupt enable bit (pie3<2>) is also set. if edge sequencing is not enabled (i.e., edge 1 must occur before edge 2), it is necessary to monitor the edge status bits and determine which edge occurred last and caused the interrupt. 19.2 ctmu module initialization the following sequence is a general guideline used to initialize the ctmu module: 1. select the current source range using the irng bits (ctmuicon<1:0>). 2. adjust the current source trim using the itrim bits (ctmuicon<7:2>). 3. configure the edge input sources for edge 1 and edge 2 by setting the edg1sel and edg2sel bits (ctmuconl<3:2 and 6:5>). 4. configure the input polarities for the edge inputs using the edg1pol and edg2pol bits (ctmuconl<4,7>). the default configuration is for negative edge polarity (high-to-low transitions). 5. enable edge sequencing using the edgseqen bit (ctmuconh<2>). by default, edge sequencing is disabled. 6. select the operating mode (measurement or time delay) with the tgen bit. the default mode is time/capacitance measurement. 7. discharge the connected circuit by setting the idissen bit (ctmuconh<1>); after waiting a sufficient time for the circuit to discharge, clear idissen. 8. disable the module by clearing the ctmuen bit (ctmuconh<7>). 9. enable the module by setting the ctmuen bit. 10. clear the edge status bits: edg2stat and edg1stat (ctmuconl<1:0>). 11. enable both edge inputs by setting the edgen bit (ctmuconh<3>). depending on the type of measurement or pulse generation being performed, one or more additional modules may also need to be initialized and configured with the ctmu module: ? edge source generation: in addition to the external edge input pins, both timer1 and the output compare/pwm1 module can be used as edge sources for the ctmu. ? capacitance or time measurement: the ctmu module uses the a/d converter to measure the voltage across a capacitor that is connected to one of the analog input channels. ? pulse generation: when generating system clock independent output pulses, the ctmu module uses comparator 2 and the associated comparator voltage reference. pic18(l)f2x/4xk22 ds41412f-page 324 ? 2010-2012 microchip technology inc. 19.3 calibrating the ctmu module the ctmu requires calibration for precise measurements of capacitance and time, as well as for accurate time delay. if the application only requires measurement of a relative change in capacitance or time, calibration is usually not necessary. an example of this type of application would include a capacitive touch switch, in which the touch circuit has a baseline capacitance, and the added capacitance of the human body changes the overall capacitance of a circuit. if actual capacitance or time measurement is required, two hardware calibrations must take place: the current source needs calibration to set it to a precise current, and the circuit being measured needs calibration to measure and/or nullify all other capacitance other than that to be measured. 19.3.1 current source calibration the current source on the ctmu module is trimable. therefore, for precise measurements, it is possible to measure and adjust this current source by placing a high precision resistor, r cal , onto an unused analog channel. an example circuit is shown in figure 19-2 . the current source measurement is performed using the following steps: 1. initialize the a/d converter. 2. initialize the ctmu. 3. enable the current source by setting edg1stat (ctmuconl<0>). 4. issue settling time delay. 5. perform a/d conversion. 6. calculate the current source current using i=v/r cal , where r cal is a high precision resistance and v is measured by performing an a/d conversion. the ctmu current source may be trimmed with the trim bits in ctmuicon using an iterative process to get an exact desired current. alternatively, the nominal value without adjustment may be used; it may be stored by the software for use in all subsequent capacitive or time measurements. to calculate the value for r cal , the nominal current must be chosen, and then the resistance can be calculated. for example, if the a/d converter reference voltage is 3.3v, use 70% of full scale, or 2.31v as the desired approximate voltage to be read by the a/d converter. if the range of the ctmu current source is selected to be 0.55 ? a, the resistor value needed is cal- culated as r cal = 2.31v/0.55 ? a , for a value of 4.2 m ? . similarly, if the current source is chosen to be 5.5 ? a, r cal would be 420,000 ? , and 42,000 ? if the current source is set to 55 ? a. figure 19-2: ctmu current source calibration circuit a value of 70% of full-scale voltage is chosen to make sure that the a/d converter was in a range that is well above the noise floor. keep in mind that if an exact cur- rent is chosen, that is to incorporate the trimming bits from ctmuicon, the resistor value of r cal may need to be adjusted accordingly. r cal may also be adjusted to allow for available resistor values. r cal should be of the highest precision available, keeping in mind the amount of precision needed for the circuit that the ctmu will be used to measure. a recommended minimum would be 0.1% tolerance. the following examples show one typical method for performing a ctmu current calibration. example 19-1 demonstrates how to initialize the a/d converter and the ctmu; this routine is typical for applications using both modules. example 19-2 demonstrates one method for the actual calibration routine. pic18(l)fxxk22 device a/d converter ctmu anx r cal current source mux a/d ? 2010-2012 microchip technology inc. ds41412f-page 325 pic18(l)f2x/4xk22 example 19-1: setup for ct mu calibration routines #include "p18cxxx.h" /**************************************************************************/ /*set up ctmu *****************************************************************/ /**************************************************************************/ void setup(void) { //ctmuconh/1 - ctmu control registers ctmuconh = 0x00; //make sure ctmu is disabled ctmuconl = 0x90; //ctmu continues to run when emulator is stopped,ctmu continues //to run in idle mode,time generation mode disabled, edges are blocked //no edge sequence order, analog current source not grounded, trigger //output disabled, edge2 polarity = positive level, edge2 source = //source 0, edge1 polarity = positive level, edge1 source = source 0, //ctmuicon - ctmu current control register ctmuicon = 0x01; //0.55ua, nominal - no adjustment /**************************************************************************/ //set up ad converter; /**************************************************************************/ trisa=0x04; //set channel 2 as an input // configure an2 as an analog channel anselabits.ansa2=1; trisabits.trisa2=1; // adcon2 adcon2bits.adfm=1; // results format 1= right justified adcon2bits.acqt=1; // acquition time 7 = 20tad 2 = 4tad 1=2tad adcon2bits.adcs=2; // clock conversion bits 6= fosc/64 2=fosc/32 // adcon1 adcon1bits.pvcfg0 =0; // vref+ = avdd adcon1bits.nvcfg1 =0; // vref- = avss // adcon0 adcon0bits.chs=2; // select adc channel adcon0bits.adon=1; // turn on adc } pic18(l)f2x/4xk22 ds41412f-page 326 ? 2010-2012 microchip technology inc. example 19-2: current calibration routine #include "p18cxxx.h" #define count 500 //@ 8mhz = 125us. #define delay for(i=0;i pic18(l)f2x/4xk22 ds41412f-page 328 ? 2010-2012 microchip technology inc. example 19-3: capacitance calibration routine #include "p18cxxx.h" #define count 25 //@ 8mhz intfrc = 62.5 us. #define etime count*2.5 //time in us #define delay for(i=0;i pic18(l)f2x/4xk22 ds41412f-page 330 ? 2010-2012 microchip technology inc. example 19-4: routine fo r capacitive touch switch #include "p18cxxx.h" #define count 500 //@ 8mhz = 125us. #define delay for(i=0;i ? 2010-2012 microchip technology inc. ds41412f-page 331 pic18(l)f2x/4xk22 19.5 measuring time with the ctmu module time can be precisely measured after the ratio ( c/i ) is measured from the current and capacitance calibration step by following these steps: 1. initialize the a/d converter and the ctmu. 2. set edg1stat. 3. set edg2stat. 4. perform an a/d conversion. 5. calculate the time between edges as t = (c/i) * v , where i is calculated in the current calibration step ( section 19.3.1 ?current source calibration? ), c is calculated in the capacitance calibration step ( section 19.3.2 ?capacitance calibration? ) and v is measured by performing the a/d conversion. it is assumed that the time measured is small enough that the capacitance, c offset , provides a valid voltage to the a/d converter. for the smallest time measure- ment, always set the a/d channel select register (ad1chs) to an unused a/d channel; the correspond- ing pin for which is not connected to any circuit board trace. this minimizes added stray capacitance, keep- ing the total circuit capacitance close to that of the a/d converter itself (4-5 pf). to measure longer time intervals, an external capacitor may be connected to an a/d channel and this channel selected when making a time measurement. figure 19-3: typical connections and internal configuration for time measurement a/d converter ctmu cted1 cted2 an x output pulse edg1 edg2 c ad r pr current source pic18(l)fxxk22 device pic18(l)f2x/4xk22 ds41412f-page 332 ? 2010-2012 microchip technology inc. 19.6 creating a delay with the ctmu module a unique feature on board the ctmu module is its ability to generate system clock independent output pulses based on an external capacitor value. this is accomplished using the internal comparator voltage reference module, comparator 2 input pin and an external capacitor. the pulse is output onto the ctpls pin. to enable this mode, set the tgen bit. see figure 19-4 for an example circuit. c pulse is chosen by the user to determine the output pulse width on ctpls. the pulse width is calculated by t =( c pulse / i )* v , where i is known from the current source measurement step ( section 19.3.1 ?current source calibration? ) and v is the internal reference voltage (cv ref ). an example use of this feature is for interfacing with variable capacitive-based sensors, such as a humidity sensor. as the humidity varies, the pulse width output on ctpls will vary. the ctpls output pin can be con- nected to an input capture pin and the varying pulse width is measured to determine the humidity in the application. follow these steps to use this feature: 1. initialize comparator 2. 2. initialize the comparator voltage reference. 3. initialize the ctmu and enable time delay generation by setting the tgen bit. 4. set edg1stat. 5. when c pulse charges to the value of the voltage reference trip point, an output pulse is generated on ctpls. figure 19-4: typical co nnections and internal co nfiguration for pulse delay generation 19.7 operation during sleep/idle modes 19.7.1 sleep mode and deep sleep modes when the device enters any sleep mode, the ctmu module current source is always disabled. if the ctmu is performing an operation that depends on the current source when sleep mode is invoked, the operation may not terminate correctly. capacitance and time measurements may return erroneous values. 19.7.2 idle mode the behavior of the ctmu in idle mode is determined by the ctmusidl bit (ctmuconh<5>). if ctmusidl is cleared, the module will continue to operate in idle mode. if ctmusidl is set, the module?s current source is disabled when the device enters idle mode. if the module is performing an operation when idle mode is invoked, in this case, the results will be similar to those with sleep mode. 19.8 ctmu peripheral module disable (pmd) when this peripheral is not used, the peripheral module disable bit can be set to disconnect all clock sources to the module, reducing power consumption to an absolute minimum. see section 3.6 ?selective peripheral module control? . c2 cv ref ctpls pic18(l)fxxk22 device current source comparator ctmu cted1 c12in1- c pulse edg1 ? 2010-2012 microchip technology inc. ds41412f-page 333 pic18(l)f2x/4xk22 19.9 effects of a reset on ctmu upon reset, all registers of the ctmu are cleared. this leaves the ctmu module disabled, its current source is turned off and all configuration options return to their default settings. the module needs to be re-initialized following any reset. if the ctmu is in the process of taking a measurement at the time of reset, the measurement will be lost. a partial charge may exist on the circuit that was being measured, and should be properly discharged before the ctmu makes subsequent attempts to make a measurement. the circuit is discharged by setting and then clearing the idissen bit (ctmuconh<1>) while the a/d converter is connected to the appropriate channel. 19.10 registers there are three control registers for the ctmu: ? ctmuconh ? ctmuconl ? ctmuicon the ctmuconh and ctmuconl registers ( register 19-1 and register 19-2 ) contain control bits for configuring the ctmu module edge source selec- tion, edge source polarity selection, edge sequencing, a/d trigger, analog circuit capacitor discharge and enables. the ctmuicon register ( register 19-3 ) has bits for selecting the current source range and current source trim. 19.11 register definitions: ctmu control register 19-1: ctmuconh: ct mu control register 0 r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 ctmuen ? ctmusidl tgen edgen edgseqen idissen cttrig 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 ctmuen: ctmu enable bit 1 = module is enabled 0 = module is disabled bit 6 unimplemented: read as ? 0 ? bit 5 ctmusidl: stop in idle mode bit 1 = discontinue module operation when device enters idle mode 0 = continue module operation in idle mode bit 4 tgen: time generation enable bit 1 = enables edge delay generation 0 = disables edge delay generation bit 3 edgen: edge enable bit 1 = edges are not blocked 0 = edges are blocked bit 2 edgseqen: edge sequence enable bit 1 = edge 1 event must occur before edge 2 event can occur 0 = no edge sequence is needed bit 1 idissen: analog current source control bit 1 = analog current source output is grounded 0 = analog current source output is not grounded bit 0 cttrig: ctmu special event trigger control bit 1 = ctmu special event trigger is enabled 0 = ctmu special event trigger is disabled pic18(l)f2x/4xk22 ds41412f-page 334 ? 2010-2012 microchip technology inc. register 19-2: ctmuconl: ct mu control register 1 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 edg2pol edg2sel<1:0> edg1pol edg1 sel1 edg1sel0 edg2stat edg1stat 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 edg2pol: edge 2 polarity select bit 1 = edge 2 programmed for a positive edge response 0 = edge 2 programmed for a negative edge response bit 6-5 edg2sel<1:0>: edge 2 source select bits 11 = cted1 pin 10 = cted2 pin 01 = eccp1 special event trigger 00 = eccp2 special event trigger bit 4 edg1pol: edge 1 polarity select bit 1 = edge 1 programmed for a positive edge response 0 = edge 1 programmed for a negative edge response bit 3-2 edg1sel<1:0>: edge 1 source select bits 11 = cted1 pin 10 = cted2 pin 01 = eccp1 special event trigger 00 = eccp2 special event trigger bit 1 edg2stat: edge 2 status bit 1 = edge 2 event has occurred 0 = edge 2 event has not occurred bit 0 edg1stat: edge 1 status bit 1 = edge 1 event has occurred 0 = edge 1 event has not occurred ? 2010-2012 microchip technology inc. ds41412f-page 335 pic18(l)f2x/4xk22 register 19-3: ctmuicon: ctmu current control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 itrim<5:0> irng<1:0> 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-2 itrim<5:0>: current source trim bits 011111 = maximum positive change from nominal current 011110 . . . 000001 = minimum positive change from nominal current 000000 = nominal current output specified by irng<1:0> 111111 = minimum negative change from nominal current . . . 100010 100001 = maximum negative change from nominal current bit 1-0 irng<1:0>: current source range select bits (see tab l e 2 7- 4 ) 11 = 100 ? base current 10 = 10 ? base current 01 = base current level 00 = current source disabled table 19-1: registers associated with ctmu module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page ctmuconh ctmuen ? ctmusidl tgen edgen edgseqen idissen cttrig 333 ctmuconl edg2pol edg2sel<1:0> edg1pol edg1sel<1:0> edg2stat edg1stat 334 ctmuicon itrim<5:0> irng<1:0> 335 ipr3 ssp2ip bcl2ip rc2ip tx2ip ctmuip tmr5gip tmr3gip tmr1gip 130 pie3 ssp2ie bcl2ie rc2ie tx2ie ctmuie tmr5gie tmr3gie tmr1gie 126 pir3 ssp2if bcl2if rc2if tx2if ctmuif tmr5gif tmr3gif tmr1gif 121 pmd2 ? ? ? ? ctmumd cmp2md cmp1md adcmd 57 legend: ? = unimplemented, read as ? 0 ?. shaded bits are not used during ctmu operation. pic18(l)f2x/4xk22 ds41412f-page 336 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 337 pic18(l)f2x/4xk22 20.0 sr latch the module consists of a single sr latch with multiple set and reset inputs as well as separate latch outputs. the sr latch module includes the following features: ? programmable input selection ? sr latch output is available internally/externally ? selectable q and q output ? firmware set and reset the sr latch can be used in a variety of analog applications, including oscillator circuits, one-shot circuit, hysteretic controllers, and analog timing applications. 20.1 latch operation the latch is a set-reset latch that does not depend on a clock source. each of the set and reset inputs are active-high. the latch can be set or reset by: ? software control (srps and srpr bits) ? comparator c1 output (sync_c1out) ? comparator c2 output (sync_c2out) ?sri pin ? programmable clock (divsrclk) the srps and the srpr bits of the srcon0 register may be used to set or reset the sr latch, respectively. the latch is reset-dominant. therefore, if both set and reset inputs are high, the latch will go to the reset state. both the srps and srpr bits are self resetting which means that a single write to either of the bits is all that is necessary to complete a latch set or reset operation. the output from comparator c1 or c2 can be used as the set or reset inputs of the sr latch. the output of either comparator can be synchronized to the timer1 clock source. see section 18.0 ?comparator module? and section 12.0 ?timer1/3/5 module with gate control? for more information. an external source on the sri pin can be used as the set or reset inputs of the sr latch. an internal clock source, divsrclk, is available and it can periodically set or reset the sr latch. the srclk<2:0> bits in the srcon0 register are used to select the clock source period. the srscke and srrcke bits of the srcon1 register enable the clock source to set or reset the sr latch, respectively. 20.2 latch output the srqen and srnqen bits of the srcon0 register control the q and q latch outputs. both of the sr latch outputs may be directly output to i/o pins at the same time. control is determined by the state of bits srqen and srnqen in the srcon0 register. the applicable tris bit of the corresponding port must be cleared to enable the port pin output driver. 20.3 divsrclk clock generation the divsrclk clock signal is generated from the peripheral clock which is pre-scaled by a value determined by the srclk<2:0> bits. see figure 20-2 and table 20-1 for additional detail. 20.4 effects of a reset upon any device reset, the sr latch is not initialized, and the srq and srnq outputs are unknown. the user?s firmware is responsible to initialize the latch output before enabling it to the output pins. pic18(l)f2x/4xk22 ds41412f-page 338 ? 2010-2012 microchip technology inc. figure 20-1: divsrclk block diagram figure 20-2: sr latch simplified block diagram 3 srclk<2:0> peripheral clock divsrclk programmable srclk divider 1:4 to 1:512 tosc 4 - 5 1 2 c y c l e s . . . srclk<2:0> = "001" 1:8 t0+4 t0 t0+8 t0+12 srps s r q q note 1: if r = 1 and s = 1 simultaneously, q = 0 , q = 1 2: pulse generator causes a pulse width of 2 t osc clock cycles. 3: name denotes the connection point at the comparator output. pulse gen ( 2 ) sr latch (1) srqen srspe srsc2e srscke divsrclk sync_c2out (3) srsc1e sync_c1out (3) srpr pulse gen ( 2 ) srrpe srrc2e srrcke divsrclk sync_c2out (3) srrc1e sync_c1out (3) srlen srnqen srlen srq srnq sri sri ? 2010-2012 microchip technology inc. ds41412f-page 339 pic18(l)f2x/4xk22 table 20-1: divsrclk frequency table srclk<2:0> divider f osc = 20 mhz f osc = 16 mhz f osc = 8 mhz f osc = 4 mhz f osc = 1 mhz 111 512 25.6 ? s32 ? s64 ? s 128 ? s 512 ? s 110 256 12.8 ? s16 ? s32 ? s64 ? s 256 ? s 101 128 6.4 ? s8 ? s16 ? s32 ? s 128 ? s 100 64 3.2 ? s4 ? s8 ? s16 ? s64 ? s 011 32 1.6 ? s2 ? s4 ? s8 ? s32 ? s 010 16 0.8 ? s1 ? s2 ? s4 ? s16 ? s 001 80.4 ? s0.5 ? s1 ? s2 ? s8 ? s 000 40.2 ? s0.25 ? s0.5 ? s1 ? s4 ? s pic18(l)f2x/4xk22 ds41412f-page 340 ? 2010-2012 microchip technology inc. 20.5 register definitions: sr latch control register 20-1: srcon0: sr latch control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 srlen srclk<2:0> srqen srnqen srps srpr bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 srlen: sr latch enable bit (1) 1 = sr latch is enabled 0 = sr latch is disabled bit 6-4 srclk<2:0>: sr latch clock divider bits 000 = generates a 2 t osc wide pulse on divsrclk every 4 peripheral clock cycles 001 = generates a 2 t osc wide pulse on divsrclk every 8 peripheral clock cycles 010 = generates a 2 t osc wide pulse on divsrclk every 16 peripheral clock cycles 011 = generates a 2 t osc wide pulse on divsrclk every 32 peripheral clock cycles 100 = generates a 2 t osc wide pulse on divsrclk every 64 peripheral clock cycles 101 = generates a 2 t osc wide pulse on divsrclk every 128 peripheral clock cycles 110 = generates a 2 t osc wide pulse on divsrclk every 256 peripheral clock cycles 111 = generates a 2 t osc wide pulse on divsrclk every 512 peripheral clock cycles bit 3 srqen: sr latch q output enable bit 1 = q is present on the srq pin 0 = q is internal only bit 2 srnqen: sr latch q output enable bit 1 =q is present on the srnq pin 0 =q is internal only bit 1 srps: pulse set input of the sr latch bit (2) 1 = pulse set input for two t osc clock cycles 0 = no effect on set input bit 0 srpr: pulse reset input of the sr latch bit (2) 1 = pulse reset input for two t osc clock cycles 0 = no effect on reset input note 1: changing the srclk bits while the sr latch is enabled may cause false triggers to the set and reset inputs of the latch. 2: set only, always reads back ? 0 ?. ? 2010-2012 microchip technology inc. ds41412f-page 341 pic18(l)f2x/4xk22 register 20-2: srcon1: sr latch control register 1 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7 srspe: sr latch peripheral set enable bit 1 = sri pin status sets sr latch 0 = sri pin status has no effect on sr latch bit 6 srscke: sr latch set clock enable bit 1 = set input of sr latch is pulsed with divsrclk 0 = set input of sr latch is not pulsed with divsrclk bit 5 srsc2e: sr latch c2 set enable bit 1 = c2 comparator output sets sr latch 0 = c2 comparator output has no effect on sr latch bit 4 srsc1e: sr latch c1 set enable bit 1 = c1 comparator output sets sr latch 0 = c1 comparator output has no effect on sr latch bit 3 srrpe: sr latch peripheral reset enable bit 1 = sri pin resets sr latch 0 = sri pin has no effect on sr latch bit 2 srrcke: sr latch reset clock enable bit 1 = reset input of sr latch is pulsed with divsrclk 0 = reset input of sr latch is not pulsed with divsrclk bit 1 srrc2e: sr latch c2 reset enable bit 1 = c2 comparator output resets sr latch 0 = c2 comparator output has no effect on sr latch bit 0 srrc1e: sr latch c1 reset enable bit 1 = c1 comparator output resets sr latch 0 = c1 comparator output has no effect on sr latch table 20-2: registers associated with the sr latch name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page srcon0 srlen srclk<2:0> srqen srnqen srps srpr 340 srcon1 srspe srscke srsc2e srsc1e srrpe srrcke srrc2e srrc1e 341 trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 trisb trisb7 trisb6 trisb5 trisb4 trisb3 trisb2 trisb1 trisb0 156 wpub wpub7 wpub6 wpub5 wpub4 wpub3 wpub2 wpub1 wpub0 157 legend: shaded bits are not used with this module. pic18(l)f2x/4xk22 ds41412f-page 342 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 343 pic18(l)f2x/4xk22 21.0 fixed voltage reference (fvr) the fixed voltage reference, or fvr, is a stable voltage reference, independent of v dd , with 1.024v, 2.048v or 4.096v selectable output levels. the output of the fvr can be configured to supply a reference voltage to the following: ? adc input channel ? adc positive reference ? comparator positive input ? digital-to-analog converter (dac) the fvr can be enabled by setting the fvren bit of the vrefcon0 register. 21.1 independent gain amplifiers the output of the fvr supplied to the adc, comparators and dac is routed through an independent programmable gain amplifier. the amplifier can be configured to amplify the 1.024v reference voltage by 1x, 2x or 4x, to produce the three possible voltage levels. the fvrs<1:0> bits of the vrefcon0 register are used to enable and configure the gain amplifier settings for the reference supplied to the dac and comparator modules. when the adc module is configured to use the fvr output, (fvr buf2) the reference is buffered through an additional unity gain amplifier. this buffer is disabled if the adc is not configured to use the fvr. for specific use of the fvr, refer to the specific module sections: section 17.0 ?analog-to-digital converter (adc) module? , section 22.0 ?digital-to-analog converter (dac) module? and section 18.0 ?com- parator module? . 21.2 fvr stabilization period when the fixed voltage reference module is enabled, it requires time for the reference and amplifier circuits to stabilize. once the circuits stabilize and are ready for use, the fvrst bit of the vrefcon0 register will be set. see table 27-3 for the minimum delay requirement. figure 21-1: voltage reference block diagram to comparators, dac x1 x2 x4 + - fvren fvrst 2 fvrs<1:0> to adc module x1 x2 x4 fvr buf1 fvr buf2 1.024v fixed voltage reference note 1: fvr_buf2_enable = ?1? when (adon = ?1?) and [(pvcfg<1:0> = ?10? ) or ( chs<4:0> = ?11111? )] fvr_buf2_enable (1) pic18(l)f2x/4xk22 ds41412f-page 344 ? 2010-2012 microchip technology inc. 21.3 register definitions: fvr control register 21-1: vrefcon0: fixed vo ltage reference control register r/w-0 r/w-0 r/w-0 r/w-1 u-0 u-0 u-0 u-0 fvren fvrst fvrs<1:0> ? ? ? ? bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7 fvren: fixed voltage reference enable bit 0 = fixed voltage reference is disabled 1 = fixed voltage reference is enabled bit 6 fvrst: fixed voltage reference ready flag bit 0 = fixed voltage reference output is not ready or not enabled 1 = fixed voltage reference output is ready for use bit 5-4 fvrs<1:0>: fixed voltage reference selection bits 00 = fixed voltage reference peripheral output is off 01 = fixed voltage reference peripheral output is 1x (1.024v) 10 = fixed voltage reference peripheral output is 2x (2.048v) (1) 11 = fixed voltage reference peripheral output is 4x (4.096v) (1) bit 3-2 reserved: read as ? 0 ?. maintain these bits clear. bit 1-0 unimplemented: read as ? 0 ?. note 1: fixed voltage reference output cannot exceed v dd . table 21-1: summary of registers asso ciated with fixed voltage reference name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page vrefcon0 fvren fvrst fvrs<1:0> ? ? ? ? 344 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by the fvr module. ? 2010-2012 microchip technology inc. ds41412f-page 345 pic18(l)f2x/4xk22 22.0 digital-to-analog converter (dac) module the digital-to-analog converter supplies a variable voltage reference, ratiometric with the input source, with 32 selectable output levels. the input of the dac can be connected to: ?external v ref pins ?v dd supply voltage ? fvr (fixed voltage reference) the output of the dac can be configured to supply a reference voltage to the following: ? comparator positive input ? adc input channel ?dacout pin the digital-to-analog converter (dac) can be enabled by setting the dacen bit of the vrefcon1 register. 22.1 output voltage selection the dac has 32 voltage level ranges. the 32 levels are set with the dacr<4:0> bits of the vrefcon2 register. the dac output voltage is determined by the following equations: equation 22-1: dac output voltage 22.2 ratiometric output level the dac output value is derived using a resistor ladder with each end of the ladder tied to a positive and negative voltage reference input source. if the voltage of either input source fluctuates, a similar fluctuation will result in the dac output value. the value of the individual resistors within the ladder can be found in section 27.0 ?electrical characteristics? . 22.3 low-power voltage state in order for the dac module to consume the least amount of power, one of the two voltage reference input sources to the resistor ladder must be disconnected. either the positive voltage source, (v src +), or the negative voltage source, (v src -) can be disabled. the negative voltage source is disabled by setting the daclps bit in the vrefcon1 register. clearing the daclps bit in the vrefcon1 register disables the positive voltage source. 22.4 output clamped to positive voltage source the dac output voltage can be set to v src + with the least amount of power consumption by performing the following: ? clearing the dacen bit in the vrefcon1 register. ? setting the daclps bit in the vrefcon1 register. ? configuring the dacpss bits to the proper positive source. ? configuring the dacrx bits to ? 11111 ? in the vrefcon2 register. this is also the method used to output the voltage level from the fvr to an output pin. see section 22.6 ?dac voltage reference output? for more information. 22.5 output clamped to negative voltage source the dac output voltage can be set to v src - with the least amount of power consumption by performing the following: ? clearing the dacen bit in the vrefcon1 register. ? clearing the daclps bit in the vrefcon1 register. ? configuring the dacpss bits to the proper negative source. ? configuring the dacrx bits to ? 00000 ? in the vrefcon2 register. this allows the comparator to detect a zero-crossing while not consuming additional current through the dac module. 22.6 dac voltage reference output the dac can be output to the dacout pin by setting the dacoe bit of the vrefcon1 register to ? 1 ?. selecting the dac reference voltage for output on the dacout pin automatically overrides the digital output buffer and digital input threshold detector functions of that pin. reading the dacout pin when it has been configured for dac reference voltage output will always return a ? 0 ?. due to the limited current drive capability, a buffer must be used on the dac voltage reference output for external connections to dacout. figure 22-2 shows an example buffering technique. v out v src +v src - ? ?? dacr<4:0> 2 5 ------------------------------ - ? ?? ?? = + v src - v src + = v dd , v ref + or fvr1 v src - = v ss or v ref - pic18(l)f2x/4xk22 ds41412f-page 346 ? 2010-2012 microchip technology inc. figure 22-1: digital-to-analog co nverter block diagram figure 22-2: voltage reference ou tput buffer example 32-to-1 mux dacr<4:0> r v ref - dacnss r r r r r r 32 dac output dacout 5 (to comparators and adc modules) dacoe v dd v ref + dacpss<1:0> 2 dacen steps digital-to-analog converter (dac) fvr buf1 r v src - v src + v ss daclps 11111 11110 00001 00000 1 0 reserved 11 10 01 00 dacout buffered dac output + ? dac module voltage reference output impedance r pic ? mcu ? 2010-2012 microchip technology inc. ds41412f-page 347 pic18(l)f2x/4xk22 22.7 operation during sleep when the device wakes up from sleep through an interrupt or a watchdog timer time-out, the contents of the vrefcon1 register are not affected. to minimize current consumption in sleep mode, the voltage reference should be disabled. 22.8 effects of a reset a device reset affects the following: ? dac is disabled ? dac output voltage is removed from the dacout pin ? the dacr<4:0> range select bits are cleared 22.9 register definitions: dac control register 22-1: vrefcon1: voltage reference control register 0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 u-0 r/w-0 dacen daclps dacoe ? dacpss<1:0> ? dacnss bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7 dacen: dac enable bit 1 = dac is enabled 0 = dac is disabled bit 6 daclps: dac low-power voltage source select bit 1 = dac positive reference source selected 0 = dac negative reference source selected bit 5 dacoe: dac voltage output enable bit 1 = dac voltage level is also an output on the dacout pin 0 = dac voltage level is disconnected from the dacout pin bit 4 unimplemented: read as ? 0 ? bit 3-2 dacpss<1:0>: dac positive source select bits 00 =v dd 01 =v ref + 10 = fvr buf1 output 11 = reserved, do not use bit 1 unimplemented: read as ? 0 ? bit 0 dacnss: dac negative source select bits 1 =v ref - 0 =v ss pic18(l)f2x/4xk22 ds41412f-page 348 ? 2010-2012 microchip technology inc. register 22-2: vrefcon2: voltage reference control register 1 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? ? dacr<4:0> bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? u = bit is unchanged x = bit is unknown -n/n = value at por and bor/value at all other resets ?1? = bit is set ?0? = bit is cleared bit 7-5 unimplemented: read as ? 0 ? bit 4-0 dacr<4:0>: dac voltage output select bits v out = ((v src +) - (v src -))*(dacr<4:0>/(2 5 )) + v src - table 22-1: registers asso ciated with dac module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page vrefcon0 fvren fvrst fvrs<1:0> ? ? ? ? 344 vrefcon1 dacen daclps dacoe ? dacpss<1:0> ? dacnss 347 vrefcon2 ? ? ? dacr<4:0> 348 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are not used by the dac module. ? 2010-2012 microchip technology inc. ds41412f-page 349 pic18(l)f2x/4xk22 23.0 high/low-voltage detect (hlvd) the pic18(l)f2x/4xk22 devices have a high/low-volt- age detect module (hlvd). this is a programmable cir- cuit that sets both a device voltage trip point and the direction of change from that point. if the device experi- ences an excursion past the trip point in that direction, an interrupt flag is set. if the interrupt is enabled, the pro- gram execution branches to the interrupt vector address and the software responds to the interrupt. the high/low-voltage detect control register ( register 23-1 ) completely controls the operation of the hlvd module. this allows the circuitry to be ?turned off? by the user under software control, which minimizes the current consumption for the device. the module?s block diagram is shown in figure 23-1 . 23.1 register - hlvd control register 23-1: hlvdcon: high/low-v oltage detect control register r/w-0 r-0 r-0 r/w-0 r/w-0 r/w-1 r/w-0 r/w-1 vdirmag bgvst irvst hlvden hlvdl<3:0> 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 vdirmag: voltage direction magnitude select bit 1 = event occurs when voltage equals or exceeds trip point (hlvdl<3:0>) 0 = event occurs when voltage equals or falls below trip point (hlvdl<3:0>) bit 6 bgvst: band gap reference voltages stable status flag bit 1 = internal band gap voltage references are stable 0 = internal band gap voltage reference is not stable bit 5 irvst: internal reference voltage stable flag bit 1 = indicates that the voltage detect logic will generate the interrupt flag at the specified voltage range 0 = indicates that the voltage detect logic will not generate the interrupt flag at the specified voltage range and the hlvd interrupt should not be enabled bit 4 hlvden: high/low-voltage detect power enable bit 1 = hlvd enabled 0 = hlvd disabled bit 3-0 hlvdl<3:0>: voltage detection level bits (1) 1111 = external analog input is used (input comes from the hlvdin pin) 1110 = maximum setting . . . 0000 = minimum setting note 1: see table 27-5 for specifications. pic18(l)f2x/4xk22 ds41412f-page 350 ? 2010-2012 microchip technology inc. the module is enabled by setting the hlvden bit (hlvdcon<4>). each time the hlvd module is enabled, the circuitry requires some time to stabilize. the irvst bit (hlvdcon<5>) is a read-only bit used to indicate when the circuit is stable. the module can only generate an interrupt after the circuit is stable and irvst is set. the vdirmag bit (hlvdcon<7>) determines the overall operation of the module. when vdirmag is cleared, the module monitors for drops in v dd below a predetermined set point. when the bit is set, the module monitors for rises in v dd above the set point. 23.2 operation when the hlvd module is enabled, a comparator uses an internally generated reference voltage as the set point. the set point is compared with the trip point, where each node in the resistor divider represents a trip point voltage. the ?trip point? voltage is the voltage level at which the device detects a high or low-voltage event, depending on the configuration of the module. when the supply voltage is equal to the trip point, the voltage tapped off of the resistor array is equal to the internal reference voltage generated by the voltage reference module. the comparator then generates an interrupt signal by setting the hlvdif bit. the trip point voltage is software programmable to any of 16 values. the trip point is selected by programming the hlvdl<3:0> bits (hlvdcon<3:0>). the hlvd module has an additional feature that allows the user to supply the trip voltage to the module from an external source. this mode is enabled when bits, hlvdl<3:0>, are set to ? 1111 ?. in this state, the comparator input is multiplexed from the external input pin, hlvdin. this gives users the flexibility of configur- ing the high/low-voltage detect interrupt to occur at any voltage in the valid operating range. figure 23-1: hlvd mo dule block diagram (with external input) set v dd 16-to-1 mux hlvden hlvdcon hlvdl<3:0> register hlvdin v dd externally generated trip point hlvdif hlvden boren internal voltage reference vdirmag 1.024v typical ? 2010-2012 microchip technology inc. ds41412f-page 351 pic18(l)f2x/4xk22 23.3 hlvd setup to set up the hlvd module: 1. select the desired hlvd trip point by writing the value to the hlvdl<3:0> bits. 2. set the vdirmag bit to detect high voltage (vdirmag = 1 ) or low voltage (vdirmag = 0 ). 3. enable the hlvd module by setting the hlvden bit. 4. clear the hlvd interrupt flag (pir2<2>), which may have been set from a previous interrupt. 5. if interrupts are desired, enable the hlvd interrupt by setting the hlvdie and gie/gieh bits (pie2<2> and intcon<7>, respectively). an interrupt will not be generated until the irvst bit is set. 23.4 current consumption when the module is enabled, the hlvd comparator and voltage divider are enabled and consume static current. the total current consumption, when enabled, is specified in section 27.0 ?electrical characteris- tics? . depending on the application, the hlvd module does not need to operate constantly. to reduce current requirements, the hlvd circuitry may only need to be enabled for short periods where the voltage is checked. after such a check, the module could be disabled. 23.5 hlvd start-up time the internal reference voltage of the hlvd module, specified in section 27.0 ?electrical characteristics? , may be used by other internal circuitry, such as the programmable brown-out reset. if the hlvd or other circuits using the voltage reference are disabled to lower the device?s current consumption, the reference voltage circuit will require time to become stable before a low or high-voltage condition can be reliably detected. this start-up time, t irvst , is an interval that is independent of device clock speed. the hlvd interrupt flag is not enabled until t irvst has expired and a stable reference voltage is reached. for this reason, brief excursions beyond the set point may not be detected during this interval (see figure 23-2 or figure 23-3 ). figure 23-2: low-voltage detect operation (vdirmag = 0 ) note: before changing any module settings (v dirmag , hlvdl<3:0>), first disable the module (hlvden = 0 ), make the changes and re-enable the module. this prevents the generation of false hlvd events. v hlvd v dd hlvdif v hlvd v dd enable hlvd t irvst hlvdif may not be set enable hlvd hlvdif hlvdif cleared in software hlvdif cleared in software hlvdif cleared in software, case 1: case 2: hlvdif remains set since hlvd condition still exists t irvst internal reference is stable internal reference is stable irvst irvst pic18(l)f2x/4xk22 ds41412f-page 352 ? 2010-2012 microchip technology inc. figure 23-3: high-voltage detect operation (vdirmag = 1 ) 23.6 applications in many applications, it is desirable to detect a drop below, or rise above, a particular voltage threshold. for example, the hlvd module could be periodically enabled to detect universal serial bus (usb) attach or detach. this assumes the device is powered by a lower voltage source than the usb when detached. an attach would indicate a high-voltage detect from, for example, 3.3v to 5v (the voltage on usb) and vice versa for a detach. this feature could save a design a few extra components and an attach signal (input pin). for general battery applications, figure 23-4 shows a possible voltage curve. over time, the device voltage decreases. when the device voltage reaches voltage v a , the hlvd logic generates an interrupt at time, t a . the interrupt could cause the execution of an isr, which would allow the application to perform ?house- keeping tasks? and a controlled shutdown before the device voltage exits the valid operating range at t b . this would give the application a time window, represented by the difference between t a and t b , to safely exit. figure 23-4: typical low-voltage detect application v hlvd v dd hlvdif v hlvd v dd enable hlvd t irvst hlvdif may not be set enable hlvd hlvdif hlvdif cleared in software hlvdif cleared in software hlvdif cleared in software, case 1: case 2: hlvdif remains set since hlvd condition still exists t irvst irvst internal reference is stable internal reference is stable irvst time voltage v a v b t a t b v a = hlvd trip point v b = minimum valid device operating voltage legend: ? 2010-2012 microchip technology inc. ds41412f-page 353 pic18(l)f2x/4xk22 23.7 operation during sleep when enabled, the hlvd circuitry continues to operate during sleep. if the device voltage crosses the trip point, the hlvdif bit will be set and the device will wake-up from sleep. device execution will continue from the interrupt vector address if interrupts have been globally enabled. 23.8 effects of a reset a device reset forces all registers to their reset state. this forces the hlvd module to be turned off. table 23-1: registers associated wi th high/low-voltage detect module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page hlvdcon vdirmag bgvst irvst hlvden hlvdl<3:0> 349 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 116 ipr2 oscfip c1ip c2ip eeip bcl1ip hlvdip tmr3ip ccp2ip 129 pie2 oscfie c1ie c2ie eeie bcl1ie hlvdie tmr3ie ccp2ie 125 pir2 oscfif c1if c2if eeif bcl1if hlvdif tmr3if ccp2if 120 trisa trisa7 trisa6 trisa5 trisa4 trisa3 trisa2 trisa1 trisa0 156 legend: ? = unimplemented locations, read as ? 0 ?. shaded bits are unused by the hlvd module. pic18(l)f2x/4xk22 ds41412f-page 354 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 355 pic18(l)f2x/4xk22 24.0 special features of the cpu pic18(l)f2x/4xk22 devices include several features intended to maximize reliability and minimize cost through elimination of external components. these are: ? oscillator selection ? resets: - power-on reset (por) - power-up timer (pwrt) - oscillator start-up timer (ost) - brown-out reset (bor) ? interrupts ? watchdog timer (wdt) ? code protection ? id locations ? in-circuit serial programming? the oscillator can be configured for the application depending on frequency, power, accuracy and cost. all of the options are discussed in detail in section 2.0 ?oscillator module (with fail-safe clock monitor)? . a complete discussion of device resets and interrupts is available in previous sections of this data sheet. in addition to their power-up and oscillator start-up timers provided for resets, pic18(l)f2x/4xk22 devices have a watchdog timer, which is either permanently enabled via the configuration bits or software controlled (if configured as disabled). the inclusion of an internal rc oscillator also provides the additional benefits of a fail-safe clock monitor (fscm) and two-speed start-up. fscm provides for background monitoring of the peripheral clock and automatic switchover in the event of its failure. two- speed start-up enables code to be executed almost immediately on start-up, while the primary clock source completes its start-up delays. all of these features are enabled and configured by setting the appropriate configuration register bits. 24.1 configuration bits the configuration bits can be programmed (read as ? 0 ?) or left unprogrammed (read as ? 1 ?) to select various device configurations. these bits are mapped starting at program memory location 300000h. the user will note that address 300000h is beyond the user program memory space. in fact, it belongs to the configuration memory space (300000h-3fffffh), which can only be accessed using table reads and table writes. programming the configuration registers is done in a manner similar to programming the flash memory. the wr bit in the eecon1 register starts a self-timed write to the configuration register. in normal operation mode, a tblwt instruction with the tblptr pointing to the configuration register sets up the address and the data for the configuration register write. setting the wr bit starts a long write to the configuration register. the configuration registers are written a byte at a time. to write or erase a configuration cell, a tblwt instruction can write a ? 1 ? or a ? 0 ? into the cell. for additional details on flash programming, refer to section 6.6 ?writing to flash program memory? . pic18(l)f2x/4xk22 ds41412f-page 356 ? 2010-2012 microchip technology inc. table 24-1: configuration bits and device ids address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 default/ unprogrammed value 300000h config1l ? ? ? ? ? ? ? ? 0000 0000 300001h config1h ieso fcmen priclken pllcfg fosc<3:0> 0010 0101 300002h config2l ? ? ? borv<1:0> boren<1:0> pwrten 0001 1111 300003h config2h ? ? wdps<3:0> wdten<1:0> 0011 1111 300004h config3l ? ? ? ? ? ? ? ? 0000 0000 300005h config3h mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx 1011 1111 300006h config4l debug xinst ? ? ?lvp (1) ? strven 1000 0101 300007h config4h ? ? ? ? ? ? ? ? 1111 1111 300008h config5l ? ? ? ?cp3 (2) cp2 (2) cp1 cp0 0000 1111 300009h config5h cpd cpb ? ? ? ? ? ? 1100 0000 30000ah config6l ? ? ? ?wrt3 (2) wrt2 (2) wrt1 wrt0 0000 1111 30000bh config6h wrtd wrtb wrtc (3) ? ? ? ? ? 1110 0000 30000ch config7l ? ? ? ?ebtr3 (2) ebtr2 (2) ebtr1 ebtr0 0000 1111 30000dh config7h ?ebtrb ? ? ? ? ? ? 0100 0000 3ffffeh devid1 (4) dev<2:0> rev<4:0> qqqq qqqq 3fffffh devid2 (4) dev<10:3> 0101 qqqq legend: ? = unimplemented, q = value depends on conditi on. shaded bits are unimplemented, read as ' 0 '. note 1: can only be changed when in high voltage programming mode. 2: available on pic18(l)fx5k22 and pic18(l)fx6k22 devices only. 3: in user mode, this bit is read-only and cannot be self-programmed. 4: see register 24-12 and register 24-13 for devid values. devid registers are read-only and cannot be programmed by the user. ? 2010-2012 microchip technology inc. ds41412f-page 357 pic18(l)f2x/4xk22 24.2 register definitions: configuration word register 24-1: config1h: co nfiguration register 1 high r/p-0 r/p-0 r/p-1 r/p-0 r/p-0 r/p-1 r/p-0 r/p-1 ieso fcmen priclken pllcfg fosc<3:0> bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7 ieso (1) : internal/external oscillator switchover bit 1 = oscillator switchover mode enabled 0 = oscillator switchover mode disabled bit 6 fcmen (1) : fail-safe clock monitor enable bit 1 = fail-safe clock monitor enabled 0 = fail-safe clock monitor disabled bit 5 priclken: primary clock enable bit 1 = primary clock is always enabled 0 = primary clock can be disabled by software bit 4 pllcfg: 4 x pll enable bit 1 = 4 x pll always enabled, oscillator multiplied by 4 0 = 4 x pll is under software control, pllen (osctune<6>) bit 3-0 fosc<3:0>: oscillator selection bits 1111 = external rc oscillator, clkout function on ra6 1110 = external rc oscillator, clkout function on ra6 1101 = ec oscillator (low power, < 500 khz) 1100 = ec oscillator, clkout function on osc2 (low power, < 500 khz) 1011 = ec oscillator (medium power, 500 khz-16 mhz) 1010 = ec oscillator, clkout function on osc2 (medium power, 500 khz-16 mhz) 1001 = internal oscillator block, clkout function on osc2 1000 = internal oscillator block 0111 = external rc oscillator 0110 = external rc oscillator, clkout function on osc2 0101 = ec oscillator (high power, >16 mhz) 0100 = ec oscillator, clkout function on osc2 (high power, >16 mhz) 0011 = hs oscillator (medium power, 4 mhz-16 mhz) 0010 = hs oscillator (high power, >16 mhz) 0001 = xt oscillator 0000 = lp oscillator note 1: when fosc<3:0> is configured for hs, xt, or lp oscillator and fcmen bit is set, then the ieso bit should also be set to prevent a false failed clock indication and to enable automatic clock switch over from the internal oscillator block to the external oscillator when the ost times out. pic18(l)f2x/4xk22 ds41412f-page 358 ? 2010-2012 microchip technology inc. register 24-2: config2l: co nfiguration register 2 low u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ?borv<1:0> (1) boren<1:0> (2) pwrten (2) bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7-5 unimplemented: read as ? 0 ? bit 4-3 borv<1:0>: brown-out reset voltage bits (1) 11 = v bor set to 1.9v nominal 10 = v bor set to 2.2v nominal 01 = v bor set to 2.5v nominal 00 = v bor set to 2.85v nominal bit 2-1 boren<1:0>: brown-out reset enable bits (2) 11 = brown-out reset enabled in hardware only (sboren is disabled) 10 = brown-out reset enabled in hardware only and disabled in sleep mode (sboren is disabled) 01 = brown-out reset enabled and controlled by software (sboren is enabled) 00 = brown-out reset disabled in hardware and software bit 0 pwrten : power-up timer enable bit (2) 1 = pwrt disabled 0 = pwrt enabled note 1: see section 27.1 ?dc characteristics: supply voltage, pic18(l)f2x/4xk22? for specifications. 2: the power-up timer is decoupled from brown-out reset, allowing these features to be independently controlled. ? 2010-2012 microchip technology inc. ds41412f-page 359 pic18(l)f2x/4xk22 register 24-3: config2h: co nfiguration register 2 high u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 ? ? wdtps<3:0> wdten<1:0> bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7-6 unimplemented: read as ? 0 ? bit 5-2 wdtps<3:0>: watchdog timer postscale select bits 1111 = 1:32,768 1110 = 1:16,384 1101 = 1:8,192 1100 = 1:4,096 1011 = 1:2,048 1010 = 1:1,024 1001 = 1:512 1000 = 1:256 0111 = 1:128 0110 = 1:64 0101 = 1:32 0100 = 1:16 0011 = 1:8 0010 = 1:4 0001 = 1:2 0000 = 1:1 bit 1-0 wdten<1:0>: watchdog timer enable bits 11 = wdt enabled in hardware; swdten bit disabled 10 = wdt controlled by the swdten bit 01 = wdt enabled when device is active, disabled when device is in sleep; swdten bit disabled 00 = wdt disabled in hardware; swdten bit disabled pic18(l)f2x/4xk22 ds41412f-page 360 ? 2010-2012 microchip technology inc. register 24-4: config3h: co nfiguration register 3 high r/p-1 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 mclre ? p2bmx t3cmx hfofst ccp3mx pbaden ccp2mx bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed x = bit is unknown bit 7 mclre: mclr pin enable bit 1 = mclr pin enabled; re3 input pin disabled 0 = re3 input pin enabled; mclr disabled bit 6 unimplemented: read as ? 0 ? bit 5 p2bmx: p2b input mux bit 1 = p2b is on rb5 (1) p2b is on rd2 (2) 0 = p2b is on rc0 bit 4 t3cmx: timer3 clock input mux bit 1 = t3cki is on rc0 0 = t3cki is on rb5 bit 3 hfofst: hfintosc fast start-up bit 1 = hfintosc starts clocking the cpu without waiting for the oscillator to stabilize 0 = the system clock is held off until the hfintosc is stable bit 2 ccp3mx: ccp3 mux bit 1 = ccp3 input/output is multiplexed with rb5 0 = ccp3 input/output is multiplexed with rc6 (1) ccp3 input/output is multiplexed with re0 (2) bit 1 pbaden: portb a/d enable bit 1 = anselb<5:0> resets to 1 , portb<5:0> pins are configured as analog inputs on reset 0 = anselb<5:0> resets to 0 , portb<4:0> pins are configured as digital i/o on reset bit 0 ccp2mx: ccp2 mux bit 1 = ccp2 input/output is multiplexed with rc1 0 = ccp2 input/output is multiplexed with rb3 note 1: pic18(l)f2xk22 devices only. 2: pic18(l)f4xk22 devices only. ? 2010-2012 microchip technology inc. ds41412f-page 361 pic18(l)f2x/4xk22 register 24-5: config4l: co nfiguration register 4 low r/p-1 r/p-0 u-0 u-0 u-0 r/p-1 u-0 r/p-1 debug (2) xinst ? ? ?lvp (1) ? stvren bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? -n = value when device is un programmed x = bit is unknown bit 7 debug : background debugger enable bit (2) 1 = background debugger disabled, rb6 and rb7 configured as general purpose i/o pins 0 = background debugger enabled, rb6 and rb7 ar e dedicated to in-circuit debug bit 6 xinst: extended instruction set enable bit 1 = instruction set extension and indexed addressing mode enabled 0 = instruction set extension and indexed addressing mode disabled (legacy mode) bit 5-3 unimplemented: read as ? 0 ? bit 2 lvp: single-supply icsp enable bit 1 = single-supply icsp enabled 0 = single-supply icsp disabled bit 1 unimplemented: read as ? 0 ? bit 0 stvren: stack full/underflow reset enable bit 1 = stack full/underflow will cause reset 0 = stack full/underflow will not cause reset note 1: can only be changed by a programmer in high-voltage programming mode. 2: the debug bit is managed automatically by device developm ent tools including debuggers and programmers. for normal device operations, this bi t should be maintained as a ? 1 ?. register 24-6: config5l: co nfiguration register 5 low u-0 u-0 u-0 u-0 r/c-1 r/c-1 r/c-1 r/c-1 ? ? ? ?cp3 (1) cp2 (1) cp1 cp0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-4 unimplemented: read as ? 0 ? bit 3 cp3: code protection bit (1) 1 = block 3 not code-protected 0 = block 3 code-protected bit 2 cp2: code protection bit (1) 1 = block 2 not code-protected 0 = block 2 code-protected bit 1 cp1: code protection bit 1 = block 1 not code-protected 0 = block 1 code-protected bit 0 cp0: code protection bit 1 = block 0 not code-protected 0 = block 0 code-protected note 1: available on pic18(l)fx5k22 and pic18(l)fx6k22 devices. pic18(l)f2x/4xk22 ds41412f-page 362 ? 2010-2012 microchip technology inc. register 24-7: config5h: co nfiguration register 5 high r/c-1 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 cpd cpb ? ? ? ? ? ? bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7 cpd: data eeprom code protection bit 1 = data eeprom not code-protected 0 = data eeprom code-protected bit 6 cpb: boot block code protection bit 1 = boot block not code-protected 0 = boot block code-protected bit 5-0 unimplemented: read as ? 0 ? register 24-8: config6l: co nfiguration register 6 low u-0 u-0 u-0 u-0 r/c-1 r/c-1 r/c-1 r/c-1 ? ? ? ?wrt3 (1) wrt2 (1) wrt1 wrt0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-4 unimplemented: read as ? 0 ? bit 3 wrt3: write protection bit (1) 1 = block 3 not write-protected 0 = block 3 write-protected bit 2 wrt2: write protection bit (1) 1 = block 2 not write-protected 0 = block 2 write-protected bit 1 wrt1: write protection bit 1 = block 1 not write-protected 0 = block 1 write-protected bit 0 wrt0: write protection bit 1 = block 0 not write-protected 0 = block 0 write-protected note 1: available on pic18(l)fx5k22 and pic18(l)fx6k22 devices. ? 2010-2012 microchip technology inc. ds41412f-page 363 pic18(l)f2x/4xk22 register 24-9: config6h: co nfiguration register 6 high r/c-1 r/c-1 r-1 u-0 u-0 u-0 u-0 u-0 wrtd wrtb wrtc (1) ? ? ? ? ? bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7 wrtd: data eeprom write protection bit 1 = data eeprom not write-protected 0 = data eeprom write-protected bit 6 wrtb: boot block write protection bit 1 = boot block not write-protected 0 = boot block write-protected bit 5 wrtc: configuration register write protection bit (1) 1 = configuration registers not write-protected 0 = configuration registers write-protected bit 4-0 unimplemented: read as ? 0 ? note 1: this bit is read-only in normal execution mode; it can be written only in program mode. register 24-10: config7l: co nfiguration register 7 low u-0 u-0 u-0 u-0 r/c-1 r/c-1 r/c-1 r/c-1 ? ? ? ? ebtr3 (1) ebtr2 (1) ebtr1 ebtr0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-4 unimplemented: read as ? 0 ? bit 3 ebtr3: table read protection bit (1) 1 = block 3 not protected from table reads executed in other blocks 0 = block 3 protected from table reads executed in other blocks bit 2 ebtr2 : table read protection bit (1) 1 = block 2 not protected from table reads executed in other blocks 0 = block 2 protected from table reads executed in other blocks bit 1 ebtr1: table read protection bit 1 = block 1 not protected from table reads executed in other blocks 0 = block 1 protected from table reads executed in other blocks bit 0 ebtr0: table read protection bit 1 = block 0 not protected from table reads executed in other blocks 0 = block 0 protected from table reads executed in other blocks note 1: available on pic18(l)fx5k22 and pic18(l)fx6k22 devices. pic18(l)f2x/4xk22 ds41412f-page 364 ? 2010-2012 microchip technology inc. register 24-11: config7h: co nfiguration register 7 high u-0 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 ? ebtrb ? ? ? ? ? ? bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7 unimplemented: read as ? 0 ? bit 6 ebtrb: boot block table read protection bit 1 = boot block not protected from table reads executed in other blocks 0 = boot block protected from table reads executed in other blocks bit 5-0 unimplemented: read as ? 0 ? register 24-12: devid1: device id register 1 rrrrrrrr dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-5 dev<2:0>: device id bits these bits, together with dev<10:3> in devid2, determine the device id. see table 24-2 for complete device id list. bit 4-0 rev<4:0>: revision id bits these bits indicate the device revision. register 24-13: devid2: device id register 2 rrrrrrrr dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 bit 7 bit 0 legend: r = readable bit u = unimplemented bit, read as ?0? -n = value when device is unprogrammed c = clearable only bit bit 7-0 dev<10:3>: device id bits these bits, together with dev<2:0> in devid1, determine the device id. see table 24-2 for complete device id list. ? 2010-2012 microchip technology inc. ds41412f-page 365 pic18(l)f2x/4xk22 table 24-2: device id table for the pic18(l)f2x/4xk22 family dev<10:3> dev<2:0> part number 0101 0100 000 pic18f46k22 001 pic18lf46k22 010 pic18f26k22 011 pic18lf26k22 0101 0101 000 pic18f45k22 001 pic18lf45k22 010 pic18f25k22 011 pic18lf25k22 0101 0110 000 pic18f44k22 001 pic18lf44k22 010 pic18f24k22 011 pic18lf24k22 0101 0111 000 pic18f43k22 001 pic18lf43k22 010 pic18f23k22 011 pic18lf23k22 pic18(l)f2x/4xk22 ds41412f-page 366 ? 2010-2012 microchip technology inc. 24.3 watchdog timer (wdt) for pic18(l)f2x/4xk22 devices, the wdt is driven by the lfintosc source. when the wdt is enabled, the clock source is also enabled. the nominal wdt period is 4 ms and has the same stability as the lfintosc oscillator. the 4 ms period of the wdt is multiplied by a 16-bit postscaler. any output of the wdt postscaler is selected by a multiplexer, controlled by bits in configu- ration register 2h. available periods range from 4 ms to 131.072 seconds (2.18 minutes). the wdt and postscaler are cleared when any of the following events occur: a sleep or clrwdt instruction is executed, the ircf bits of the osccon register are changed or a clock failure has occurred. figure 24-1: wdt block diagram note 1: the clrwdt and sleep instructions clear the wdt and postscaler counts when executed. 2: changing the setting of the ircf bits of the osccon register clears the wdt and postscaler counts. 3: when a clrwdt instruction is executed, the postscaler count will be cleared. lfintosc source wdt wake-up reset wdt counter programmable postscaler 1:1 to 1:32,768 enable wdt wdtps<3:0> swdten wdten clrwdt 4 from power reset all device resets sleep ? 128 change on ircf bits managed modes ? 2010-2012 microchip technology inc. ds41412f-page 367 pic18(l)f2x/4xk22 24.3.1 control register register 24-14 shows the wdtcon register. this is a readable and writable register which contains a control bit that allows software to override the wdt enable configuration bit, but only if the configuration bit has disabled the wdt. 24.4 register definitions: wdt control table 24-4: configuration registers associated with watchdog timer register 24-14: wdtcon: wat chdog timer control register u-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-0 ? ? ? ? ? ? ?swdten (1) bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown bit 7-1 unimplemented: read as ? 0 ? bit 0 swdten: software enable or disable the watchdog timer bit (1) 1 = wdt is turned on 0 = wdt is turned off (reset value) note 1: this bit has no effect if the configuration bit, wdten, is enabled. table 24-3: registers associated with watchdog timer name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page rcon ipen sboren ? ri to pd por bor 60 wdtcon ? ? ? ? ? ? ?swdten 367 legend: ? = unimplemented, read as ? 0 ?. shaded bits are not used by the watchdog timer. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 reset values on page config2h ? ? wdps<3:0> wdten<1:0> 359 legend: ? = unimplemented, read as ? 0 ?. shaded bits are not used by the watchdog timer. pic18(l)f2x/4xk22 ds41412f-page 368 ? 2010-2012 microchip technology inc. 24.5 program verification and code protection the overall structure of the code protection on the pic18 flash devices differs significantly from other pic ? microcontroller devices. the user program memory is divided into three or five blocks, depending on the device. one of these is a boot block of 0.5k or 2k bytes, depending on the device. the remainder of the memory is divided into individual blocks on binary boundaries. each of the blocks has three code protection bits asso- ciated with them. they are: ? code-protect bit (cpn) ? write-protect bit (wrtn) ? external block table read bit (ebtrn) figure 24-2 shows the program memory organization for 8, 16 and 32-kbyte devices and the specific code protection bit associated with each block. the actual locations of the bits are summarized in table 24-5 . figure 24-2: code-protected progra m memory for pic18(l)f2x/4xk22 memory size/device block code protection controlled by: 8kbytes (pic18(l)fx3k22) 16 kbytes (pic18(l)fx4k22) 32 kbytes (pic18(l)fx5k22) 64 kbytes (pic18(l)fx6k22) boot block (000h-1ffh) boot block (000h-7ffh) boot block (000h-7ffh) boot block (000h-7ffh) cpb, wrtb, ebtrb block 0 (200h-fffh) block 0 (800h-1fffh) block 0 (800h-1fffh) block 0 (800h-3fffh) cp0, wrt0, ebtr0 block 1 (1000h-1fffh) block 1 (2000h-3fffh) block 1 (2000h-3fffh) block 1 (4000h-7fffh) cp1, wrt1, ebtr1 unimplemented read ? 0 ?s (2000h-1fffffh) unimplemented read ? 0 ?s (4000h-1fffffh) block 2 (4000h-5fffh) block 2 (8000h-bfffh) cp2, wrt2, ebtr2 block 3 (6000h-7fffh) block 3 (c000h-ffffh) cp3, wrt3, ebtr3 unimplemented read ? 0 ?s (8000h-1fffffh) unimplemented read ? 0 ?s (10000h-1fffffh) (unimplemented memory space) table 24-5: configuration registers associated with code protection file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 300008h config5l ? ? ? ?cp3 (1) cp2 (1) cp1 cp0 300009h config5h cpd cpb ? ? ? ? ? ? 30000ah config6l ? ? ? ?wrt3 (1) wrt2 (1) wrt1 wrt0 30000bh config6h wrtd wrtb wrtc (2) ? ? ? ? ? 30000ch config7l ? ? ? ? ebtr3 (1) ebtr2 (1) ebtr1 ebtr0 30000dh config7h ? ebtrb ? ? ? ? ? ? legend: shaded bits are unimplemented. note 1: available on pic18(l)fx5k22 and pic18(l)fx6k22 devices only. 2: in user mode, this bit is read-only and cannot be self-programmed. ? 2010-2012 microchip technology inc. ds41412f-page 369 pic18(l)f2x/4xk22 24.5.1 program memory code protection the program memory may be read to or written from any location using the table read and table write instructions. the device id may be read with table reads. the configuration registers may be read and written with the table read and table write instructions. in normal execution mode, the cpn bits have no direct effect. cpn bits inhibit external reads and writes. a block of user memory may be protected from table writes if the wrtn configuration bit is ? 0 ?. the ebtrn bits control table reads. for a block of user memory with the ebtrn bit cleared to ? 0 ?, a table read instruction that executes from within that block is allowed to read. a table read instruction that executes from a location outside of that block is not allowed to read and will result in reading ? 0 ?s. figures 24-3 through 24-5 illustrate table write and table read protection. figure 24-3: table write (wrtn) disallowed note: code protection bits may only be written to a ? 0 ? from a ? 1 ? state. it is not possible to write a ? 1 ? to a bit in the ? 0 ? state. code pro- tection bits are only set to ? 1 ? by a full chip erase or block erase function. the full chip erase and block erase functions can only be initiated via icsp? or an external programmer. 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh wrtb, ebtrb = 11 wrt0, ebtr0 = 01 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblwt* tblptr = 0008ffh pc = 001ffeh tblwt* pc = 005ffeh register values program memory configuration bit settings results: all table writes disabled to blockn whenever wrtn = 0 . pic18(l)f2x/4xk22 ds41412f-page 370 ? 2010-2012 microchip technology inc. figure 24-4: external block table read (ebtrn) disallowed figure 24-5: external block table read (ebtrn) allowed wrtb, ebtrb = 11 wrt0, ebtr0 = 10 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblrd* tblptr = 0008ffh pc = 003ffeh results: all table reads from external blocks to blockn are disabled whenever ebtrn = 0 . tablat register returns a value of ? 0 ?. register values program memory configuration bit settings 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh wrtb, ebtrb = 11 wrt0, ebtr0 = 10 wrt1, ebtr1 = 11 wrt2, ebtr2 = 11 wrt3, ebtr3 = 11 tblrd* tblptr = 0008ffh pc = 001ffeh register values program memory configuration bit settings results: table reads permitted within blockn, even when ebtrbn = 0 . tablat register returns the value of the data at the location tblptr. 000000h 0007ffh 000800h 001fffh 002000h 003fffh 004000h 005fffh 006000h 007fffh ? 2010-2012 microchip technology inc. ds41412f-page 371 pic18(l)f2x/4xk22 24.5.2 data eeprom code protection the entire data eeprom is protected from external reads and writes by two bits: cpd and wrtd. cpd inhibits external reads and writes of data eeprom. wrtd inhibits internal and external writes to data eeprom. the cpu can always read data eeprom under normal operation, regardless of the protection bit settings. 24.5.3 configuration register protection the configuration registers can be write-protected. the wrtc bit controls protection of the configuration registers. in normal execution mode, the wrtc bit is readable only. wrtc can only be written via icsp or an external programmer. 24.6 id locations eight memory locations (200000h-200007h) are designated as id locations, where the user can store checksum or other code identification numbers. these locations are both readable and writable during normal execution through the tblrd and tblwt instructions or during program/verify. the id locations can be read when the device is code-protected. 24.7 in-circuit serial programming pic18(l)f2x/4xk22 devices can be serially programmed while in the end application circuit. this is simply done with two lines for clock and data and three other lines for power, ground and the programming voltage. this allows customers to manufacture boards with unprogrammed devices and then program the microcontroller just before shipping the product. this also allows the most recent firmware or a custom firmware to be programmed. 24.8 in-circuit debugger when the debug configuration bit is programmed to a ? 0 ?, the in-circuit debugger functionality is enabled. this function allows simple debugging functions when used with mplab ? ide. when the microcontroller has this feature enabled, some resources are not available for general use. table 24-6 shows which resources are required by the background debugger. table 24-6: debugger resources to use the in-circuit debugger function of the microcontroller, the design must implement in-circuit serial programming connections to the following pins: ?mclr /v pp /re3 ?v dd ?v ss ?rb7 ?rb6 this will interface to the in-circuit debugger module available from microchip or one of the third party development tool companies. 24.9 single-supply icsp programming the lvp configuration bit enables single-supply icsp programming (formerly known as low-voltage icsp programming or lvp). when single-supply program- ming is enabled, the microcontroller can be programmed without requiring high voltage being applied to the mclr /v pp /re3 pin. see ? pic18(l)f2xk22/4xk22 flash memory programming ? (ds41398) for more details about low voltage programming. the lvp bit may be set or cleared only when using standard high-voltage programming (v ihh applied to the mclr /v pp /re3 pin). once lvp has been disabled, only the standard high-voltage programming is available and must be used to program the device. memory that is not code-protected can be erased using either a block erase, or erased row by row, then written at any specified v dd . if code-protected memory is to be erased, a block erase is required. i/o pins: rb6, rb7 note 1: high-voltage programming is always available, regardless of the state of the lvp bit, by applying v ihh to the mclr pin. 2: by default, single-supply icsp is enabled in unprogrammed devices (as supplied from microchip) and erased devices. 3: while in low-voltage icsp mode, mclr is always enabled, regardless of the mclre bit, and the re3 pin can no longer be used as a general purpose input. pic18(l)f2x/4xk22 ds41412f-page 372 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 373 pic18(l)f2x/4xk22 25.0 instruction set summary pic18(l)f2x/4xk22 devices incorporate the standard set of 75 pic18 core instructions, as well as an extended set of eight new instructions, for the optimization of code that is recursive or that utilizes a software stack. the extended set is discussed later in this section. 25.1 standard instruction set the standard pic18 instruction set adds many enhancements to the previous pic ? mcu instruction sets, while maintaining an easy migration from these pic ? mcu instruction sets. most instructions are a single program memory word (16 bits), but there are four instructions that require two program memory locations. each single-word instruction is a 16-bit word divided into an opcode, which specifies the instruction type and one or more operands, which further specify the operation of the instruction. the instruction set is highly orthogonal and is grouped into four basic categories: ? byte-oriented operations ? bit-oriented operations ? literal operations ? control operations the pic18 instruction set summary in table 25-2 lists byte-oriented , bit-oriented , literal and control operations. table 25-1 shows the opcode field descriptions. most byte-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the destination of the result (specified by ?d?) 3. the accessed memory (specified by ?a?) the file register designator ?f? specifies which file register is to be used by the instruction. the destination designator ?d? specifies where the result of the opera- tion is to be placed. if ?d? is zero, the result is placed in the wreg register. if ?d? is one, the result is placed in the file register specified in the instruction. all bit-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the bit in the file register (specified by ?b?) 3. the accessed memory (specified by ?a?) the bit field designator ?b? selects the number of the bit affected by the operation, while the file register designator ?f? represents the number of the file in which the bit is located. the literal instructions may use some of the following operands: ? a literal value to be loaded into a file register (specified by ?k?) ? the desired fsr register to load the literal value into (specified by ?f?) ? no operand required (specified by ???) the control instructions may use some of the following operands: ? a program memory address (specified by ?n?) ? the mode of the call or return instructions (specified by ?s?) ? the mode of the table read and table write instructions (specified by ?m?) ? no operand required (specified by ???) all instructions are a single word, except for four double-word instructions. these instructions were made double-word to contain the required information in 32 bits. in the second word, the four msbs are ? 1 ?s. if this second word is executed as an instruction (by itself), it will execute as a nop . all single-word instructions are executed in a single instruction cycle, unless a conditional test is true or the program counter is changed as a result of the instruc- tion. in these cases, the execution takes two instruction cycles, with the additional instruction cycle(s) executed as a nop . the double-word instructions execute in two instruction cycles. one instruction cycle consists of four oscillator periods. thus, for an oscillator frequency of 4 mhz, the normal instruction execution time is 1 ? s. if a conditional test is true, or the program counter is changed as a result of an instruction, the instruction execution time is 2 ? s. two-word branch instructions (if true) would take 3 ? s. figure 25-1 shows the general formats that the instruc- tions can have. all examples use the convention ?nnh? to represent a hexadecimal number. the instruction set summary, shown in ta bl e 2 5 -2 , lists the standard instructions recognized by the microchip assembler (mpasm tm ). section 25.1.1 ?standard instruction set? provides a description of each instruction. pic18(l)f2x/4xk22 ds41412f-page 374 ? 2010-2012 microchip technology inc. table 25-1: opcode field descriptions field description a ram access bit a = 0 : ram location in access ram (bsr register is ignored) a = 1 : ram bank is specified by bsr register bbb bit address within an 8-bit file register (0 to 7). bsr bank select register. used to select the current ram bank. c, dc, z, ov, n alu status bits: c arry, d igit c arry, z ero, ov erflow, n egative. d destination select bit d = 0 : store result in wreg d = 1 : store result in file register f dest destination: either the wreg register or the specified register file location. f 8-bit register file address (00h to ffh) or 2-bit fsr designator (0h to 3h). f s 12-bit register file address (000h to ff fh). this is the source address. f d 12-bit register file address (000h to fffh). this is the destination address. gie global interrupt enable bit. k literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-bit value). label label name. mm the mode of the tblptr register for the table read and table write instructions. only used with table read and table write instructions: * no change to register (such as tb lptr with table reads and writes) *+ post-increment register (such as tblptr with table reads and writes) *- post-decrement register (such as tblptr with table reads and writes) +* pre-increment register (such as tblptr with table reads and writes) n the relative address (2?s complement number) for relative branch instructions or the direct address for call / branch and return instructions. pc program counter. pcl program counter low byte. pch program counter high byte. pclath program counter high byte latch. pclatu program counter upper byte latch. pd power-down bit. prodh product of multiply high byte. prodl product of multiply low byte. s fast call/return mode select bit s = 0 : do not update into/from shadow registers s = 1 : certain registers loaded into/from shadow registers (fast mode) tblptr 21-bit table pointer (points to a program memory location). tablat 8-bit table latch. to time-out bit. tos top-of-stack. u unused or unchanged. wdt watchdog timer. wreg working register (accumulator). x don?t care (? 0 ? or ? 1 ?). the assembler will generate code with x = 0 . it is the recommended form of use for compatibility with all microchip software tools. z s 7-bit offset value for indirect addr essing of register files (source). z d 7-bit offset value for indirect addressing of register files (destination). { } optional argument. [text] indicates an indexed address. (text) the contents of text . [expr] ? 2010-2012 microchip technology inc. ds41412f-page 375 pic18(l)f2x/4xk22 figure 25-1: general format for instructions byte-oriented file register operations 15 10 9 8 7 0 d = 0 for result destination to be wreg register opcode d a f (file #) d = 1 for result destination to be file register (f) a = 0 to force access bank bit-oriented file register operations 15 12 11 9 8 7 0 opcode b (bit #) a f (file #) b = 3-bit position of bit in file register (f) literal operations 15 8 7 0 opcode k (literal) k = 8-bit immediate value byte to byte move operations (2-word) 15 12 11 0 opcode f (source file #) call , goto and branch operations 15 8 7 0 opcode n<7:0> (literal) n = 20-bit immediate value a = 1 for bsr to select bank f = 8-bit file register address a = 0 to force access bank a = 1 for bsr to select bank f = 8-bit file register address 15 12 11 0 1111 n<19:8> (literal) 15 12 11 0 1111 f (destination file #) f = 12-bit file register address control operations example instruction addwf myreg, w, b movff myreg1, myreg2 bsf myreg, bit, b movlw 7fh goto label 15 8 7 0 opcode n<7:0> (literal) 15 12 11 0 1111 n<19:8> (literal) call myfunc 15 11 10 0 opcode n<10:0> (literal) s = fast bit bra myfunc 15 8 7 0 opcode n<7:0> (literal) bc myfunc s pic18(l)f2x/4xk22 ds41412f-page 376 ? 2010-2012 microchip technology inc. table 25-2: pic18(l)f2x/ 4xk22 instruction set mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb byte-oriented operations addwf addwfc andwf clrf comf cpfseq cpfsgt cpfslt decf decfsz dcfsnz incf incfsz infsnz iorwf movf movff movwf mulwf negf rlcf rlncf rrcf rrncf setf subfwb subwf subwfb swapf tstfsz xorwf f, d, a f, d, a f, d, a f, a f, d, a f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f s , f d f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a add wreg and f add wreg and carry bit to f and wreg with f clear f complement f compare f with wreg, skip = compare f with wreg, skip > compare f with wreg, skip < decrement f decrement f, skip if 0 decrement f, skip if not 0 increment f increment f, skip if 0 increment f, skip if not 0 inclusive or wreg with f move f move f s (source) to 1st word f d (destination) 2nd word move wreg to f multiply wreg with f negate f rotate left f through carry rotate left f (no carry) rotate right f through carry rotate right f (no carry) set f subtract f from wreg with borrow subtract wreg from f subtract wreg from f with borrow swap nibbles in f test f, skip if 0 exclusive or wreg with f 1 1 1 1 1 1 (2 or 3) 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 (2 or 3) 1 0010 0010 0001 0110 0001 0110 0110 0110 0000 0010 0100 0010 0011 0100 0001 0101 1100 1111 0110 0000 0110 0011 0100 0011 0100 0110 0101 0101 0101 0011 0110 0001 01da 00da 01da 101a 11da 001a 010a 000a 01da 11da 11da 10da 11da 10da 00da 00da ffff ffff 111a 001a 110a 01da 01da 00da 00da 100a 01da 11da 10da 10da 011a 10da ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff c, dc, z, ov, n c, dc, z, ov, n z, n z z, n none none none c, dc, z, ov, n none none c, dc, z, ov, n none none z, n z, n none none none c, dc, z, ov, n c, z, n z, n c, z, n z, n none c, dc, z, ov, n c, dc, z, ov, n c, dc, z, ov, n none none z, n 1, 2 1, 2 1,2 2 1, 2 4 4 1, 2 1, 2, 3, 4 1, 2, 3, 4 1, 2 1, 2, 3, 4 4 1, 2 1, 2 1 1, 2 1, 2 1, 2 1, 2 4 1, 2 note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as i nput and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and where applicable, ?d? = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensur es that all program memory locations have a valid instruction. ? 2010-2012 microchip technology inc. ds41412f-page 377 pic18(l)f2x/4xk22 bit-oriented operations bcf bsf btfsc btfss btg f, b, a f, b, a f, b, a f, b, a f, b, a bit clear f bit set f bit test f, skip if clear bit test f, skip if set bit toggle f 1 1 1 (2 or 3) 1 (2 or 3) 1 1001 1000 1011 1010 0111 bbba bbba bbba bbba bbba ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff none none none none none 1, 2 1, 2 3, 4 3, 4 1, 2 control operations bc bn bnc bnn bnov bnz bov bra bz call clrwdt daw goto nop nop pop push rcall reset retfie retlw return sleep n n n n n n n n n k, s ? ? k ? ? ? ? n s k s ? branch if carry branch if negative branch if not carry branch if not negative branch if not overflow branch if not zero branch if overflow branch unconditionally branch if zero call subroutine 1st word 2nd word clear watchdog timer decimal adjust wreg go to address 1st word 2nd word no operation no operation pop top of return stack (tos) push top of return stack (tos) relative call software device reset return from interrupt enable return with literal in wreg return from subroutine go into standby mode 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 2 1 (2) 2 1 1 2 1 1 1 1 2 1 2 2 2 1 1110 1110 1110 1110 1110 1110 1110 1101 1110 1110 1111 0000 0000 1110 1111 0000 1111 0000 0000 1101 0000 0000 0000 0000 0000 0010 0110 0011 0111 0101 0001 0100 0nnn 0000 110s kkkk 0000 0000 1111 kkkk 0000 xxxx 0000 0000 1nnn 0000 0000 1100 0000 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0000 0000 kkkk kkkk 0000 xxxx 0000 0000 nnnn 1111 0001 kkkk 0001 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0100 0111 kkkk kkkk 0000 xxxx 0110 0101 nnnn 1111 000s kkkk 001s 0011 none none none none none none none none none none to , pd c none none none none none none all gie/gieh, peie/giel none none to , pd 4 table 25-2: pic18(l)f2x/4xk22 instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as i nput and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and where applicable, ?d? = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensures that all program memory locations have a valid instruction. pic18(l)f2x/4xk22 ds41412f-page 378 ? 2010-2012 microchip technology inc. literal operations addlw andlw iorlw lfsr movlb movlw mullw retlw sublw xorlw k k k f, k k k k k k k add literal and wreg and literal with wreg inclusive or literal with wreg move literal (12-bit) 2nd word to fsr(f) 1st word move literal to bsr<3:0> move literal to wreg multiply literal with wreg return with literal in wreg subtract wreg from literal exclusive or literal with wreg 1 1 1 2 1 1 1 2 1 1 0000 0000 0000 1110 1111 0000 0000 0000 0000 0000 0000 1111 1011 1001 1110 0000 0001 1110 1101 1100 1000 1010 kkkk kkkk kkkk 00ff kkkk 0000 kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk c, dc, z, ov, n z, n z, n none none none none none c, dc, z, ov, n z, n data memory ? program memory operations tblrd* tblrd*+ tblrd*- tblrd+* tblwt* tblwt*+ tblwt*- tblwt+* table read table read with post-increment table read with post-decrement table read with pre-increment table write table write with post-increment table write with post-decrement table write with pre-increment 2 2 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1000 1001 1010 1011 1100 1101 1110 1111 none none none none none none none none table 25-2: pic18(l)f2x/4xk22 instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as i nput and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and where applicable, ?d? = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are two-word instructions. the se cond word of these instructions will be executed as a nop unless the first word of the instruction retrieves the information embe dded in these 16 bits. this ensur es that all program memory locations have a valid instruction. ? 2010-2012 microchip technology inc. ds41412f-page 379 pic18(l)f2x/4xk22 25.1.1 standard instruction set addlw add literal to w syntax: addlw k operands: 0 ? k ? 255 operation: (w) + k ? w status affected: n, ov, c, dc, z encoding: 0000 1111 kkkk kkkk description: the contents of w are added to the 8-bit literal ?k? and the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : addlw 15h before instruction w = 10h after instruction w = 25h addwf add w to f syntax: addwf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (w) + (f) ? dest status affected: n, ov, c, dc, z encoding: 0010 01da ffff ffff description: add w to register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : addwf reg, 0, 0 before instruction w = 17h reg = 0c2h after instruction w = 0d9h reg = 0c2h note: all pic18 instructions may take an optional label argument preceding the instruction mnemonic for use in symbolic addressing. if a label is used, the instruction format then becomes: {label} instruction argument(s). pic18(l)f2x/4xk22 ds41412f-page 380 ? 2010-2012 microchip technology inc. addwfc add w and carry bit to f syntax: addwfc f {,d {,a}} operands: 0 ? f ? 255 d ?? [0,1] a ?? [0,1] operation: (w) + (f) + (c) ? dest status affected: n,ov, c, dc, z encoding: 0010 00da ffff ffff description: add w, the carry flag and data mem- ory location ?f?. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed in data memory location ?f?. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : addwfc reg, 0, 1 before instruction carry bit = 1 reg = 02h w=4dh after instruction carry bit = 0 reg = 02h w = 50h andlw and literal with w syntax: andlw k operands: 0 ? k ? 255 operation: (w) .and. k ? w status affected: n, z encoding: 0000 1011 kkkk kkkk description: the contents of w are and?ed with the 8-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : andlw 05fh before instruction w=a3h after instruction w = 03h ? 2010-2012 microchip technology inc. ds41412f-page 381 pic18(l)f2x/4xk22 andwf and w with f syntax: andwf f {,d {,a}} operands: 0 ? f ? 255 d ?? [0,1] a ?? [0,1] operation: (w) .and. (f) ? dest status affected: n, z encoding: 0001 01da ffff ffff description: the contents of w are and?ed with register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : andwf reg, 0, 0 before instruction w = 17h reg = c2h after instruction w = 02h reg = c2h bc branch if carry syntax: bc n operands: -128 ? n ? 127 operation: if carry bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0010 nnnn nnnn description: if the carry bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bc 5 before instruction pc = address (here) after instruction if carry = 1; pc = address (here + 12) if carry = 0; pc = address (here + 2) pic18(l)f2x/4xk22 ds41412f-page 382 ? 2010-2012 microchip technology inc. bcf bit clear f syntax: bcf f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [0,1] operation: 0 ? f status affected: none encoding: 1001 bbba ffff ffff description: bit ?b? in register ?f? is cleared. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : bcf flag_reg, 7, 0 before instruction flag_reg = c7h after instruction flag_reg = 47h bn branch if negative syntax: bn n operands: -128 ? n ? 127 operation: if negative bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0110 nnnn nnnn description: if the negative bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bn jump before instruction pc = address (here) after instruction if negative = 1; pc = address (jump) if negative = 0; pc = address (here + 2) ? 2010-2012 microchip technology inc. ds41412f-page 383 pic18(l)f2x/4xk22 bnc branch if not carry syntax: bnc n operands: -128 ? n ? 127 operation: if carry bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0011 nnnn nnnn description: if the carry bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnc jump before instruction pc = address (here) after instruction if carry = 0; pc = address (jump) if carry = 1; pc = address (here + 2) bnn branch if not negative syntax: bnn n operands: -128 ? n ? 127 operation: if negative bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0111 nnnn nnnn description: if the negative bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnn jump before instruction pc = address (here) after instruction if negative = 0; pc = address (jump) if negative = 1; pc = address (here + 2) pic18(l)f2x/4xk22 ds41412f-page 384 ? 2010-2012 microchip technology inc. bnov branch if not overflow syntax: bnov n operands: -128 ? n ? 127 operation: if overflow bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0101 nnnn nnnn description: if the overflow bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnov jump before instruction pc = address (here) after instruction if overflow = 0; pc = address (jump) if overflow = 1; pc = address (here + 2) bnz branch if not zero syntax: bnz n operands: -128 ? n ? 127 operation: if zero bit is ? 0 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0001 nnnn nnnn description: if the zero bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnz jump before instruction pc = address (here) after instruction if zero = 0; pc = address (jump) if zero = 1; pc = address (here + 2) ? 2010-2012 microchip technology inc. ds41412f-page 385 pic18(l)f2x/4xk22 bra unconditional branch syntax: bra n operands: -1024 ? n ? 1023 operation: (pc) + 2 + 2n ? pc status affected: none encoding: 1101 0nnn nnnn nnnn description: add the 2?s complement number ?2n? to the pc. since the pc will have incre- mented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is a two-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation example : here bra jump before instruction pc = address (here) after instruction pc = address (jump) bsf bit set f syntax: bsf f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [0,1] operation: 1 ? f status affected: none encoding: 1000 bbba ffff ffff description: bit ?b? in register ?f? is set. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : bsf flag_reg, 7, 1 before instruction flag_reg = 0ah after instruction flag_reg = 8ah pic18(l)f2x/4xk22 ds41412f-page 386 ? 2010-2012 microchip technology inc. btfsc bit test file, skip if clear syntax: btfsc f, b {,a} operands: 0 ? f ? 255 0 ? b ? 7 a ?? [0,1] operation: skip if (f) = 0 status affected: none encoding: 1011 bbba ffff ffff description: if bit ?b? in register ?f? is ? 0 ?, then the next instruction is skipped. if bit ?b? is ? 0 ?, then the next instruction fetched during the current instruction execution is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfsc : : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (true) if flag<1> = 1; pc = address (false) btfss bit test file, skip if set syntax: btfss f, b {,a} operands: 0 ? f ? 255 0 ? b < 7 a ?? [0,1] operation: skip if (f) = 1 status affected: none encoding: 1010 bbba ffff ffff description: if bit ?b? in register ?f? is ? 1 ?, then the next instruction is skipped. if bit ?b? is ? 1 ?, then the next instruction fetched during the current instruction execution is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfss : : flag, 1, 0 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (false) if flag<1> = 1; pc = address (true) ? 2010-2012 microchip technology inc. ds41412f-page 387 pic18(l)f2x/4xk22 btg bit toggle f syntax: btg f, b {,a} operands: 0 ? f ? 255 0 ? b < 7 a ?? [0,1] operation: (f ) ? f status affected: none encoding: 0111 bbba ffff ffff description: bit ?b? in data memory location ?f? is inverted. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : btg portc, 4, 0 before instruction: portc = 0111 0101 [75h] after instruction: portc = 0110 0101 [65h] bov branch if overflow syntax: bov n operands: -128 ? n ? 127 operation: if overflow bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0100 nnnn nnnn description: if the overflow bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bov jump before instruction pc = address (here) after instruction if overflow = 1; pc = address (jump) if overflow = 0; pc = address (here + 2) pic18(l)f2x/4xk22 ds41412f-page 388 ? 2010-2012 microchip technology inc. bz branch if zero syntax: bz n operands: -128 ? n ? 127 operation: if zero bit is ? 1 ? (pc) + 2 + 2n ? pc status affected: none encoding: 1110 0000 nnnn nnnn description: if the zero bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bz jump before instruction pc = address (here) after instruction if zero = 1; pc = address (jump) if zero = 0; pc = address (here + 2) call subroutine call syntax: call k {,s} operands: 0 ? k ? 1048575 s ?? [0,1] operation: (pc) + 4 ? tos, k ? pc<20:1>, if s = 1 (w) ? ws, (status) ? statuss, (bsr) ? bsrs status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 110s k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: subroutine call of entire 2-mbyte memory range. first, return address (pc + 4) is pushed onto the return stack. if ?s? = 1 , the w, status and bsr registers are also pushed into their respective shadow registers, ws, statuss and bsrs. if ?s? = 0 , no update occurs (default). then, the 20-bit value ?k? is loaded into pc<20:1>. call is a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k?<7:0>, push pc to stack read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : here call there, 1 before instruction pc = address (here) after instruction pc = address (there) tos = address (here + 4) ws = w bsrs = bsr statuss = status ? 2010-2012 microchip technology inc. ds41412f-page 389 pic18(l)f2x/4xk22 clrf clear f syntax: clrf f {,a} operands: 0 ? f ? 255 a ?? [0,1] operation: 000h ? f 1 ? z status affected: z encoding: 0110 101a ffff ffff description: clears the contents of the specified register. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : clrf flag_reg, 1 before instruction flag_reg = 5ah after instruction flag_reg = 00h clrwdt clear watchdog timer syntax: clrwdt operands: none operation: 000h ? wdt, 000h ? wdt postscaler, 1 ? to, 1 ? pd status affected: to , pd encoding: 0000 0000 0000 0100 description: clrwdt instruction resets the watchdog timer. it also resets the post- scaler of the wdt. status bits, to and pd , are set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data no operation example : clrwdt before instruction wdt counter = ? after instruction wdt counter = 00h wdt postscaler = 0 to =1 pd =1 pic18(l)f2x/4xk22 ds41412f-page 390 ? 2010-2012 microchip technology inc. comf complement f syntax: comf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f ) ? dest status affected: n, z encoding: 0001 11da ffff ffff description: the contents of register ?f? are complemented. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : comf reg, 0, 0 before instruction reg = 13h after instruction reg = 13h w=ech cpfseq compare f with w, skip if f = w syntax: cpfseq f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (f) ? (w), skip if (f) = (w) (unsigned comparison) status affected: none encoding: 0110 001a ffff ffff description: compares the contents of data memory location ?f? to the contents of w by performing an unsigned subtraction. if ?f? = w, then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfseq reg, 0 nequal : equal : before instruction pc address = here w=? reg = ? after instruction if reg = w; pc = address (equal) if reg ? w; pc = address (nequal) ? 2010-2012 microchip technology inc. ds41412f-page 391 pic18(l)f2x/4xk22 cpfsgt compare f with w, skip if f > w syntax: cpfsgt f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (f) ? ?? w), skip if (f) > (w) (unsigned comparison) status affected: none encoding: 0110 010a ffff ffff description: compares the contents of data memory location ?f? to the contents of the w by performing an unsigned subtraction. if the contents of ?f? are greater than the contents of wreg , then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfsgt reg, 0 ngreater : greater : before instruction pc = address (here) w= ? after instruction if reg ? w; pc = address (greater) if reg ? w; pc = address (ngreater) cpfslt compare f with w, skip if f < w syntax: cpfslt f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (f) ? ?? w), skip if (f) < (w) (unsigned comparison) status affected: none encoding: 0110 000a ffff ffff description: compares the contents of data memory location ?f? to the contents of w by performing an unsigned subtraction. if the contents of ?f? are less than the contents of w, then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfslt reg, 1 nless : less : before instruction pc = address (here) w= ? after instruction if reg < w; pc = address (less) if reg ? w; pc = address (nless) pic18(l)f2x/4xk22 ds41412f-page 392 ? 2010-2012 microchip technology inc. daw decimal adjust w register syntax: daw operands: none operation: if [w<3:0> > 9] or [dc = 1] then (w<3:0>) + 6 ? w<3:0>; else ( w<3:0>) ? w<3:0>; if [w<7:4> + dc > 9] or [c = 1] then ( w<7:4>) + 6 + dc ? w<7:4> ? ; else (w<7:4>) + dc ? w<7:4> status affected: c encoding: 0000 0000 0000 0111 description: daw adjusts the eight-bit value in w, resulting from the earlier addition of two variables (each in packed bcd format) and produces a correct packed bcd result. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register w process data write w example1 : daw before instruction w=a5h c=0 dc = 0 after instruction w = 05h c=1 dc = 0 example 2 : before instruction w=ceh c=0 dc = 0 after instruction w = 34h c=1 dc = 0 decf decrement f syntax: decf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? 1 ? dest status affected: c, dc, n, ov, z encoding: 0000 01da ffff ffff description: decrement register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : decf cnt, 1, 0 before instruction cnt = 01h z=0 after instruction cnt = 00h z=1 ? 2010-2012 microchip technology inc. ds41412f-page 393 pic18(l)f2x/4xk22 decfsz decrement f, skip if 0 syntax: decfsz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? 1 ? dest, skip if result = 0 status affected: none encoding: 0010 11da ffff ffff description: the contents of register ?f? are decremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here decfsz cnt, 1, 1 goto loop continue before instruction pc = address (here) after instruction cnt = cnt - 1 if cnt = 0; pc = address (continue) if cnt ? 0; pc = address (here + 2) dcfsnz decrement f, skip if not 0 syntax: dcfsnz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? 1 ? dest, skip if result ? 0 status affected: none encoding: 0100 11da ffff ffff description: the contents of register ?f? are decremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is not ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here dcfsnz temp, 1, 0 zero : nzero : before instruction temp = ? after instruction temp = temp ? 1, if temp = 0; pc = address (zero) if temp ? 0; pc = address (nzero) pic18(l)f2x/4xk22 ds41412f-page 394 ? 2010-2012 microchip technology inc. goto unconditional branch syntax: goto k operands: 0 ? k ? 1048575 operation: k ? pc<20:1> status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 1111 k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: goto allows an unconditional branch anywhere within entire 2-mbyte memory range. the 20-bit value ?k? is loaded into pc<20:1>. goto is always a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k?<7:0>, no operation read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : goto there after instruction pc = address (there) incf increment f syntax: incf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) + 1 ? dest status affected: c, dc, n, ov, z encoding: 0010 10da ffff ffff description: the contents of register ?f? are incremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : incf cnt, 1, 0 before instruction cnt = ffh z=0 c=? dc = ? after instruction cnt = 00h z=1 c=1 dc = 1 ? 2010-2012 microchip technology inc. ds41412f-page 395 pic18(l)f2x/4xk22 incfsz increment f, skip if 0 syntax: incfsz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) + 1 ? dest, skip if result = 0 status affected: none encoding: 0011 11da ffff ffff description: the contents of register ?f? are incremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here incfsz cnt, 1, 0 nzero : zero : before instruction pc = address (here) after instruction cnt = cnt + 1 if cnt = 0; pc = address (zero) if cnt ? 0; pc = address (nzero) infsnz increment f, skip if not 0 syntax: infsnz f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) + 1 ? dest, skip if result ? 0 status affected: none encoding: 0100 10da ffff ffff description: the contents of register ?f? are incremented. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if the result is not ? 0 ?, the next instruction, which is already fetched, is discarded and a nop is executed instead, making it a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here infsnz reg, 1, 0 zero nzero before instruction pc = address (here) after instruction reg = reg + 1 if reg ? 0; pc = address (nzero) if reg = 0; pc = address (zero) pic18(l)f2x/4xk22 ds41412f-page 396 ? 2010-2012 microchip technology inc. iorlw inclusive or literal with w syntax: iorlw k operands: 0 ? k ? 255 operation: (w) .or. k ? w status affected: n, z encoding: 0000 1001 kkkk kkkk description: the contents of w are ored with the eight-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : iorlw 35h before instruction w=9ah after instruction w=bfh iorwf inclusive or w with f syntax: iorwf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (w) .or. (f) ? dest status affected: n, z encoding: 0001 00da ffff ffff description: inclusive or w with register ?f?. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : iorwf result, 0, 1 before instruction result = 13h w = 91h after instruction result = 13h w = 93h ? 2010-2012 microchip technology inc. ds41412f-page 397 pic18(l)f2x/4xk22 lfsr load fsr syntax: lfsr f, k operands: 0 ? f ? 2 0 ? k ? 4095 operation: k ? fsrf status affected: none encoding: 1110 1111 1110 0000 00ff k 7 kkk k 11 kkk kkkk description: the 12-bit literal ?k? is loaded into the file select register pointed to by ?f?. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? msb process data write literal ?k? msb to fsrfh decode read literal ?k? lsb process data write literal ?k? to fsrfl example : lfsr 2, 3abh after instruction fsr2h = 03h fsr2l = abh movf move f syntax: movf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: f ? dest status affected: n, z encoding: 0101 00da ffff ffff description: the contents of register ?f? are moved to a destination dependent upon the status of ?d?. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed back in register ?f? (default). location ?f? can be anywhere in the 256-byte bank. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write w example : movf reg, 0, 0 before instruction reg = 22h w=ffh after instruction reg = 22h w = 22h pic18(l)f2x/4xk22 ds41412f-page 398 ? 2010-2012 microchip technology inc. movff move f to f syntax: movff f s ,f d operands: 0 ? f s ? 4095 0 ? f d ? 4095 operation: (f s ) ? f d status affected: none encoding: 1st word (source) 2nd word (destin.) 1100 1111 ffff ffff ffff ffff fff f s fff f d description: the contents of source register ?f s ? are moved to destination register ?f d ?. location of source ?f s ? can be anywhere in the 4096-byte data space (000h to fffh) and location of destination ?f d ? can also be anywhere from 000h to fffh. either source or destination can be w (a useful special situation). movff is particularly useful for transferring a data memory location to a peripheral register (such as the transmit buffer or an i/o port). the movff instruction cannot use the pcl, tosu, tosh or tosl as the destination register. words: 2 cycles: 2 (3) q cycle activity: q1 q2 q3 q4 decode read register ?f? (src) process data no operation decode no operation no dummy read no operation write register ?f? (dest) example : movff reg1, reg2 before instruction reg1 = 33h reg2 = 11h after instruction reg1 = 33h reg2 = 33h movlb move literal to low nibble in bsr syntax: movlw k operands: 0 ? k ? 255 operation: k ? bsr status affected: none encoding: 0000 0001 kkkk kkkk description: the eight-bit literal ?k? is loaded into the bank select register (bsr). the value of bsr<7:4> always remains ? 0 ?, regardless of the value of k 7 :k 4 . words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write literal ?k? to bsr example : movlb 5 before instruction bsr register = 02h after instruction bsr register = 05h ? 2010-2012 microchip technology inc. ds41412f-page 399 pic18(l)f2x/4xk22 movlw move literal to w syntax: movlw k operands: 0 ? k ? 255 operation: k ? w status affected: none encoding: 0000 1110 kkkk kkkk description: the eight-bit literal ?k? is loaded into w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : movlw 5ah after instruction w=5ah movwf move w to f syntax: movwf f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (w) ? f status affected: none encoding: 0110 111a ffff ffff description: move data from w to register ?f?. location ?f? can be anywhere in the 256-byte bank. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : movwf reg, 0 before instruction w=4fh reg = ffh after instruction w=4fh reg = 4fh pic18(l)f2x/4xk22 ds41412f-page 400 ? 2010-2012 microchip technology inc. mullw multiply literal with w syntax: mullw k operands: 0 ? k ? 255 operation: (w) x k ? prodh:prodl status affected: none encoding: 0000 1101 kkkk kkkk description: an unsigned multiplication is carried out between the contents of w and the 8-bit literal ?k?. the 16-bit result is placed in the prodh:prodl register pair. prodh contains the high byte. w is unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this operation. a zero result is possible but not detected. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write registers prodh: prodl example : mullw 0c4h before instruction w=e2h prodh = ? prodl = ? after instruction w=e2h prodh = adh prodl = 08h mulwf multiply w with f syntax: mulwf f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: (w) x (f) ? prodh:prodl status affected: none encoding: 0000 001a ffff ffff description: an unsigned multiplication is carried out between the contents of w and the register file location ?f?. the 16-bit result is stored in the prodh:prodl register pair. prodh contains the high byte. both w and ?f? are unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this operation. a zero result is possible but not detected. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write registers prodh: prodl example : mulwf reg, 1 before instruction w=c4h reg = b5h prodh = ? prodl = ? after instruction w=c4h reg = b5h prodh = 8ah prodl = 94h ? 2010-2012 microchip technology inc. ds41412f-page 401 pic18(l)f2x/4xk22 negf negate f syntax: negf f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: ( f ) + 1 ? f status affected: n, ov, c, dc, z encoding: 0110 110a ffff ffff description: location ?f? is negated using two?s complement. the result is placed in the data memory location ?f?. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : negf reg, 1 before instruction reg = 0011 1010 [3ah] after instruction reg = 1100 0110 [c6h] nop no operation syntax: nop operands: none operation: no operation status affected: none encoding: 0000 1111 0000 xxxx 0000 xxxx 0000 xxxx description: no operation. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation example : none. pic18(l)f2x/4xk22 ds41412f-page 402 ? 2010-2012 microchip technology inc. pop pop top of return stack syntax: pop operands: none operation: (tos) ? bit bucket status affected: none encoding: 0000 0000 0000 0110 description: the tos value is pulled off the return stack and is discarded. the tos value then becomes the previous value that was pushed onto the return stack. this instruction is provided to enable the user to properly manage the return stack to incorporate a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation pop tos value no operation example : pop goto new before instruction tos = 0031a2h stack (1 level down) = 014332h after instruction tos = 014332h pc = new push push top of return stack syntax: push operands: none operation: (pc + 2) ? tos status affected: none encoding: 0000 0000 0000 0101 description: the pc + 2 is pushed onto the top of the return stack. the previous tos value is pushed down on the stack. this instruction allows implementing a software stack by modifying tos and then pushing it onto the return stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode push pc + 2 onto return stack no operation no operation example : push before instruction tos = 345ah pc = 0124h after instruction pc = 0126h tos = 0126h stack (1 level down) = 345ah ? 2010-2012 microchip technology inc. ds41412f-page 403 pic18(l)f2x/4xk22 rcall relative call syntax: rcall n operands: -1024 ? n ? 1023 operation: (pc) + 2 ? tos, (pc) + 2 + 2n ? pc status affected: none encoding: 1101 1nnn nnnn nnnn description: subroutine call with a jump up to 1k from the current location. first, return address (pc + 2) is pushed onto the stack. then, add the 2?s complement number ?2n? to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc + 2 + 2n. this instruction is a two-cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?n? push pc to stack process data write to pc no operation no operation no operation no operation example : here rcall jump before instruction pc = address (here) after instruction pc = address (jump) tos = address (here + 2) reset reset syntax: reset operands: none operation: reset all registers and flags that are affected by a mclr reset. status affected: all encoding: 0000 0000 1111 1111 description: this instruction provides a way to execute a mclr reset by software. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode start reset no operation no operation example : reset after instruction registers = reset value flags* = reset value pic18(l)f2x/4xk22 ds41412f-page 404 ? 2010-2012 microchip technology inc. retfie return from interrupt syntax: retfie {s} operands: s ? [0,1] operation: (tos) ? pc, 1 ? gie/gieh or peie/giel, if s = 1 (ws) ? w, (statuss) ? status, (bsrs) ? bsr, pclatu, pclath are unchanged. status affected: gie/gieh, peie/giel. encoding: 0000 0000 0001 000s description: return from interrupt. stack is popped and top-of-stack (tos) is loaded into the pc. interrupts are enabled by setting either the high or low priority global interrupt enable bit. if ?s? = 1 , the contents of the shadow registers, ws, statuss and bsrs, are loaded into their corresponding registers, w, status and bsr. if ?s? = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation pop pc from stack set gieh or giel no operation no operation no operation no operation example : retfie 1 after interrupt pc = tos w=ws bsr = bsrs status = statuss gie/gieh, peie/giel = 1 retlw return literal to w syntax: retlw k operands: 0 ? k ? 255 operation: k ? w, (tos) ? pc, pclatu, pclath are unchanged status affected: none encoding: 0000 1100 kkkk kkkk description: w is loaded with the eight-bit literal ?k?. the program counter is loaded from the top of the stack (the return address). the high address latch (pclath) remains unchanged. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data pop pc from stack, write to w no operation no operation no operation no operation example : call table ; w contains table ; offset value ; w now has ; table value : table addwf pcl ; w = offset retlw k0 ; begin table retlw k1 ; : : retlw kn ; end of table before instruction w = 07h after instruction w = value of kn ? 2010-2012 microchip technology inc. ds41412f-page 405 pic18(l)f2x/4xk22 return return from subroutine syntax: return {s} operands: s ? [0,1] operation: (tos) ? pc, if s = 1 (ws) ? w, (statuss) ? status, (bsrs) ? bsr, pclatu, pclath are unchanged status affected: none encoding: 0000 0000 0001 001s description: return from subroutine. the stack is popped and the top of the stack (tos) is loaded into the program counter. if ?s?= 1 , the contents of the shadow registers, ws, statuss and bsrs, are loaded into their corresponding registers, w, status and bsr. if ?s? = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation process data pop pc from stack no operation no operation no operation no operation example : return after instruction: pc = tos rlcf rotate left f through carry syntax: rlcf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f pic18(l)f2x/4xk22 ds41412f-page 406 ? 2010-2012 microchip technology inc. rlncf rotate left f (no carry) syntax: rlncf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f ? 2010-2012 microchip technology inc. ds41412f-page 407 pic18(l)f2x/4xk22 rrncf rotate right f (no carry) syntax: rrncf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f pic18(l)f2x/4xk22 ds41412f-page 408 ? 2010-2012 microchip technology inc. sleep enter sleep mode syntax: sleep operands: none operation: 00h ? wdt, 0 ? wdt postscaler, 1 ? to , 0 ? pd status affected: to , pd encoding: 0000 0000 0000 0011 description: the power-down status bit (pd ) is cleared. the time-out status bit (to ) is set. watchdog timer and its post- scaler are cleared. the processor is put into sleep mode with the oscillator stopped. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data go to sleep example : sleep before instruction to =? pd =? after instruction to =1 ? pd =0 ? if wdt causes wake-up, this bit is cleared. subfwb subtract f from w with borrow syntax: subfwb f {,d {,a}} operands: 0 ?? f ?? 255 d ? [0,1] a ? [0,1] operation: (w) ? (f) ? (c ) ?? dest status affected: n, ov, c, dc, z encoding: 0101 01da ffff ffff description: subtract register ?f? and carry flag (borrow) from w (2?s complement method). if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in index ed litera l offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subfwb reg, 1, 0 before instruction reg = 3 w=2 c=1 after instruction reg = ff w=2 c=0 z=0 n = 1 ; result is negative example 2 : subfwb reg, 0, 0 before instruction reg = 2 w=5 c=1 after instruction reg = 2 w=3 c=1 z=0 n = 0 ; result is positive example 3 : subfwb reg, 1, 0 before instruction reg = 1 w=2 c=0 after instruction reg = 0 w=2 c=1 z = 1 ; result is zero n=0 ? 2010-2012 microchip technology inc. ds41412f-page 409 pic18(l)f2x/4xk22 sublw subtract w from literal syntax: sublw k operands: 0 ?? k ?? 255 operation: k ? (w) ?? w status affected: n, ov, c, dc, z encoding: 0000 1000 kkkk kkkk description w is subtracted from the eight-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example 1: sublw 02h before instruction w = 01h c=? after instruction w = 01h c = 1 ; result is positive z=0 n=0 example 2 : sublw 02h before instruction w = 02h c=? after instruction w = 00h c = 1 ; result is zero z=1 n=0 example 3 : sublw 02h before instruction w = 03h c=? after instruction w = ffh ; (2?s complement) c = 0 ; result is negative z=0 n=1 subwf subtract w from f syntax: subwf f {,d {,a}} operands: 0 ?? f ?? 255 d ? [0,1] a ? [0,1] operation: (f) ? (w) ?? dest status affected: n, ov, c, dc, z encoding: 0101 11da ffff ffff description: subtract w from register ?f? (2?s complement method). if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in index ed litera l offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subwf reg, 1, 0 before instruction reg = 3 w=2 c=? after instruction reg = 1 w=2 c = 1 ; result is positive z=0 n=0 example 2 : subwf reg, 0, 0 before instruction reg = 2 w=2 c=? after instruction reg = 2 w=0 c=1; result is zero z=1 n=0 example 3 : subwf reg, 1, 0 before instruction reg = 1 w=2 c=? after instruction reg = ffh ;(2?s complement) w=2 c = 0 ; result is negative z=0 n=1 pic18(l)f2x/4xk22 ds41412f-page 410 ? 2010-2012 microchip technology inc. subwfb subtract w from f with borrow syntax: subwfb f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f) ? (w) ? (c ) ?? dest status affected: n, ov, c, dc, z encoding: 0101 10da ffff ffff description: subtract w and the carry flag (borrow) from register ?f? (2?s comple- ment method). if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subwfb reg, 1, 0 before instruction reg = 19h (0001 1001) w =0dh (0000 1101) c=1 after instruction reg = 0ch (0000 1100) w =0dh (0000 1101) c=1 z=0 n = 0 ; result is positive example 2 : subwfb reg, 0, 0 before instruction reg = 1bh (0001 1011) w =1ah (0001 1010) c=0 after instruction reg = 1bh (0001 1011) w = 00h c=1 z = 1 ; result is zero n=0 example 3: subwfb reg, 1, 0 before instruction reg = 03h (0000 0011) w =0eh (0000 1110) c=1 after instruction reg = f5h (1111 0101) ; [2?s comp] w =0eh (0000 1110) c=0 z=0 n = 1 ; result is negative swapf swap f syntax: swapf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (f<3:0>) ? dest<7:4>, (f<7:4>) ? dest<3:0> status affected: none encoding: 0011 10da ffff ffff description: the upper and lower nibbles of register ?f? are exchanged. if ?d? is ? 0 ?, the result is placed in w. if ?d? is ? 1 ?, the result is placed in register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset a ddressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : swapf reg, 1, 0 before instruction reg = 53h after instruction reg = 35h ? 2010-2012 microchip technology inc. ds41412f-page 411 pic18(l)f2x/4xk22 tblrd table read syntax: tblrd ( *; *+; *-; +*) operands: none operation: if tblrd *, (prog mem (tblptr)) ? tablat; tblptr ? no change; if tblrd *+, (prog mem (tblptr)) ? tablat; (tblptr) + 1 ? tblptr; if tblrd *-, (prog mem (tblptr)) ? tablat; (tblptr) ? 1 ? tblptr; if tblrd +*, (tblptr) + 1 ? tblptr; (prog mem (tblptr)) ? tablat; status affected: none encoding: 0000 0000 0000 10nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction is used to read the contents of program memory (p.m.). to address the program memory, a pointer called table pointer (tblptr) is used. the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2-mbyte address range. tblptr[0] = 0 : least significant byte of program memory word tblptr[0] = 1 : most significant byte of program memory word the tblrd instruction can modify the value of tblptr as follows: ? no change ? post-increment ? post-decrement ? pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read program memory) no operation no operation (write tab- lat) tblrd table read (continued) example1 : tblrd *+ ; before instruction tablat = 55h tblptr = 00a356h memory (00a356h) = 34h after instruction tablat = 34h tblptr = 00a357h example2 : tblrd +* ; before instruction tablat = aah tblptr = 01a357h memory (01a357h) = 12h memory (01a358h) = 34h after instruction tablat = 34h tblptr = 01a358h pic18(l)f2x/4xk22 ds41412f-page 412 ? 2010-2012 microchip technology inc. tblwt table write syntax: tblwt ( *; *+; *-; +*) operands: none operation: if tblwt*, (tablat) ? holding register; tblptr ? no change; if tblwt*+, (tablat) ? holding register; (tblptr) + 1 ? tblptr; if tblwt*-, (tablat) ? holding register; (tblptr) ? 1 ? tblptr; if tblwt+*, (tblptr) + 1 ? tblptr; (tablat) ? holding register; status affected: none encoding: 0000 0000 0000 11nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction uses the three lsbs of tblptr to determine which of the eight holding registers the tablat is written to. the holding registers are used to program the contents of program memory (p.m.). (refer to section 6.0 ?flash program memory? for additional details on pro- gramming flash memory.) the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2-mbyte address range. the lsb of the tblptr selects which byte of the program memory location to access. tblptr[0] = 0 : least significant byte of program memory word tblptr[0] = 1 : most significant byte of program memory word the tblwt instruction can modify the value of tblptr as follows: ? no change ? post-increment ? post-decrement ? pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read tablat) no operation no operation (write to holding register ) tblwt table write (continued) example1 : tblwt *+; before instruction tablat = 55h tblptr = 00a356h holding register (00a356h) = ffh after instructions (table write completion) tablat = 55h tblptr = 00a357h holding register (00a356h) = 55h example 2 : tblwt +*; before instruction tablat = 34h tblptr = 01389ah holding register (01389ah) = ffh holding register (01389bh) = ffh after instruction (table write completion) tablat = 34h tblptr = 01389bh holding register (01389ah) = ffh holding register (01389bh) = 34h ? 2010-2012 microchip technology inc. ds41412f-page 413 pic18(l)f2x/4xk22 tstfsz test f, skip if 0 syntax: tstfsz f {,a} operands: 0 ? f ? 255 a ? [0,1] operation: skip if f = 0 status affected: none encoding: 0110 011a ffff ffff description: if ?f? = 0 , the next instruction fetched during the current instruction execution is discarded and a nop is executed, making this a two-cycle instruction. if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here tstfsz cnt, 1 nzero : zero : before instruction pc = address (here) after instruction if cnt = 00h, pc = address (zero) if cnt ? 00h, pc = address (nzero) xorlw exclusive or literal with w syntax: xorlw k operands: 0 ?? k ?? 255 operation: (w) .xor. k ?? w status affected: n, z encoding: 0000 1010 kkkk kkkk description: the contents of w are xored with the 8-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : xorlw 0afh before instruction w=b5h after instruction w=1ah pic18(l)f2x/4xk22 ds41412f-page 414 ? 2010-2012 microchip technology inc. xorwf exclusive or w with f syntax: xorwf f {,d {,a}} operands: 0 ? f ? 255 d ? [0,1] a ? [0,1] operation: (w) .xor. (f) ?? dest status affected: n, z encoding: 0001 10da ffff ffff description: exclusive or the contents of w with register ?f?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in the register ?f? (default). if ?a? is ? 0 ?, the access bank is selected. if ?a? is ? 1 ?, the bsr is used to select the gpr bank. if ?a? is ? 0 ? and the extended instruction set is enabled, this instruction operates in indexed literal offset addressing mode whenever f ?? 95 (5fh). see section 25.2.3 ?byte-oriented and bit-oriented instructions in indexed literal offset mode? for details. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : xorwf reg, 1, 0 before instruction reg = afh w=b5h after instruction reg = 1ah w=b5h ? 2010-2012 microchip technology inc. ds41412f-page 415 pic18(l)f2x/4xk22 25.2 extended instruction set in addition to the standard 75 instructions of the pic18 instruction set, pic18(l)f2x/4xk22 devices also provide an optional extension to the core cpu functionality. the added features include eight additional instructions that augment indirect and indexed addressing operations and the implementation of indexed literal offset addressing mode for many of the standard pic18 instructions. the additional features of the extended instruction set are disabled by default. to enable them, users must set the xinst configuration bit. the instructions in the extended set can all be classified as literal operations, which either manipulate the file select registers, or use them for indexed addressing. two of the instructions, addfsr and subfsr , each have an additional special instantiation for using fsr2. these versions ( addulnk and subulnk ) allow for automatic return after execution. the extended instructions are specifically implemented to optimize re-entrant program code (that is, code that is recursive or that uses a software stack) written in high-level languages, particularly c. among other things, they allow users working in high-level languages to perform certain operations on data structures more efficiently. these include: ? dynamic allocation and deallocation of software stack space when entering and leaving subroutines ? function pointer invocation ? software stack pointer manipulation ? manipulation of variables located in a software stack a summary of the instructions in the extended instruc- tion set is provided in tab l e 2 5- 3 . detailed descriptions are provided in section 25.2.2 ?extended instruction set? . the opcode field descriptions in tab l e 2 5- 1 apply to both the standard and extended pic18 instruction sets. 25.2.1 extended instruction syntax most of the extended instructions use indexed arguments, using one of the file select registers and some offset to specify a source or destination register. when an argument for an instruction serves as part of indexed addressing, it is enclosed in square brackets (?[ ]?). this is done to indicate that the argument is used as an index or offset. mpasm? assembler will flag an error if it determines that an index or offset value is not bracketed. when the extended instruction set is enabled, brackets are also used to indicate index arguments in byte- oriented and bit-oriented instructions. this is in addition to other changes in their syntax. for more details, see section 25.2.3.1 ?extended instruction syntax with standard pic18 commands? . table 25-3: extensions to the pic18 instruction set note: the instruction set extension and the indexed literal offset addressing mode were designed for optimizing applications written in c; the user may likely never use these instructions directly in assembler. the syntax for these commands is pro- vided as a reference for users who may be reviewing code that has been generated by a compiler. note: in the past, square brackets have been used to denote optional arguments in the pic18 and earlier instruction sets. in this text and going forward, optional arguments are denoted by braces (?{ }?). mnemonic, operands description cycles 16-bit instruction word status affected msb lsb addfsr addulnk callw movsf movss pushl subfsr subulnk f, k k z s , f d z s , z d k f, k k add literal to fsr add literal to fsr2 and return call subroutine using wreg move z s (source) to 1st word f d (destination) 2nd word move z s (source) to 1st word z d (destination) 2nd word store literal at fsr2, decrement fsr2 subtract literal from fsr subtract literal from fsr2 and return 1 2 2 2 2 1 1 2 1110 1110 0000 1110 1111 1110 1111 1110 1110 1110 1000 1000 0000 1011 ffff 1011 xxxx 1010 1001 1001 ffkk 11kk 0001 0zzz ffff 1zzz xzzz kkkk ffkk 11kk kkkk kkkk 0100 zzzz ffff zzzz zzzz kkkk kkkk kkkk none none none none none none none none pic18(l)f2x/4xk22 ds41412f-page 416 ? 2010-2012 microchip technology inc. 25.2.2 extended instruction set addfsr add literal to fsr syntax: addfsr f, k operands: 0 ? k ? 63 f ? [ 0, 1, 2 ] operation: fsr(f) + k ? fsr(f) status affected: none encoding: 1110 1000 ffkk kkkk description: the 6-bit literal ?k? is added to the contents of the fsr specified by ?f?. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to fsr example: addfsr 2, 23h before instruction fsr2 = 03ffh after instruction fsr2 = 0422h addulnk add literal to fsr2 and return syntax: addulnk k operands: 0 ? k ? 63 operation: fsr2 + k ? fsr2, (tos) ?? pc status affected: none encoding: 1110 1000 11kk kkkk description: the 6-bit literal ?k? is added to the contents of fsr2. a return is then executed by loading the pc with the tos. the instruction takes two cycles to execute; a nop is performed during the second cycle. this may be thought of as a special case of the addfsr instruction, where f = 3 (binary ? 11 ?); it operates only on fsr2. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to fsr no operation no operation no operation no operation example: addulnk 23h before instruction fsr2 = 03ffh pc = 0100h after instruction fsr2 = 0422h pc = (tos) note: all pic18 instructions may take an optional label argument preceding the instruction mnemonic for use in symbolic addressing. if a label is used, the instruction syntax then becomes: {label} instruction argument(s). ? 2010-2012 microchip technology inc. ds41412f-page 417 pic18(l)f2x/4xk22 callw subroutine call using wreg syntax: callw operands: none operation: (pc + 2) ? tos, (w) ? pcl, (pclath) ? pch, (pclatu) ? pcu status affected: none encoding: 0000 0000 0001 0100 description first, the return address (pc + 2) is pushed onto the return stack. next, the contents of w are written to pcl; the existing value is discarded. then, the contents of pclath and pclatu are latched into pch and pcu, respectively. the second cycle is executed as a nop instruction while the new next instruction is fetched. unlike call , there is no option to update w, status or bsr. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read wreg push pc to stack no operation no operation no operation no operation no operation example : here callw before instruction pc = address (here) pclath = 10h pclatu = 00h w = 06h after instruction pc = 001006h tos = address (here + 2) pclath = 10h pclatu = 00h w = 06h movsf move indexed to f syntax: movsf [z s ], f d operands: 0 ? z s ? 127 0 ? f d ? 4095 operation: ((fsr2) + z s ) ? f d status affected: none encoding: 1st word (source) 2nd word (destin.) 1110 1111 1011 ffff 0zzz ffff zzzz s ffff d description: the contents of the source register are moved to destination register ?f d ?. the actual address of the source register is determined by adding the 7-bit literal offset ?z s ? in the first word to the value of fsr2. the address of the destination register is specified by the 12-bit literal ?f d ? in the second word. both addresses can be anywhere in the 4096-byte data space (000h to fffh). the movsf instruction cannot use the pcl, tosu, tosh or tosl as the destination register. if the resultant source address points to an indirect addressing register, the value returned will be 00h. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode determine source addr determine source addr read source reg decode no operation no dummy read no operation write register ?f? (dest) example : movsf [05h], reg2 before instruction fsr2 = 80h contents of 85h = 33h reg2 = 11h after instruction fsr2 = 80h contents of 85h = 33h reg2 = 33h pic18(l)f2x/4xk22 ds41412f-page 418 ? 2010-2012 microchip technology inc. movss move indexed to indexed syntax: movss [z s ], [z d ] operands: 0 ? z s ? 127 0 ? z d ? 127 operation: ((fsr2) + z s ) ? ((fsr2) + z d ) status affected: none encoding: 1st word (source) 2nd word (dest.) 1110 1111 1011 xxxx 1zzz xzzz zzzz s zzzz d description the contents of the source register are moved to the destination register. the addresses of the source and destination registers are determined by adding the 7-bit literal offsets ?z s ? or ?z d ?, respectively, to the value of fsr2. both registers can be located anywhere in the 4096-byte data memory space (000h to fffh). the movss instruction cannot use the pcl, tosu, tosh or tosl as the destination register. if the resultant source address points to an indirect addressing register, the value returned will be 00h. if the resultant destination address points to an indirect addressing register, the instruction will execute as a nop . words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode determine source addr determine source addr read source reg decode determine dest addr determine dest addr write to dest reg example : movss [05h], [06h] before instruction fsr2 = 80h contents of 85h = 33h contents of 86h = 11h after instruction fsr2 = 80h contents of 85h = 33h contents of 86h = 33h pushl store literal at fsr2, decrement fsr2 syntax: pushl k operands: 0 ??? k ? 255 operation: k ? (fsr2), fsr2 ? 1 ? fsr2 status affected: none encoding: 1111 1010 kkkk kkkk description: the 8-bit literal ?k? is written to the data memory address specified by fsr2. fsr2 is decremented by 1 after the operation. this instruction allows users to push values onto a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read ?k? process data write to destination example : pushl 08h before instruction fsr2h:fsr2l = 01ech memory (01ech) = 00h after instruction fsr2h:fsr2l = 01ebh memory (01ech) = 08h ? 2010-2012 microchip technology inc. ds41412f-page 419 pic18(l)f2x/4xk22 subfsr subtract literal from fsr syntax: subfsr f, k operands: 0 ? k ? 63 f ? [ 0, 1, 2 ] operation: fsr(f) ? k ? fsrf status affected: none encoding: 1110 1001 ffkk kkkk description: the 6-bit literal ?k? is subtracted from the contents of the fsr specified by ?f?. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : subfsr 2, 23h before instruction fsr2 = 03ffh after instruction fsr2 = 03dch subulnk subtract literal from fsr2 and return syntax: subulnk k operands: 0 ? k ? 63 operation: fsr2 ? k ? fsr2 (tos) ?? pc status affected: none encoding: 1110 1001 11kk kkkk description: the 6-bit literal ?k? is subtracted from the contents of the fsr2. a return is then executed by loading the pc with the tos. the instruction takes two cycles to execute; a nop is performed during the second cycle. this may be thought of as a special case of the subfsr instruction, where f = 3 (binary ? 11 ?); it operates only on fsr2. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination no operation no operation no operation no operation example : subulnk 23h before instruction fsr2 = 03ffh pc = 0100h after instruction fsr2 = 03dch pc = (tos) pic18(l)f2x/4xk22 ds41412f-page 420 ? 2010-2012 microchip technology inc. 25.2.3 byte-oriented and bit-oriented instructions in indexed literal offset mode in addition to eight new commands in the extended set, enabling the extended instruction set also enables indexed literal offset addressing mode ( section 5.7.1 ?indexed addressing with literal offset? ). this has a significant impact on the way that many commands of the standard pic18 instruction set are interpreted. when the extended set is disabled, addresses embedded in opcodes are treated as literal memory locations: either as a location in the access bank (?a? = 0 ), or in a gpr bank designated by the bsr (?a? = 1 ). when the extended instruction set is enabled and ?a? = 0 , however, a file register argument of 5fh or less is interpreted as an offset from the pointer value in fsr2 and not as a literal address. for practical purposes, this means that all instructions that use the access ram bit as an argument ? that is, all byte-oriented and bit- oriented instructions, or almost half of the core pic18 instructions ? may behave differently when the extended instruction set is enabled. when the content of fsr2 is 00h, the boundaries of the access ram are essentially remapped to their original values. this may be useful in creating backward compatible code. if this technique is used, it may be necessary to save the value of fsr2 and restore it when moving back and forth between c and assembly routines in order to preserve the stack pointer. users must also keep in mind the syntax requirements of the extended instruction set (see section 25.2.3.1 ?extended instruction syntax with standard pic18 commands? ). although the indexed literal offset addressing mode can be very useful for dynamic stack and pointer manipulation, it can also be very annoying if a simple arithmetic operation is carried out on the wrong register. users who are accustomed to the pic18 programming must keep in mind that, when the extended instruction set is enabled, register addresses of 5fh or less are used for indexed literal offset addressing. representative examples of typical byte-oriented and bit-oriented instructions in the indexed literal offset addressing mode are provided on the following page to show how execution is affected. the operand condi- tions shown in the examples are applicable to all instructions of these types. 25.2.3.1 extended instruction syntax with standard pic18 commands when the extended instruction set is enabled, the file register argument, ?f?, in the standard byte-oriented and bit-oriented commands is replaced with the literal offset value, ?k?. as already noted, this occurs only when ?f? is less than or equal to 5fh. when an offset value is used, it must be indicated by square brackets (?[ ]?). as with the extended instructions, the use of brackets indicates to the compiler that the value is to be interpreted as an index or an offset. omitting the brackets, or using a value greater than 5fh within brackets, will generate an error in the mpasm assembler. if the index argument is properly bracketed for indexed literal offset addressing, the access ram argument is never specified; it will automatically be assumed to be ? 0 ?. this is in contrast to standard operation (extended instruction set disabled) when ?a? is set on the basis of the target address. declaring the access ram bit in this mode will also generate an error in the mpasm assembler. the destination argument, ?d?, functions as before. in the latest versions of the mpasm? assembler, language support for the extended instruction set must be explicitly invoked. this is done with either the command line option, /y , or the pe directive in the source listing. 25.2.4 considerations when enabling the extended instruction set it is important to note that the extensions to the instruc- tion set may not be beneficial to all users. in particular, users who are not writing code that uses a software stack may not benefit from using the extensions to the instruction set. additionally, the indexed literal offset addressing mode may create issues with legacy applications written to the pic18 assembler. this is because instructions in the legacy code may attempt to address registers in the access bank below 5fh. since these addresses are interpreted as literal offsets to fsr2 when the instruction set extension is enabled, the application may read or write to the wrong data addresses. when porting an application to the pic18(l)f2x/ 4xk22, it is very important to consider the type of code. a large, re-entrant application that is written in ?c? and would benefit from efficient compilation will do well when using the instruction set extensions. legacy applications that heavily use the access bank will most likely not benefit from using the extended instruction set. note: enabling the pic18 instruction set extension may cause legacy applications to behave erratically or fail entirely. ? 2010-2012 microchip technology inc. ds41412f-page 421 pic18(l)f2x/4xk22 addwf add w to indexed (indexed literal offset mode) syntax: addwf [k] {,d} operands: 0 ? k ? 95 d ? [0,1] operation: (w) + ((fsr2) + k) ? dest status affected: n, ov, c, dc, z encoding: 0010 01d0 kkkk kkkk description: the contents of w are added to the contents of the register indicated by fsr2, offset by the value ?k?. if ?d? is ? 0 ?, the result is stored in w. if ?d? is ? 1 ?, the result is stored back in register ?f? (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read ?k? process data write to destination example : addwf [ofst] , 0 before instruction w = 17h ofst = 2ch fsr2 = 0a00h contents of 0a2ch = 20h after instruction w = 37h contents of 0a2ch = 20h bsf bit set indexed (indexed literal offset mode) syntax: bsf [k], b operands: 0 ? f ? 95 0 ? b ? 7 operation: 1 ? ((fsr2) + k) status affected: none encoding: 1000 bbb0 kkkk kkkk description: bit ?b? of the register indicated by fsr2, offset by the value ?k?, is set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : bsf [flag_ofst], 7 before instruction flag_ofst = 0ah fsr2 = 0a00h contents of 0a0ah = 55h after instruction contents of 0a0ah = d5h setf set indexed (indexed literal offset mode) syntax: setf [k] operands: 0 ? k ? 95 operation: ffh ? ((fsr2) + k) status affected: none encoding: 0110 1000 kkkk kkkk description: the contents of the register indicated by fsr2, offset by ?k?, are set to ffh. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read ?k? process data write register example : setf [ofst] before instruction ofst = 2ch fsr2 = 0a00h contents of 0a2ch = 00h after instruction contents of 0a2ch = ffh pic18(l)f2x/4xk22 ds41412f-page 422 ? 2010-2012 microchip technology inc. 25.2.5 special considerations with microchip mplab ? ide tools the latest versions of microchip?s software tools have been designed to fully support the extended instruction set of the pic18(l)f2x/4xk22 family of devices. this includes the mplab c18 c compiler, mpasm assembly language and mplab integrated development environment (ide). when selecting a target device for software development, mplab ide will automatically set default configuration bits for that device. the default setting for the xinst configuration bit is ? 0 ?, disabling the extended instruction set and indexed literal offset addressing mode. for proper execution of applications developed to take advantage of the extended instruction set, xinst must be set during programming. to develop software for the extended instruction set, the user must enable support for the instructions and the indexed addressing mode in their language tool(s). depending on the environment being used, this may be done in several ways: ? a menu option, or dialog box within the environment, that allows the user to configure the language tool and its settings for the project ? a command line option ? a directive in the source code these options vary between different compilers, assemblers and development environments. users are encouraged to review the documentation accompanying their development systems for the appropriate information. ? 2010-2012 microchip technology inc. ds41412f-page 423 pic18(l)f2x/4xk22 26.0 development support the pic ? microcontrollers and dspic ? digital signal controllers are supported with a full range of software and hardware development tools: ? integrated development environment - mplab ? ide software ? compilers/assemblers/linkers - mplab c compiler for various device families - hi-tech c ? for various device families - mpasm tm assembler -mplink tm object linker/ mplib tm object librarian - mplab assembler/linker/librarian for various device families ? simulators - mplab sim software simulator ?emulators - mplab real ice? in-circuit emulator ? in-circuit debuggers - mplab icd 3 - pickit? 3 debug express ? device programmers - pickit? 2 programmer - mplab pm3 device programmer ? low-cost demonstration/development boards, evaluation kits, and starter kits 26.1 mplab integrated development environment software the mplab ide software brings an ease of software development previously unseen in the 8/16/32-bit microcontroller market. the mplab ide is a windows ? operating system-based application that contains: ? a single graphical interface to all debugging tools - simulator - programmer (sold separately) - in-circuit emulator (sold separately) - in-circuit debugger (sold separately) ? a full-featured editor with color-coded context ? a multiple project manager ? customizable data windows with direct edit of contents ? high-level source code debugging ? mouse over variable inspection ? drag and drop variables from source to watch windows ? extensive on-line help ? integration of select third party tools, such as iar c compilers the mplab ide allows you to: ? edit your source files (either c or assembly) ? one-touch compile or assemble, and download to emulator and simulator tools (automatically updates all project information) ? debug using: - source files (c or assembly) - mixed c and assembly - machine code mplab ide supports multiple debugging tools in a single development paradigm, from the cost-effective simulators, through low-cost in-circuit debuggers, to full-featured emulators. this eliminates the learning curve when upgrading to tools with increased flexibility and power. pic18(l)f2x/4xk22 ds41412f-page 424 ? 2010-2012 microchip technology inc. 26.2 mplab c compilers for various device families the mplab c compiler code development systems are complete ansi c compilers for microchip?s pic18, pic24 and pic32 families of microcontrollers and the dspic30 and dspic33 families of digital signal control- lers. these compilers provide powerful integration capabilities, superior code optimization and ease of use. for easy source level debugging, the compilers provide symbol information that is optimized to the mplab ide debugger. 26.3 hi-tech c for various device families the hi-tech c compiler code development systems are complete ansi c compilers for microchip?s pic family of microcontrollers and the dspic family of digital signal controllers. these compilers provide powerful integration capabilities, omniscient code generation and ease of use. for easy source level debugging, the compilers provide symbol information that is optimized to the mplab ide debugger. the compilers include a macro assembler, linker, pre- processor, and one-step driver, and can run on multiple platforms. 26.4 mpasm assembler the mpasm assembler is a full-featured, universal macro assembler for pic10/12/16/18 mcus. the mpasm assembler generates relocatable object files for the mplink object linker, intel ? standard hex files, map files to detail memory usage and symbol reference, absolute lst files that contain source lines and generated machine code and coff files for debugging. the mpasm assembler features include: ? integration into mplab ide projects ? user-defined macros to streamline assembly code ? conditional assembly for multi-purpose source files ? directives that allow complete control over the assembly process 26.5 mplink object linker/ mplib object librarian the mplink object linker combines relocatable objects created by the mpasm assembler and the mplab c18 c compiler. it can link relocatable objects from precompiled libraries, using directives from a linker script. the mplib object librarian manages the creation and modification of library files of precompiled code. when a routine from a library is called from a source file, only the modules that contain that routine will be linked in with the application. this allows large libraries to be used efficiently in many different applications. the object linker/library features include: ? efficient linking of single libraries instead of many smaller files ? enhanced code maintainability by grouping related modules together ? flexible creation of libraries with easy module listing, replacement, deletion and extraction 26.6 mplab assembler, linker and librarian for various device families mplab assembler produces relocatable machine code from symbolic assembly language for pic24, pic32 and dspic devices. mplab c compiler uses the assembler to produce its object file. the assembler generates relocatable object files that can then be archived or linked with other relocatable object files and archives to create an executable file. notable features of the assembler include: ? support for the entire device instruction set ? support for fixed-point and floating-point data ? command line interface ? rich directive set ? flexible macro language ? mplab ide compatibility ? 2010-2012 microchip technology inc. ds41412f-page 425 pic18(l)f2x/4xk22 26.7 mplab sim software simulator the mplab sim software simulator allows code development in a pc-hosted environment by simulat- ing the pic mcus and dspic ? dscs on an instruction level. on any given instruction, the data areas can be examined or modified and stimuli can be applied from a comprehensive stimulus controller. registers can be logged to files for further run-time analysis. the trace buffer and logic analyzer display extend the power of the simulator to record and track program execution, actions on i/o, most peripherals and internal registers. the mplab sim software simulator fully supports symbolic debugging using the mplab c compilers, and the mpasm and mplab assemblers. the soft- ware simulator offers the flexibility to develop and debug code outside of the hardware laboratory envi- ronment, making it an excellent, economical software development tool. 26.8 mplab real ice in-circuit emulator system mplab real ice in-circuit emulator system is microchip?s next generation high-speed emulator for microchip flash dsc and mcu devices. it debugs and programs pic ? flash mcus and dspic ? flash dscs with the easy-to-use, powerful graphical user interface of the mplab integrated development environment (ide), included with each kit. the emulator is connected to the design engineer?s pc using a high-speed usb 2.0 interface and is connected to the target with either a connector compatible with in- circuit debugger systems (rj11) or with the new high- speed, noise tolerant, low-voltage differential signal (lvds) interconnection (cat5). the emulator is field upgradable through future firmware downloads in mplab ide. in upcoming releases of mplab ide, new devices will be supported, and new features will be added. mplab real ice offers significant advantages over competitive emulators including low-cost, full-speed emulation, run-time variable watches, trace analysis, complex breakpoints, a ruggedized probe interface and long (up to three meters) interconnection cables. 26.9 mplab icd 3 in-circuit debugger system mplab icd 3 in-circuit debugger system is micro- chip's most cost effective high-speed hardware debugger/programmer for microchip flash digital sig- nal controller (dsc) and microcontroller (mcu) devices. it debugs and programs pic ? flash microcon- trollers and dspic ? dscs with the powerful, yet easy- to-use graphical user interface of mplab integrated development environment (ide). the mplab icd 3 in-circuit debugger probe is con- nected to the design engineer's pc using a high-speed usb 2.0 interface and is connected to the target with a connector compatible with the mplab icd 2 or mplab real ice systems (rj-11). mplab icd 3 supports all mplab icd 2 headers. 26.10 pickit 3 in-circuit debugger/ programmer and pickit 3 debug express the mplab pickit 3 allows debugging and program- ming of pic ? and dspic ? flash microcontrollers at a most affordable price point using the powerful graphical user interface of the mplab integrated development environment (ide). the mplab pickit 3 is connected to the design engineer's pc using a full speed usb interface and can be connected to the target via an microchip debug (rj-11) connector (compatible with mplab icd 3 and mplab real ice). the connector uses two device i/o pins and the reset line to imple- ment in-circuit debugging and in-circuit serial pro- gramming?. the pickit 3 debug express include the pickit 3, demo board and microcontroller, hookup cables and cdrom with user?s guide, lessons, tutorial, compiler and mplab ide software. pic18(l)f2x/4xk22 ds41412f-page 426 ? 2010-2012 microchip technology inc. 26.11 pickit 2 development programmer/debugger and pickit 2 debug express the pickit? 2 development programmer/debugger is a low-cost development tool with an easy to use inter- face for programming and debugging microchip?s flash families of microcontrollers. the full featured windows ? programming interface supports baseline (pic10f, pic12f5xx, pic16f5xx), midrange (pic12f6xx, pic16f), pic18f, pic24, dspic30, dspic33, and pic32 families of 8-bit, 16-bit, and 32-bit microcontrollers, and many microchip serial eeprom products. with microchip?s powerful mplab integrated development environment (ide) the pickit? 2 enables in-circuit debugging on most pic ? microcon- trollers. in-circuit-debugging runs, halts and single steps the program while the pic microcontroller is embedded in the application. when halted at a break- point, the file registers can be examined and modified. the pickit 2 debug express include the pickit 2, demo board and microcontroller, hookup cables and cdrom with user?s guide, lessons, tutorial, compiler and mplab ide software. 26.12 mplab pm3 device programmer the mplab pm3 device programmer is a universal, ce compliant device programmer with programmable voltage verification at v ddmin and v ddmax for maximum reliability. it features a large lcd display (128 x 64) for menus and error messages and a modu- lar, detachable socket assembly to support various package types. the icsp? cable assembly is included as a standard item. in stand-alone mode, the mplab pm3 device programmer can read, verify and program pic devices without a pc connection. it can also set code protection in this mode. the mplab pm3 connects to the host pc via an rs-232 or usb cable. the mplab pm3 has high-speed communications and optimized algorithms for quick programming of large memory devices and incorporates an mmc card for file storage and data applications. 26.13 demonstration/development boards, evaluation kits, and starter kits a wide variety of demonstration, development and evaluation boards for various pic mcus and dspic dscs allows quick application development on fully func- tional systems. most boards include prototyping areas for adding custom circuitry and provide application firmware and source code for examination and modification. the boards support a variety of features, including leds, temperature sensors, switches, speakers, rs-232 interfaces, lcd displays, potentiometers and additional eeprom memory. the demonstration and development boards can be used in teaching environments, for prototyping custom circuits and for learning about various microcontroller applications. in addition to the picdem? and dspicdem? demon- stration/development board series of circuits, microchip has a line of evaluation kits and demonstration software for analog filter design, k ee l oq ? security ics, can, irda ? , powersmart battery management, seeval ? evaluation system, sigma-delta adc, flow rate sensing, plus many more. also available are starter kits that contain everything needed to experience the specified device. this usually includes a single application and debug capability, all on one board. check the microchip web page ( www.microchip.com ) for the complete list of demonstration, development and evaluation kits. ? 2010-2012 microchip technology inc. ds41412f-page 427 pic18(l)f2x/4xk22 27.0 electrical characteristics absolute maximum ratings (?) ambient temperature under bias................................................................................................. ............-40c to +125c storage temperature ............................................................................................................ .................. -65c to +150c voltage on any pin with respect to v ss (except v dd , and mclr ) .................................................. -0.3v to (v dd + 0.3v) voltage on v dd with respect to v ss pic18lf2x/4xk22 ................................................................................................... -0.3v to +4. 5v pic18f2x/4xk22 ..................................................................................................... -0.3v to +6 .5v voltage on mclr with respect to v ss (note 2) ............................................................................................0v to +11.0v total power dissipation (note 1) ............................................................................................................................... 1.0w maximum current out of v ss pin (-40c to +85c) .............................................................................................. 300 ma maximum current out of v ss pin (+85c to +125c)............................................................................................ 125 ma maximum current into v dd pin (-40c to +85c) ................................................................................................ 200 ma maximum current into v dd pin (+85c to +125c) ................................................................................................85 ma input clamp current, i ik (v i < 0 or v i > v dd ) ??????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????? ? 20 ma output clamp current, i ok (v o < 0 or v o > v dd ) ??????????????????????????????????????????????????????????????? ??????????????????????????????????????????????? ? 20 ma maximum output current sunk by any i/o pin..................................................................................... .....................25 ma maximum output current sourced by any i/o pin .................................................................................. ..................25 ma maximum current sunk by ? all ports (-40c to +85c)........................................................................................... 200 ma maximum current sunk by ? all ports (+85c to +125c)......................................................................................... 110 ma maximum current sourced by all ports (-40c to +85c) .......................................................................... ............185 ma maximum current sourced by all ports (+85c to +125c) ......................................................................... ............70 ma note 1: power dissipation is calculated as follows: pdis = v dd x {i dd ? ? i oh } + ? {(v dd ? v oh ) x i oh } + ? (v ol x i ol ) 2: voltage spikes below v ss at the mclr /v pp /re3 pin, inducing currents greater than 80 ma, may cause latch-up. thus, a series resistor of 50-100 ? should be used when applying a ?low? level to the mclr /v pp / re3 pin, rather than pulling this pin directly to v ss . ? notice: stresses above those listed under ?absolute maximum ratings? may cause permanent damage to the device. this is a stress rating only and functional operation of the device at those or any other conditions above those indicated in the operation listings of this specification is not implied. exposure to maximum rating conditions for extended periods may affect device reliability. pic18(l)f2x/4xk22 ds41412f-page 428 ? 2010-2012 microchip technology inc. figure 27-1: pic18lf2x/4xk22 family voltage-frequency graph (industrial temperature) figure 27-2: pic18lf2x/4xk22 family voltage-frequency graph (extended temperature) frequency (mhz) voltage 3.6v 1.8v 64 3.0v 2.7v 2.3v 10 20 60 30 40 note 1: maximum frequency 20 mhz, 1.8v to 2.7v, -40c to +85c 2: maximum frequency 64 mhz, 2.7v to 3.6v, -40c to +85c 5.5v 5.0v 4.0v 48 16 frequency (mhz) voltage 3.6v 1.8v 64 3.0v 2.7v 2.3v 10 20 60 30 40 note 1: maximum frequency 16 mhz, 1.8v to 2.7v, +85c to +125c 2: maximum frequency 48 mhz, 2.7v to 3.6v, +85c to +125c 5.5v 5.0v 4.0v 48 16 ? 2010-2012 microchip technology inc. ds41412f-page 429 pic18(l)f2x/4xk22 figure 27-3: pic18f2x/4xk22 family voltage-frequency graph (industrial temperature) figure 27-4: pic18f2x/4xk22 family voltage-frequency graph (extended temperature) frequency (mhz) voltage 3.6v 1.8v 64 3.0v 2.7v 2.3v 10 20 60 30 40 note 1: maximum frequency 20 mhz, 2.3v to 2.7v, -40c to +85c 2: maximum frequency 64 mhz, 2.7v to 5.5v, -40c to +85c 5.5v 5.0v 4.0v 48 16 frequency (mhz) voltage 3.6v 1.8v 64 3.0v 2.7v 2.3v 10 20 60 30 40 note 1: maximum frequency 16 mhz, 2.3v to 2.7v, +85c to +125c 2: maximum frequency 48 mhz, 2.7v to 5.5v, +85c to +125c 5.5v 5.0v 4.0v 48 16 pic18(l)f2x/4xk22 ds41412f-page 430 ? 2010-2012 microchip technology inc. 27.1 dc characteristics: supply voltage, pic18(l)f2x/4xk22 pic18(l)f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. symbol characteristic min typ max units conditions d001 v dd supply voltage pic18lf2x/4xk22 1.8 ? 3.6 v pic18f2x/4xk22 2.3 ? 5.5 v d002 v dr ram data retention voltage (1) 1.5 ? ? v d003 v por v dd start voltage to ensure internal power-on reset signal ? ? 0.7 v see section on power-on reset for details d004 s vdd v dd rise rate to ensure internal power-on reset signal 0.05 ? ? v/ms see section on power-on reset for details d005 v bor brown-out reset voltage borv<1:0> = 11 (2) 1.75 1.9 2.05 v borv<1:0> = 10 2.05 2.2 2.35 v borv<1:0> = 01 2.35 2.5 2.65 v borv<1:0> = 00 (3) 2.65 2.85 3.05 v note 1: this is the limit to which v dd can be lowered in sleep mode, or during a device reset, without losing ram data. 2: on pic18lf2x/4xk22 devices with bor enabled, operation is supported until a bor occurs. this is valid although v dd may be below the minimum rated supply voltage. 3: with bor enabled, full-speed operation (f osc = 64 mhz or 48 mhz) is supported until a bor occurs. this is valid although v dd may be below the minimum voltage for this frequency. ? 2010-2012 microchip technology inc. ds41412f-page 431 pic18(l)f2x/4xk22 27.2 dc characteristics: power-down current, pic18(l)f2x/4xk22 pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ +25c typ +60c max +85c max +125c units conditions v dd notes power-down base current (ipd) (1) d006 sleep mode 0.01 0.04 2 10 ? a1.8v wdt, bor, fvr and sosc disabled, all peripherals inactive 0.01 0.06 2 10 ? a3.0v 12 13 25 35 ? a 2.3v 13 14 30 40 ? a 3.0v 13 14 35 50 ? a 5.0v power-down module differential current (delta ipd) d007 watchdog timer 0.3 0.3 2.5 2.5 ? a1.8v 0.5 0.5 2.5 2.5 ? a3.0v 0.35 0.35 5.0 5.0 ? a 2.3v 0.5 0.5 5.0 5.0 ? a 3.0v 0.5 0.5 5.0 5.0 ? a 5.0v d008 brown-out reset (2) 88.515 16 ? a2.0v 99.515 16 ? a3.0v 3.4 3.4 15 16 ? a 2.3v 3.8 3.8 15 16 ? a 3.0v 5.2 5.2 15 16 ? a 5.0v d010 high/low voltage detect (2) 6.5 6.7 15 15 ? a2.0v 77.515 15 ? a3.0v 2.1 2.1 15 15 ? a 2.3v 2.4 2.4 15 15 ? a 3.0v 3.2 3.2 15 15 ? a 5.0v d011 secondary oscillator 0.5 1 3 10 ? a1.8v 32 khz on sosc 0.6 1.1 4 10 ? a3.0v 0.5 1 3 10 ? a 2.3v 0.6 1.1 4 10 ? a 3.0v 0.6 1.1 5 10 ? a 5.0v note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: on pic18lf2x/4xk22 the bor, hlvd and fvr enable internal band gap reference. with more than one of these modules enabled, the current consumption will be less than the sum of the specifications. on pic18f2x/4xk22, the internal band gap reference is always enabled and its current consumption is included in the power-down base current (i pd ). 3: a/d converter differential currents apply only in run mode. in sleep or idle mode both the adc and the frc turn off as soon as conversion (if any) is complete. pic18(l)f2x/4xk22 ds41412f-page 432 ? 2010-2012 microchip technology inc. d015 comparators 771818 ? a1.8v lp mode 771818 ? a3.0v 7 7 18 18 ? a 2.3v 7 7 18 18 ? a 3.0v 8 8 20 20 ? a 5.0v d016 comparators 38 38 95 95 ? a1.8v hp mode 40 40 105 105 ? a3.0v 39 39 95 95 ? a 2.3v 40 40 105 105 ? a 3.0v 40 40 105 105 ? a 5.0v d017 dac 14 14 25 25 ? a2.0v 20 20 35 35 ? a3.0v 15 15 30 30 ? a 2.3v 20 20 35 35 ? a 3.0v 32 32 60 60 ? a 5.0v d018 fvr (2) 15 16 25 25 ? a1.8v 15 16 25 25 ? a3.0v 28 28 45 45 ? a 2.3v 31 31 55 55 ? a 3.0v 66 66 100 100 ? a 5.0v d013 a/d converter (3) 185 185 370 370 ? a1.8v a/d on, not converting 210 210 400 400 ? a3.0v 200 200 380 380 ? a 2.3v 210 210 400 400 ? a 3.0v 250 250 450 450 ? a 5.0v 27.2 dc characteristics: power-down current, pic18(l)f2x/4xk22 (continued) pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ +25c typ +60c max +85c max +125c units conditions v dd notes note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: on pic18lf2x/4xk22 the bor, hlvd and fvr enable internal band gap reference. with more than one of these modules enabled, the current consumption will be less than the sum of the specifications. on pic18f2x/4xk22, the internal band gap reference is always enabled and its current consumption is included in the power-down base current (i pd ). 3: a/d converter differential currents apply only in run mode. in sleep or idle mode both the adc and the frc turn off as soon as conversion (if any) is complete. ? 2010-2012 microchip technology inc. ds41412f-page 433 pic18(l)f2x/4xk22 27.3 dc characteristics: rc run supply current, pic18(l)f2x/4xk22 pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions d020 supply current (i dd ) (1),(2) 3.6 23 ? a-40c v dd = 1.8v f osc = 31 khz ( rc_run mode, lfintosc source) 3.9 25 ? a +25c 3.9 ? ? a +60c 3.9 28 ? a +85c 4.0 30 ? a 125c d021 8.1 26 ? a-40c v dd = 3.0v 8.4 30 ? a +25c 8.6 ? ? a +60c 8.7 35 ? a +85c 10.7 40 ? a +125c d022 16 35 ? a -40c v dd = 2.3v f osc = 31 khz ( rc_run mode, lfintosc source) 17 35 ? a +25c 18 35 ? a +85c 19 50 ? a +125c d023 18 50 ? a -40c v dd = 3.0v 20 50 ? a +25c 21 50 ? a +85c 22 60 ? a +125c d024 19 55 ? a -40c v dd = 5.0v 21 55 ? a +25c 22 55 ? a +85c 23 70 ? a +125c d025 0.14 0.25 ma -40c to +125c v dd = 1.8v f osc = 500 khz ( rc_run mode, mfintosc source) d026 0.17 0.30 ma -40c to +125c v dd = 3.0v d027 0.18 0.25 ma -40c to +125c v dd = 2.3v f osc = 500 khz ( rc_run mode, mfintosc source) d028 0.20 0.30 ma -40c to +125c v dd = 3.0v d029 0.25 0.35 ma -40c to +125c v dd = 5.0v note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; osc1 = external square wave, from rail-to-rail (pri_run and pri_idle only). pic18(l)f2x/4xk22 ds41412f-page 434 ? 2010-2012 microchip technology inc. d030 0.35 0.50 ma -40c to +125c v dd = 1.8v f osc = 1 mhz ( rc_run mode, hfintosc source) d031 0.45 0.65 ma -40c to +125c v dd = 3.0v d032 0.40 0.60 ma -40c to +125c v dd = 2.3v f osc = 1 mhz ( rc_run mode, hfintosc source) d033 0.50 0.65 ma -40c to +125c v dd = 3.0v d034 0.55 0.75 ma -40c to +125c v dd = 5.0v d035 1.3 2.0 ma -40c to +125c v dd = 1.8v f osc = 16 mhz ( rc_run mode, hfintosc source) d036 2.2 3.0 ma -40c to +125c v dd = 3.0v d037 1.7 2.0 ma -40c to +125c v dd = 2.3v f osc = 16 mhz ( rc_run mode, hfintosc source) d038 2.2 3.0 ma -40c to +125c v dd = 3.0v d039 2.5 3.5 ma -40c to +125c v dd = 5.0v d041 6.2 8.5 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( rc_run mode, hfintosc + pll source) d043 6.2 8.5 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( rc_run mode, hfintosc + pll source) d044 6.8 9.5 ma -40c to +125c v dd = 5.0v 27.3 dc characteristics: rc run supply current, pic18(l)f2x/4xk22 (continued) pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; osc1 = external square wave, from rail-to-rail (pri_run and pri_idle only). ? 2010-2012 microchip technology inc. ds41412f-page 435 pic18(l)f2x/4xk22 27.4 dc characteristics: rc idle supply current, pic18(l)f2x/4xk22 pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions d045 supply current (i dd ) (1),(2) 0.5 18 ? a -40c v dd = 1.8v f osc = 31 khz ( rc_idle mode, lfintosc source) 0.6 18 ? a+25c 0.7 ? ? a+60c 0.75 20 ? a+85c 2.3 22 ? a +125c d046 1.1 20 ? a -40c v dd = 3.0v 1.2 20 ? a+25c 1.3 ? ? a+60c 1.4 22 ? a+85c 3.2 25 ? a +125c d047 17 30 ? a -40c v dd = 2.3v f osc = 31 khz ( rc_idle mode, lfintosc source) 13 30 ? a +25c 14 30 ? a +85c 15 45 ? a +125c d048 19 35 ? a -40c v dd = 3.0v 15 35 ? a +25c 16 35 ? a +85c 17 50 ? a +125c d049 21 40 ? a -40c v dd = 5.0v 15 40 ? a +25c 16 40 ? a +85c 18 60 ? a +125c d050 0.11 0.20 ma -40c to +125c v dd = 1.8v f osc = 500 khz ( rc_idle mode, mfintosc source) d051 0.12 0.25 ma -40c to +125c v dd = 3.0v d052 0.14 0.21 ma -40c to +125c v dd = 2.3v f osc = 500 khz ( rc_idle mode, mfintosc source) d053 0.15 0.25 ma -40c to +125c v dd = 3.0v d054 0.20 0.31 ma -40c to +125c v dd = 5.0v note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; osc1 = external square wave, from rail-to-rail (pri_run and pri_idle only). pic18(l)f2x/4xk22 ds41412f-page 436 ? 2010-2012 microchip technology inc. d055 0.25 0.40 ma -40c to +125c v dd = 1.8v f osc = 1 mhz ( rc_idle mode, hfintosc source) d056 0.35 0.50 ma -40c to +125c v dd = 3.0v d057 0.30 0.45 ma -40c to +125c v dd = 2.3v f osc = 1 mhz ( rc_idle mode, hfintosc source) d058 0.40 0.50 ma -40c to +125c v dd = 3.0v d059 0.45 0.60 ma -40c to +125c v dd = 5.0v d060 0.50 0.7 ma -40c to +125c v dd = 1.8v f osc = 16 mhz ( rc_idle mode, hfintosc source) d061 0.80 1.1 ma -40c to +125c v dd = 3.0v d062 0.65 1.0 ma -40c to +125c v dd = 2.3v f osc = 16 mhz ( rc_idle mode, hfintosc source) d063 0.80 1.1 ma -40c to +125c v dd = 3.0v d064 0.95 1.2 ma -40c to +125c v dd = 5.0v d066 2.5 3.5 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( rc_idle mode, hfintosc + pll source) d068 2.5 3.5 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( rc_idle mode, hfintosc + pll source) d069 3.0 4.5 ma -40c to +125c v dd = 5.0v 27.4 dc characteristics: rc idle supply current, pic18(l)f2x/4xk22 (continued) pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; osc1 = external square wave, from rail-to-rail (pri_run and pri_idle only). ? 2010-2012 microchip technology inc. ds41412f-page 437 pic18(l)f2x/4xk22 27.5 dc characteristics: primary run supply current, pic18(l)f2x/4xk22 pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions d070 supply current (i dd ) (1),(2) 0.11 0.20 ma -40c to +125c v dd = 1.8v f osc = 1 mhz ( pri_run mode, ecm source) d071 0.17 0.25 ma -40c to +125c v dd = 3.0v d072 0.15 0.25 ma -40c to +125c v dd = 2.3v f osc = 1 mhz ( pri_run mode, ecm source) d073 0.20 0.30 ma -40c to +125c v dd = 3.0v d074 0.25 0.35 ma -40c to +125c v dd = 5.0v d075 1.45 2.0 ma -40c to +125c v dd = 1.8v f osc = 20 mhz ( pri_run mode, ech source) d076 2.60 3.5 ma -40c to +125c v dd = 3.0v d077 1.95 2.5 ma -40c to +125c v dd = 2.3v f osc = 20 mhz ( pri_run mode, ech source) d078 2.65 3.5 ma -40c to +125c v dd = 3.0v d079 2.95 4.5 ma -40c to +125c v dd = 5.0v d080 7.5 10 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( pri_run , ech oscillator) d081 7.5 10 ma -40c to +125c v dd = 3.0v f osc = 64 mhz ( pri_run mode, ech source) d082 8.5 11.5 ma -40c to +125c v dd = 5.0v d083 1.0 1.5 ma -40c to +125c v dd = 1.8v f osc = 4 mhz 16 mhz internal ( pri_run mode, ecm + pll source) d084 1.8 3.0 ma -40c to +125c v dd = 3.0v d085 1.4 2.0 ma -40c to +125c v dd = 2.3v f osc = 4 mhz 16 mhz internal ( pri_run mode, ecm + pll source) d086 1.85 2.5 ma -40c to +125c v dd = 3.0v d087 2.1 3.0 ma -40c to +125c v dd = 5.0v d088 6.35 9.0 ma -40c to +125c v dd = 3.0v f osc = 16 mhz 64 mhz internal ( pri_run mode, ech + pll source) d089 6.35 9.0 ma -40c to +125c v dd = 3.0v f osc = 16 mhz 64 mhz internal ( pri_run mode, ech + pll source) d090 7.0 10 ma -40c to +125c v dd = 5.0v note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; osc1 = external square wave, from rail-to-rail (pri_run and pri_idle only). pic18(l)f2x/4xk22 ds41412f-page 438 ? 2010-2012 microchip technology inc. 27.6 dc characteristics: primary idle supply current, pic18(l)f2x/4xk22 pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions d100 supply current (i dd ) (1),(2) 0.030 0.050 ma -40c to +125c v dd = 1.8v fosc = 1 mhz ( pri_idle mode, ecm source) d101 0.045 0.065 ma -40c to +125c v dd = 3.0v d102 0.06 0.12 ma -40c to +125c v dd = 2.3v fosc = 1 mhz ( pri_idle mode, ecm source) d103 0.08 0.15 ma -40c to +125c v dd = 3.0v d104 0.13 0.20 ma -40c to +125c v dd = 5.0v d105 0.45 0.8 ma -40c to +125c v dd = 1.8v fosc = 20 mhz ( pri_idle mode, ech source) d106 0.70 1.0 ma -40c to +125c v dd = 3.0v d107 0.55 0.8 ma -40c to +125c v dd = 2.3v fosc = 20 mhz ( pri_idle mode, ech source) d108 0.75 1.0 ma -40c to +125c v dd = 3.0v d109 0.90 1.2 ma -40c to +125c v dd = 5.0v d110 2.25 3.0 ma -40c to +125c v dd = 3.0v fosc = 64 mhz ( pri_idle mode, ech source) d111 2.25 3.0 ma -40c to +125c v dd = 3.0v fosc = 64 mhz ( pri_idle mode, ech source) d112 2.60 3.5 ma -40c to +125c v dd = 5.0v d113 0.35 0.6 ma -40c to +125c v dd = 1.8v fosc = 4 mhz 16 mhz internal ( pri_idle mode, ecm + pll source) d114 0.55 0.8 ma -40c to +125c v dd = 3.0v d115 0.45 0.6 ma -40c to +125c v dd = 2.3v fosc = 4 mhz 16 mhz internal ( pri_idle mode, ecm + pll source) d116 0.60 0.9 ma -40c to +125c v dd = 3.0v d117 0.70 1.0 ma -40c to +125c v dd = 5.0v d118 2.2 3.0 ma -40c to +125c v dd = 3.0v fosc = 16 mhz 64 mhz internal ( pri_idle mode, ech + pll source) d119 2.2 3.0 ma -40c to +125c v dd = 3.0v fosc = 16 mhz 64 mhz internal ( pri_idle mode, ech + pll source) d120 2.5 3.5 ma -40c to +125c v dd = 5.0v note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; osc1 = external square wave, from rail-to-rail (pri_run and pri_idle only). ? 2010-2012 microchip technology inc. ds41412f-page 439 pic18(l)f2x/4xk22 . 27.7 dc characteristics: secondary oscillator supply current, pic18(l)f2x/4xk22 pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions d130 supply current (i dd ) (1),( 2) 3.5 23 ? a -40c v dd = 1.8v fosc = 32 khz ( sec_run mode, sosc source) 3.7 25 ? a+25c 3.8 ? ? a+60c 4.0 28 ? a+85c 5.1 30 ? a +125c d131 6.2 26 ? a -40c v dd = 3.0v 6.4 30 ? a+25c 6.5 ? ? a+60c 6.8 35 ? a+85c 7.8 40 ? a +125c d132 15 35 ? a -40c v dd = 2.3v fosc = 32 khz ( sec_run mode, sosc source) 16 35 ? a +25c 17 35 ? a +85c 19 50 ? a +125c d133 18 50 ? a -40c v dd = 3.0v 19 50 ? a +25c 21 50 ? a +85c 22 60 ? a +125c d134 19 55 ? a -40c v dd = 5.0v 20 55 ? a +25c 22 55 ? a +85c 23 70 ? a +125c note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; sosci / sosco = complementary external square wave, from rail-to-rail. pic18(l)f2x/4xk22 ds41412f-page 440 ? 2010-2012 microchip technology inc. d135 0.9 18 ? a -40c v dd = 1.8v fosc = 32 khz ( sec_idle mode, sosc source) 1.0 18 ? a+25c 1.1 ? ? a+60c 1.3 20 ? a+85c 2.3 22 ? a +125c d136 1.3 20 ? a -40c v dd = 3.0v 1.4 20 ? a+25c 1.5 ? ? a+60c 1.8 22 ? a+85c 2.9 25 ? a +125c d137 12 30 ? a -40c v dd = 2.3v fosc = 32 khz ( sec_idle mode, sosc source) 13 30 ? a +25c 14 30 ? a +85c 16 45 ? a +125c d138 13 35 ? a -40c v dd = 3.0v 14 35 ? a +25c 16 35 ? a +85c 18 50 ? a +125c d139 14 40 ? a -40c v dd = 5.0v 15 40 ? a +25c 16 40 ? a +85c 18 60 ? a +125c 27.7 dc characteristics: secondary oscillator supply current, pic18(l)f2x/4xk22 pic18lf2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c pic18f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. device characteristics typ max units conditions note 1: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. test condition: all peripheral module control bits in pmd0, pmd1 and pmd2 set to ? 1 ?. 2: the test conditions for all i dd measurements in active operation mode are: all i/o pins set as outputs driven to vss; mclr = v dd ; sosci / sosco = complementary external square wave, from rail-to-rail. ? 2010-2012 microchip technology inc. ds41412f-page 441 pic18(l)f2x/4xk22 27.8 dc characteristics:input/output characteristics, pic18(l)f2x/4xk22 dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ? +125c param no. symbol characteristic min typ? max units conditions v il input low voltage i/o port: d140 with ttl buffer ? ? 0.8 v 4.5v ? v dd ? 5.5v d140a ? ? 0.15 v dd v1.8v ? v dd ? 4.5v d141 with schmitt trigger buffer ??0.2v dd v2.0v ? v dd ? 5.5v with i 2 c? levels ? ? 0.3 v dd v with smbus levels ? ? 0.8 v 2.7v ? v dd ? 5.5v d142 mclr , osc1 (rc mode) (1) ??0.2v dd v d142a osc1 (hs mode) ? ? 0.3 v dd v v ih input high voltage i/o ports: ? ? d147 with ttl buffer 2.0 ? ? v 4.5v ? v dd ?? 5.5v d147a 0.25 v dd + 0.8 ??v1.8v ? v dd ? 4.5v d148 with schmitt trigger buffer 0.8 v dd ??v2.0v ? v dd ? 5.5v with i 2 c? levels 0.7 v dd ??v with smbus levels 2.1 ? ? v 2.7v ? v dd ? 5.5v d149 mclr 0.8 v dd ??v d150a osc1 (hs mode) 0.7 v dd ??v d150b osc1 (rc mode) (1) 0.9 v dd ??v i il input leakage i/o and mclr (2),(3) v ss ?? v pin ?? v dd , pin at high-impedance d155 i/o ports and mclr ? ? ? ? 0.1 0.7 4 35 50 100 200 1000 na na na na ?? +25c (4) +60c +85c +125c i pu weak pull-up current (4) d158 i purb portb weak pull-up current 25 25 85 130 200 300 ? a ? a v dd = 3.3v, v pin = v ss v dd = 5.0v, v pin = v ss note 1: in rc oscillator configuration, the osc1/clkin pin is a schmitt trigger input. it is not recommended that the pic ? device be driven with an external clock while in rc mode. 2: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 3: negative current is defined as current sourced by the pin. 4: parameter is characterized but not tested. pic18(l)f2x/4xk22 ds41412f-page 442 ? 2010-2012 microchip technology inc. v ol output low voltage d159 i/o ports ??0.6v i ol = 8 ma, v dd = 5v i ol = 6 ma, v dd = 3.3v i ol = 1.8 ma, v dd = 1.8v v oh output high voltage (3) d161 i/o ports v dd - 0.7 ? ? v i oh = 3.5 ma, v dd = 5v i oh = 3 ma, v dd = 3.3v i oh = 1 ma, v dd = 1.8v 27.8 dc characteristics:input/output characteristics, pic18(l)f2x/4xk22 (continued) dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c ?? t a ? +125c param no. symbol characteristic min typ? max units conditions note 1: in rc oscillator configuration, the osc1/clkin pin is a schmitt trigger input. it is not recommended that the pic ? device be driven with an external clock while in rc mode. 2: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 3: negative current is defined as current sourced by the pin. 4: parameter is characterized but not tested. ? 2010-2012 microchip technology inc. ds41412f-page 443 pic18(l)f2x/4xk22 27.9 memory programming requirements dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. sym characteristic min typ? max units conditions internal program memory programming specifications (1) d170 v pp voltage on mclr /v pp pin 8 ? 9 v (note 3), (note 4) d171 i ddp supply current during programming ??10ma data eeprom memory d172 e d byte endurance 100k ? ? e/w -40 ? c to +85 ? c d173 v drw v dd for read/write v ddmin ? v ddmax v using eecon to read/ write d175 t dew erase/write cycle time ? 3 4 ms d176 t retd characteristic retention ? 40 ? year provided no other specifications are violated d177 t ref number of total erase/write cycles before refresh (2) 1m 10m ? e/w -40c to +85c program flash memory d178 e p cell endurance 10k ? ? e/w -40 ? c to +85 ? c (note 5) d179 v pr v dd for read v ddmin ? v ddmax v d181 v iw v dd for row erase or write 2.2 ? v ddmax v pic18lf2x/4xk22 d182 v iw v ddmin ? v ddmax v pic18f2x/4xk22 d183 t iw self-timed write cycle time ? 2 ? ms d184 t retd characteristic retention ? 40 ? year provided no other specifications are violated ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: these specifications are for programming the on-chip program memory through the use of table write instruc- tions. 2: refer to section 7.8 ?using the data eeprom? for a more detailed discussion on data eeprom endurance. 3: required only if single-supply programming is disabled. 4: the mplab icd 2 does not support variable v pp output. circuitry to limit the mplab icd 2 v pp voltage must be placed between themplab icd 2 and target system when programming or debugging with the mplab icd 2. 5: self-write and block erase. pic18(l)f2x/4xk22 ds41412f-page 444 ? 2010-2012 microchip technology inc. 27.10 analog characteristics table 27-1: comparator specifications operating conditions: 1.8v < vdd < 5.5v, -40c < ta < +125c (unless otherwise stated) param no. sym characteristics min typ max units comments cm01 v ioff input offset voltage ? 3 40 mv high-power mode v ref = v dd /2 ? 4 60 mv low-power mode v ref = v dd /2 cm02 v icm input common-mode voltage v ss ?v dd v cm04* t resp response time (1) ? 200 400 ns high-power mode ? 600 3500 ns low-power mode cm05* t mc 2 ov comparator mode change to output valid ?? 10 ? s * these parameters are characterized but not tested. note 1: response time measured with one comparator input at v dd /2, while the other input transitions from v ss to v dd . table 27-2: digital-to-analog converter (dac) specifications operating conditions: 2.0v < v dd < 5.5v, -40c < t a < +125c (unless otherwise stated) param no. sym characteristics min typ max units comments cv01* c lsb step size (2) ?v dd /32 ? v cv02* c acc absolute accuracy ? ? ?? 1/2 lsb ? v src ?? 2.0v cv03* c r unit resistor value (r) ? 5k ? ? cv04* c st settling time (1) ??10 ? s cv05* v src + dac positive reference v src - +2 ? v dd v cv06* v src - dac negative reference v ss ?v src + -2 v cv07* ? v src dac reference range (v src + - v src -) 2?v dd v * these parameters are characterized but not tested. note 1: settling time measured while cvrr = 1 and cvr3:cvr0 transitions from ? 0000 ? to ? 1111 ?. 2: see section 22.0 ?digital-to-analog converter (dac) module? for more information. ? 2010-2012 microchip technology inc. ds41412f-page 445 pic18(l)f2x/4xk22 table 27-3: fixed voltage reference (fvr) specifications operating conditions: -40c < ta < +125c (unless otherwise stated) param no. sym characteristics min typ max units comments vr01 v rout vr voltage output to adc 0.973 1.024 1.085 v 1x output, v dd ?? 2.5v 1.946 2.048 2.171 v 2x output, v dd ?? 2.5v 3.891 4.096 4.342 v 4x output, v dd ?? 4.75v (pic18f2x/4xk22) vr02 v rout vr voltage output all other modules 0.942 1.024 1.096 v 1x output, v dd ?? 2.5v 1.884 2.048 2.191 v 2x output, v dd ?? 2.5v 3.768 4.096 4.383 v 4x output, v dd ?? 4.75v (pic18f2x/4xk22) vr04* t stable settling time ? 25 100 ? s 0 to 125c * these parameters are characterized but not tested. table 27-4: charge time measurement unit (ctmu) specifications operating conditions: 1.8v < vdd < 5.5v, -40c < ta < +125c (unless otherwise stated) param no. sym characteristics min typ (1) max units comments ct01 i out 1 ctmu current source, base range ?0.55? ? airng<1:0>=01 ct02 i out 2 ctmu current source, 10x range ?5.5? ? airng<1:0>=10 ct03 i out 3 ctmu current source, 100x range ?55? ? airng<1:0>=11 v dd ? 3.0v note 1: nominal value at center point of current trim range (ctmuicon<7:2>=000000). pic18(l)f2x/4xk22 ds41412f-page 446 ? 2010-2012 microchip technology inc. figure 27-5: high/low-voltage detect characteristics v hlvd hlvdif v dd (hlvdif set by hardware) (hlvdif can be cleared by software) table 27-5: high/low-volt age detect characteristics standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param no. symbol characteristic hlvdl<3:0> min typ? max units conditions hlvd voltage on v dd transition high-to- low 0000 1.69 1.84 1.99 v 0001 1.92 2.07 2.22 v 0010 2.08 2.28 2.48 v 0011 2.24 2.44 2.64 v 0100 2.34 2.54 2.74 v 0101 2.54 2.74 2.94 v 0110 2.62 2.87 3.12 v 0111 2.76 3.01 3.26 v 1000 3.00 3.30 3.60 v 1001 3.18 3.48 3.78 v 1010 3.44 3.69 3.94 v 1011 3.66 3.91 4.16 v 1100 3.90 4.15 4.40 v 1101 4.11 4.41 4.71 v 1110 4.39 4.74 5.09 v 1111 v(hlvdin pin) v ? production tested at t amb = 25c. specifications over temperature limits ensured by characterization. ? 2010-2012 microchip technology inc. ds41412f-page 447 pic18(l)f2x/4xk22 27.11 ac (timing) characteristics 27.11.1 timing parameter symbology the timing parameter symbols have been created using one of the following formats: 1. tpps2pps 3. t cc : st (i 2 c? specifications only) 2. tpps 4. ts (i 2 c specifications only) t f frequency t time lowercase letters (pp) and their meanings: pp cc ccp1 osc osc1 ck clkout rd rd cs cs rw rd or wr di sdi sc sck do sdo ss ss dt data in t0 t0cki io i/o port t1 t13cki mc mclr wr wr uppercase letters and their meanings: s f fall p period hhigh rrise i invalid (high-impedance) v valid l low z high-impedance i 2 c only aa output access high high buf bus free low low t cc : st (i 2 c specifications only) cc hd hold su setup st dat data input hold sto stop condition sta start condition pic18(l)f2x/4xk22 ds41412f-page 448 ? 2010-2012 microchip technology inc. 27.11.2 timing conditions the temperature and voltages specified in tab l e 2 7- 6 apply to all timing specifications unless otherwise noted. figure 27-6 specifies the load conditions for the timing specifications. table 27-6: temperature and voltage specifications ? ac figure 27-6: load conditions for de vice timing specifications ac characteristics standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ?? +125c operating voltage v dd range as described in section 27.1 ?dc characteristics: supply voltage, pic18(l)f2x/4xk22? and section 27.9 ?memory programming requirements? . v dd /2 c l r l pin pin v ss v ss c l r l =464 ? c l = 50 pf for all pins except osc2/clkout and including d and e outputs as ports load condition 1 load condition 2 legend : ? 2010-2012 microchip technology inc. ds41412f-page 449 pic18(l)f2x/4xk22 27.11.3 timing diagrams and specifications figure 27-7: external clock timing (all modes exce pt pll) osc1 clkout q4 q1 q2 q3 q4 q1 1 2 3 3 4 4 table 27-7: external clock timing requirements param. no. symbol characteristic min max units conditions 1a f osc external clkin frequency (1) dc dc dc 0.5 16 64 mhz mhz mhz ec, ecio oscillator mode (low power) ec, ecio oscillator mode (medium power) ec, ecio oscillator mode (high power) oscillator frequency (1) dc 4 mhz rc oscillator mode 5 200 khz lp oscillator mode 0.1 4 mhz xt oscillator mode 4 4 mhz hs oscillator mode, v dd < 2.7v 4 16 mhz hs oscillator mode, v dd ?? 2.7v, medium-power mode (hsmp) 4 20 mhz hs oscillator mode, v dd ?? 2.7v, high-power mode (hshp) 1t osc external clkin period (1) 2.0 62.5 15.6 ? ? ? ? s ns ns ec, ecio oscillator mode (low power) ec, ecio oscillator mode (medium power) ec, ecio oscillator mode (high power) oscillator period (1) 250 ? ns rc oscillator mode 5 200 ? s lp oscillator mode 0.25 250 10 250 ? s ns xt oscillator mode hs oscillator mode, v dd < 2.7v 62.5 250 ns hs oscillator mode, v dd ?? 2.7v, medium-power mode (hsmp) 50 250 ns hs oscillator mode, v dd ?? 2.7v, high-power mode (hshp) 2t cy instruction cycle time (1) 62.5 ? ns t cy = 4/f osc 3t os l, t os h external clock in (osc1) high or low time 2.5 ? ? s lp oscillator mode 30 ? ns xt oscillator mode 10 ? ns hs oscillator mode 4t os r, t os f external clock in (osc1) rise or fall time ? 50 ns lp oscillator mode ? 20 ns xt oscillator mode ? 7.5 ns hs oscillator mode note 1: instruction cycle period (t cy ) equals four times the input oscillator time base period for all configurations except pll. all specified values are based on characterization data for that particular oscillator type under standard operating condi- tions with the device executing code. exceeding these specif ied limits may result in an unstable oscillator operation and/or higher than expected current consumption. all devices are tested to operate at ?min.? values with an external clock applied to the osc1/clkin pin. when an external cloc k input is used, the ?max.? cycle time limit is ?dc? (no clock) for all devices. pic18(l)f2x/4xk22 ds41412f-page 450 ? 2010-2012 microchip technology inc. table 27-8: pll clock timing specifications param. no. sym characteristic min max units conditions f10 f osc oscillator frequency range 4 5 mhz v dd < 2.7v, -40c to +85c 44mhz v dd < 2.7v, +85c to +125c 416mhz 2.7v ? v dd , -40c to +85c 412mhz 2.7v ? v dd , +85c to +125c f11 f sys on-chip vco system frequency 16 20 mhz v dd < 2.7v, -40c to +85c 16 16 mhz v dd < 2.7v, +85c to +125c 16 64 mhz 2.7v ? v dd , -40c to +85c 16 48 mhz 2.7v ? v dd , +85c to +125c f12 t rc pll start-up time (lock time) ? 2 ms table 27-9: ac characteristics:internal oscillators accuracy pic18(l)f46k22 standard operating conditions (unless otherwise stated) operating temperature -40c ? t a ? +125c param. no. characteristics freq. tolerance min typ? max units conditions oa1 internal calibrated hfintosc frequency (1) ? 2% ? 16.0 ? mhz 0c ?? t a ?? +60c, v dd ?? 2.5v ? 3% ? 16.0 ? mhz +60c ?? t a ?? +85c, v dd ?? 2.5v ? 5% ? 16.0 ? mhz -40c ?? t a ?? +125c oa2 internal calibrated mfintosc frequency (1) ? 2% ? 500 ? khz 0c ?? t a ?? +60c, v dd ?? 2.5v ? 3% ? 500 ? khz +60c ?? t a ?? +85c, v dd ?? 2.5v ? 5% ? 500 ? khz -40c ?? t a ?? +125c oa3 internal calibrated lfintosc frequency (1) ? 20% ? 31 ? khz -40c ?? t a ?? +125c ? data in ?typ? column is at 3.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: to ensure these oscillator frequency tolerances, v dd and v ss must be capacitively decoupled as close to the device as possible. 0.1 ? f and 0.01 ? f values in parallel are recommended. ? 2010-2012 microchip technology inc. ds41412f-page 451 pic18(l)f2x/4xk22 figure 27-8: clkout and i/o timing note: refer to figure 27-6 for load conditions. osc1 clkout i/o pin (input) i/o pin (output) q4 q1 q2 q3 10 13 14 17 20, 21 19 18 15 11 12 16 old value new value table 27-10: clkout and i/o timing requirements param. no. symbol characteristic min typ max units conditions 10 tosh2ckl osc1 ? to clkout ? ? 75 200 ns (note 1) 11 tosh2ckh osc1 ? to clkout ? ? 75 200 ns (note 1) 12 tckr clkout rise time ? 35 100 ns (note 1) 13 tckf clkout fall time ? 35 100 ns (note 1) 14 tckl2iov clkout ? to port out valid ? ? 0.5 t cy + 20 ns (note 1) 15 tiov2ckh port in valid before clkout ? 0.25 t cy + 25 ? ? ns (note 1) 16 tckh2ioi port in hold after clkout ? 0??ns (note 1) 17 tosh2iov osc1 ? (q1 cycle) to port out valid ? 50 150 ns 18 tosh2ioi osc1 ? (q2 cycle) to port input invalid (i/o in hold time) 100 ? ? ns 19 tiov2osh port input valid to osc1 ?? (i/o in setup time) 0 ? ? ns 20 tior port output rise time ? ? 40 15 72 32 ns ns v dd = 1.8v v dd = 3.3v - 5.0v 21 tiof port output fall time ? ? 28 15 55 30 ns ns v dd = 1.8v v dd = 3.3v - 5.0v 22? t inp intx pin high or low time 20 ? ? ns 23? t rbp rb<7:4> change kbix high or low time t cy ??ns ? these parameters are asynchr onous events not related to any internal clock edges. note 1: measurements are taken in rc mode, where clkout output is 4 x t osc . pic18(l)f2x/4xk22 ds41412f-page 452 ? 2010-2012 microchip technology inc. figure 27-9: reset, watchdog timer, oscillator start-up timer and power-up timer timing figure 27-10: brown-out r eset timing v dd mclr internal por pwrt time-out osc time-out internal reset watchdog timer reset 33 32 30 31 34 i/o pins 34 note: refer to figure 27-6 for load conditions. v dd bv dd 35 v bgap = 1.2v v ivrst enable internal internal reference 36 reference voltage voltage stable ? 2010-2012 microchip technology inc. ds41412f-page 453 pic18(l)f2x/4xk22 note 1: minimum pulse width that will consistently trigger a reset or interrupt. shorter pulses may intermittently trigger a response. figure 27-11: timer0 and timer1 exte rnal clock timings table 27-11: reset, watchdog timer, oscillator start-up timer, power-up timer and brown-out reset requirements param. no. symbol characteristic min typ max units conditions 30 tmcl mclr pulse width (low) 2 ? ? ? s 31 t wdt watchdog timer time-out period (no postscaler) 3.5 4.1 4.7 ms 1:1 prescaler 32 t ost oscillation start-up timer period 1024 t osc ? 1024 t osc ?t osc = osc1 period 33 t pwrt power-up timer period 54.8 64.4 74.1 ms 34 t ioz i/o high-impedance from mclr low or watchdog timer reset ?2? ? s 35 t bor brown-out reset pulse width 200 1 ?? ? sv dd ? b vdd (see d005 ) 36 t ivrst internal reference voltage stable ? 25 35 ? s 37 t hlvd high/low-voltage detect pulse width 200 1 ?? ? sv dd ? v hlvd 38 t csd cpu start-up time 5 ? 10 ? s 39 t iobst time for hf-intosc to stabilize ? 0.25 1 ms note: refer to figure 27-6 for load conditions. 46 47 45 48 41 42 40 t0cki t1oso/t13cki tmr0 or tmr1 pic18(l)f2x/4xk22 ds41412f-page 454 ? 2010-2012 microchip technology inc. figure 27-12: capture/compare/pwm timings (all ccp modules) table 27-12: timer0 and timer1/3/5 external clock requirements param. no. symbol characteristic min max units conditions 40 tt0h t0cki high pulse width no prescaler 0.5 t cy + 20 ? ns with prescaler 10 ? ns 41 tt0l t0cki low pulse width no prescaler 0.5 t cy + 20 ? ns with prescaler 10 ? ns 42 tt0p t0cki period no prescaler t cy + 10 ? ns with prescaler greater of: 20 ns or (t cy + 40)/n ? ns n = prescale value (1, 2, 4,..., 256) 45 tt1h txcki high time synchronous, no prescaler 0.5 t cy + 20 ? ns synchronous, with prescaler 10 ? ns asynchronous 30 ? ns 46 tt1l txcki low time synchronous, no prescaler 0.5 t cy + 5 ? ns synchronous, with prescaler 10 ? ns asynchronous 30 ? ns 47 tt1p txcki input period synchronous greater of: 20 ns or (t cy + 40)/n ? ns n = prescale value (1, 2, 4, 8) asynchronous 60 ? ns ft1 txcki clock input frequency range dc 50 khz 48 tcke2tmri delay from external txcki clock edge to timer increment 2 t osc 7 t osc ? note: refer to figure 27-6 for load conditions. ccpx (capture mode) 50 51 52 ccpx 53 54 (compare or pwm mode) ? 2010-2012 microchip technology inc. ds41412f-page 455 pic18(l)f2x/4xk22 figure 27-13: example spi ma ster mode timing (cke = 0 ) table 27-13: capture/compare/pwm requirements (all ccp modules) param . no. symbol characteristic min max units conditions 50 tccl ccpx input low time no prescaler 0.5 t cy + 20 ? ns with prescaler 10 ? ns 51 tcch ccpx input high time no prescaler 0.5 t cy + 20 ? ns with prescaler 10 ? ns 52 tccp ccpx input period 3 t cy + 40 n ?nsn = prescale value (1, 4 or 16) 53 tccr ccpx output fall time ? 25 ns 54 tccf ccpx output fall time ? 25 ns ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi 70 71 72 73 74 75, 76 78 79 80 79 78 msb lsb bit 6 - - - - - -1 msb in lsb in bit 6 - - - -1 note: refer to figure 27-6 for load conditions. pic18(l)f2x/4xk22 ds41412f-page 456 ? 2010-2012 microchip technology inc. figure 27-14: example spi ma ster mode timing (cke = 1 ) table 27-14: example spi mode requirements (master mode, cke = 0 or 1 ) param. no. symbol characteristic min max units conditions 70 tssl2sch, tssl2scl ss ? to sck ? or sck ? input t cy ?ns 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 25 ? ns 74 tsch2dil, ts c l 2 d i l hold time of sdi data input to sck edge 25 ? ns 75 tdor sdo data output rise time ? 30 ns 76 tdof sdo data output fall time ? 20 ns 78 tscr sck output rise time (master mode) ?30ns 79 tscf sck output fall time (master mode) ? 20 ns 80 tsch2dov, tscl2dov sdo data output valid after sck edge ? 20 ns 81 tdov2sch, tdov2scl sdo data output setup to sck edge t cy ?ns ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi 81 71 72 74 75, 76 78 80 msb 79 73 msb in bit 6 - - - - - -1 lsb in bit 6 - - - -1 lsb note: refer to figure 27-6 for load conditions. ? 2010-2012 microchip technology inc. ds41412f-page 457 pic18(l)f2x/4xk22 figure 27-15: example spi slave mode timing (cke = 0 ) ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi 70 71 72 73 74 75, 76 77 78 79 80 79 78 sdi msb lsb bit 6 - - - - - -1 msb in bit 6 - - - -1 lsb in 83 note: refer to figure 27-6 for load conditions. table 27-15: example spi mode requirements (slave mode timing, cke = 0 or 1 ) param. no. symbol characteristic min max units conditions 70 tssl2sch, tssl2scl ss ? to sck ? or sck ? input t cy ?ns 71 tsch sck input high time continuous 25 ? ns 72 tscl sck input low time continuous 30 ? ns 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 25 ? ns 74 tsch2dil, tscl2dil hold time of sdi data input to sck edge 25 ? ns 75 tdor sdo data output rise time ? 30 ns 76 tdof sdo data output fall time ? 20 ns 77 tssh2doz ss ? to sdo output high-impedance 10 50 ns 80 tsch2dov, tscl2dov sdo data output valid after sck edge ? 60 ns 82 tssl2dov sdo data output valid after ss ? edge ? 60 ns 83 tsch2ssh, tscl2ssh ss ? after sck edge 1.5 t cy + 40 ? ns pic18(l)f2x/4xk22 ds41412f-page 458 ? 2010-2012 microchip technology inc. figure 27-16: example spi slave mode timing (cke = 1 ) figure 27-17: i 2 c? bus start/stop bits timing ss sck (ckp = 0 ) sck (ckp = 1 ) sdo sdi 70 71 72 82 sdi 74 75, 76 msb bit 6 - - - - - -1 lsb 77 msb in bit 6 - - - -1 lsb in 80 83 note: refer to figure 27-6 for load conditions. note: refer to figure 27-6 for load conditions. 91 92 93 scl sda start condition stop condition 90 ? 2010-2012 microchip technology inc. ds41412f-page 459 pic18(l)f2x/4xk22 figure 27-18: i 2 c? bus data timing table 27-16: i 2 c? bus start/stop bits requirements (slave mode) param. no. symbol characteristic min max units conditions 90 t su : sta start condition 100 khz mode 4700 ? ns only relevant for repeated start condition setup time 400 khz mode 600 ? 91 t hd : sta start condition 100 khz mode 4000 ? ns after this period, the first clock pulse is generated hold time 400 khz mode 600 ? 92 t su : sto stop condition 100 khz mode 4700 ? ns setup time 400 khz mode 600 ? 93 t hd : sto stop condition 100 khz mode 4000 ? ns hold time 400 khz mode 600 ? note: refer to figure 27-6 for load conditions. 90 91 92 100 101 103 106 107 109 109 110 102 scl sda in sda out pic18(l)f2x/4xk22 ds41412f-page 460 ? 2010-2012 microchip technology inc. table 27-17: i 2 c? bus data requirements (slave mode) param. no. symbol characteristic min max units conditions 100 t high clock high time 100 khz mode 4.0 ? ? s must operate at a minimum of 1.5 mhz 400 khz mode 0.6 ? ? s must operate at a minimum of 10 mhz ssp module 1.5 t cy ? 101 t low clock low time 100 khz mode 4.7 ? ? s must operate at a minimum of 1.5 mhz 400 khz mode 1.3 ? ? s must operate at a minimum of 10 mhz ssp module 1.5 t cy ? 102 t r sda and scl rise time 100 khz mode ? 1000 ns 400 khz mode 20 + 0.1 c b 300 ns c b is specified to be from 10 to 400 pf 103 t f sda and scl fall time 100 khz mode ? 300 ns 400 khz mode 20 + 0.1 c b 300 ns c b is specified to be from 10 to 400 pf 90 t su : sta start condition setup time 100 khz mode 4.7 ? ? s only relevant for repeated start condition 400 khz mode 0.6 ? ? s 91 t hd : sta start condition hold time 100 khz mode 4.0 ? ? s after this period, the first clock pulse is generated 400 khz mode 0.6 ? ? s 106 t hd : da t data input hold time 100 khz mode 0 ? ns 400 khz mode 0 0.9 ? s 107 t su : dat data input setup time 100 khz mode 250 ? ns (note 2) 400 khz mode 100 ? ns 92 t su : sto stop condition setup time 100 khz mode 4.7 ? ? s 400 khz mode 0.6 ? ? s 109 t aa output valid from clock 100 khz mode ? 3500 ns (note 1) 400 khz mode ? ? ns 110 t buf bus free time 100 khz mode 4.7 ? ? s time the bus must be free before a new transmission can start 400 khz mode 1.3 ? ? s d102 c b bus capacitive loading ? 400 pf note 1: as a transmitter, the device must provide this internal minimum delay time to bridge the undefined region (min. 300 ns) of the falling edge of scl to avoid unintended generation of start or stop conditions. 2: a fast mode i 2 c bus device can be used in a standard mode i 2 c bus system but the requirement, t su : dat ? 250 ns, must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must output the next data bit to the sda line, t r max. + t su : dat = 1000 + 250 = 1250 ns (according to the standard mode i 2 c bus specification), before the scl line is released. ? 2010-2012 microchip technology inc. ds41412f-page 461 pic18(l)f2x/4xk22 figure 27-19: master ssp i 2 c? bus start/stop bits timing waveforms figure 27-20: master ssp i 2 c? bus data timing note: refer to figure 27-6 for load conditions. 91 93 scl sda start condition stop condition 90 92 table 27-18: master ssp i 2 c? bus start/stop bits requirements param. no. symbol characteristic min max units conditions 90 t su : sta start condition 100 khz mode 2(t osc )(brg + 1) ? ns only relevant for repeated start condition setup time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 91 t hd : sta start condition 100 khz mode 2(t osc )(brg + 1) ? ns after this period, the first clock pulse is generated hold time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 92 t su : sto stop condition 100 khz mode 2(t osc )(brg + 1) ? ns setup time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 93 t hd : sto stop condition 100 khz mode 2(t osc )(brg + 1) ? ns hold time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? note 1: maximum pin capacitance = 10 pf for all i 2 c pins. note: refer to figure 27-6 for load conditions. 90 91 92 100 101 103 106 107 109 109 110 102 scl sda in sda out pic18(l)f2x/4xk22 ds41412f-page 462 ? 2010-2012 microchip technology inc. table 27-19: master ssp i 2 c? bus data requirements param. no. symbol characteristic min max units conditions 100 t high clock high time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 101 t low clock low time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 102 t r sda and scl rise time 100 khz mode ? 1000 ns c b is specified to be from 10 to 400 pf 400 khz mode 20 + 0.1 c b 300 ns 1 mhz mode (1) ? 300 ns 103 t f sda and scl fall time 100 khz mode ? 300 ns c b is specified to be from 10 to 400 pf 400 khz mode 20 + 0.1 c b 300 ns 1 mhz mode (1) ? 100 ns 90 t su : sta start condition setup time 100 khz mode 2(t osc )(brg + 1) ? ms only relevant for repeated start condition 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 91 t hd : sta start condition hold time 100 khz mode 2(t osc )(brg + 1) ? ms after this period, the first clock pulse is generated 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 106 t hd : dat data input hold time 100 khz mode 0 ? ns 400 khz mode 0 0.9 ms 107 t su : dat data input setup time 100 khz mode 250 ? ns (note 2) 400 khz mode 100 ? ns 92 t su : sto stop condition setup time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 109 t aa output valid from clock 100 khz mode ? 3500 ns 400 khz mode ? 1000 ns 1 mhz mode (1) ??ns 110 t buf bus free time 100 khz mode 4.7 ? ms time the bus must be free before a new trans- mission can start 400 khz mode 1.3 ? ms d102 c b bus capacitive loading ? 400 pf note 1: maximum pin capacitance = 10 pf for all i 2 c pins. 2: a fast mode i 2 c bus device can be used in a standard mode i 2 c bus system, but parameter 107 ? 250 ns must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must output the next data bit to the sda line, parameter 102 + parameter 107 = 1000 + 250 = 1250 ns (for 100 khz mode), before the scl line is released. ? 2010-2012 microchip technology inc. ds41412f-page 463 pic18(l)f2x/4xk22 figure 27-21: eusart synchrono us transmission (master/slave) timing figure 27-22: eusart synchro nous receive (master/slave) timing 121 121 120 122 txx/ckx rxx/dtx pin pin note: refer to figure 27-6 for load conditions. table 27-20: eusart synchrono us transmission requirements param. no. symbol characteristic min max units conditions 120 tckh2dtv sync xmit (master & slave) clock high to data out valid ?40ns 121 tckrf clock out rise time and fall time (master mode) ?20ns 122 tdtrf data out rise time and fall time ?20ns 125 126 txx/ckx rxx/dtx pin pin note: refer to figure 27-6 for load conditions. table 27-21: eusart synchro nous receive requirements param. no. symbol characteristic min max units conditions 125 tdtv2ckl sync rcv (master & slave) data setup before ck ? (dt setup time) 10 ? ns 126 tckl2dtl data hold after ck ? (dt hold time) 15 ? ns pic18(l)f2x/4xk22 ds41412f-page 464 ? 2010-2012 microchip technology inc. figure 27-23: a/d conversion timing table 27-22: a/d converter characteristics:pic18(l)f2x/4xk22 pic18(l)f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature tested at +25c param. no. symbol characteristic min typ max units conditions a01 n r resolution ? ? 10 bits ? v ref ? 3.0v a03 e il integral linearity error ? 0.5 1 lsb ? v ref = 3.0v a04 e dl differential linearity error ? 0.5 1 lsb ? v ref ? 3.0v a06 e off offset error ? 0.7 2 lsb ? v ref ? 3.0v a07 e gn gain error ? 0.7 2 lsb ? v ref ? 3.0v a08 e totl total error ? 0.8 3 lsb ? v ref ? 3.0v a20 ? v ref reference voltage range (v refh ? v refl ) 2?v dd v a21 v refh reference voltage high v dd /2 ? v dd + 0.3 v a22 v refl reference voltage low v ss ? 0.3v ? v dd /2 v a25 v ain analog input voltage v refl ?v refh v a30 z ain recommended impedance of analog voltage source ?? 3k ? note: the a/d conversion result never decreases with an increase in the input voltage and has no missing codes. 131 130 132 bsf adcon0, go q4 a/d clk a/d data adres adif go sample old_data sampling stopped done new_data (note 2) 987 21 0 note 1: if the a/d clock source is selected as rc, a time of t cy is added before the a/d clock starts. this allows the sleep instruction to be executed. 2: this is a minimal rc delay (typically 100 ns), which also disconnects the holding capacitor from the analog input. .. . . . . t cy ? 2010-2012 microchip technology inc. ds41412f-page 465 pic18(l)f2x/4xk22 table 27-23: a/d conversion requirements pic18(l)f2x/4xk22 standard operating conditions (unless otherwise stated) operating temperature tested at +25c param. no. symbol characteristic min typ max units conditions 130 t ad a/d clock period 1 ? 25 ? s-40 ? c to +85 ? c 1? 4 ? s+85 ? c to +125 ? c 131 t cnv conversion time (not including acquisition time) (note 1) 12 ? 12 t ad 132 t acq acquisition time (note 2) 1.4 ? ? ? sv dd = 3v, rs = 50 ? 135 t swc switching time from convert ? sample ? ? (note 3) 136 t dis discharge time 2 ? 2 t ad note 1: adres register may be read on the following t cy cycle. 2: the time for the holding capacitor to acquire the ?new? input voltage when the voltage changes full scale after the con- version (v dd to v ss or v ss to v dd ). the source impedance ( r s ) on the input channels is 50 ? . 3: on the following cycle of the device clock. pic18(l)f2x/4xk22 ds41412f-page 466 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 467 pic18(l)f2x/4xk22 28.0 dc and ac characteristics graphs and charts note: the graphs and tables provided following this note are a statistical summary based on a limited number of samples and are provided for informational purposes only. the performance characteristics listed herein are not tested or guaranteed. in some graphs or tables, the data presented may be outside the specified operating range (e.g. outside specified power supply range) and therefore, outside the warranted range. pic18(l)f2x/4xk22 ds41412f-page 468 ? 2010-2012 microchip technology inc. figure 28-1: pic18lf2x/4xk22 base i pd figure 28-2: pic18f2x/4xk22 base i pd typ. 25 c typ. 60 c max. 85 c limited accuracy 0.001 0.01 0.1 1 10 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typ. 25 c typ. 60 c max. 85 c 10 15 20 25 30 35 40 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 469 pic18(l)f2x/4xk22 figure 28-3: pic18lf2x/4xk22 delta i pd watchdog timer (wdt) figure 28-4: pic18f2x/4xk22 delta i pd watchdog timer (wdt) typ. max. 0.0 0.5 1.0 1.5 2.0 2.5 3.0 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 0 1 2 3 4 5 6 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 470 ? 2010-2012 microchip technology inc. figure 28-5: pic18lf2x/4xk22 delta i pd brown-out reset (bor) figure 28-6: pic18f2x/4xk22 delta i pd brown-out reset (bor) typical max. 85 c 6 7 8 9 10 11 12 13 14 15 16 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 85 c 3 5 7 9 11 13 15 17 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 471 pic18(l)f2x/4xk22 figure 28-7: pic18lf2x/4xk22 delta i pd high/low-voltage detect (hlvd) figure 28-8: pic18f2x/4xk22 delta i pd high/low-voltage detect (hlvd) typical max. 4 6 8 10 12 14 16 18 20 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 0 2 4 6 8 10 12 14 16 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 472 ? 2010-2012 microchip technology inc. figure 28-9: pic18lf2x/4xk22 delta i pd secondary oscillator figure 28-10: pic18f2x/4xk22 delta i pd secondary oscillator typ. 25 c typ. 60 c max. 85 c 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 85 c 0 1 2 3 4 5 6 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 473 pic18(l)f2x/4xk22 figure 28-11: pic18lf2x/4xk22 delta i pd comparator low-power mode figure 28-12: pic18f2x/4xk22 delta i pd comparator low-power mode typical max. 6 8 10 12 14 16 18 20 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 0 5 10 15 20 25 30 35 40 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 474 ? 2010-2012 microchip technology inc. figure 28-13: pic18lf2x/4xk22 delta i pd comparator high-power mode figure 28-14: pic18f2x/4xk22 delta i pd comparator high-power mode typ. max. 0 20 40 60 80 100 120 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 0 20 40 60 80 100 120 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 475 pic18(l)f2x/4xk22 figure 28-15: pic18lf2x/4xk22 delta i pd dac figure 28-16: pic18f2x/4xk22 delta i pd dac typical max. 10 15 20 25 30 35 40 45 50 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 0 10 20 30 40 50 60 70 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 476 ? 2010-2012 microchip technology inc. figure 28-17: pic18lf2x/4xk22 delta i pd fvr figure 28-18: pic18f2x/4xk22 delta i pd fvr typ. 25 c typ. 60 c max. 10 12 14 16 18 20 22 24 26 28 30 1.8 2.1 2.4 2.7 3 3.3 3.6 i pd (a) v dd (v) typical max. 0 20 40 60 80 100 120 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i pd (a) v dd (v) note 1: on the pic18f2x/4xk22, enabling the fvr results in significantly more sleep current when the part enters voltage regulation mode at v dd ~ 3.2v. ? 2010-2012 microchip technology inc. ds41412f-page 477 pic18(l)f2x/4xk22 figure 28-19: pic18(l)f2x/4xk22 delta i dd a/d convertor 1 typical max. 0 50 100 150 200 250 300 350 400 450 500 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) note 1: a/d converter differential currents apply only in run mode. in sleep or idle mode, both the adc and the frc turn off as soon as conversion (if any) is complete. pic18(l)f2x/4xk22 ds41412f-page 478 ? 2010-2012 microchip technology inc. figure 28-20: pic18lf2x/4xk22 typical i dd : rc_run lf-intosc 31 khz figure 28-21: pic18lf2x/4xk22 maximum i dd : rc_run lf-intosc 31 khz -40 c 25 c 85 c 125 c 0 2 4 6 8 10 12 14 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (a) v dd (v) -40 c 25 c 85 c 125 c 5 15 25 35 45 55 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 479 pic18(l)f2x/4xk22 figure 28-22: pic18f2x/4xk22 typical i dd : rc_run lf-intosc 31 khz figure 28-23: pic18f2x/4xk22 maximum i dd : rc_run lf-intosc 31 khz -40 c 25 c 85 c 125 c 10 12 14 16 18 20 22 24 26 28 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) -40 c to +85 c 125 c 5 15 25 35 45 55 65 75 85 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 480 ? 2010-2012 microchip technology inc. figure 28-24: pic18lf2x/4xk22 i dd : rc_run mf-intosc 500 khz figure 28-25: pic18f2x/4xk22 i dd : rc_run mf-intosc 500 khz typical max 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) typical max. 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 2.3 2.7 3.1 3.5 3. 9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 481 pic18(l)f2x/4xk22 figure 28-26: pic18lf2x/4xk22 typical i dd : rc_run hf-intosc figure 28-27: pic18lf2x/4xk22 maximum i dd : rc_run hf-intosc 1 mhz 2 mhz 4 mhz 8 mhz 16 mhz 0.00 0.50 1.00 1.50 2.00 2.50 3.00 3.50 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 1 mhz 2 mhz 4 mhz 8 mhz 16 mhz 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 482 ? 2010-2012 microchip technology inc. figure 28-28: pic18f2x/4xk22 typical i dd : rc_run hf-intosc figure 28-29: pic18f2x/4xk22 maximum i dd : rc_run hf-intosc 1 mhz 2 mhz 4 mhz 8 mhz 16 mhz 0 0.5 1 1.5 2 2.5 3 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 1 mhz 2 mhz 4 mhz 8 mhz 16 mhz 0 0.5 1 1.5 2 2.5 3 3.5 4 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 483 pic18(l)f2x/4xk22 figure 28-30: pic18lf2x/4xk22 typical i dd : rc_run hf-intosc with pll figure 28-31: pic18lf2x/4xk22 maximum i dd : rc_run hf-intosc with pll 16 mhz 32 mhz 64 mhz 0 1 2 3 4 5 6 7 8 9 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 2 4 6 8 10 12 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 484 ? 2010-2012 microchip technology inc. figure 28-32: pic18f2x/4xk22 typical i dd : rc_run hf-intosc with pll figure 28-33: pic18f2x/4xk22 maximum i dd : rc_run hf-intosc with pll 16 mhz 32 mhz 64 mhz 0 1 2 3 4 5 6 7 8 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 2 4 6 8 10 12 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 485 pic18(l)f2x/4xk22 figure 28-34: pic18lf2x/4xk22 typical i dd : rc_idle lf-intosc 31 khz figure 28-35: pic18lf2x/4xk22 maximum i dd : rc_idle lf-intosc 31 khz -40 c 25 c 85 c 125 c 0 0.5 1 1.5 2 2.5 3 3.5 4 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (a) v dd (v) 25 c 85 c 125 c 0 5 10 15 20 25 30 35 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 486 ? 2010-2012 microchip technology inc. figure 28-36: pic18f2x/4xk22 typical i dd : rc_idle lf-intosc 31 khz figure 28-37: pic18f2x/4xk22 maximum i dd : rc_idle lf-intosc 31 khz -40 c 25 c 85 c 125 c 12 13 14 15 16 17 18 19 20 21 22 2.3 2.7 3.1 3.5 3. 9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) -40c -40 c to +85 c 125 c 10 20 30 40 50 60 70 2.3 2.7 3.1 3.5 3. 9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 487 pic18(l)f2x/4xk22 figure 28-38: pic18lf2x/4xk22 i dd : rc_idle mf-intosc 500 khz figure 28-39: pic18f2x/4xk22 i dd : rc_idle mf-intosc 500 khz typical max. 0 0.05 0.1 0.15 0.2 0.25 0.3 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) typical max. 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 488 ? 2010-2012 microchip technology inc. figure 28-40: pic18lf2x/4xk22 typical i dd : rc_idle hf-intosc figure 28-41: pic18lf2x/4xk22 maximum i dd : rc_idle hf-intosc 1 mhz 2 mhz 4 mhz 8 mhz 16 mhz 0 0.2 0.4 0.6 0.8 1 1.2 1.8 2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 i dd (ma) v dd (v) 1 mhz 4 mhz 8 mhz 16 mhz 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 489 pic18(l)f2x/4xk22 figure 28-42: pic18f2x/4xk22 typical i dd : rc_idle hf-intosc figure 28-43: pic18f2x/4xk22 maximum i dd : rc_idle hf-intosc 1 mhz 2 mhz 4 mhz 8 mhz 16 mhz 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 1 mhz 4 mhz 8 mhz 16 mhz 0 0.2 0.4 0.6 0.8 1 1.2 1.4 2.3 2.7 3.1 3.5 3. 9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 490 ? 2010-2012 microchip technology inc. figure 28-44: pic18lf2x/4xk22 typical i dd : rc_idle hf-intosc with pll figure 28-45: pic18lf2x/4xk22 maximum i dd : rc_idle hf-intosc with pll 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 491 pic18(l)f2x/4xk22 figure 28-46: pic18f2x/4xk22 typical i dd : rc_idle hf-intosc with pll figure 28-47: pic18f2x/4xk22 maximum i dd : rc_idle hf-intosc with pll 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 492 ? 2010-2012 microchip technology inc. figure 28-48: pic18lf2x/4xk22 typical i dd : pri_run ec medium power figure 28-49: pic18lf2x/4xk22 maximum i dd : pri_run ec medium power 1 mhz 4 mhz 10 mhz 16 mhz 0.0 0.5 1.0 1.5 2.0 2.5 3.0 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 1 mhz 4 mhz 10 mhz 16 mhz 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 493 pic18(l)f2x/4xk22 figure 28-50: pic18f2x/4xk22 typical i dd : pri_run ec medium power figure 28-51: pic18f2x/4xk22 maximum i dd : pri_run ec medium power 1 mhz 4 mhz 10 mhz 16 mhz 0 0.5 1 1.5 2 2.5 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 1 mhz 4 mhz 10 mhz 16 mhz 0 0.5 1 1.5 2 2.5 3 3.5 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 494 ? 2010-2012 microchip technology inc. figure 28-52: pic18lf2x/4xk22 typical i dd : pri_run ec high power figure 28-53: pic18lf2x/4xk22 maximum i dd : pri_run ec high power 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 2 4 6 8 10 12 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 2 4 6 8 10 12 14 16 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 495 pic18(l)f2x/4xk22 figure 28-54: pic18f2x/4xk22 typical i dd : pri_run ec high power figure 28-55: pic18f2x/4xk22 maximum i dd : pri_run ec high power 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 1 2 3 4 5 6 7 8 9 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 2 4 6 8 10 12 14 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 496 ? 2010-2012 microchip technology inc. figure 28-56: pic18lf2x/4xk22 typical i dd : pri_run ec with pll figure 28-57: pic18lf2x/4xk22 maximium i dd : pri_run ec with pll 16 mhz 32 mhz 64 mhz 0 1 2 3 4 5 6 7 8 9 10 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 2 4 6 8 10 12 14 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 497 pic18(l)f2x/4xk22 figure 28-58: pic18f2x/4xk22 typical i dd : pri_run ec with pll figure 28-59: pic18f2x/4xk22 maximum i dd : pri_run ec with pll 16 mhz 32 mhz 64 mhz 0 1 2 3 4 5 6 7 8 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 2 4 6 8 10 12 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 498 ? 2010-2012 microchip technology inc. figure 28-60: pic18lf2x/4xk22 typical i dd : pri_idle ec medium power figure 28-61: pic18lf2x/4xk22 maximum i dd : pri_idle ec medium power 1 mhz 4 mhz 10 mhz 16 mhz 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 1 mhz 4 mhz 10 mhz 16 mhz 0 0.2 0.4 0.6 0.8 1 1.2 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 499 pic18(l)f2x/4xk22 figure 28-62: pic18f2x/4xk22 typical i dd : pri_idle ec medium power figure 28-63: pic18f2x/4xk22 maximum i dd : pri_idle ec medium power 1 mhz 4 mhz 10 mhz 16 mhz 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 2.3 2.7 3.1 3.5 3. 9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 1 mhz 4 mhz 10 mhz 16 mhz 0 0.2 0.4 0.6 0.8 1 1.2 2.3 2.7 3.1 3.5 3. 9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 500 ? 2010-2012 microchip technology inc. figure 28-64: pic18lf2x/4xk22 typical i dd : pri_idle ec high power figure 28-65: pic18lf2x/4xk22 maximum i dd : pri_idle ec high power 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 501 pic18(l)f2x/4xk22 figure 28-66: pic18f2x/4xk22 typical i dd : pri_idle ec high power figure 28-67: pic18f2x/4xk22 maximum i dd : pri_idle ec high power 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 4 mhz 10 mhz 16 mhz 20 mhz 40 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 4 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 502 ? 2010-2012 microchip technology inc. figure 28-68: pic18lf2x/4xk22 typical i dd : pri_idle ec with pll figure 28-69: pic18lf2x/4xk22 maximum i dd : pri_idle ec with pll 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (ma) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 503 pic18(l)f2x/4xk22 figure 28-70: pic18f2x/4xk22 typical i dd : pri_idle ec with pll figure 28-71: pic18f2x/4xk22 maximum i dd : pri_idle ec with pll 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) 16 mhz 32 mhz 64 mhz 0 0.5 1 1.5 2 2.5 3 3.5 4 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (ma) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 504 ? 2010-2012 microchip technology inc. figure 28-72: pic18lf2x/4xk22 typical i dd : sec_run 32.768 khz figure 28-73: pic18lf2x/4xk22 maximum i dd : sec_run 32.768 khz -40 c 25 c 60 c 85 c 125 c 3 4 5 6 7 8 9 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (a) v dd (v) -40 c 25 c 85 c 5 10 15 20 25 30 35 40 45 50 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 505 pic18(l)f2x/4xk22 figure 28-74: pic18f2x/4xk22 typical i dd : sec_run 32.768 khz figure 28-75: pic18f2x/4xk22 maximum i dd : sec_run 32.768 khz -40 c 25 c 85 c 14 15 16 17 18 19 20 21 22 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) -40 c to +85 c 125 c 5 15 25 35 45 55 65 75 85 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 506 ? 2010-2012 microchip technology inc. figure 28-76: pic18lf2x/4xk22 typical i dd : sec_idle 32.768 khz figure 28-77: pic18lf2x/4xk22 maximum i dd : sec_idle 32.768 khz -40 c 25 c 60 c 85 c 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 2.1 2.3 1.8 2.1 2.4 2.7 3 3.3 3.6 i dd (a) v dd (v) -40 c 25 c 85 c 0 5 10 15 20 25 1.8 2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 i dd (a) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 507 pic18(l)f2x/4xk22 figure 28-78: pic18f2x/4xk22 typical i dd : sec_idle 32.768 khz figure 28-79: pic18f2x/4xk22 maximum i dd : sec_idle 32.768 khz -40 c 25 c 85 c 10 11 12 13 14 15 16 17 18 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) -40 c to +85 c 125 c 23 28 33 38 43 48 53 58 63 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 i dd (a) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 508 ? 2010-2012 microchip technology inc. figure 28-80: pic18(l)f2x/4xk22 ttl buffer input low voltage figure 28-81: pic18(l)f2x/4xk22 schmitt trigger buffer input low voltage -40 c 25 c 85 c 125 c 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.8 2.2 2.6 3 3.4 3.8 4.2 4.6 5 5.4 v il (v) v dd (v) max . -40 c 25 c 85 c 125 c max. 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 1.8 2.3 2.8 3.3 3.8 4.3 4.8 5.3 v il (v) v dd (v) ? 2010-2012 microchip technology inc. ds41412f-page 509 pic18(l)f2x/4xk22 figure 28-82: pic18(l)f2x/4xk22 ttl buffer input high voltage figure 28-83: pic18(l)f2x/4xk22 schmitt trigger buffer input high voltage -40 c 25 c 85 c 125 c 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 2.1 1.8 2.2 2.6 3 3.4 3.8 4.2 4.6 5 5.4 v ih (v) v dd (v) min. -40 c 25 c 85 c 125 c min. 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 1.8 2.2 2.6 3 3.4 3.8 4.2 4.6 5 5.4 v ih (v) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 510 ? 2010-2012 microchip technology inc. figure 28-84: pic18(l)f2x/ 4xk22 pin input leakage typical max. 1.00e-11 1.00e-10 1.00e-09 1.00e-08 1.00e-07 1.00e-06 1.00e-05 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 input leakage (a) temperature (c) ? 2010-2012 microchip technology inc. ds41412f-page 511 pic18(l)f2x/4xk22 figure 28-85: pic18(l)f2x/4xk22 output low voltage figure 28-86: pic18(l)f2x/4xk22 output high voltage 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 0 5 10 15 20 25 30 v ol (v) i ol (ma) typ. 5v typ. 3v typ. 2v max. 2v max. 3v max. 5v 0 1 2 3 4 5 0 5 10 15 20 25 v oh (v) i oh (ma) typ. 5v min. 5v typ. 3v min. 3v min. 2v typ. 2v pic18(l)f2x/4xk22 ds41412f-page 512 ? 2010-2012 microchip technology inc. figure 28-87: pic18(l)f2x/4xk22 comparator offset voltage, normal-power mode; v dd =5.5v figure 28-88: pic18(l)f2x/4xk22 comparator offset voltage, normal-power mode; v dd =3.0v 6 sigma typical 0 10 20 30 40 50 60 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 abs. offset (mv) v ref (v) 6 sigma typical 0 5 10 15 20 25 30 35 40 45 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1. 4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 abs. offset (mv) v ref (v) ? 2010-2012 microchip technology inc. ds41412f-page 513 pic18(l)f2x/4xk22 figure 28-89: pic18lf2x/4xk22 comparator offset voltage, normal-power mode; v dd =1.8v 6 sigma typical 0 5 10 15 20 25 30 35 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 abs. offset (mv) v ref (v) pic18(l)f2x/4xk22 ds41412f-page 514 ? 2010-2012 microchip technology inc. figure 28-90: pic18(l)f2x/4xk22 comparator offset voltage, low-power mode; v dd =5.5v figure 28-91: pic18(l)f2x/4xk22 comparator offset voltage, low-power mode; v dd =3.0v 6 sigma typical 0 10 20 30 40 50 60 70 80 90 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 abs. offset (mv) v ref (v) 6 sigma typical 0 10 20 30 40 50 60 70 80 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1. 4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 abs. offset (mv) v ref (v) ? 2010-2012 microchip technology inc. ds41412f-page 515 pic18(l)f2x/4xk22 figure 28-92: pic18lf2x/4xk22 comparator offset voltage, low-power mode; v dd =1.8v 6 sigma typical 0 10 20 30 40 50 60 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 abs. offset (mv) v ref (v) pic18(l)f2x/4xk22 ds41412f-page 516 ? 2010-2012 microchip technology inc. figure 28-93: pic18(l)f2x/4xk 22 typical dac abs. error v dd = 2.5v, 3.0v, & 5.5v max lsb limit 5.5v 3.0v 2.5v 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 absolute error (lsb) v ref (v) minimum v ref limit ? 2010-2012 microchip technology inc. ds41412f-page 517 pic18(l)f2x/4xk22 figure 28-94: pic18(l)f2x/4xk22 ty pical fixed voltage reference 1 x output figure 28-95: pic18(l)f2x/4xk22 ty pical fixed voltage reference 1 x output 2.5v 5.5v 1.005 1.010 1.015 1.020 1.025 1.030 1.035 -40 -20 0 20 40 60 80 100 120 fvr x1 (v) temperature (c) max. min. 0.96 0.98 1.00 1.02 1.04 1.06 1.08 1.10 2.5 3 3.5 4 4.5 5 5.5 fvr x1 (v) v dd (v) - 40 c 25 c 85 c 125 c pic18(l)f2x/4xk22 ds41412f-page 518 ? 2010-2012 microchip technology inc. figure 28-96: pic18(l)f2x/4xk22 ty pical fixed voltage reference 2 x output figure 28-97: pic18(l)f2x/4xk22 ty pical fixed voltage reference 2 x output 2.5v 5.5v 2.015 2.025 2.035 2.045 2.055 2.065 -40 -20 0 20 40 60 80 100 120 fvr x2 (v) temperature (c) min. max. 1.90 1.95 2.00 2.05 2.10 2.15 2.20 2.5 3 3.5 4 4.5 5 5.5 fvr x2 (v) v dd (v) -40c 25 c 85c 125c ? 2010-2012 microchip technology inc. ds41412f-page 519 pic18(l)f2x/4xk22 figure 28-98: pic18f2x/4xk22 typical fixed voltage reference 4 x output figure 28-99: pic18f2x/4xk22 typical fixed voltage reference 4 x output 4.5v 5.5v 4.03 4.05 4.07 4.09 4.11 4.13 -40 -20 0 20 40 60 80 100 120 fvr x4 (v) temperature (c) min. max. 3.85 3.90 3.95 4.00 4.05 4.10 4.15 4.20 4.25 4.30 4.35 4.40 4.5 4.7 4.9 5.1 5.3 5.5 fvr x4 (v) v dd (v) 25 c - 40 c 85 c 125 c pic18(l)f2x/4xk22 ds41412f-page 520 ? 2010-2012 microchip technology inc. figure 28-100: pic18(l)f2x/4xk22 hf-intosc frequency vs. temperature at 16 mh z min / max: 2%, t = 0c to +70c +2% / -3%, t = +70c to +85c 5%, t = -40c to 0c and +85c to +125c 15.20 15.36 15.52 15.68 15.84 16.00 16.16 16.32 16.48 16.64 16.80 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 freq (mhz) temp (c) max. min. typical ? 2010-2012 microchip technology inc. ds41412f-page 521 pic18(l)f2x/4xk22 figure 28-101: pic18lf2x/4xk22 ty pical lf-intosc frequency vs. v dd min/max = 31.25 khz 15%, t = -40c to +85c figure 28-102: pic18f2x/4xk22 typical lf-intosc frequency vs. v dd min/max = 31.25 khz 15%, t = -40c to +85c -40 c 25 c 85 c 125 c 29.0 29.5 30.0 30.5 31.0 31.5 32.0 32.5 1.8 2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 frequency (khz) v dd (v) -40 c 25 c 85 c 125 c 29.0 29.5 30.0 30.5 31.0 31.5 32.0 32.5 2.3 2.7 3.1 3.5 3.9 4.3 4.7 5.1 5.5 frequency (khz) v dd (v) pic18(l)f2x/4xk22 ds41412f-page 522 ? 2010-2012 microchip technology inc. figure 28-103: pic18lf2x/4xk22 typical lf-intosc frequency vs. temperature min/max = 31.25 khz 15%, t = -40c to +85c figure 28-104: pic18f2x/4xk22 typical lf-intosc frequency vs. temperature min/max = 31.25 khz 15%, t = -40c to +85c 1.8v 3v 3.6v 29.0 29.5 30.0 30.5 31.0 31.5 32.0 32.5 -40 -20 0 20 40 60 80 100 120 frequency (khz) temperature (c) 2.5v 3.0v 5.5v 29.0 29.5 30.0 30.5 31.0 31.5 32.0 32.5 -40 -20 0 20 40 60 80 100 120 frequency (khz) temperature (c) ? 2010-2012 microchip technology inc. ds41412f-page 523 pic18(l)f2x/4xk22 29.0 packaging information 29.1 package marking information legend: xx...x customer-specific information or microchip part number y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week ?01?) nnn alphanumeric traceability code pb-free jedec designator for matte tin (sn) * this package is pb-free. the pb-free jedec designator ( ) can be found on the outer packaging for this package. note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line, thus limiting the number of available characters for customer-specific information. 3 e 3 e 28-lead spdip (.300?) example pic18f25k22 0810017 3 e 28-lead soic (7.50 mm) example yywwnnn xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx pic18f25k22 3 e 28-lead ssop (5.30 mm) example pic18f25k22 0810017 3 e -e/ss -e/sp -e/so 0810017 pic18(l)f2x/4xk22 ds41412f-page 524 ? 2010-2012 microchip technology inc. package marking information (continued) legend: xx...x customer-specific information or microchip part number y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week ?01?) nnn alphanumeric traceability code pb-free jedec designator for matte tin (sn) * this package is pb-free. the pb-free jedec designator ( ) can be found on the outer packaging for this package. note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line, thus limiting the number of available characters for customer-specific information. 3 e 3 e 28-lead qfn (6x6 mm) example xxxxxxxx xxxxxxxx yywwnnn pin 1 pin 1 18f25k22 -e/ml 0610017 28-lead uqfn (4x4x0.5 mm) example pin 1 pin 1 e/mv 810017 3 e pic18 f23k22 40-lead pdip (600 mil) example xxxxxxxxxxxxxxxxxx yywwnnn xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx pic18f45k22 3 e -e/p 3 e 0810017 ? 2010-2012 microchip technology inc. ds41412f-page 525 pic18(l)f2x/4xk22 package marking information (continued) legend: xx...x customer-specific information or microchip part number y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week ?01?) nnn alphanumeric traceability code pb-free jedec designator for matte tin (sn) * this package is pb-free. the pb-free jedec designator ( ) can be found on the outer packaging for this package. note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line, thus limiting the number of available characters for customer-specific information. 3 e 3 e 40-lead uqfn (5x5x0.5 mm) example pin 1 pin 1 pic18f -i/mv 0810017 3 e 45k22 44-lead qfn (8x8x0.9 mm) example xxxxxxxxxxx xxxxxxxxxxx yywwnnn xxxxxxxxxxx pin 1 pin 1 1845k22 -e/ml 0810017 3 e 44-lead tqfp (10x10x1 mm) example xxxxxxxxxx yywwnnn xxxxxxxxxx xxxxxxxxxx 18f45k22 -e/pt 0810017 3 e pic18(l)f2x/4xk22 ds41412f-page 526 ? 2010-2012 microchip technology inc. 29.2 package details the following sections give the technical details of the packages. note 1 n 12 d e1 eb c e l a2 e b b1 a1 a 3 ? 2010-2012 microchip technology inc. ds41412f-page 527 pic18(l)f2x/4xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging pic18(l)f2x/4xk22 ds41412f-page 528 ? 2010-2012 microchip technology inc. note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging ? 2010-2012 microchip technology inc. ds41412f-page 529 pic18(l)f2x/4xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging pic18(l)f2x/4xk22 ds41412f-page 530 ? 2010-2012 microchip technology inc. l l1 c a2 a1 a e e1 d n 1 2 note 1 b e ? 2010-2012 microchip technology inc. ds41412f-page 531 pic18(l)f2x/4xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging pic18(l)f2x/4xk22 ds41412f-page 532 ? 2010-2012 microchip technology inc. d exposed d2 e b k e2 e l n note 1 1 2 2 1 n a a1 a3 top view bottom view pad ? 2010-2012 microchip technology inc. ds41412f-page 533 pic18(l)f2x/4xk22 pic18(l)f2x/4xk22 ds41412f-page 534 ? 2010-2012 microchip technology inc. note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging ? 2010-2012 microchip technology inc. ds41412f-page 535 pic18(l)f2x/4xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging pic18(l)f2x/4xk22 ds41412f-page 536 ? 2010-2012 microchip technology inc. n note 1 e1 d 123 a a1 b1 be c eb e l a2 ? 2010-2012 microchip technology inc. ds41412f-page 537 pic18(l)f2x/4xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging pic18(l)f2x/4xk22 ds41412f-page 538 ? 2010-2012 microchip technology inc. note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging ? 2010-2012 microchip technology inc. ds41412f-page 539 pic18(l)f2x/4xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging pic18(l)f2x/4xk22 ds41412f-page 540 ? 2010-2012 microchip technology inc. d exposed pad d2 e b k l e2 2 1 n note 1 2 1 e n bottom view top view a3 a1 a ? 2010-2012 microchip technology inc. ds41412f-page 541 pic18(l)f2x/4xk22 pic18(l)f2x/4xk22 ds41412f-page 542 ? 2010-2012 microchip technology inc. a e e1 d d1 e b note 1 note 2 n 123 c a1 l a2 l1 ? 2010-2012 microchip technology inc. ds41412f-page 543 pic18(l)f2x/4xk22 note: for the most current package drawings, please see the microchip packaging specification located at http://www.microchip.com/packaging pic18(l)f2x/4xk22 ds41412f-page 544 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 545 pic18(l)f2x/4xk22 appendix a: revision history revision a (february 2010) initial release of this document. revision b (april 2010) updated figures 2-4, 12-1 and 18-2; updated registers 2-2, 10-4, 10-5, 10-7, 17-2, 24-1 and 24-5; updated sections 10.3.2, 18.8.4, synchronizing comparator output to timer1; updated sections 27.2, 27-3, 27-4, 27-5, 27-6, 27-7 and 27-9; updated tables 27-2, 27-3, 27-4 and 27-7; other minor corrections. revision c (july 2010) added 40-pin uqfn diagram; updated table 2 and table 1-3 to add 40-uqfn column; updated table 1-1 to add ?40-pin uqfn?; updated figure 27-1; added figure 27-2; updated table 27-6; added 40-lead uqfn package marking information and details; updated packaging information section; updated table b-1 to add ?40-pin uqfn?; updated product identification system section; other minor corrections. revision d (november 2010) updated the data sheet to new format; revised tables 1-2, 1-3, 5-2, 10-1, 10-5, 10-6, 10-8, 10-9, 10-11, 10- 14, 14-13 and register 14-5; updated the electrical characteristics section. revision e (january 2012) updated section 2.5.2, ec mode; updated table 3-2; removed table 3-3; updated section 14.4.8; removed cm2con register; updated the electrical characteristics section; updated the packaging information section; updated the char. data section; other minor corrections. revision f (may 2012) minor corrections; release of final data sheet. pic18(l)f2x/4xk22 ds41412f-page 546 ? 2010-2012 microchip technology inc. appendix b: device differences the differences between the devices listed in this data sheet are shown in tab l e b - 1 . table b-1: device differences features (1) pic18f23k22 pic18lf23k22 pic18f24k22 pic18lf24k22 pic18f25k22 pic18lf25k22 pic18f26k22 pic18lf26k22 pic18f43k22 pic18lf43k22 pic18f44k22 pic18lf44k22 pic18f45k22 pic18lf45k22 pic18f46k22 pic18lf46k22 program memory (bytes) 8192 16384 32768 65536 8192 16384 32768 65536 sram (bytes) 512 768 1536 3896 512 768 1536 3896 eeprom (bytes) 256 256 256 1024 256 256 256 1024 interrupt sources 26 26 33 33 26 26 33 33 i/o ports ports a, b, c, (e) ports a, b, c, (e) ports a, b, c, (e) ports a, b, c, (e) ports a, b, c, d, e ports a, b, c, d, e ports a, b, c, d, e ports a, b, c, d, e capture/compare/pwm modules (ccp) 22 2 2 2 2 2 2 enhanced ccp modules (eccp) full bridge 11 1 1 2 2 2 2 eccp module half bridge 22 2 2 1 1 1 1 10-bit analog-to-digital module 17 input channels 17 input channels 17 input channels 17 input channels 28 input channels 28 input channels 28 input channels 28 input channels packages 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 28-pin uqfn 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 28-pin uqfn 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 28-pin pdip 28-pin soic 28-pin ssop 28-pin qfn 40-pin pdip 40-pin uqfn 44-pin tqfp 44-pin qfn 40-pin pdip 40-pin uqfn 44-pin tqfp 44-pin qfn 40-pin pdip 40-pin uqfn 44-pin tqfp 44-pin qfn 40-pin pdip 40-pin uqfn 44-pin tqfp 44-pin qfn note 1: pic18fxxk22: operating voltage, 2.3v-5.5v. pic18lfxxk22: operating voltage, 1.8v-3.6v. ? 2010-2012 microchip technology inc. ds41412f-page 547 pic18(l)f2x/4xk22 index a a/d analog port pins, configuring .................................. 310 associated registers ............................................... 310 conversions ............................................................. 301 converter characteristics ........................................ 464 discharge ................................................................. 302 selecting and configuring acquisition time ............ 298 absolute maximum ratings ............................................. 427 ac (timing) characteristics ............................................. 447 load conditions for device timing specifications ... 448 parameter symbology ............................................. 447 temperature and voltage specific ations ................. 448 timing conditions .................................................... 448 ac characteristics internal rc accuracy ............................................... 450 access bank mapping with indexed literal offset mode ................. 94 ackstat ........................................................................ 246 ackstat status flag ..................................................... 246 adc ................................................................................. 297 acquisition requirements ........................................ 308 block diagram .......................................................... 297 calculating acquisition time .................................... 308 channel selection .................................................... 298 configuration ............................................................ 298 conversion clock ..................................................... 299 conversion procedure ............................................. 303 internal sampling switch (r ss ) i mpedance ............. 308 interrupts .................................................................. 299 operation ................................................................. 301 operation during sleep ........................................... 302 port configuration .................................................... 298 power management ................................................. 302 reference voltage (v ref ) ........................................ 298 result formatting ..................................................... 300 source impedance ................................................... 308 special event trigger ............................................... 302 starting an a/d conversion ..................................... 300 adcon0 register ............................................................ 304 adcon1 register ............................................................ 305 adcon2 register ............................................................ 306 addfsr .......................................................................... 416 addlw ............................................................................ 379 addulnk ........................................................................ 416 addwf ............................................................................ 379 addwfc ......................................................................... 380 adresh register (adfm = 0) ........................................ 307 adresh register (adfm = 1) ........................................ 307 adresl register (adfm = 0) ......................................... 307 adresl register (adfm = 1) ......................................... 307 analog input connection considerations ......................... 315 analog-to-digital converter. see adc andlw ............................................................................ 380 andwf ............................................................................ 381 assembler mpasm assembler .................................................. 424 b bank select register (bsr) ............................................... 76 baudcon register ......................................................... 279 bc .................................................................................... 381 bcf .................................................................................. 382 bf ............................................................................ 246, 248 bf status flag ......................................................... 246, 248 block diagrams (ccp) capture mode operation .............................. 180 adc ......................................................................... 297 adc transfer function ............................................ 309 analog input model .......................................... 309, 315 ccp pwm ............................................................... 186 comparator 1 ........................................................... 312 compare .................................................................. 183 crystal operation ....................................................... 35 ctmu ...................................................................... 321 ctmu current source calibration circuit ............... 324 ctmu typical connections and internal configuration for pulse delay generation ....... 332 ctmu typical connections and internal configuration for time measurement .............. 331 digital-to-analog converter (dac) .......................... 346 eusart receive .................................................... 268 eusart transmit ................................................... 267 external por circuit (slow v dd power-up) .............. 61 external rc mode ..................................................... 36 fail-safe clock monitor (fscm) ................................ 44 generic i/o port ....................................................... 135 high/low-voltage detect with external input .......... 350 interrupt logic .......................................................... 114 on-chip reset circuit ................................................ 59 pic18f46k22 ............................................................ 16 pwm (enhanced) .................................................... 190 reads from flash program memory ......................... 99 resonator operation ................................................. 35 table read operation ............................................... 95 table write operation ............................................... 96 table writes to flash program memory .................. 101 timer0 in 16-bit mode ............................................. 161 timer0 in 8-bit mode ............................................... 160 timer1/3/5 ............................................................... 163 timer1/3/5 gate ....................................... 169, 170, 171 timer2/4/6 ............................................................... 175 voltage reference ................................................... 343 voltage reference output buffer example ............. 346 watchdog timer ...................................................... 366 bn .................................................................................... 382 bnc ................................................................................. 383 bnn ................................................................................. 383 bnov .............................................................................. 384 bnz ................................................................................. 384 bor. see brown-out reset. bov ................................................................................. 387 bra ................................................................................. 385 break character (12-bit) transmit and receive .............. 287 brown-out reset (bor) ..................................................... 62 detecting ................................................................... 62 disabling in sleep mode ............................................ 62 minimum enable time ............................................... 62 software enabled ...................................................... 62 bsf .................................................................................. 385 btfsc ............................................................................. 386 btfss ............................................................................. 386 btg ................................................................................. 387 bz .................................................................................... 388 c c compilers pic18(l)f2x/4xk22 ds41412f-page 548 ? 2010-2012 microchip technology inc. mplab c18 ............................................................. 424 call ................................................................................ 388 callw ............................................................................. 417 capture module. see enhanced capture/compare/ pwm(eccp) capture/compare/pwm ................................................... 179 capture/compare/pwm (ccp) associated registers w/ capture ..................... 181, 182 associated registers w/ compare ................... 184, 185 associated registers w/ enhanced pwm ................ 204 associated registers w/ pwm ......................... 189, 203 associated registers w/ standard pwm ................. 189 capture mode .......................................................... 180 ccpx pin configuration ........................................... 180 compare mode ........................................................ 183 ccpx pin configuration ................................... 183 software interrupt mode .......................... 180, 183 special event trigger ....................................... 184 timer1 mode resource ........................... 180, 183 prescaler .................................................................. 181 pwm mode duty cycle ........................................................ 187 effects of reset ................................................ 188 example pwm frequencies and resolutions, 20 mhz ............................... 188 example pwm frequencies and resolutions, 32 mhz ............................... 188 example pwm frequencies and resolutions, 8 mhz .................................. 188 operation in sleep mode ................................. 188 resolution ........................................................ 188 system clock frequency changes .................. 188 pwm operation ....................................................... 186 pwm overview ........................................................ 186 pwm period ............................................................. 187 pwm setup .............................................................. 186 ccptmrs0 register ....................................................... 208 ccptmrs1 register ....................................................... 208 ccpxcon (eccpx) register .......................................... 205 clock accuracy with asynchronous operation ................ 276 clock sources external modes .......................................................... 34 ec ...................................................................... 34 hs ...................................................................... 35 lp ....................................................................... 35 ost .................................................................... 34 rc ...................................................................... 36 xt ...................................................................... 35 internal modes ........................................................... 36 frequency selection .......................................... 38 intosc ............................................................. 36 intoscio .......................................................... 36 lfintosc ......................................................... 38 selecting the 31 khz source ...................................... 29 selection using osccon register ........................... 29 clock switching .................................................................. 41 clrf ................................................................................ 389 clrwdt .......................................................................... 389 cm1con0 register ......................................................... 317 cm2con1 register ......................................................... 318 code examples 16 x 16 signed multiply routine .............................. 112 16 x 16 unsigned multiply routine .......................... 112 8 x 8 signed multiply routine .................................. 111 8 x 8 unsigned multiply routine .............................. 111 a/d conversion ........................................................ 303 capacitance calibration routine ............................. 328 capacitive touch switch routine ............................ 330 changing between capture prescalers ................... 181 clearing ram using indirect addressing .................. 90 computed goto using an offset value ................... 73 current calibration routine ..................................... 326 data eeprom read ............................................... 107 data eeprom refresh routine .............................. 108 data eeprom write ............................................... 107 erasing a flash program memory row ................... 100 fast register stack ................................................... 73 initializing porta .................................................... 135 initializing portb .................................................... 140 initializing portc ................................................... 144 initializing portd ................................................... 148 initializing porte .................................................... 151 reading a flash program memory word .................. 99 saving status, wreg and bsr registers in ram . 132 setup for ctmu calibration routines ..................... 325 writing to flash program memory ................... 102?103 code protection ............................................................... 355 comf .............................................................................. 390 comparator associated registers ............................................... 319 operation ................................................................. 311 operation during sleep ........................................... 315 response time ........................................................ 313 comparator module c1 output state versus input conditions ................ 313 comparator specifications ............................................... 444 comparator voltage reference (cv ref ) effects of a reset .................................................... 315 comparator voltage reference (cv ref ) response time ........................................................ 313 comparators c2out as t1/3/5 gate ............................................ 166 effects of a reset .................................................... 315 compare module. see enhanced capture/compare/ pwm (eccp) computed goto ............................................................... 73 config1h register ........................................................ 357 config2h register ........................................................ 359 config2l register ........................................................ 358 config3h register ........................................................ 360 config4l register ........................................................ 361 config5h register ........................................................ 362 config5l register ........................................................ 361 config6h register ........................................................ 363 config6l register ........................................................ 362 config7h register ........................................................ 364 config7l register ........................................................ 363 configuration bits ............................................................ 355 configuration register protection .................................... 371 context saving during interrupts ..................................... 132 cpfseq .......................................................................... 390 cpfsgt .......................................................................... 391 cpfslt ........................................................................... 391 ctmu associated registers ............................................... 335 calibrating ............................................................... 324 creating a delay with ............................................... 332 effects of a reset .................................................... 333 initialization .............................................................. 323 measuring capacitance with .................................... 329 ? 2010-2012 microchip technology inc. ds41412f-page 549 pic18(l)f2x/4xk22 measuring time with ................................................ 331 operation ................................................................. 322 operation during idle mode ..................................... 332 operation during sleep mode ................................. 332 customer change notification service ............................ 557 customer notification service .......................................... 557 customer support ............................................................ 557 cv ref voltage reference specifications ........................ 444 d data addressing modes ..................................................... 90 comparing addressing modes with the extended instruction set enabled ..................................... 93 direct .......................................................................... 90 indexed literal offset ................................................. 92 instructions affected .......................................... 92 indirect ....................................................................... 90 inherent and literal .................................................... 90 data eeprom code protection ....................................................... 371 data eeprom memory associated registers ............................................... 109 eeadr and eeadrh registers ............................. 105 eecon1 and eecon2 registers ........................... 105 operation during code-protect ............................... 108 protection against spurious write ........................... 108 reading .................................................................... 107 using ........................................................................ 108 write verify .............................................................. 107 writing ...................................................................... 107 data memory ..................................................................... 76 access bank .............................................................. 82 and the extended instruction set ............................... 92 bank select register (bsr) ....................................... 76 general purpose registers ........................................ 82 map for pic18f/lf23k22 and pic18f/lf43k22 devices .............................................................. 77 map for pic18f/lf24k22 and pic18f/lf44k22 devices .............................................................. 78 special function registers ........................................ 82 daw ................................................................................. 392 dc and ac characteristics .............................................. 467 dc characteristics input/output ............................................................. 441 power-down current ............................................... 431 primary idle supply current ..................................... 438 primary run supply current .................................... 437 rc idle supply current ............................................ 435 rc run supply current ........................................... 433 secondary oscillator supply current ....................... 439 supply voltage ......................................................... 430 dcfsnz .......................................................................... 393 decf ............................................................................... 392 decfsz ........................................................................... 393 development support ...................................................... 423 device differences ........................................................... 546 device overview details on individual family members ....................... 14 features (table) .......................................................... 15 new core features .................................................... 13 other special features .............................................. 14 device reset timers .......................................................... 63 pll lock time-out ..................................................... 63 power-up timer (pwrt) ........................................... 63 time-out sequence .................................................... 63 devid1 register .............................................................. 364 devid2 register ............................................................. 364 digital-to-analog converter (dac) .................................. 345 associated registers ............................................... 348 effects of a reset .................................................... 346 direct addressing .............................................................. 91 e eccp/ccp. see enhanced capture/compare/pwm eccpxas register .......................................................... 209 eecon1 register ...................................................... 97, 106 effect on standard pic instructions ................................. 420 effects of power managed modes on various clock sources ........................................................... 40 effects of reset pwm mode .............................................................. 188 electrical characteristics ................................................. 427 enhanced capture/compare/pwm (eccp) .................... 179 enhanced pwm mode ............................................. 190 auto-restart .................................................... 198 auto-shutdown ................................................ 197 direction change in full-bridge output mode . 196 full-bridge application ..................................... 194 full-bridge mode ............................................. 194 half-bridge application .................................... 193 half-bridge application examples ................... 199 half-bridge mode ............................................. 193 output relationships (active-high and active-low) .............................................. 191 output relationships diagram ......................... 192 programmable dead band delay .................... 199 shoot-through current ..................................... 199 start-up considerations ................................... 201 enhanced universal synchronous asynchronous receiver transmitter (eusart) ............................. 267 errata ................................................................................. 12 eusart .......................................................................... 267 asynchronous mode ................................................ 269 12-bit break transmit and receive ................. 287 associated registers, receive ........................ 275 associated registers, transmit ....................... 271 auto-wake-up on break .................................. 285 baud rate generator (brg) ........................... 280 clock accuracy ................................................ 276 receiver .......................................................... 272 setting up 9-bit mode with address detect ..... 274 transmitter ...................................................... 269 baud rate generator (brg) associated registers ....................................... 281 auto baud rate detect .................................... 284 baud rate error, calculating ........................... 280 baud rates, asynchronous modes ................. 281 formulas .......................................................... 280 high baud rate select (brgh bit) ................. 280 clock polarity synchronous mode .......................................... 288 data polarity asynchronous receive .................................... 272 asynchronous transmit ................................... 269 synchronous mode .......................................... 288 interrupts asychronous receive ...................................... 273 asynchronous receive .................................... 273 asynchronous transmit ................................... 269 synchronous master mode .............................. 288, 293 associated registers, receive ........................ 292 associated registers, transmit ............... 289, 294 pic18(l)f2x/4xk22 ds41412f-page 550 ? 2010-2012 microchip technology inc. reception ......................................................... 291 transmission .................................................... 288 synchronous slave mode associated registers, receive ........................ 295 reception ......................................................... 295 transmission .................................................... 293 extended instruction set addfsr .................................................................. 416 addulnk ................................................................ 416 and using mplab tools .......................................... 422 callw ..................................................................... 417 considerations for use ............................................ 420 movsf .................................................................... 417 movss .................................................................... 418 pushl ..................................................................... 418 subfsr .................................................................. 419 subulnk ................................................................ 419 syntax ...................................................................... 415 f fail-safe clock monitor .............................................. 44, 355 fail-safe condition clearing ...................................... 44 fail-safe detection .................................................... 44 fail-safe operation .................................................... 44 reset or wake-up from sleep .................................... 44 fast register stack ............................................................ 72 fixed voltage reference (fvr) associated registers ............................................... 344 flash program memory ...................................................... 95 associated registers ............................................... 103 control registers ....................................................... 96 eecon1 and eecon2 ..................................... 96 tablat (table latch) register ......................... 98 tblptr (table pointer) register ...................... 98 erase sequence ...................................................... 100 erasing ..................................................................... 100 operation during code-protect ............................... 103 reading ...................................................................... 99 table pointer boundaries based on operation ........................ 98 table pointer boundaries .......................................... 98 table reads and table writes .................................. 95 write sequence ....................................................... 101 writing to ................................................................. 101 protection against spurious writes ................. 103 unexpected termination .................................. 103 write verify ...................................................... 103 g goto ............................................................................... 394 h hardware multiplier .......................................................... 111 introduction .............................................................. 111 operation ................................................................. 111 performance comparison ........................................ 111 high/low-voltage detect ................................................. 349 applications .............................................................. 352 associated registers ............................................... 353 characteristics ......................................................... 446 current consumption ............................................... 351 effects of a reset ..................................................... 353 operation ................................................................. 350 during sleep .................................................... 353 setup ........................................................................ 351 start-up time ........................................................... 351 typical low-voltage detect application .................. 352 hlvd. see high/low-voltage detect. ............................. 349 i i 2 c mode (msspx) acknowledge sequence timing .............................. 250 bus collision during a repeated start condition .................. 255 during a stop condition .................................. 256 effects of a reset .................................................... 251 i 2 c clock rate w/brg ............................................. 258 master mode operation ......................................................... 242 reception ........................................................ 248 start condition timing ............................. 244, 245 transmission ................................................... 246 multi-master communication, bus collision and arbitration ........................................................ 252 multi-master mode ................................................... 251 read/write bit information (r/w bit) ....................... 227 slave mode transmission ................................................... 232 sleep operation ....................................................... 251 stop condition timing ............................................. 250 id locations ............................................................. 355, 371 incf ................................................................................ 394 incfsz ............................................................................ 395 in-circuit debugger .......................................................... 371 in-circuit serial programming (icsp) ...................... 355, 371 indexed literal offset addressing and standard pic18 instructions ............................. 420 indexed literal offset mode ............................................. 420 indirect addressing ............................................................ 91 infsnz ............................................................................ 395 instruction cycle ................................................................ 74 clocking scheme ....................................................... 74 instruction flow/pipelining ................................................. 74 instruction set .................................................................. 373 addlw .................................................................... 379 addwf .................................................................... 379 addwf (indexed literal offset mode) .................... 421 addwfc ................................................................. 380 andlw .................................................................... 380 andwf .................................................................... 381 bc ............................................................................ 381 bcf ......................................................................... 382 bn ............................................................................ 382 bnc ......................................................................... 383 bnn ......................................................................... 383 bnov ...................................................................... 384 bnz ......................................................................... 384 bov ......................................................................... 387 bra ......................................................................... 385 bsf .......................................................................... 385 bsf (indexed literal offset mode) .......................... 421 btfsc ..................................................................... 386 btfss ..................................................................... 386 btg ......................................................................... 387 bz ............................................................................ 388 call ........................................................................ 388 clrf ....................................................................... 389 clrwdt ................................................................. 389 comf ...................................................................... 390 cpfseq .................................................................. 390 cpfsgt .................................................................. 391 cpfslt ................................................................... 391 ? 2010-2012 microchip technology inc. ds41412f-page 551 pic18(l)f2x/4xk22 daw ......................................................................... 392 dcfsnz .................................................................. 393 decf ....................................................................... 392 decfsz ................................................................... 393 extended instruction set .......................................... 415 general format ........................................................ 375 goto ...................................................................... 394 incf ......................................................................... 394 incfsz .................................................................... 395 infsnz .................................................................... 395 iorlw ..................................................................... 396 iorwf ..................................................................... 396 lfsr ........................................................................ 397 movf ....................................................................... 397 movff .................................................................... 398 movlb .................................................................... 398 movlw ................................................................... 399 movwf ................................................................... 399 mullw .................................................................... 400 mulwf .................................................................... 400 negf ....................................................................... 401 nop ......................................................................... 401 opcode field descriptions ....................................... 374 pop ......................................................................... 402 push ....................................................................... 402 rcall ..................................................................... 403 reset ..................................................................... 403 retfie .................................................................... 404 retlw .................................................................... 404 return .................................................................. 405 rlcf ........................................................................ 405 rlncf ..................................................................... 406 rrcf ....................................................................... 406 rrncf ................................ .................................... 407 setf ........................................................................ 407 setf (indexed literal offset mode) ........................ 421 sleep ..................................................................... 408 subfwb .................................................................. 408 sublw .................................................................... 409 subwf .................................................................... 409 subwfb .................................................................. 410 swapf .................................................................... 410 tblrd ..................................................................... 411 tblwt ..................................................................... 412 tstfsz ................................................................... 413 xorlw .................................................................... 413 xorwf .................................................................... 414 intcon register ............................................................. 116 intcon registers ........................................................... 115 intcon2 register ........................................................... 117 intcon3 register ........................................................... 118 internal oscillator block hfintosc frequency drift ....................................... 38 pll in hfintosc modes .......................................... 39 internal rc oscillator use with wdt .......................................................... 366 internal sampling switch (r ss ) i mpedance ..................... 308 internet address ............................................................... 557 interrupt sources ............................................................. 355 adc ......................................................................... 299 interrupt-on-change (rb7:rb4) .............................. 140 intn pin ................................................................... 132 portb, interrupt-on-change .................................. 132 tmr0 ....................................................................... 132 tmr0 overflow ........................................................ 161 interrupts tmr1/3/5 ................................................................. 168 iorlw ............................................................................. 396 iorwf ............................................................................. 396 ipr registers ................................................................... 115 ipr1 register .................................................................. 128 ipr2 register .................................................................. 129 ipr3 register .................................................................. 130 ipr4 register .................................................................. 131 ipr5 register .................................................................. 131 l lfsr ............................................................................... 397 low-voltage icsp programming. see single-supply icsp programming m map .............................................................................. 79, 80 master clear (mclr ) ......................................................... 61 master synchronous serial port. see msspx memory organization data memory ............................................................. 76 program memory ....................................................... 69 microchip internet web site ............................................. 557 movf .............................................................................. 397 movff ............................................................................ 398 movlb ............................................................................ 398 movlw ........................................................................... 399 movsf ............................................................................ 417 movss ............................................................................ 418 movwf ........................................................................... 399 mplab asm30 assembler, linker, librarian .................. 424 mplab integrated development environment software . 423 mplab pm3 device programmer ................................... 426 mplab real ice in-circuit emulator system ............... 425 mplink object linker/mplib object librarian ............... 424 msspx ............................................................................. 211 spi mode ................................................................. 214 sspxbuf register .................................................. 217 sspxsr register .................................................... 217 mullw ............................................................................ 400 mulwf ............................................................................ 400 n negf ............................................................................... 401 nop ................................................................................. 401 o osccon register ....................................................... 32, 33 oscillator configuration ec .............................................................................. 27 ecio .......................................................................... 27 hs .............................................................................. 27 hspll ....................................................................... 27 lp .............................................................................. 27 rc ............................................................................. 27 xt .............................................................................. 27 oscillator selection .......................................................... 355 oscillator start-up timer (ost) ................................... 40, 63 oscillator switching fail-safe clock monitor ............................................. 44 two-speed clock start-up ........................................ 42 osctune register ........................................................... 37 p p1a/p1b/p1c/p1d. see enhanced capture/compare/ pwm (eccp) .......................................................... 190 pic18(l)f2x/4xk22 ds41412f-page 552 ? 2010-2012 microchip technology inc. packaging information ..................................................... 523 marking .................................................................... 523 pie registers ................................................................... 115 pie1 register ................................................................... 124 pie2 register ................................................................... 125 pie3 register3 ................................................................. 126 pie4 register ................................................................... 127 pie5 register ................................................................... 127 pir1 register ................................................................... 119 pir2 register ................................................................... 120 pll frequency multiplier ................................................... 39 pop .................................................................................. 402 por. see power-on reset. porta associated registers ............................................... 137 porta register ...................................................... 135 trisa register ........................................................ 135 portb associated registers ............................................... 143 portb register ...................................................... 140 portc associated registers ............................................... 147 portc register ...................................................... 144 portd associated registers ............................................... 151 portd register ...................................................... 148 trisd register ........................................................ 148 porte associated registers ............................................... 152 porte register ...................................................... 151 power managed modes ..................................................... 47 and a/d operation ................................................... 302 effects on clock sources ........................................... 40 entering ...................................................................... 47 exiting idle and sleep modes .................................... 54 by interrupt ......................................................... 54 by reset ............................................................. 54 by wdt time-out ............................................... 54 without a start-up delay .................................... 54 idle modes ................................................................. 51 pri_idle ........................................................... 52 rc_idle ............................................................ 53 sec_idle .......................................................... 52 multiple sleep functions ............................................ 48 run modes ................................................................. 48 pri_run ........................................................... 48 sec_run .......................................................... 48 selecting .................................................................... 47 sleep mode ................................................................ 51 summary (table) ........................................................ 47 power-on reset (por) ...................................................... 61 power-up timer (pwrt) ........................................... 63 time-out sequence .................................................... 63 power-up delays ................................................................ 40 power-up timer (pwrt) .................................................... 40 prescaler, timer0 ............................................................. 161 pri_idle mode ................................................................. 52 pri_run mode ................................................................. 48 program counter ................................................................ 70 pcl, pch and pcu registers ................................... 70 pclath and pclatu registers .............................. 70 program memory and extended instruction set ..................................... 94 code protection ....................................................... 369 instructions ................................................................. 75 two-word .......................................................... 75 interrupt vector .......................................................... 69 look-up tables .......................................................... 73 map and stack (diagram) .......................................... 70 reset vector .............................................................. 69 program verification and code protection ...................... 368 associated registers ............................................... 368 pstrxcon register ....................................................... 210 push ............................................................................... 402 push and pop instructions .............................................. 72 pushl ............................................................................. 418 pwm (eccp module) pwm steering .......................................................... 200 steering synchronization ......................................... 200 pwm mode. see enhanced capture/compare/pwm ..... 190 pwm steering .................................................................. 200 pwmxcon register ........................................................ 210 r ram. see data memory. rc_idle mode .................................................................. 53 rc_run ............................................................................ 48 rcall ............................................................................. 403 rcon register .................................................................. 60 bit status during initialization .................................... 67 rcreg ............................................................................ 274 rcsta register .............................................................. 278 reader response ............................................................ 558 register rcreg register ..................................................... 284 register file ....................................................................... 82 registers adcon0 (adc control 0) ....................................... 304 adcon1 (adc control 1) ....................................... 305 adcon2 (adc control 2) ....................................... 306 adresh (adc result high) with adfm = 0) ......... 307 adresh (adc result high) with adfm = 1) ......... 307 adresl (adc result low) with adfm = 0) ........... 307 adresl (adc result low) with adfm = 1) ........... 307 baudcon (baud rate control) .............................. 279 baudcon (eusart baud rate control) .............. 279 ccptmrs0 (pwm timer selection control 0) ....... 208 ccptmrs1 (pwm timer selection control 1) ....... 208 ccpxcon (eccpx control) .................................... 205 cm1con0 (c1 control) ........................................... 317 cm2con1 (c2 control) ........................................... 318 config1h (configuration 1 high) .......................... 357 config2h (configuration 2 high) .......................... 359 config2l (configuration 2 low) ........................... 358 config3h (configuration 3 high) .......................... 360 config4l (configuration 4 low) ........................... 361 config5h (configuration 5 high) .......................... 362 config5l (configuration 5 low) ........................... 361 config6h (configuration 6 high) .......................... 363 config6l (configuration 6 low) ........................... 362 config7h (configuration 7 high) .......................... 364 config7l (configuration 7 low) ........................... 363 ctmuconh (ctmu control high) ......................... 333 ctmuconl (ctmu control low) .......................... 334 ctmuicon (ctmu current control) ...................... 335 devid1 (device id 1) .............................................. 364 devid2 (device id 2) .............................................. 364 eccpxas (ccpx auto-shutdown control) ............. 209 eecon1 (data eeprom control 1) ................. 97, 106 hlvdcon (high/low-voltage detect control) ....... 349 intcon (interrupt control) ...................................... 116 ? 2010-2012 microchip technology inc. ds41412f-page 553 pic18(l)f2x/4xk22 intcon2 (interrupt control 2) ................................. 117 intcon3 (interrupt control 3) ................................. 118 ipr1 (peripheral interrupt priority 1) ........................ 128 ipr2 (peripheral interrupt priority 2) ........................ 129 ipr3 (peripheral interrupt priority) ........................... 130 ipr4 (peripheral interrupt priority) ........................... 131 ipr5 (peripheral interrupt priority) ........................... 131 osccon (oscillator control) ..... ......................... 32, 33 osctune (oscillator tuning) ................................... 37 pie1 (peripheral interrupt enable 1) ........................ 124 pie2 (peripheral interrupt enable 2) ........................ 125 pie3 (peripheral interrupt enable] ........................... 126 pie4 (peripheral interrupt enable) ........................... 127 pie5 (peripheral interrupt enable) ........................... 127 pir1 (peripheral interrupt request 1) ..................... 119 pir2 (peripheral interrupt request 2) ..................... 120 pstrxcon (pwm steering control) ...................... 210 pwmxcon (enhanced pwm control) .................... 210 rcon (reset control) ....................................... 60, 131 rcsta (receive status and control) ...................... 278 slrcon (port slew rate control) ....................... 158 srcon0 (sr latch control 0) ................................ 340 srcon1 (sr latch control 1) ................................ 341 sspxadd (msspx address and baud rate, i 2 c mode) ........................................................ 265 sspxcon1 (msspx control 1) ............................... 260 sspxcon2 (sspx control 2) .................................. 262 sspxmsk (sspx mask) .......................................... 264 sspxstat (sspx status) ....................................... 259 status ..................................................................... 89 stkptr (stack pointer) ............................................ 72 t0con (timer0 control) .......................................... 159 txcon ..................................................................... 177 txcon (timer1/3/5 control) .................................... 172 txgcon (timer1/3/5 gate control) ........................ 173 txsta (transmit status and control) ..................... 277 vrefcon0 ............................................................. 344 vrefcon1 ............................................................. 347 vrefcon2 ............................................................. 348 wdtcon (watchdog timer control) ...................... 367 reset ............................................................................. 403 reset state of registers .................................................... 67 resets .............................................................................. 355 brown-out reset (bor) ........................................... 355 oscillator start-up timer (ost) ............................... 355 power-on reset (por) ............................................ 355 power-up timer (pwrt) ......................................... 355 retfie ............................................................................ 404 retlw ............................................................................ 404 return .......................................................................... 405 return address stack ........................................................ 70 return stack pointer (stkptr) ........................................ 71 revision history ............................................................... 545 rlcf ................................................................................ 405 rlncf ............................................................................. 406 rrcf ............................................................................... 406 rrncf ................. ........................................................... 407 s sec_idle mode ................................................................ 52 sec_run mode ................................................................ 48 setf ................................................................................ 407 shoot-through current ..................................................... 199 single-supply icsp programming. sleep ............................................................................. 408 sleep osc1 and osc2 pin states ...................................... 41 sleep mode ....................................................................... 51 slew rate ........................................................................ 153 slrcon register ........................................................... 158 software simulator (mplab sim) ................................... 425 spbrg ............................................................................ 280 spbrgh ......................................................................... 280 special event trigger ...................................................... 302 special function registers ................................................ 82 map ............................................................................ 83 spi mode (msspx) associated registers ............................................... 221 spi clock ................................................................. 217 sr latch associated registers ............................................... 341 effects of a reset .................................................... 337 srcon0 register ........................................................... 340 srcon1 register ........................................................... 341 sspxadd register .......................................................... 265 sspxcon1 register ....................................................... 260 sspxcon2 register ....................................................... 262 sspxmsk register .......................................................... 264 sspxov .......................................................................... 248 sspxov status flag ....................................................... 248 sspxstat register ........................................................ 259 r/w bit .................................................................... 227 stack full/underflow resets .............................................. 72 standard instructions ....................................................... 373 status register .............................................................. 89 stkptr register .............................................................. 72 subfsr .......................................................................... 419 subfwb ......................................................................... 408 sublw ............................................................................ 409 subulnk ........................................................................ 419 subwf ............................................................................ 409 subwfb ......................................................................... 410 swapf ............................................................................ 410 t t0con register .............................................................. 159 table pointer operations (table) ........................................ 98 table reads/table writes ................................................. 73 tblrd ............................................................................. 411 tblwt ............................................................................ 412 time-out in various situations (table) ................................ 64 timer0 ............................................................................. 159 associated registers ............................................... 161 operation ................................................................. 160 overflow interrupt .................................................... 161 prescaler ................................................................. 161 prescaler assignment (psa bit) .............................. 161 prescaler select (t0ps2:t0ps0 bits) ..................... 161 prescaler. see prescaler, timer0. reads and writes in 16-bit mode ............................ 160 source edge select (t0se bit) ............................... 160 source select (t0cs bit) ........................................ 160 switching prescaler assignment ............................. 161 timer1/3/5 ....................................................................... 163 associated registers ................................................ 174 asynchronous counter mode .................................. 165 reading and writing ........................................ 165 clock source selection ........................................... 164 interrupt ................................................................... 168 operation ................................................................. 164 operation during sleep ........................................... 168 oscillator .................................................................. 165 pic18(l)f2x/4xk22 ds41412f-page 554 ? 2010-2012 microchip technology inc. prescaler .................................................................. 165 timer1/3/5 gate selecting source .............................................. 166 tmrxh register ...................................................... 163 tmrxl register ....................................................... 163 timer2/4/6 ........................................................................ 175 associated registers ................................................. 178 timers timer1/3/5 txcon ............................................................. 172 txgcon .......................................................... 173 timer2/4/6 txcon ............................................................. 177 timing diagrams a/d conversion ........................................................ 464 acknowledge sequence .......................................... 250 asynchronous reception ......................................... 275 asynchronous transmission .................................... 270 asynchronous transmission (back to back) ........... 271 auto wake-up bit (wue) during normal operation 286 auto wake-up bit (wue) during sleep ................... 286 automatic baud rate calculator .............................. 285 baud rate generator with clock arbitration ............ 243 brg reset due to sda arbitration during start condition .......................................................... 254 brown-out reset (bor) ........................................... 452 bus collision during a repeated start condition (case 1) ........................................................... 255 bus collision during a repeated start condition (case 2) ........................................................... 255 bus collision during a start condition (scl = 0) .... 254 bus collision during a stop condition (case 1) ...... 256 bus collision during a stop condition (case 2) ...... 256 bus collision during start condition (sda only) ..... 253 bus collision for transmit and acknowledge ........... 252 capture/compare/pwm (ccp) ................................ 454 clko and i/o .......................................................... 451 clock synchronization ............................................. 240 clock/instruction cycle .............................................. 74 comparator output .................................................. 311 eusart synchronous receive (master/slave) ...... 463 eusart synchronous transmission (master/slave) .................................................. 463 example spi master mode (cke = 0) ..................... 455 example spi master mode (cke = 1) ..................... 456 example spi master mode timing .......................... 455 example spi slave mode (cke = 0) ....................... 457 example spi slave mode (cke = 1) ....................... 458 external clock (all modes except pll) .................... 449 fail-safe clock monitor (fscm) ................................ 45 first start bit timing ................................................ 244 full-bridge pwm output .......................................... 195 half-bridge pwm output ................................. 193, 199 high/low-voltage detect characteristics ................ 446 high-voltage detect operation (vdirmag = 1) ...... 352 i 2 c bus data ............................................................ 459 i 2 c bus start/stop bits ............................................. 458 i 2 c master mode (7 or 10-bit transmission) ........... 247 i 2 c master mode (7-bit reception) .......................... 249 i 2 c stop condition receive or transmit mode ........ 251 internal oscillator switch timing ................................ 43 low-voltage detect operation (vdirmag = 0) ....... 351 master ssp i 2 c bus data ........................................ 461 master ssp i 2 c bus start/stop bits ........................ 461 pwm auto-shutdown ............................................... 198 firmware restart ............................................. 198 pwm direction change ........................................... 196 pwm direction change at near 100% duty cycle .. 197 pwm output (active-high) ...................................... 191 pwm output (active-low) ....................................... 192 repeat start condition ............................................ 245 reset, watchdog timer (wdt), oscillator start-up timer (ost), power-up timer (pwrt) ........... 452 send break character sequence ............................ 287 slow rise time (mclr tied to v dd , v dd rise > t pwrt ) ............................................................... 65 spi mode (master mode) ......................................... 217 synchronous reception (master mode, sren) ...... 292 synchronous transmission ..................................... 289 synchronous transmission (through txen) .......... 289 time-out sequence on por w/pll enabled (mclr tied to v dd ) .......................................... 66 time-out sequence on power-up (mclr not tied to v dd , case 1) ................................... 64 time-out sequence on power-up (mclr not tied to v dd , case 2) ................................... 65 time-out sequence on power-up (mclr tied to v dd , v dd rise < t pwrt ) ....................... 64 timer0 and timer1 external clock .......................... 453 timer1/3/5 incrementing edge ................................ 169 transition for entry to sec_run mode .................... 49 transition for entry to sleep mode ............................ 51 transition for wake from sleep (hspll) .................. 52 transition from rc_run mode to pri_run mode .. 50 transition from sec_run mode to pri_run mode (hspll) ................................................... 49 transition timing for entry to idle mode .................... 52 transition timing for wake from idle to run mode ... 53 timing diagrams and specifications ............................... 449 a/d conversion requirements ................................ 464 capture/compare/pwm requirements ................... 455 clko and i/o requirements ................................... 451 eusart synchronous receive requirements ....... 463 eusart synchronous transmission requirements .................................................. 463 example spi mode requirements (master mode, cke = 0) .................................. 456 (slave mode, cke = 0) .................................... 457 external clock requirements .................................. 449 i 2 c bus data requirements (slave mode) .............. 460 i 2 c bus start/stop bits requirements (slave mode) ................................................... 459 master ssp i 2 c bus data requirements ................ 462 master ssp i 2 c bus start/stop bits requirements . 461 pll clock ................................................................ 450 reset, watchdog timer, oscillator start-up timer, power-up timer and brown-out reset requirements .................................................. 453 timer0 and timer1 external clock requirements ... 454 top-of-stack access .......................................................... 71 tstfsz ........................................................................... 413 two-speed clock start-up mode ....................................... 42 two-speed start-up ......................................................... 355 two-word instructions example cases .......................................................... 75 txcon (timer2/4/6) register .......................................... 177 txcon register .............................................................. 172 txgcon register ............................................................ 173 txreg ............................................................................ 269 txsta register ............................................................... 277 ? 2010-2012 microchip technology inc. ds41412f-page 555 pic18(l)f2x/4xk22 brgh bit ................................................................. 280 v voltage reference (vr) specifications ........................................................... 445 v ref . s ee adc reference voltage vrefcon0 register ....................................................... 344 vrefcon1 (digital-to-analog converter control 0) register .................................................................... 347 vrefcon2 (digital-to-analog converter control 1) register .................................................................... 348 w wake-up on break ........................................................... 285 watchdog timer (wdt) ........................................... 355, 366 associated registers ............................................... 367 control register ....................................................... 367 programming considerations .................................. 366 wcol ...................................................... 243, 246, 248, 250 wcol status flag ................................... 243, 246, 248, 250 wdtcon register .......................................................... 367 www address ................................................................. 557 www, on-line support .................................................... 12 x xorlw ............................................................................ 413 xorwf ............................................................................ 414 pic18(l)f2x/4xk22 ds41412f-page 556 ? 2010-2012 microchip technology inc. notes: ? 2010-2012 microchip technology inc. ds41412f-page 557 pic18(l)f2x/4xk22 the microchip web site microchip provides online support via our www site at www.microchip.com . this web site is used as a means to make files and information easily available to customers. accessible by using your favorite internet browser, the web site contains the following information: ? product support ? data sheets and errata, application notes and sample programs, design resources, user?s guides and hardware support documents, latest software releases and archived software ? general technical support ? frequently asked questions (faq), technical support requests, online discussion groups, microchip consultant program member listing ? business of microchip ? product selector and ordering guides, latest microchip press releases, listing of seminars and events, listings of microchip sales offices, distributors and factory representatives customer change notification service microchip?s customer notification service helps keep customers current on microchip products. subscribers will receive e-mail notification whenever there are changes, updates, revisions or errata related to a specified product family or development tool of interest. to register, access the microchip web site at www.microchip.com . under ?support?, click on ?customer change notification? and follow the registration instructions. customer support users of microchip products can receive assistance through several channels: ? distributor or representative ? local sales office ? field application engineer (fae) ? technical support ? development systems information line customers should contact their distributor, representative or field application engineer (fae) for support. local sales offices are also available to help customers. a listing of sales offices and locations is included in the back of this document. technical support is available through the web site at: http://microchip.com/support pic18(l)f2x/4xk22 ds41412f-page 558 ? 2010-2012 microchip technology inc. reader response it is our intention to provide you with the best documentation possible to ensure successful use of your microchip product. if you wish to provide your comments on organization, clarity, subject matter, and ways in which our documentation can better serve you, please fax your comments to the technical publications manager at (480) 792-4150. please list the following information, and use this outline to provide us with your comments about this document. to: technical publications manager re: reader response total pages sent ________ from: name company address city / state / zip / country telephone: (_______) _________ - _________ application (optional): would you like a reply? y n device: literature number: questions: fax: (______) _________ - _________ ds41412f pic18(l)f2x/4xk22 1. what are the best features of this document? 2. how does this document meet your hardware and software development needs? 3. do you find the organization of this document easy to follow? if not, why? 4. what additions to the document do you think would enhance the structure and subject? 5. what deletions from the document could be made without affecting the overall usefulness? 6. is there any incorrect or misleading information (what and where)? 7. how would you improve this document? ? 2010-2012 microchip technology inc. ds41412f-page 559 pic18(l)f2x/4xk22 product identification system to order or obtain information, e. g., on pricing or delivery, refer to the factory or the listed sales office . part no. x /xx xxx pattern package temperature range device device: pic18f46k22, pic18lf46k22 pic18f45k22, pic18lf45k22 pic18f44k22, pic18lf44k22 pic18f43k22, pic18lf43k22 pic18f26k22, pic18lf26k22 pic18f25k22, pic18lf25k22 pic18f24k22, pic18lf24k22 pic18f23k22, pic18lf23k22 tape and reel option: blank = standard packaging (tube or tray) t = tape and reel (1), (2) temperature range: e= -40 ? c to +125 ? c (extended) i= -40 ? c to +85 ? c (industrial) package: ml = qfn mv = uqfn p=pdip pt = tqfp (thin quad flatpack) so = soic sp = skinny plastic dip ss = ssop pattern: qtp, sqtp, code or special requirements (blank otherwise) examples: a) pic18(l)f45k22-e/p 301 = extended temp., pdip package, qtp pattern #301. b) pic18f46k22-i/so = industrial temp., soic package. c) pic18f46k22-e/p = extended temp., pdip package. d) pic18f46k22t-i/ml = tape and reel, industrial temp., qfn package. note 1: tape and reel option is available for ml, mv, pt, so and ss packages with industrial temperature range only. 2: tape and reel identifier only appears in catalog part number description. this identifier is used for ordering purposes and is not printed on the device package. [x] (2) tape and reel option - ds41412f-page 560 ? 2010-2012 microchip technology inc. americas corporate office 2355 west chandler blvd. chandler, az 85224-6199 tel: 480-792-7200 fax: 480-792-7277 technical support: http://www.microchip.com/ support web address: www.microchip.com atlanta duluth, ga tel: 678-957-9614 fax: 678-957-1455 boston westborough, ma tel: 774-760-0087 fax: 774-760-0088 chicago itasca, il tel: 630-285-0071 fax: 630-285-0075 cleveland independence, oh tel: 216-447-0464 fax: 216-447-0643 dallas addison, tx tel: 972-818-7423 fax: 972-818-2924 detroit farmington hills, mi tel: 248-538-2250 fax: 248-538-2260 indianapolis noblesville, in tel: 317-773-8323 fax: 317-773-5453 los angeles mission viejo, ca tel: 949-462-9523 fax: 949-462-9608 santa clara santa clara, ca tel: 408-961-6444 fax: 408-961-6445 toronto mississauga, ontario, canada tel: 905-673-0699 fax: 905-673-6509 asia/pacific asia pacific office suites 3707-14, 37th floor tower 6, the gateway harbour city, kowloon hong kong tel: 852-2401-1200 fax: 852-2401-3431 australia - sydney tel: 61-2-9868-6733 fax: 61-2-9868-6755 china - beijing tel: 86-10-8569-7000 fax: 86-10-8528-2104 china - chengdu tel: 86-28-8665-5511 fax: 86-28-8665-7889 china - chongqing tel: 86-23-8980-9588 fax: 86-23-8980-9500 china - hangzhou tel: 86-571-2819-3187 fax: 86-571-2819-3189 china - hong kong sar tel: 852-2401-1200 fax: 852-2401-3431 china - nanjing tel: 86-25-8473-2460 fax: 86-25-8473-2470 china - qingdao tel: 86-532-8502-7355 fax: 86-532-8502-7205 china - shanghai tel: 86-21-5407-5533 fax: 86-21-5407-5066 china - shenyang tel: 86-24-2334-2829 fax: 86-24-2334-2393 china - shenzhen tel: 86-755-8203-2660 fax: 86-755-8203-1760 china - wuhan tel: 86-27-5980-5300 fax: 86-27-5980-5118 china - xian tel: 86-29-8833-7252 fax: 86-29-8833-7256 china - xiamen tel: 86-592-2388138 fax: 86-592-2388130 china - zhuhai tel: 86-756-3210040 fax: 86-756-3210049 asia/pacific india - bangalore tel: 91-80-3090-4444 fax: 91-80-3090-4123 india - new delhi tel: 91-11-4160-8631 fax: 91-11-4160-8632 india - pune tel: 91-20-2566-1512 fax: 91-20-2566-1513 japan - osaka tel: 81-66-152-7160 fax: 81-66-152-9310 japan - yokohama tel: 81-45-471- 6166 fax: 81-45-471-6122 korea - daegu tel: 82-53-744-4301 fax: 82-53-744-4302 korea - seoul tel: 82-2-554-7200 fax: 82-2-558-5932 or 82-2-558-5934 malaysia - kuala lumpur tel: 60-3-6201-9857 fax: 60-3-6201-9859 malaysia - penang tel: 60-4-227-8870 fax: 60-4-227-4068 philippines - manila tel: 63-2-634-9065 fax: 63-2-634-9069 singapore tel: 65-6334-8870 fax: 65-6334-8850 taiwan - hsin chu tel: 886-3-5778-366 fax: 886-3-5770-955 taiwan - kaohsiung tel: 886-7-536-4818 fax: 886-7-330-9305 taiwan - taipei tel: 886-2-2500-6610 fax: 886-2-2508-0102 thailand - bangkok tel: 66-2-694-1351 fax: 66-2-694-1350 europe austria - wels tel: 43-7242-2244-39 fax: 43-7242-2244-393 denmark - copenhagen tel: 45-4450-2828 fax: 45-4485-2829 france - paris tel: 33-1-69-53-63-20 fax: 33-1-69-30-90-79 germany - munich tel: 49-89-627-144-0 fax: 49-89-627-144-44 italy - milan tel: 39-0331-742611 fax: 39-0331-466781 netherlands - drunen tel: 31-416-690399 fax: 31-416-690340 spain - madrid tel: 34-91-708-08-90 fax: 34-91-708-08-91 uk - wokingham tel: 44-118-921-5869 fax: 44-118-921-5820 worldwide sales and service 11/29/11 |
Price & Availability of PIC1845K50 |
|
|
All Rights Reserved © IC-ON-LINE 2003 - 2022 |
[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy] |
Mirror Sites : [www.datasheet.hk]
[www.maxim4u.com] [www.ic-on-line.cn]
[www.ic-on-line.com] [www.ic-on-line.net]
[www.alldatasheet.com.cn]
[www.gdcy.com]
[www.gdcy.net] |