![]() |
|
If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
Datasheet File OCR Text: |
? 2016 microchip technology inc. preliminary ds40001839a-page 1 pic16(l)f18326/18346 description pic16(l)f18326/18346 microcontrollers feature analog, core independent peripherals and communication peripherals, combined with extreme low power (xlp) for a wide range of general purpose and low-power a pplications. the peripheral pin select (pps) functionality enables pin mapping when using the digital peripherals (clc, cwg, ccp, pwm and communications) to add flexibility to the application design. core features c compiler optimized risc architecture only 49 instructions operating speed: - dc C 32 mhz clock input - 125 ns minimum instruction cycle interrupt capability 16-level deep hardware stack up to four 8-bit timers up to three 16-bit timers low-current power-on reset (por) configurable power-up timer (pwrte) brown-out reset (bor) with fast recovery low-power bor (lpbor) option extended watchdog timer (wdt) with dedicated on-chip oscillator for reliable operation programmable code protection memory 28 kbytes program flash memory 2 kb data sram memory 256b of eeprom direct, indirect and relative addressing modes operating characteristics operating voltage range: - 1.8v to 3.6v (pic16lf18326/18346) - 2.3v to 5.5v (pic16f18326/18346) temperature range: - industrial: -40c to 85c - extended: -40c to 125c extreme low-power (xlp) features sleep mode: 40 na @ 1.8v, typical watchdog timer: 250 na @ 1.8v, typical secondary oscillator: 300 na @ 32 khz operating current: -8 ? a @ 32 khz, 1.8v, typical -37 ? a/mhz @ 1.8v, typical power-saving functionality idle mode: ability to put the cpu core to sleep while internal peripherals continue operating from the system clock doze mode: ability to run the cpu core slower than the system clock used by the internal peripherals sleep mode: lowest power consumption peripheral module disable (pmd): peripheral power disable hardware module to minimize power consumption of unused peripherals digital peripherals configurable logic cell (clc): - four clcs - integrated combinational and sequential logic complementary waveform generator (cwg): - two cwgs - rising and falling edge dead-band control - full-bridge, half-bridge, 1-channel drive - multiple signal sources capture/compare/pwm (ccp) modules: - four ccps - 16-bit resolution for capture/compare modes - 10-bit resolution for pwm mode pulse-width modulators (pwm): -two 10-bit pwms numerically controlled oscillator (nco): - precision linear frequency generator (@50% duty cycle) with 0.0001% step size of source input clock - input clock: 0 hz < f nco < 32 mhz - resolution: f nco /2 20 serial communications: - eusart - rs-232, rs-485, lin compatible - auto-baud detect, auto-wake-up on start - master synchronous serial port (mssp) - spi -i 2 c, smbus, pmbus? compatible data signal modulator (dsm): - modulates a carrier signal with digital data to create custom carrier synchronized output waveforms full-featured, low pin count microcontrollers with xlp downloaded from: http:///
pic16(l)f18326/18346 ds40001839a-page 2 preliminary ? 2016 microchip technology inc. up to 18 i/o pins: - individually programmable pull-ups - slew rate control - interrupt-on-change with edge-select - input level selection control (st or ttl) - digital open-drain enable peripheral pin select (pps): - i/o pin remapping of digital peripherals timer modules: -timer0: - 8/16-bit timer/counter - synchronous or asynchronous operation - programmable prescaler/postscaler - time base for capture/compare function - timer1/3/5 with gate control: - 16-bit timer/counter - programmable internal or external clock sources - multiple gate sources - multiple gate modes - time base for capture/compare function - timer2/4/6: - 8-bit timers - programmable prescaler/postscaler - time base for pwm function analog peripherals 10-bit analog-to-digital converter (adc): - 17 external channels - conversion available during sleep comparator: - two comparators - fixed voltage reference at non-inverting input(s) - comparator outputs externally accessible 5-bit digital-to-analog converter (dac): - 5-bit resolution, rail-to-rail - positive reference selection - unbuffered i/o pin output - internal connections to adcs and comparators voltage reference: - fixed voltage reference with 1.024v, 2.048v and 4.096v output levels flexible oscillator structure high-precision internal oscillator: - software-selectable frequency range up to 32 mhz - 1% at nominal 4 mhz calibration point 4x pll with external sources low-power internal 31 khz oscillator (lfintosc) external low-power 32 khz crystal oscillator (sosc) external oscillator block with: - three crystal/resonator modes up to 20 mhz - three external clock modes up to 20 mhz - fail-safe clock monitor - allows for safe shutdown if peripheral clock stops - oscillator start-up timer (ost) - ensures stability of crystal oscillator sources downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 3 pic16(l)f18326/18346 table 1: pic16(l)f183xx family types device data sheet index program memory (kb) program memory (kw) eeprom (b) ram (b) i/os (1) 10-bit adcs comparators 5-bit dac timers 0/1/2 ccp/pwm cwg eusart spi i 2 c clc nco pps icd (2) pic16(l)f18313 ( a ) 3.5 2 256 256 6 5 1 1 1/1/1 2/2 1 1 1 1 2 1 y i pic16(l)f18323 ( a ) 3.5 2 256 256 12 11 2 1 1/1/1 2/2 1 1 1 1 2 1 y i pic16(l)f18324 ( b ) 7 4 256 512 12 11 2 1 1/3/3 4/2 2 1 1 1 4 1 y i pic16(l)f18325 ( c ) 14 8 256 1k 12 11 2 1 1/3/3 4/2 2 1 2 2 4 1 y i pic16(l)f18326 ( d ) 28 16 256 2k 12 11 2 1 1/3/3 4/2 2 1 2 2 4 1 y i pic16(l)f18344 ( b ) 7 4 256 512 18 17 2 1 1/3/3 4/2 2 1 1 1 4 1 y i pic16(l)f18345 ( c ) 14 8 256 1k 18 17 2 1 1/3/3 4/2 2 1 2 2 4 1 y i pic16(l)f18346 ( d ) 28 16 256 2k 18 17 2 1 1/3/3 4/2 2 1 2 2 4 1 y i note 1: one pin is input-only. 2: debugging methods: (i) C integrated on chip; e C using emulation header. data sheet index: (unshaded devices are described in this document.) note a: ds40001799 pic16(l)f18313/18323 data sheet,full-featured , low pin count microcontrollers with xlp b: ds40001800 pic16(l)f18324/18344 data sheet,full-featured, low pin count microcontrollers with xlp c: ds40001795 pic16(l)f18325/18345 data sheet,full-featured, low pin count microcontrollers with xlp d: ds40001839 pic16(l)f18326/18346 data sheet,full-featured, low pin count microcontrollers with xlp note: for other small form-factor package availability and marking information, please visit http://www.microchip.com/packaging or contact your local sales office. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 4 preliminary ? 2016 microchip technology inc. pin diagrams figure 1: 14-pin pdip, soic, tssop figure 2: 16-pin uqfn (4x4) figure 3: 20-pin pdip, soic, ssop 12 3 4 5 6 7 v dd ra5 ra4 v pp /mclr /ra3 rc5rc4 rc3 ra0/icspdat ra1/icspclk ra2 rc0 rc1 rc2 1413 12 11 10 9 8 v ss pic16(l)f18326 note: see table 2 for location of all peripheral functions. 23 1 9 10 11 12 rc4 4 v ss ra0/icspdat ra1/icspclk ra2 rc0 nc nc v dd ra5 ra4 ra3/mclr /v pp rc5 rc3 rc2 rc1 67 58 1514 16 13 pic16(l)f18326 note 1: see table 2 for location of all peripheral functions. 2: it is recommended that the exposed bottom pad be connected to v ss , but must not be the main v ss connection to the device. pic16(l)f18346 23 4 5 6 7 89 10 v dd ra5 ra4 mclr /v pp /ra3 rc5rc4 rc3 rc6 rc7 rb7 ra0 ra1 ra2 rc0 rc1 rc2 rb4 rb5 rb6 2019 18 17 16 15 14 1312 11 v ss 1 note: see table 3 for location of all peripheral functions. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 5 pic16(l)f18326/18346 figure 4: 20-pin uqfn (4x4) 23 4 5 1 67 8 9 20 19 18 17 16 10 12 13 14 15 11 pic16(l)f18346 rb4 rb5 rb6 rb7 rc7 m clr /v pp /ra3 rc5rc4 rc3 rc6 ra4 ra5 v dd v ss ra0 ra1 ra2 rc0 rc1 rc2 note 1: see ta b l e 3 for location of all peripheral functions. 2: it is recommended that the exposed bottom pad be connected to v ss , but must not be the main v ss connection to the device. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 6 preliminary ? 2016 microchip technology inc. pin allocation tables table 2: 14/16-pin allocation table (pic16(l)f18326) i/o (2) 14-pin pdip/soic/tssop 16-pin uqfn adc reference comparator nco dac dsm timers ccp pwm cwg mssp eusart clc clkr interrupt pull-up basic ra0 13 12 ana0 c1in0+ dac1out ss2 (1) ioc y icddat/ icspdat ra1 12 11 ana1 v ref + c1in0- c2in0- d a c 1 ref + ioc y icdclk/ icspclk ra2 11 10 ana2 v ref - dac1 ref - t0cki (1) ccp3 (1) cwg1in (1) cwg2in (1) int (1) ioc y ra3 4 3 ioc y mclr v pp ra4 3 2 ana4 t1g (1) sosco ioc y clkout osc2 ra5 2 1 ana5 t1cki (1) soscin sosci clcin3 (1) ioc y clkin osc1 rc0 10 9 anc0 c2in0+ t5cki (1) sck1 (1) scl1 (1,3,4) ioc y rc1 9 8 anc1 c1in1- c2in1- ccp4 (1) s d i 1 (1) sda1 (1,3,4) clcin2 (1) ioc y rc2 8 7 anc2 c1in2- c2in2- mdcin1 (1) ioc y rc3 7 6 anc3 c1in3- c2in3- m d m i n (1) t5g (1) ccp2 (1) ss1 (1) clcin0 (1) ioc y rc4 6 5 anc4 t3g (1) sck2 (1) scl2 (1,3,4) ? clcin1 (1) ioc y rc5 5 4 anc5 m d c i n 2 (1) t3cki (1) ccp1 (1) s d i 2 (1) sda2 (1,3,4) rx (1) dt (1,3) ioc y v dd 1 16 v dd note 1: default peripheral input. input can be moved to any other pin wit h the pps input selection registers. 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selectio n registers. 3: these peripheral functions are bidirectional. the output pin selections must be the same as the input pin select ions. 4: these pins are configured for i 2 c logic levels; clock and data signals may be assigned to any of these pins. assignments to th e other pins (e.g., ra5) will ope rate, but logic levels will be standard ttl/ st as selected by the inlvl register. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 7 pic16(l)f18326/18346 v ss 14 13 v ss out (2) c1out nco1 dsm tmr0 ccp1 pwm5 cwg1a cwg2a sda1 (3) sda2 (3) ck clc1out clkr c2out ccp2 pwm6 cwg1b cwg2b scl1 (3) scl2 (3) dt (3) clc2out ccp3 cwg1c cwg2c sdo1 sdo2 tx clc3out ccp4 cwg1d cwg2d sck1 sck2 clc4out table 2: 14/16-pin allocation table (pic16(l)f18326) (continued) i/o (2) 14-pin pdip/soic/tssop 16-pin uqfn adc reference comparator nco dac dsm timers ccp pwmcwg mssp eusart clc clkr interrupt pull-up basic note 1: default peripheral input. input can be moved to any other pin wit h the pps input selection registers. 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selectio n registers. 3: these peripheral functions are bidirectional. the output pin selections must be the same as the input pin select ions. 4: these pins are configured for i 2 c logic levels; clock and data signals may be assigned to any of these pins. assignments to th e other pins (e.g., ra5) will ope rate, but logic levels will be standard ttl/ st as selected by the inlvl register. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 8 preliminary ? 2016 microchip technology inc. table 3: 20-pin allocation table (pic16(l)f18346) i/o (2) 20-pin pdip/soic/ssop 20-pin uqfn adc reference comparator nco dac dsm timers ccp pwmcwg mssp eusart clc clkr interrupt pull-up basic ra0 19 16 ana0 c1in0+ dac1out ioc y icddat icspdat ra1 18 15 ana1 v ref + c1in0- c2in0- d a c 1 ref + ? s s 2 ioc y icdclk icspclk ra2 17 14 ana2 v ref - dac1 ref - t0cki (1) ccp3 (1) cwg1in (1) cwg2in (1) clcin0 (1) ioc int (1) y ra3 4 1 ioc y mclr v pp ra4 3 20 ana4 t1g (1) t3g (1) t5g (1) sosco ccp4 (1) ioc y clkout osc2 ra5 2 19 ana5 t1cki (1) t3cki (1) t5cki (1) soscin sosci ioc y clkin osc1 rb4 13 10 anb4 sdi1 (1) sda1 (1,3,4) clcin2 (1) ioc y rb5 12 9 anb5 sdi2 (1) sda2 (1,3,4) rx (1) dt (1) clcin3 (1) ioc y rb6 11 8 anb6 sck1 (1) scl1 (1,3,4) ioc y rb7 10 7 anb7 sck2 (1) scl2 (1,3,4) ioc y rc0 16 13 anc0 c2in0+ ioc y rc1 15 12 anc1 c1in1- c2in1- ioc y rc2 14 11 anc2 c1in2- c2in2- mdcin1 (1) ioc y note 1: default peripheral input. input can be moved to any other pin wit h the pps input selection registers. 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selectio n registers. 3: these peripheral functions are bidirectional. the output pin selections must be the same as the input pin select ions. 4: these pins are configured for i 2 c logic levels; clock and data signals may be assigned to any of these pins. assignment s to other pins (e.g., ra5) will operate , but logic levels will be standard ttl/st as selected by the inlvl register. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 9 pic16(l)f18326/18346 rc3 7 4 anc3 c1in3- c2in3- m d m i n (1) ccp2 (1) clcin1 (1) ioc y rc4 6 3 anc4 ioc y rc5 5 2 anc5 mdcin2 (1) ccp1 (1) ioc y rc6 8 5 anc6 ss 1 (1) ioc y rc7 9 6 anc7 ioc y v dd 1 18 v dd v ss 20 17 v ss out (2) c1out nco1 dsm tmr0 ccp1 pwm5 cwg1a cwg2a sdo1 sdo2 dt (3) clc1out clkr c2out ccp2 pwm6 cwg1b cwg2b sck1 sck2 ck clc2out ccp3 cwg1c cwg2c scl1 (3) scl2 (3) tx clc3out ccp4 cwg1d cwg2d sda1 (3) sda2 (3) clc4out table 3: 20-pin allocation table (pic16(l)f18346) (continued) i/o (2) 20-pin pdip/soic/ssop 20-pin uqfn adc reference comparator nco dac dsm timers ccp pwmcwg mssp eusart clc clkr interrupt pull-up basic note 1: default peripheral input. input can be moved to any other pin wit h the pps input selection registers. 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selectio n registers. 3: these peripheral functions are bidirectional. the output pin selections must be the same as the input pin select ions. 4: these pins are configured for i 2 c logic levels; clock and data signals may be assigned to any of these pins. assignment s to other pins (e.g., ra5) will operate , but logic levels will be standard ttl/st as selected by the inlvl register. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 10 preliminary ? 2016 microchip technology inc. table of contents 1.0 device overview ............................................................................ ................................. ........................................................... 11 2.0 enhanced mid-range cpu .......................................... ................................................... ......... .................................................. 22 3.0 memory organization .......................................... ................................................... ............ ........................................................ 24 4.0 device configuration .................................................................... .................................... .......................................................... 59 5.0 resets .......................................................................................... .............................................................................................. 66 6.0 oscillator module (with fail-safe clock monitor) .............. ....................................................................................................... .. 74 7.0 interrupts .................................................................................... ................................................................................................ 93 8.0 power-saving operation modes ....................................... ................................................... ...... .............................................. 110 9.0 watchdog timer (wdt) ........................................ ................................................... ............. ................................................... 116 10.0 nonvolatile memory (nvm) control....................... .................................................................... ............................................... 120 11.0 i/o ports ....................................................................................... ........................... ................................................................. 138 12.0 peripheral pin select (pps) module ....................................... .................................................. ............................................... 158 13.0 peripheral module disable ........................................... ................................................... .... ..................................................... 164 14.0 interrupt-on-change ............................................ ................................................... ......... ......................................................... 170 15.0 fixed voltage reference (fvr) ....................................... ................................................... ... ................................................. 177 16.0 temperature indicator module ........................................ ................................................... .... .................................................. 180 17.0 comparator module......................................... ................................................... .............. ........................................................ 182 18.0 pulse-width modulation (pwm) ................................... ................................................... ......... ................................................ 191 19.0 complementary waveform generator (cwg) module ........... ................................................................... .............................. 197 20.0 configurable logic cell (clc)........................................ ................................................... ... .................................................... 219 21.0 analog-to-digital converter (adc) module ..................................... .............................................. ........................................... 234 22.0 numerically controlled oscillator (nco1) module ................................. ........................................... ....................................... 248 23.0 5-bit digital-to-analog converter (dac1) module ....................................... ..................................... ........................................ 259 24.0 data signal modulator (dsm) module........................................ ................................................. ............................................. 263 25.0 timer0 module .............................................. ................................................... ............. ........................................................... 274 26.0 timer1/3/5 module with gate control.................................... ................................................... . ............................................... 281 27.0 timer 2/4/6 module .......................................... ................................................... ............ ......................................................... 294 28.0 capture/compare/pwm modules ........................................ ................................................... ..... ............................................ 299 29.0 master synchronous serial port (msspx) module ............................... ............................................... .................................... 312 30.0 enhanced universal synchronous asynchronous receiver transmitter (eusart1) ........................................ ......... ............ 365 31.0 reference clock output module ...................................... ................................................... ..... ................................................ 390 32.0 in-circuit serial programming? (icsp?) .......................... ........................................................... .......................................... 393 33.0 instruction set summary ....................................................... ............................................. ...................................................... 395 34.0 electrical specifications........................................... ....................................................... .......................................................... 409 35.0 dc and ac characteristics graphs and charts ................................... ............................................. ....................................... 439 36.0 development support............................................ ................................................... ......... ....................................................... 440 37.0 packaging information............................................. ......................................................... ........................................................ 444 appendix a: data sheet revision history.................................... ................................................... . .................................................. 467 the microchip website.................................................................... ...................................... ............................................................. 468 customer change notification service ....................................... ................................................... . ................................................... 468 customer support ............................................... ................................................... ............. ............................................................... 468 product identification system.................................................. ................................................................................................. .......... 469 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 11 pic16(l)f18326/18346 to our valued customers it is our intention to provide our valued customers with the best 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 regar ding this publication, please contact the marketing communications department via e-mail at docerrors@microchip.com . we welcome your feedback. most current data sheet to obtain the most up-to-date version of this data sheet, please register at our worldwide website 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 versi on number, (e.g., ds30000000a is ve rsion a of document ds30000000). 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: microchips worldwide website; 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 website at www.microchip.com to receive the most current information on all of our products. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 12 preliminary ? 2016 microchip technology inc. 1.0 device overview the pic16(l)f18326/18346 devices are described within this data sheet. pic16(l)f18326 are available in 14-pin pdip, soic, tssop and 16-pin uqfn packages. pic16(l)f18346 are available in 20-pin pdip, soic, ssop and uqfn packages. see section 37.0 ?packaging information? for further packaging information. figure 1-1 shows a block diagram of the pic16(l)f18326/18346 devices. table 1-2 shows the pinout descriptions. reference table 1-1 for peripherals available per device. table 1-1: device peripheral summary peripheral pic16(l)f18326 pic16(l)f18346 analog-to-digital converter (adc) temperature indicator digital-to-analog converter (dac) dac1 fixed voltage reference (fvr) adcfvr cdafvr digital signal modulator (dsm) dsm1 numerically controlled oscillator (nco) nco1 capture/compare/pwm (ccp/eccp) modules ccp1 ccp2 ccp3 ccp4 comparators c1 c2 complementary waveform generator (cwg) cwg1 cwg2 configurable logic cell (clc) clc1 clc2 clc3 clc4 enhanced universal synchronous/asynchronous receiver/transmit- ter (eusart) eusart1 master synchronous serial port (mssp) mssp1 mssp2 pulse-width modulator (pwm) pwm5 pwm6 timers timer0 timer1 timer2 timer3 timer4 timer5 timer6 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 13 pic16(l)f18326/18346 figure 1-1: pic16(l)f18326/18346 block diagram porta portc cpu program flash memory ram timing generation lfintosc oscillator mclr clkin clkout adc 10-bit fvr te m p . indicator eusart1 comparators mssp1/2 timer2/4/6 timer1/3/5 timer0 dac ccps pwms nco1 hfintosc/ clcs cwg1/2 dsm note 1: pic16(l)f18346 only. portb (1) see figure 2-1 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 14 preliminary ? 2016 microchip technology inc. table 1-2: pic16(l)f18326 pinout description name function input type output type description ra0/ana0/c1in0+/dac1out/ ss2 (1) / icddat/icspdat ra0 ttl/st cmos general purpose i/o. ana0 an D adc channel a0 input. c1in0+ an D comparator c1 positive input. dac1out D an digital-to-analog converter output. ss2 ttl/st D slave select 2 input. icddat ttl/st cmos in-circuit debug data i/o. icspdat ttl/st cmos icsp? data i/o. ra1/ana1/v ref +/c1in0-/ c2in0-/dac1 ref +/ icdclk/ icspclk ra1 ttl/st cmos general purpose i/o. ana1 an D adc channel a1 input. v ref +a n D adc positive voltage reference input. c1in0- an comparator c1 negative input. c2in0- an D comparator c2 negative input. dac1 ref + D an digital-to-analog converter positive reference input. icdclk ttl/st cmos in-circuit debug clock i/o. icspclk ttl/st cmos icsp? clock i/o. ra2/ana2/v ref -/ dac1 ref -/ t0cki (1) / ccp3 (1) /cwg1in (1) / cwg2in (1) /int (1) ra2 ttl/st cmos general purpose i/o. ana2 an D adc channel a2 input. v ref -a n D adc negative voltage reference input. dac1 ref - D an digital-to-analog converter negative reference input. t0cki ttl/st D timer0 clock input. ccp3 ttl/st cmos capture/compare/pwm 3 input. cwg1in ttl/st D complementary waveform generator 1 input. cwg2in ttl/st D complementary waveform generator 2 input. int ttl/st D external interrupt input. ra3/mclr /v pp ra3 ttl/st cmos general purpose i/o. mclr ttl/st D master clear with internal pull-up. v pp hv D programming voltage. ra4/ana4/t1g (1) / sosco/ clkout/osc2 ra4 ttl/st cmos general purpose i/o. ana4 an D adc channel a4 input. t1g st D timer1 gate input. sosco D xtal secondary oscillator connection. clkout D cmos f osc /4 output. osc2 D xtal crystal/resonator (lp, xt, hs modes). legend: an = analog input or output cmos =cmos compatible input or output od = open-drain ttl = ttl compatible input st =schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal =crystal levels note 1: default peripheral input. input can be moved to any other pin with the pps input selection registers. see register 12-1 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 15 pic16(l)f18326/18346 ra5/ana5/t1cki (1) / soscin/ sosci/ clcin3 (1) /clkin/ osc1 ra5 ttl/st cmos general purpose i/o. ana5 an D adc channel a5 input. t1cki ttl/st D timer1 clock input. soscin ttl/st D secondary oscillator input connection. sosci xtal D secondary oscillator connection. clcin3 ttl/st D configurable logic cell 3 input. clkin ttl/st D external clock input. osc1 xtal D crystal/resonator (lp, xt, hs modes). rc0/anc0/c2in0+/ t5cki (1) / sck1 (1) / scl1 (1,3) rc0 ttl/st cmos general purpose i/o. anc0 an D adc channel c0 input. c2in0+ an D comparator c2 positive input. t5cki ttl/st D timer5 clock input. sck1 ttl/st cmos spi clock 1. scl1 i 2 co di 2 c clock 1. rc1/anc1/c1in1-/c2in1-/ ccp4 (1) /sdi1 (1) / sda1 (1,3) / clcin2 (1) rc1 ttl/st cmos general purpose i/o. anc1 an D adc channel c1 input. c1in1- an D comparator c1 negative input. c2in1- an D comparator c2 negative input. ccp4 ttl/st cmos capture/compare/pwm 4 input. sdi1 ttl/st cmos spi data input 1. sda1 i 2 co di 2 c data 1. clcin2 ttl/st D configurable logic cell 2 input. rc2/anc2/c1in2-/c2in2-/ mdcin1 (1) rc2 ttl/st cmos general purpose i/o. anc2 an D adc channel c2 input. c1in2- an D comparator c1 negative input. c2in2- an D comparator c2 negative input. mdcin1 ttl/st D modular carrier input 1. rc3/anc3/c1in3-/c2in3-/ mdmin (1) /t5g (1) / ccp2 (1) / ss1 (1) /clcin0 (1) rc3 ttl/st cmos general purpose i/o. anc3 an D adc channel c3 input. c1in3- an D comparator c1 negative input. c2in3- an D comparator c2 negative input. mdmin ttl/st D modular source input. t5g ttl/st D timer5 gate input. ccp2 ttl/st cmos capture/compare/pwm 2 input. ss1 ttl/st D slave select 1 input. clcin0 ttl/st D configurable logic cell 0 input. table 1-2: pic16(l)f18326 pinout description (continued) name function input type output type description legend: an = analog input or output cmos =cmos compatible input or output od = open-drain ttl = ttl compatible input st =schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal =crystal levels note 1: default peripheral input. input can be moved to any ot her pin with the pps input selection registers. see register 12-1 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 16 preliminary ? 2016 microchip technology inc. rc4/anc4/t3g (1) / sck2 (1) / scl2 (1,3) / clcin1 (1) rc4 ttl/st cmos general purpose i/o. anc4 an D adc channel c4 input. t3g ttl/st D timer3 gate input. sck2 ttl/st cmos spi clock 2. scl2 i 2 co di 2 c clock 2. clcin1 ttl/st D configurable logic cell 1 input. rc5/anc5/mdcin2 (1) / t3cki (1) /ccp1 (1) /sdi2 (1) / sda2 (1,3) /rx (1) / dt rc5 ttl/st cmos general purpose i/o. anc5 an D adc channel c5 input. mdcin2 ttl/st D modular carrier input 2. t3cki ttl/st D timer3 clock input. ccp1 ttl/st cmos capture/compare/pwm 1 input. sdi2 ttl/st cmos spi data 2. sda2 i 2 co di 2 c data 2. rx ttl/st cmos eusart asynchronous input. dt ttl/st cmos eusart synchronous data output. v dd v dd power D positive supply. v ss v ss power D ground reference. table 1-2: pic16(l)f18326 pinout description (continued) name function input type output type description legend: an = analog input or output cmos =cmos compatible input or output od = open-drain ttl = ttl compatible input st =schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal =crystal levels note 1: default peripheral input. input can be moved to any other pin with the pps input selection registers. see register 12-1 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 17 pic16(l)f18326/18346 out (2) c1 D cmos comparator c1 output. c2 D cmos comparator c2 output. nco1 D cmos numerically controlled oscillator output. dsm D cmos digital signal modulator output. tmr0 D cmos timer0 clock output. ccp1 D cmos capture/compare/pwm 1 output. ccp2 D cmos capture/compare/pwm 2 output. ccp3 D cmos capture/compare/pwm 3 output. ccp4 D cmos capture/compare/pwm 4 output. pwm5 D cmos pulse-width modulator 5 output. pwm6 D cmos pulse-width modulator 6 output. cwg1a D cmos complementary waveform generator 1 output a. cwg2a D cmos complementary waveform generator 2 output a. cwg1b D cmos complementary waveform generator 1 output b. cwg2b D cmos complementary waveform generator 2 output b. cwg1c D cmos complementary waveform generator 1 output c. cwg2c D cmos complementary waveform generator 2 output c. cwg1d D cmos complementary waveform generator 1 output d. cwg2d D cmos complementary waveform generator 2 output d. sda1 (3) i 2 co di 2 c data output. sda2 (3) i 2 co di 2 c data output. scl1 (3) i 2 co di 2 c clock output. scl2 (3) i 2 co di 2 c clock output. sdo1 D cmos spi1 data output. sd02 D cmos spi2 data output. sck1 D cmos spi1 clock output. sck2 D cmos spi2 clock output. tx/ck D cmos asynchronous tx data/synchronous clock output. dt (3) D cmos eusart synchronous data output. clc1out D cmos configurable logic cell 1 source output. clc2out D cmos configurable logic cell 2 source output. clc3out D cmos configurable logic cell 3 source output. clc4out D cmos configurable logic cell 4 source output. clkr D cmos clock reference output. table 1-2: pic16(l)f18326 pinout description (continued) name function input type output type description legend: an = analog input or output cmos =cmos compatible input or output od = open-drain ttl = ttl compatible input st =schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal =crystal levels note 1: default peripheral input. input can be moved to any ot her pin with the pps input selection registers. see register 12-1 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 18 preliminary ? 2016 microchip technology inc. table 1-3: pic16(l)f18346 pinout description name function input type output type description ra0/ana0/c1in0+/dac1out/ icddat/icspdat ra0 ttl/st cmos general purpose i/o. ana0 an D adc channel a0 input. c1in0+ an D comparator c1 positive input. dac1out D an digital-to-analog converter output. icddat ttl/st cmos in-circuit debug data i/o. icspdat ttl/st cmos icsp? data i/o. ra1/ana1/v ref +/c1in0-/ c2in0-/ dac1 ref +/ss2 (1) )/ icdclk/ icspclk ra1 ttl/st cmos general purpose i/o. ana1 an D adc channel a1 input. v ref +a n D adc positive voltage reference input. c1in0- an comparator c1 negative input. c2in0- an D comparator c2 negative input. dac1 ref +a n D digital-to-analog converter positive reference input. ss2 ttl/st D slave select 2 input. icdclk ttl/st cmos in-circuit debug clock i/o. icspclk ttl/st cmos icsp tm clock i/o. ra2/ana2/v ref -/ dac1 ref -/ t0cki (1) / ccp3 (1) /cwg1in (1) / cwg2in (1) /clcin0 (1) / int (1) ra2 ttl/st cmos general purpose i/o. ana2 an D adc channel a2 input. v ref -a n D adc negative voltage reference input. dac1 ref -a n D digital-to-analog converter negative reference input. t0cki ttl/st D timer0 clock input. ccp3 ttl/st cmos capture/compare/pwm 3 input. cwg1in ttl/st D complementary waveform generator 1 input. cwg2in ttl/st D complementary waveform generator 2 input. clcin0 ttl/st D configurable logic cell 0 input. int ttl/st D external interrupt input. ra3/mclr /v pp ra3 ttl/st cmos general purpose i/o. mclr ttl/st D master clear with internal pull-up. v pp hv D programming voltage. ra4/ana4/t1g(1)/t3g (1) / t5g (1) /sosco/ccp4 (1) / clkout/osc2 ra4 ttl/st cmos general purpose i/o. ana4 an D adc channel a4 input. t1g ttl/st D timer1 gate input. t3g ttl/st D timer3 gate input. t5g ttl/st D timer5 gate input. sosco D xtal secondary oscillator connection. ccp4 ttl/st cmos capture/compare/pwm 4 input. clkout D cmos fosc/4 output. osc2 D xtal crystal/resonator (lp, xt, hs modes). legend: an = analog input or output cmos= cmos compatible input or output od = open-drain ttl = ttl compatible input st = schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal = crystal levels note 1: default peripheral input. input can be moved to any other pin with the pps input selection registers. see register 12-2 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 19 pic16(l)f18326/18346 ra5/ana5/t1cki (1) / t3cki (1) / t5cki (1) / soscin/sosci/ clkin/osc1 ra5 ttl/st cmos general purpose i/o. ana5 an D adc channel a5 input. t1cki ttl/st D timer1 clock input. t3cki ttl/st D timer3 clock input. t5cki ttl/st D timer5 clock input. soscin ttl/st D secondary oscillator input connection. sosci xtal D secondary oscillator connection. clkin ttl/st D external clock input. osc1 xtal D crystal/resonator (lp, xt, hs modes). rb4/anb4/sdi1 (1) / sda1 (1,3) / clcin2 (1) rb4 ttl/st cmos general purpose i/o. anb4 an D adc channel b4 input. sdi1 ttl/st cmos spi data input 1. sda1 i 2 co di 2 c data 1. clcin2 ttl/st D configurable logic cell 2 input. rb5/anb5/sdi2 (1) / sda2 (1,3) / rx (1) /dt/clcin3 (1) rb5 ttl/st cmos general purpose i/o. anb5 an D adc channel b5 input. sdi2 ttl/st cmos spi data input 2. sda2 i 2 co di 2 c data 2. rx ttl/st cmos eusart asynchronous input. dt ttl/st cmos eusart synchronous data output. clcin3 ttl/st D configurable logic cell 3 input. rb6/anb6/sck1 (1) / scl1 (1,3) rb6 ttl/st cmos general purpose i/o. anb6 an D adc channel b6 input. sck1 ttl/st cmos spi clock 1. scl1 i 2 co di 2 c clock 1. rb7/anb7/sck2 (1) / scl2 (1,3) rb7 ttl/st cmos general purpose i/o. anb7 an D adc channel b7 input. sck2 ttl/st cmos spi clock 2. scl2 i 2 co di 2 c clock 2. rc0/anc0/c2in0+ rc0 ttl/st cmos general purpose i/o. anc0 an D adc channel c0 input. c2in0+ an D comparator c2 positive input. rc1/anc1/c1in1-/c2in1- rc1 ttl/st cmos general purpose i/o. anc1 an D adc channel c1 input. c1in1- an D comparator c1 negative input. c2in1- an D comparator c2 negative input. rc2/anc2/c1in2-/c2in2-/ mdcin1 (1) rc2 ttl/st cmos general purpose i/o. anc2 an D adc channel c2 input. c1in2- an D comparator c1 negative input. c2in2- an D comparator c2 negative input. mdcin1 ttl/st D modular carrier input 1. table 1-3: pic16(l)f18346 pinout description (continued) name function input type output type description legend: an = analog input or output cmos= cmos compatible input or output od = open-drain ttl = ttl compatible input st = schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal = crystal levels note 1: default peripheral input. input can be moved to any ot her pin with the pps input selection registers. see register 12-2 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 20 preliminary ? 2016 microchip technology inc. rc3/anc3/c1in3-/c2in3-/ mdmin (1) / ccp2 (1) /clcin1 (1) / rc3 ttl/st cmos general purpose i/o. anc3 an D adc channel c3 input. c1in3- an D comparator c1 negative input. c2in3- an D comparator c2 negative input. mdmin ttl/st D modular source input. ccp2 ttl/st cmos capture/compare/pwm 2 input. clcin1 ttl/st D configurable logic cell 1 input. rc4/anc4 rc4 ttl/st cmos general purpose i/o. anc4 an D adc channel c4 input. rc5/anc5/mdcin2 (1) / ccp1 (1) rc5 ttl/st cmos general purpose i/o. anc5 an D adc channel c5 input. mdcin2 ttl/st D modular carrier input 2. ccp1 ttl/st cmos capture/compare/pwm 1 input. rc6/anc6/ss1 (1) rc6 ttl/st cmos general purpose i/o. anc6 an D adc channel c6 input. ss1 ttl/st D slave select 1 input. rc7/anc7 rc7 ttl/st cmos general purpose i/o. anc7 an D adc channel c7 input. v dd v dd power D positive supply. v ss v ss power D ground reference. table 1-3: pic16(l)f18346 pinout description (continued) name function input type output type description legend: an = analog input or output cmos= cmos compatible input or output od = open-drain ttl = ttl compatible input st = schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal = crystal levels note 1: default peripheral input. input can be moved to any other pin with the pps input selection registers. see register 12-2 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 21 pic16(l)f18326/18346 out (2) c1 D cmos comparator c1 output. c2 D cmos comparator c2 output. nco1 D cmos numerically controlled oscillator output. dsm D cmos digital signal modulator output. tmr0 D cmos timer0 clock output. ccp1 D cmos capture/compare/pwm 1 output. ccp2 D cmos capture/compare/pwm 2 output. ccp3 D cmos capture/compare/pwm 3 output. ccp4 D cmos capture/compare/pwm 4 output. pwm5 D cmos pulse-width modulator 5 output. pwm6 D cmos pulse-width modulator 6 output. cwg1a D cmos complementary waveform generator 1 output a. cwg2a D cmos complementary waveform generator 2 output a. cwg1b D cmos complementary waveform generator 1 output b. cwg2b D cmos complementary waveform generator 2 output b. cwg1c D cmos complementary waveform generator 1 output c. cwg2c D cmos complementary waveform generator 2 output c. cwg1d D cmos complementary waveform generator 1 output d. cwg2d D cmos complementary waveform generator 2 output d. sda1 (3) i 2 co di 2 c data output. sda2 (3) i 2 co di 2 c data output. scl1 (3) i 2 co di 2 c clock output. scl2 (3) i 2 co di 2 c clock output. sdo1 D cmos spi1 data output. sd02 D cmos spi2 data output. sck1 D cmos spi1 clock output. sck2 D cmos spi2 clock output. tx/ck D cmos asynchronous tx data/synchronous clock output. dt (3) D cmos eusart synchronous data output. clc1out D cmos configurable logic cell 1 source output. clc2out D cmos configurable logic cell 2 source output. clc3out D cmos configurable logic cell 3 source output. clc4out D cmos configurable logic cell 4 source output. clkr D cmos clock reference output. table 1-3: pic16(l)f18346 pinout description (continued) name function input type output type description legend: an = analog input or output cmos= cmos compatible input or output od = open-drain ttl = ttl compatible input st = schmitt trigger input with cmos levels i 2 c = schmitt trigger input with i 2 c hv = high voltage xtal = crystal levels note 1: default peripheral input. input can be moved to any ot her pin with the pps input selection registers. see register 12-2 . 2: all pin outputs default to port latch data. any pin can be selected as a digital peripheral output with the pps output selection registers. see register 12-2 . 3: these i 2 c functions are bidirectional. the output pin selections must be the same as the input pin selections. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 22 preliminary ? 2016 microchip technology inc. 2.0 enhanced mid-range cpu this family of devices contains an enhanced mid-range 8-bit cpu core. the cpu has 49 instructions. interrupt capability includes automatic context saving. the hardware stack is 16-levels deep and has overflow and underflow reset capability. direct, indirect, and relative addressing modes are available. two file select registers (fsrs) provide the ability to read program and data memory. automatic interrupt context saving 16-level stack with overflow and underflow file select registers instruction set figure 2-1: core block diagram data bus 8 14 program bus instruction reg program counter 8 level stack (13-bit) direct addr 7 12 addr mux fsr reg status reg mux alu power-up timer oscillator start-up timer power-on reset watchdog timer instruction decode & control timing generation osc1/clkin osc2/clkout v dd 8 8 brown-out reset 12 3 v ss internal oscillator block data bus 8 14 program bus instruction reg program counter 8 level stack (13-bit) direct addr 7 addr mux fsr reg status reg mux alu w reg instruction decode & control timing generation v dd 8 8 3 v ss internal oscillator block 15 data bus 8 14 program bus instruction reg program counter 16-level stack (15-bit) direct addr 7 ram addr addr mux indirect addr fsr0 reg status reg mux alu instruction decode and control timing generation v dd 8 8 3 v ss internal oscillator block ram fsr reg fsr reg fsr1 reg 15 15 mux 15 program memory read (pmr) 12 fsr reg fsr reg bsr reg 5 configuration configuration configuration nonvolatile memory downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 23 pic16(l)f18326/18346 2.1 automatic interrupt context saving during interrupts, certain registers are automatically saved in shadow registers and restored when returning from the interrupt. this saves stack space and user code. see section 7.5 ?automatic context saving? for more information. 2.2 16-level stack with overflow and underflow these devices have a hardware stack memory 15 bits wide and 16 words deep. a stack overflow or underflow will set the appropriate bit (stkovf or stkunf) in the pcon register, and if enabled, will cause a software reset. see section 3.4 ?stack? for more details. 2.3 file select registers there are two 16-bit file select registers (fsr). fsrs can access all file registers, program memory, and data eeprom, which allows one data pointer for all mem- ory. when an fsr points to program memory, there is one additional instruction cycle in instructions using indf to allow the data to be fetched. general purpose memory can now also be addressed linearly, providing the ability to access contiguous data larger than 80 bytes. there are also new instructions to support the fsrs. see section 3.5 ?indirect addressing? for more details. 2.4 instruction set there are 49 instructions for the enhanced mid-range cpu to support the features of the cpu. see section 33.0 ?instruction set summary? for more details. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 24 preliminary ? 2016 microchip technology inc. 3.0 memory organization these devices contain the following types of memory: program memory - configuration words -device id -user id - program flash memory data memory - core registers - special function registers - general purpose ram - common ram - data eeprom the following features are associated with access and control of program memory and data memory: pcl and pclath stack indirect addressing nvmreg access 3.1 program memory organization the enhanced mid-range core has a 15-bit program counter capable of addressing 32k x 14 program memory space. table 3-1 shows the memory sizes implemented. accessing a location above these boundaries will cause a wrap-around within the implemented memory space. the reset vector is at 0000h and the interrupt vector is at 0004h (see figure 3-1 ). table 3-1: device sizes and addresses device program memory size (words) last program memory address pic16(l)f18326/18346 16384 3fffh downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 25 pic16(l)f18326/18346 figure 3-1: program memory map and stack for pic16(l)f18326/18346 3.1.1 reading program memory as data there are two methods of accessing constants in program memory. the first method is to use tables of retlw instructions. the second method is to set an fsr to point to the program memory. 3.1.1.1 retlw instruction the retlw instruction can be used to provide access to tables of constants. the recommended way to create such a table is shown in example 3-1 . example 3-1: retlw instruction the brw instruction makes this type of table very simple to implement. if your code must remain portable with previous generations of microcontrollers, the older table read method must be used because the brw instruction is not available in some devices, such as the pic16f6xx, pic16f7xx, pic16f8xx, and pic16f9xx devices. pc<14:0> 15 0000h 0004h stack level 0 stack level 15 reset vector interrupt vector stack level 1 0005h on-chip program memory page 0-3 3fffh wraps to page 0 wraps to page 0 wraps to page 0 4000h call , callw return , retlw interrupt, retfie rollover to page 0 rollover to page 0 7fffh constants brw ;add index in w to ;program counter to ;select data retlw data0 ;index0 data retlw data1 ;index1 data retlw data2 retlw data3 my_function ; lots of code movlw data_index call constants ; the constant is in w downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 26 preliminary ? 2016 microchip technology inc. 3.1.1.2 indirect read with fsr the program memory can be accessed as data by setting bit 7 of the fsrxh register and reading the matching indfx register. the moviw instruction will place the lower eight bits of the addressed word in the w register. writes to the program memory cannot be performed via the indf registers. instructions that access the program memory via the fsr require one extra instruction cycle to complete. example 3-2 demonstrates accessing the program memory via an fsr. the high directive will set bit 7 if a label points to a location in the program memory. example 3-2: accessing program memory via fsr 3.2 data memory organization the data memory is partitioned into 32 memory banks with 128 bytes in each bank. each bank consists of ( figure 3-2 ): 12 core registers special function registers (sfr) up to 80 bytes of general purpose ram (gpr) 16 bytes of common ram the active bank is selected by writing the bank number into the bank select register (bsr). unimplemented memory will read as 0 . all data memory can be accessed either directly (via instructions that use the file registers) or indirectly via the two file select registers (fsr). see section 3.5 ?indirect addressing?? for more information. data memory uses a 12-bit address. the upper seven bits of the address define the bank address and the lower five bits select the registers/ram in that bank. figure 3-2: banked-memory partitioning 3.2.1 core registers the core registers contain the registers that directly affect the basic operation. the core registers occupy the first 12 addresses of every data memory bank (addresses x00h/x80h through x0bh/x8bh). these registers are listed below in ta b l e 3 - 2 . for detailed information, see tab le 3- 4 . table 3-2: core registers constants retlw data0 ;index0 data retlw data1 ;index1 data retlw data2 retlw data3 my_function ; lots of code movlw low constants movwf fsr1l movlw high constants movwf fsr1h moviw 0[fsr1] ;the program memory is in w 0bh 0ch 1fh 20h 6fh 70h 7fh 00h common ram (16 bytes) general purpose ram (80 bytes maximum) core registers (12 bytes) special function registers memory region 7-bit bank offset addresses bankx x00h or x80h indf0 x01h or x81h indf1 x02h or x82h pcl x03h or x83h status x04h or x84h fsr0l x05h or x85h fsr0h x06h or x86h fsr1l x07h or x87h fsr1h x08h or x88h bsr x09h or x89h wreg x0ah or x8ah pclath x0bh or x8bh intcon downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 27 pic16(l)f18326/18346 3.2.1.1 27 the status register, shown in register 3-1 , contains: the arithmetic status of the alu the reset status the status register can be the destination for any instruction, like any other register. if the status register is the destination for an instruction that affects the z, dc or c bits, then the write to these three bits is disabled. these bits are set or cleared according to the device logic. furthermore, the to and pd bits are not writable. therefore, the result of an instruction with the status register as destination may be different than intended. for example, clrf status will clear the upper three bits and set the z bit. this leaves the status register as 000u u1uu (where u = unchanged). it is recommended, therefore, that only bcf, bsf, swapf and movwf instructions are used to alter the status register, because these instructions do not affect any status bits. for other instructions not affecting any status bits (refer to section 3.0 ?memory organization? ). note 1: the c and dc bits operate as borrow and digit borrow out bits, respectively, in subtraction. register 3-1: status: status register u-0 u-0 u-0 r-1/q r-1/q r/w-0/u r/w-0/u r/w-0/u to pd zd c (1) c (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 q = value depends on condition bit 7-5 unimplemented: read as 0 bit 4 to : time-out bit 1 = after power-up, clrwdt instruction or sleep instruction 0 = a wdt time-out occurred bit 3 pd : power-down bit 1 = after power-up or by the clrwdt instruction 0 = by execution of the sleep instruction 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/digit 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 (1) ( 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 twos complement of the second operand. for rotate ( rrf , rlf ) instructions, this bit is loaded with either the high-order or low-order bit of the source register. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 28 preliminary ? 2016 microchip technology inc. 3.2.2 special function register the special function registers are registers used by the application to control the desired operation of peripheral functions in the device. the special function registers occupy the 20 bytes after the core registers of every data memory bank (addresses x0ch/x8ch through x1fh/x9fh), with the exception of banks 27, 28, and 29 (pps and clc registers). the registers associated with the operation of the peripherals are described in the appropriate peripheral chapter of this data sheet. 3.2.3 general purpose ram there are up to 80 bytes of gpr in each data memory bank. the special function registers occupy the 20 bytes after the core registers of every data memory bank (addresses x0ch/x8ch through x1fh/x9fh), with the exception of banks 27, 28, and 29 (pps and clc registers). 3.2.3.1 linear access to gpr the general purpose ram can be accessed in a non-banked method via the fsrs. this can simplify access to large memory structures. see section 3.5.2 ?linear data memory? for more information. 3.2.4 common ram there are 16 bytes of common ram accessible from all banks. 3.2.5 device memory maps the memory maps for pic16(l)f18326/18346 are as shown in tab l e 3 - 4 . table 3-3: special function register summary banks 0-31 (all banks) (1) address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets all banks 000h indf0 addressing this location uses contents of fsr0h/fsr0l to address data memo ry (not a physical register) xxxx xxxx xxxx xxxx 001h indf1 addressing this location uses contents of fsr1h/fsr1l to address data memo ry (not a physical register) xxxx xxxx xxxx xxxx 002h pcl program counter (pc) least significant byte 0000 0000 0000 0000 003h status t o pd zd cc ---1 1000 ---q quuu 004h fsr0l indirect data memory address 0 low pointer 0000 0000 uuuu uuuu 005h fsr0h indirect data memory address 0 high pointer 0000 0000 0000 0000 006h fsr1l indirect data memory address 1 low pointer 0000 0000 uuuu uuuu 007h fsr1h indirect data memory address 1 high pointer 0000 0000 0000 0000 008h bsr bsr4 bsr3 bsr2 bsr1 bsr0 ---0 0000 ---0 0000 009h wreg working register 0000 0000 uuuu uuuu 00ah pclath write buffer for the upper 7 bits of the program counter -000 0000 -000 0000 00bh intcon gie peie i n t e d g 00-- ---1 00-- ---1 legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: these registers can be accessed from any bank. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 29 pic16(l)f18326/18346 table 3-4: special function register summary banks 0-31 address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets bank 0 cpu core registers; see ta b l e 3 - 2 for specifics 00ch porta ra5 ra4 ra3 ra2 ra1 ra0 --xx xxxx --uu uuuu 00dh portb x unimplemented x rb7 rb6 rb5 rb4 xxxx ---- uuuu ---- 00eh portc x rc5 rc4 rc3 rc2 rc1 rc0 --xx xxxx --uu uuuu x rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 xxxx xxxx uuuu uuuu 00fh unimplemented 010h pir0 t m r 0 i fi o c i f i n t f --00 ---0 --00 ---0 011h pir1 tmr1gif adif rcif txif ssp1if bcl1if tmr2if tmr1if 0000 0000 0000 0000 012h pir2 tmr6if c2if c1if nvmif ssp2if bcl2if tmr4if nco1if 0000 0000 0000 0000 013h pir3 osfif cswif tmr3gif tmr3if clc4if clc3if clc2if clc1if 0000 0000 0000 0000 014h pir4 cwg2if cwg1if tmr5gif tmr5if ccp4if ccp3if ccp2if ccp1if 0000 0000 0000 0000 015h tmr0l tmr0l<7:0> xxxx xxxx xxxx xxxx 016h tmr0h tmr0h<7:0> 1111 1111 1111 1111 017h t0con0 t0en t0out t016bit t0outps<3:0> 0-00 0000 0-00 0000 018h t0con1 t0cs<2:0> t0async t0ckps<3:0> 0000 0000 0000 0000 019h tmr1l tmr1l<7:0> xxxx xxxx uuuu uuuu 01ah tmr1h tmr1h<7:0> xxxx xxxx uuuu uuuu 01bh t1con tmr1cs<1:0> t1ckps<1:0> t1sosc t1sync t m r 1 o n 0000 00-0 uuuu uu-u 01ch t1gcon tmr1ge t1gpol t1gtm t1gspm t1ggo/ done t1gval t1gss<1:0> 0000 0x00 uuuu uxuu 01dh tmr2 tmr2<7:0> 0000 0000 0000 0000 01eh pr2 pr2<7:0> 1111 1111 1111 1111 01fh t2con t2outps<3:0> tmr2on t2ckps<1:0> -000 0000 -000 0000 legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 30 preliminary ? 2016 microchip technology inc. bank 1 cpu core registers; see table 3-2 for specifics 08ch trisa trisa5 trisa4 trisa2 trisa1 trisa0 --11 -111 --11 -111 08dh trisb x unimplemented x trisb7 trisb6 trisb5 trisb4 1111 ---- 1111 ---- 08eh trisc x trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 --11 1111 --11 1111 x trisc7 trisc6 trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 1111 1111 1111 1111 08fh unimplemented 090h pie0 D tmr0ie iocie i n t e --00 ---0 --00 ---0 091h pie1 tmr1gie adie rcie txie ssp1ie bcl1ie tmr2ie tmr1ie 0000 0000 0000 0000 092h pie2 tmr6ie c2ie c1ie nvmie ssp2ie bcl2ie tmr4ie nco1ie 0000 0000 0000 0000 093h pie3 osfie cswie tmr3gie tmr 3ie clc4ie clc3ie clc2ie clc1ie 0000 0000 0000 0000 094h pie4 cwg2ie cwg1ie tmr5gie tmr5ie ccp4ie ccp3ie ccp2ie ccp1ie 0000 0000 0000 0000 095h unimplemented 096h unimplemented 097h wdtcon wdtps<4:0> swdten --01 0110 --01 0110 098h unimplemented 099h unimplemented 09ah unimplemented 09bh adresl adres<7:0> xxxx xxxx uuuu uuuu 09ch adresh adres<9:8> xxxx xxxx uuuu uuuu 09dh adcon0 chs<5:0> go/done adon 0000 0000 0000 0000 09eh adcon1 adfm adcs<2:0> adnref adpref<1:0> 0000 -000 0000 -000 09fh adact adact<3:0> ---0 0000 ---0 0000 table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 31 pic16(l)f18326/18346 bank 2 cpu core registers; see table 3-2 for specifics 10ch lata l a t a 5l a t a 4 lata2 lata1 lata0 --xx -xxx --uu -uuu 10dh latb x unimplemented x latb7 latb6 latb5 latb4 xxxx ---- uuuu ---- 10eh latc x latc5 latc4 latc3 latc2 latc1 latc0 --xx xxxx --uu uuuu x latc7 latc6 latc5 latc4 latc3 latc2 latc1 latc0 xxxx xxxx uuuu uuuu 10fh unimplemented 110h unimplemented 111h cm1con0 c1on c1out c 1 p o l c1sp c1hys c1sync 00-0 -100 00-0 -100 112h cm1con1 c1intp c1intn c1pch<2:0> c1nch<2:0> 0000 0000 0000 0000 113h cm2con0 c2on c2out c 2 p o l c2sp c2hys c2sync 00-0 -100 00-0 -100 114h cm2con1 c2intp c2intn c2pch<2:0> c2nch<2:0> 0000 0000 0000 0000 115h cmout mc2out mc1out ---- --00 ---- --00 116h borcon sboren borrdy 1--- ---q u--- ---u 117h fvrcon fvren fvrrdy tsen tsrng cdafvr<1:0> adfvr<1:0> 0q00 0000 0q00 0000 118h daccon0 dac1en d a c 1 o e dac1pss<1:0> d a c 1 n s s 0-0- 00-0 0-0- 00-0 119h daccon1 d a c 1 r < 4 : 0 > ---0 0000 ---0 0000 11ah to 11fh unimplemented table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 32 preliminary ? 2016 microchip technology inc. bank 3 cpu core registers; see table 3-2 for specifics 18ch ansela D D ansa5 ansa4 D ansa2 ansa1 ansa0 --xx -xxx --uu -uuu 18dh anselb x D unimplemented D x ansb7 ansb6 ansb5 ansb4 D D D D xxxx ---- uuuu ---- 18eh anselc x D D ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 --xx xxxx --uu uuuu D x ansc7 ansc6 ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 xxxx xxxx uuuu uuuu 18fh D D unimplemented 190h D D unimplemented 191h D D unimplemented 192h D D unimplemented 193h D D unimplemented 194h D D unimplemented 195h D D unimplemented 196h D D unimplemented 197h vregcon (1) D D D D D D vregpm reserved ---- --01 ---- --01 198h D D unimplemented 199h rc1reg rc1reg<7:0> 0000 0000 0000 0000 19ah tx1reg tx1reg<7:0> 0000 0000 0000 0000 19bh sp1brgl sp1brg<7:0> 0000 0000 0000 0000 19ch sp1brgh sp1brg<15:8> 0000 0000 0000 0000 19dh rc1sta spen rx9 sren cren adden ferr oerr rx9d 0000 000x 0000 000x 19eh tx1sta csrc tx9 txen sync sendb brgh tmrt tx9d 0000 0010 0000 0010 19fh baud1con abdovf rcidl sckp brg16 wue abden 01-0 0-00 01-0 0-00 table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 33 pic16(l)f18326/18346 bank 4 cpu core registers; see table 3-2 for specifics 20ch wpua D D wpua5 wpua4 wpua3 wpua2 wpua1 wpua0 --00 0000 --00 0000 20dh wpub x D unimplemented D D D x wpub7 wpub6 wpub5 wpub4 D D D D 0000 ---- 0000 ---- 20eh wpuc x D D D wpuc5 wpuc4 wpuc3 wpuc2 wpuc1 wpuc0 --00 0000 --00 0000 D x wpuc7 wpuc6 wpuc5 wpuc4 wpuc3 wpuc2 wpuc1 wpuc0 0000 0000 0000 0000 20fh D D unimplemented D D 210h D D unimplemented D D 211h ssp1buf ssp1buf<7:0> xxxx xxxx uuuu uuuu 212h ssp1add ssp1add<7:0> 0000 0000 0000 0000 213h ssp1msk ssp1msk<7:0> 1111 1111 1111 1111 214h ssp1stat smp cke d/a ps r / w ua bf 0000 0000 0000 0000 215h ssp1con1 wcol sspov sspen ckp sspm<3:0> 0000 0000 0000 0000 216h ssp1con2 gcen ackstat ackdt acken rcen pen rsen sen 0000 0000 0000 0000 217h ssp1con3 acktim pcie scie boen sdaht sbcde ahen dhen 0000 0000 0000 0000 218h D D unimplemented D D 219h ssp2buf ssp2buf<7:0> xxxx xxxx uuuu uuuu 21ah ssp2add ssp2add<7:0> 0000 0000 0000 0000 21bh ssp2msk ssp2msk<7:0> 1111 1111 1111 1111 21ch ssp2stat smp cke d/a ps r / w ua bf 0000 0000 0000 0000 21dh ssp2con1 wcol sspov sspen ckp sspm<3:0> 0000 0000 0000 0000 21eh ssp2con2 gcen ackstat ackdt acken rcen pen rsen sen 0000 0000 0000 0000 21fh ssp2con3 acktim pcie scie boen sdaht sbcde ahen dhen 0000 0000 0000 0000 table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 34 preliminary ? 2016 microchip technology inc. bank 5 cpu core registers; see table 3-2 for specifics 28ch odcona D D odca5 odca4 D odca2 odca1 odca0 --00 -000 --00 -000 28dh odconb x D unimplemented D D D x odcb7 odcb6 odcb5 odcb4 D D D D 0000 ---- 0000 ---- 28eh odconc x D D D odcc5 odcc4 odcc3 odcc2 odcc1 odcc0 --00 0000 --00 0000 D x odcc7 odcc6 odcc5 odcc4 odcc3 odcc2 odcc1 odcc0 0000 0000 0000 0000 28fh D unimplemented D D 290h D D unimplemented D D 291h ccpr1l ccpr1<7:0> xxxx xxxx xxxx xxxx 292h ccpr1h ccpr1<15:8> xxxx xxxx xxxx xxxx 293h ccp1con ccp1en D ccp1out ccp1fmt ccp1mode<3:0> 0-x0 0000 0-x0 0000 294h ccp1cap D D D ccp1cts<3:0> ---- 0000 ---- xxxx 295h ccpr2l ccpr2<7:0> xxxx xxxx xxxx xxxx 296h ccpr2h ccpr2<15:8> xxxx xxxx xxxx xxxx 297h ccp2con ccp2en D ccp2out ccp2fmt ccp2mode<3:0> 0-x0 0000 0-x0 0000 298h ccp2cap D D D D ccp2cts<3:0> ---- 0000 ---- xxxx 299h D D unimplemented D D 29ah D D unimplemented D D 29bh D D unimplemented D D 29ch D D unimplemented D D 29dh D D unimplemented D D 29eh D D unimplemented D D 29fh ccptmrs c4tsel<1:0> c3tsel<1:0> c2tsel<1:0> c1tsel<1:0> 0101 0101 0101 0101 table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 35 pic16(l)f18326/18346 bank 6 cpu core registers; see table 3-2 for specifics 30ch slrcona D D slra5 slra4 D slra2 slra1 slra0 --11 -111 --11 -111 30dh slrconb x D unimplemented D D D x slrb7 slrb6 slrb5 slrb4 D D D D 1111 ---- 1111 ---- 30eh slrconc x D D D slrc5 slrc4 slrc3 slrc2 slrc1 slrc0 --11 1111 --11 1111 D x slrc7 slrc6 slrc5 slrc4 slrc3 slrc2 slrc1 slrc0 1111 1111 1111 1111 30fh D D unimplemented D D 310h D D unimplemented D D 311h ccpr3l ccpr3<7:0> xxxx xxxx xxxx xxxx 312h ccpr3h ccpr3<15:8> xxxx xxxx xxxx xxxx 313h ccp3con ccp3en D ccp3out ccp3fmt ccp3mode<3:0> 0-x0 0000 0-x0 0000 314h ccp3cap D D D D ccp3cts<3:0> ---- 0000 ---- xxxx 315h ccpr4l ccpr4<7:0> xxxx xxxx xxxx xxxx 316h ccpr4h ccpr4<15:8> xxxx xxxx xxxx xxxx 317h ccp4con ccp4en D ccp4out ccp4fmt ccp4mode<3:0> 0-x0 0000 0-x0 0000 318h ccp4cap D D D D ccp4cts<3:0> ---- 0000 ---- xxxx 319h to 31fh D D unimplemented D D table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 36 preliminary ? 2016 microchip technology inc. bank 7 cpu core registers; see table 3-2 for specifics 38ch inlvla D D inlvla5 inlvla4 inlvla3 inlvla2 inlvla1 inlvla0 --11 1111 --11 1111 38dh inlvlb x D unimplemented D D D x inlvlb7 inlvlb6 inlvlb5 inlvlb4 D D D D 1111 ---- 1111 ---- 38eh inlvlc x D D D inlvlc5 inlvlc4 inlvlc3 inlvlc2 inlvlc1 inlvlc0 --11 1111 --11 1111 D x inlvlc7 inlvlc6 inlvlc5 inlvlc4 inlvlc3 inlvlc2 inlvlc1 inlvlc0 1111 1111 1111 1111 38fh D D unimplemented D D 390h D D unimplemented D D 391h iocap D D iocap5 iocap4 iocap3 iocap2 iocap1 iocap0 --00 0000 --00 0000 392h iocan D D iocan5 iocan4 iocan3 iocan2 iocan1 iocan0 --00 0000 --00 0000 393h iocaf D D iocaf5 iocaf4 iocaf3 iocaf2 iocaf1 iocaf0 --00 0000 --00 0000 394h iocbp x D unimplemented D D D x iocbp7 iocbp6 iocbp5 iocbp4 D D D D 0000 ---- 0000 ---- 395h iocbn x D unimplemented D D D x iocbn7 iocbn6 iocbn5 iocbn4 D D D D 0000 ---- 0000 ---- 396h iocbf x D unimplemented D D D x iocbf7 iocbf6 iocbf5 iocbf4 D D D D 0000 ---- 0000 ---- 397h ioccp x D D D ioccp5 ioccp4 ioccp3 ioccp2 ioccp1 ioccp0 --00 0000 --00 0000 D x ioccp7 ioccp6 ioccp5 ioccp4 ioccp3 ioccp2 ioccp1 ioccp0 0000 0000 0000 0000 398h ioccn x D D D ioccn5 ioccn4 ioccn3 ioccn2 ioccn1 ioccn0 --00 0000 --00 0000 D x ioccn7 ioccn6 ioccn5 ioccn4 ioccn3 ioccn2 ioccn1 ioccn0 0000 0000 0000 0000 399h ioccf x D D D ioccf5 ioccf4 ioccf3 ioccf2 ioccf1 ioccf0 --00 0000 --00 0000 D x ioccf7 ioccf6 ioccf5 ioccf4 ioccf3 ioccf2 ioccf1 ioccf0 0000 0000 0000 0000 table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 37 pic16(l)f18326/18346 bank 7 cpu core registers; see table 3-2 for specifics 39ah clkrcon clkren D D clkrdc<1:0> clkrdiv<2:0> 0--1 0000 0--1 0001 39bh D D unimplemented D D 39ch mdcon mden D D mdopol mdout D D mdbit 0--0 0--0 0--0 0--0 39dh mdsrc D D D D mdms<3:0> ---- xxxx 0--- uuuu 39eh mdcarh D mdchpol mdchsync D mdch<3:0> -xx- xxxx -uu- uuuu 39fh mdcarl D mdclpol mdclsync D mdcl<3:0> -xx- xxxx -uu- uuuu table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 38 preliminary ? 2016 microchip technology inc. bank 8 cpu core registers; see table 3-2 for specifics 40ch to 410h D D unimplemented D D 411h tmr3l tmr3l<7:0> xxxx xxxx uuuu uuuu 412h tmr3h tmr3h<7:0> xxxx xxxx uuuu uuuu 413h t3con tmr3cs<1:0> t3ckps<1:0> t3sosc t3sync D tmr3on 0000 00-0 uuuu uu-u 414h t3gcon tmr3ge t3gpol t3gtm t3gspm t3ggo/ done t3gval t3gss<1:0> 0000 0x00 uuuu uxuu 415h tmr4 tmr4<7:0> 0000 0000 0000 0000 416h pr4 pr4<7:0> 1111 1111 1111 1111 417h t4con D t4outps<3:0> tmr4on t4ckps<1:0> -000 0000 -000 0000 418h tmr5l tmr5l<7:0> xxxx xxxx uuuu uuuu 419h tmr5h tmr5h<7:0> xxxx xxxx uuuu uuuu 41ah t5con tmr5cs<1:0> t5ckps<1:0> t5sosc t5sync D tmr5on 0000 00-0 uuuu uu-u 41bh t5gcon tmr5ge t5gpol t5gtm t5gspm t5ggo/ done t5gval t5gss<1:0> 0000 0x00 uuuu uxuu 41ch tmr6 tmr6<7:0> 0000 0000 0000 0000 41dh pr6 pr6<7:0> 1111 1111 1111 1111 41eh t6con D t6outps<3:0> tmr6on t6ckps<1:0> -000 0000 -000 0000 41fh D D unimplemented D D table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 39 pic16(l)f18326/18346 bank 9 cpu core registers; see table 3-2 for specifics 48ch to 497h D D unimplemented D D 498h nco1accl nco1acc<7:0> 0000 0000 0000 0000 499h nco1acch nco1acc<15:8> 0000 0000 0000 0000 49ah nco1accu D D D nco1acc<19:16> ---- 0000 ---- 0000 49bh nco1incl nco1inc<7:0> 0000 0001 0000 0001 49ch nco1inch nco1inc<15:8> 0000 0000 0000 0000 49dh nco1incu D D D D nco1inc<19:16> ---- 0000 ---- 0000 49eh nco1con n1en D n1out n1pol D D D n1pfm 0-00 ---0 0-00 ---0 49fh nco1clk n1pws<2:0> D D D n1cks<1:0> 000- --00 000- --00 table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 40 preliminary ? 2016 microchip technology inc. bank 10-11 cpu core registers; see table 3-2 for specifics 50ch to 51fh D D unimplemented D D 58ch to 59fh D D unimplemented D D bank 12 60ch D D unimplemented D D 60dh D D unimplemented D D 60eh D D unimplemented D D 60fh D D unimplemented D D 610h D D unimplemented D D 611h D D unimplemented D D 612h D D unimplemented D D 613h D D unimplemented D D 614h D D unimplemented D D 615h D D unimplemented D D 616h D D unimplemented D D 617h pwm5dcl pwm5dc<1:0> D D D D D D xx-- ---- uu-- ---- 618h pwm5dch pwm5dc<9:2> xxxx xxxx uuuu uuuu 619h pwm5con pwm5en D pwm5out pwm5pol D D D D 0-00 ---- 0-00 ---- 61ah pwm6dcl pwm6dc<1:0> D D D D D D xx-- ---- uu-- ---- 61bh pwm6dch pwm6dc<9:2> xxxx xxxx uuuu uuuu 61ch pwm6con pwm6en D pwm6out pwm6pol D D D D 0-00 ---- 0-00 ---- 61dh to 61eh D D unimplemented D D 61fh pwmtmrs D D D D p6tsel<1:0> p5tsel<1:0> ---- 0101 ---- 0101 table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 41 pic16(l)f18326/18346 bank 13 cpu core registers; see table 3-2 for specifics 68ch D D unimplemented D D 68dh D D unimplemented D D 68eh D D unimplemented D D 68fh D D unimplemented D D 690h D D unimplemented D D 691h cwg1clkcon D D D D D D D cs ---- ---0 ---- ---0 692h cwg1dat D D D D dat<3:0> ---- 0000 ---- 0000 693h cwg1dbr D D dbr<5:0> --00 0000 --00 0000 694h cwg1dbf D D dbf<5:0> --00 0000 --00 0000 695h cwg1con0 en ld D D D mode<2:0> 00-- -000 00-- -000 696h cwg1con1 D D in D pold polc polb pola --x- 0000 --x- 0000 697h cwg1as0 shutdown ren lsbd<1:0> lsac<1:0> D D 0001 01-- 0001 01-- 698h cwg1as1 D D D as4e as3e as2e as1e as0e ---0 0000 ---0 0000 699h cwg1str ovrd ovrc ovrb ovra strd strc strb stra 0000 0000 0000 0000 69ah to 69fh D D unimplemented D D table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 42 preliminary ? 2016 microchip technology inc. bank 14 cpu core registers; see table 3-2 for specifics 70ch D D unimplemented D D 70dh D D unimplemented D D 70eh D D unimplemented D D 70fh D D unimplemented D D 710h D D unimplemented D D 711h cwg2clkcon D D D D D D D cs ---- ---0 ---- ---0 712h cwg2dat D D D D dat<3:0> ---- 0000 ---- 0000 713h cwg2dbr D D dbr<5:0> --00 0000 --00 0000 714h cwg2dbf D D dbf<5:0> --00 0000 --00 0000 715h cwg2con0 en ld D D D mode<2:0> 00-- -000 00-- -000 716h cwg2con1 D D in D pold polc polb pola --x- 0000 --x- 0000 717h cwg2as0 shutdown ren lsbd<1:0> lsac<1:0> D D 0001 01-- 0001 01-- 718h cwg2as1 D D D as4e as3e as2e as1e as0e ---0 0000 ---0 0000 719h cwg2str ovrd ovrc ovrb ovra strd strc strb stra 0000 0000 0000 0000 71ah to 71fh D D unimplemented D D table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 43 pic16(l)f18326/18346 banks 15-16 cpu core registers; see table 3-2 for specifics 78ch to 79fh D D unimplemented D D 80ch to 81fh D D unimplemented D D bank 17 88ch D D unimplemented D D 88dh D D unimplemented D D 88eh D D unimplemented D D 88fh D D unimplemented D D 890h D D unimplemented D D 891h nvmadrl nvmadr<7:0> 0000 0000 0000 0000 892h nvmadrh D nvmadr<14:8> 1000 0000 1000 0000 893h nvmdatl nvmdat<7:0> 0000 0000 0000 0000 894h nvmdath D D nvmdat<13:8> --00 0000 --00 0000 895h nvmcon1 D nvmregs lwlo free wrerr wren wr rd -000 x000 -000 q000 896h nvmcon2 nvmcon2 0000 0000 0000 0000 897h D D unimplemented D D 898h D D unimplemented D D 899h D D unimplemented D D 89ah D D unimplemented D D 89bh pcon0 stkovf stkunf D rwdt rmclr ri por bor 00-1 110q qq-q qquu 89ch to 89fh D D unimplemented D D table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 44 preliminary ? 2016 microchip technology inc. bank 18 cpu core registers; see table 3-2 for specifics 90ch D D unimplemented D D 90dh D D unimplemented D D 90eh D D unimplemented D D 90fh D D unimplemented D D 910h D D unimplemented D D 911h pmd0 syscmd fvrmd D D D nvmmd clkrmd iocmd 00-- -000 00-- -000 912h pmd1 ncomd tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md tmr0md 0--- -000 0--- -000 913h pmd2 D dacmd adcmd D D cmp2md cmp1md D -00- --0- -00- --0- 914h pmd3 cwg2md cwg1md pwm6md pwm5md ccp4md ccp3md ccp2md ccp1md -000 --00 -000 --00 915h pmd4 D D uart1md D D mssp2md mssp1md D --0- --0- --0- --0- 916h pmd5 D D D clc4md clc3md clc2md clc1md dsmmd ---- -000 ---- -000 917h D D unimplemented D D 918h cpudoze idlen dozen roi doe D doze<2:0> 000- -000 000- -000 919h osccon1 D nosc<2:0> ndiv<3:0> -qqq 0000 -qqq 0000 91ah osccon2 D cosc<2:0> cdiv<3:0> -qqq 0000 -qqq 0000 91bh osccon3 cswhold soscpwr soscbe ordy noscr D D D 0000 0--- 0000 0--- 91ch oscstat1 extor hfor D lfor sor ador D pllr qq-q qq-q qq-q qq-q 91dh oscen extoen hfoen D lfoen soscen adoen D D 00-0 00-- 00-0 00-- 91eh osctune D D hftun<5:0> --10 0000 --10 0000 91fh oscfrq D D D D hffrq<3:0> ---- -qqq ---- -qqq table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 45 pic16(l)f18326/18346 banks 19-27 cpu core registers; see table 3-2 for specifics 98ch to 9efh unimplemented a0ch to a6fh unimplemented a8ch to aefh unimplemented b0ch to b6fh unimplemented b8ch to befh unimplemented c0ch to c6fh unimplemented c8ch to cefh unimplemented d0ch to d6fh unimplemented d8ch to defh unimplemented table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 46 preliminary ? 2016 microchip technology inc. bank 28 cpu core registers; see table 3-2 for specifics e0ch D D unimplemented D D e0dh D D unimplemented D D e0eh D D unimplemented D D e0fh ppslock D D D D D D D ppslocked ---- ---0 ---- ---0 e10h intpps D D D intpps<4:0> ---0 0010 ---u uuuu e11h t0ckipps D D D t0ckipps<4:0> ---0 0010 ---u uuuu e12h t1ckipps D D D t1ckipps<4:0> ---0 0101 ---u uuuu e13h t1gpps D D D t1gpps<4:0> ---0 0100 ---u uuuu e14h ccp1pps D D D ccp1pps<4:0> ---1 0011 ---u uuuu e15h ccp2pps D D D ccp2pps<4:0> ---1 0101 ---u uuuu e16h ccp3pps D D D ccp3pps<4:0> ---0 0010 ---u uuuu e17h ccp4pps x D D D D ccp4pps<4:0> ---1 0001 ---u uuuu D x D D D ccp4pps<4:0> ---0 0100 ---u uuuu e18h cwg1pps D D D cwg1pps<4:0> ---0 0010 ---u uuuu e19h cwg2pps D D D cwg2pps<4:0> ---0 0010 ---u uuuu e1ah mdcin1pps D D D mdcin1pps<4:0> ---1 0010 ---u uuuu e1bh mdcin2pps D D D mdcin2pps<4:0> ---1 0101 ---u uuuu e1ch mdminpps D D D mdminpps<4:0> ---1 0011 ---u uuuu e1dh ssp2clkpps x D D D D ssp2clkpps<4:0> ---1 0100 ---u uuuu D x D D D ssp2clkpps<4:0> ---0 1111 ---u uuuu e1eh ssp2datpps x D D D D ssp2datpps<4:0> ---1 0101 ---u uuuu D x D D D ssp2datpps<4:0> ---0 1101 ---u uuuu e1fh ssp2sspps x D D D D ssp2sspps<4:0> ---0 0000 ---u uuuu D x D D D ssp2sspps<4:0> ---0 0001 ---u uuuu e20h ssp1clkpps x D D D D ssp1clkpps<4:0> ---1 0000 ---u uuuu D x D D D ssp1clkpps<4:0> ---0 1110 ---u uuuu table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 47 pic16(l)f18326/18346 bank 28 cpu core registers; see table 3-2 for specifics e21h ssp1datpps x D D D D ssp1datpps<4:0> ---1 0001 ---u uuuu D x D D D ssp1datpps<4:0> ---0 1100 ---u uuuu e22h ssp1sspps x D D D D ssp1sspps<4:0> ---1 0011 ---u uuuu D x D D D ssp1sspps<4:0> ---1 0100 ---u uuuu e23h D D unimplemented D D e24h rxpps x D D D D rxpps<4:0> ---1 0101 ---u uuuu D x D D D rxpps<4:0> ---0 1101 ---u uuuu e25h txpps x D D D D txpps<4:0> ---1 0100 ---u uuuu D x D D D txpps<4:0> ---0 1111 ---u uuuu e26h D D unimplemented D D e27h D D unimplemented D D e28h clcin0pps x D D D D clcin0pps<4:0> ---1 0011 ---u uuuu D x D D D clcin0pps<4:0> ---0 0010 ---u uuuu e29h clcin1pps x D D D D clcin1pps<4:0> ---0 0100 ---u uuuu D x D D D clcin1pps<4:0> ---1 0011 ---u uuuu e2ah clcin2pps x D D D D clcin2pps<4:0> ---1 0001 ---u uuuu D x D D D clcin2pps<4:0> ---0 1100 ---u uuuu e2bh clcin3pps x D D D D clcin3pps<4:0> ---0 0101 ---u uuuu D x D D D clcin3pps<4:0> ---0 1101 ---u uuuu e2ch t3ckipps x D D D D t3ckipps<4:0> ---1 0001 ---u uuuu D x D D D t3ckipps<4:0> ---0 0101 ---u uuuu e2dh t3gpps x D D D D t3gpps<4:0> ---1 0001 ---u uuuu D x D D D t3gpps<4:0> ---1 0100 ---u uuuu e2eh t5ckipps x D D D D t5ckipps<4:0> ---1 0001 ---u uuuu D x D D D t5ckipps<4:0> ---0 0101 ---u uuuu e2fh t5gpps x D D D D t5gpps<4:0> ---1 0001 ---u uuuu D x D D D t5gpps<4:0> ---1 0100 ---u uuuu table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 48 preliminary ? 2016 microchip technology inc. bank 29 cpu core registers; see table 3-2 for specifics e8dh D D unimplemented D D e8eh D D unimplemented D D e8fh D D unimplemented D D e90h ra0pps D D D ra0pps<4:0> ---0 0000 ---u uuuu e91h ra1pps D D D ra1pps<4:0> ---0 0000 ---u uuuu e92h ra2pps D D D ra2pps<4:0> ---0 0000 ---u uuuu e93h D D unimplemented D D e94h ra4pps D D D ra4pps<4:0> ---0 0000 ---u uuuu e95h ra5pps D D D ra5pps<4:0> ---0 0000 ---u uuuu e96h D D unimplemented D D e97h D D unimplemented D D e98h D D unimplemented D D e99h D D unimplemented D D e9ah D D unimplemented D D e9bh D D unimplemented D D e9ch rb4pps x D unimplemented D D D x D D D rb4pps<4:0> ---0 0000 ---u uuuu e9dh rb5pps x D unimplemented D D D x D D D rb5pps<4:0> ---0 0000 ---u uuuu e9eh rb6pps x D unimplemented D D D x D D D rb6pps<4:0> ---0 0000 ---u uuuu e9fh rb7pps x D unimplemented D D D x D D D rb7pps<4:0> ---0 0000 ---u uuuu table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 49 pic16(l)f18326/18346 bank 29 cpu core registers; see table 3-2 for specifics ea0h rc0pps D D D rc0pps<4:0> ---0 0000 ---u uuuu ea1h rc1pps D D D rc1pps<4:0> ---0 0000 ---u uuuu ea2h rc2pps D D D rc2pps<4:0> ---0 0000 ---u uuuu ea3h rc3pps D D D rc3pps<4:0> ---0 0000 ---u uuuu ea4h rc4pps D D D rc4pps<4:0> ---0 0000 ---u uuuu ea5h rc5pps D D D rc5pps<4:0> ---0 0000 ---u uuuu ea6h rc6pps x D unimplemented D D D x D D D rc6pps<4:0> ---0 0000 ---u uuuu ea7h rc7pps x D unimplemented D D D x D D D rc7pps<4:0> ---0 0000 ---u uuuu table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 50 preliminary ? 2016 microchip technology inc. bank 30 cpu core registers; see table 3-2 for specifics f0ch D D unimplemented D D f0dh D D unimplemented D D f0eh D D unimplemented D D f0fh clcdata D D D D mlc4out mlc3out mlc2out mlc1out ---- 0000 ---- 0000 f10h clc1con lc1en D lc1out lc1intp lc1intn lc1mode<2:0> 0-00 0000 0-00 0000 f11h clc1pol lc1pol D D D lc1g4pol lc1g3pol lc1g2pol lc1g1pol 0--- xxxx 0--- uuuu f12h clc1sel0 D D lc1d1s<5:0> --xx xxxx --uu uuuu f13h clc1sel1 D D lc1d2s<5:0> --xx xxxx --uu uuuu f14h clc1sel2 D D lc1d3s<5:0> --xx xxxx --uu uuuu f15h clc1sel3 D D lc1d4s<5:0> --xx xxxx --uu uuuu f16h clc1gls0 lc1g1d4t lc1g1d4n lc1g1d3t lc1g1d3n lc1g1d2t lc1g1d2n lc1g1d1t lc 1g1d1n xxxx xxxx uuuu uuuu f17h clc1gls1 lc1g2d4t lc1g2d4n lc1g2d3t lc1g2d3n lc1g2d2t lc1g2d2n lc1g2d1t lc 1g2d1n xxxx xxxx uuuu uuuu f18h clc1gls2 lc1g3d4t lc1g3d4n lc1g3d3t lc1g3d3n lc1g3d2t lc1g3d2n lc1g3d1t lc 1g3d1n xxxx xxxx uuuu uuuu f19h clc1gls3 lc1g4d4t lc1g4d4n lc1g4d3t lc1g4d3n lc1g4d2t lc1g4d2n lc1g4d1t lc 1g4d1n xxxx xxxx uuuu uuuu f1ah clc2con lc2en D lc2out lc2intp lc2intn lc2mode<2:0> 0-00 0000 0-00 0000 f1bh clc2pol lc2pol D D D lc2g4pol lc2g3pol lc2g2pol lc2g1pol 0--- xxxx 0--- uuuu f1ch clc2sel0 D D lc2d1s<5:0> --xx xxxx --uu uuuu f1dh clc2sel1 D D lc2d2s<5:0> --xx xxxx --uu uuuu f1eh clc2sel2 D D lc2d3s<5:0> --xx xxxx --uu uuuu f1fh clc2sel3 D D lc2d4s<5:0> --xx xxxx --uu uuuu table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 51 pic16(l)f18326/18346 bank 30 cpu core registers; see table 3-2 for specifics f20h clc2gls0 lc2g1d4t lc2g1d4n lc2g1d3t lc2g1d3n lc2g1d2t lc2g1d2n lc2g1d1t lc 2g1d1n xxxx xxxx uuuu uuuu f21h clc2gls1 lc2g2d4t lc2g2d4n lc2g2d3t lc2g2d3n lc2g2d2t lc2g2d2n lc2g2d1t lc 2g2d1n xxxx xxxx uuuu uuuu f22h clc2gls2 lc2g3d4t lc2g3d4n lc2g3d3t lc2g3d3n lc2g3d2t lc2g3d2n lc2g3d1t lc 2g3d1n xxxx xxxx uuuu uuuu f23h clc2gls3 lc2g4d4t lc2g4d4n lc2g4d3t lc2g4d3n lc2g4d2t lc2g4d2n lc2g4d1t lc 2g4d1n xxxx xxxx uuuu uuuu f24h clc3con lc3en D lc3out lc3intp lc3intn lc3mode<2:0> 0-00 0000 0-00 0000 f25h clc3pol lc3pol D D D lc3g4pol lc3g3pol lc3g2pol lc3g1pol 0--- xxxx 0--- uuuu f26h clc3sel0 D D lc3d1s<5:0> --xx xxxx --uu uuuu f27h clc3sel1 D D lc3d2s<5:0> --xx xxxx --uu uuuu f28h clc3sel2 D D lc3d3s<5:0> --xx xxxx --uu uuuu f29h clc3sel3 D D lc3d4s<5:0> --xx xxxx --uu uuuu f2ah clc3gls0 lc3g1d4t lc3g1d4n lc3g1d3t lc3g1d3n lc3g1d2t lc3g1d2n lc3g1d1t lc 3g1d1n xxxx xxxx uuuu uuuu f2bh clc3gls1 lc3g2d4t lc3g2d4n lc3g2d3t lc3g2d3n lc3g2d2t lc3g2d2n lc3g2d1t lc 3g2d1n xxxx xxxx uuuu uuuu f2ch clc3gls2 lc3g3d4t lc3g3d4n lc3g3d3t lc3g3d3n lc3g3d2t lc3g3d2n lc3g3d1t lc3g3d1n xxxx xxxx uuuu uuuu f2dh clc3gls3 lc3g4d4t lc3g4d4n lc3g4d3t lc3g4d3n lc3g4d2t lc3g4d2n lc3g4d1t lc3g4d1n xxxx xxxx uuuu uuuu f2eh clc4con lc4en D lc4out lc4intp lc4intn lc4mode<2:0> 0-00 0000 0-00 0000 f2fh clc4pol lc4pol D D D lc4g4pol lc4g3pol lc4g2pol lc4g1pol 0--- xxxx 0--- uuuu f30h clc4sel0 D D lc4d1s<5:0> --xx xxxx --uu uuuu f31h clc4sel1 D D lc4d2s<5:0> --xx xxxx --uu uuuu f32h clc4sel2 D D lc4d3s<5:0> --xx xxxx --uu uuuu f33h clc4sel3 D D lc4d4s<5:0> --xx xxxx --uu uuuu f34h clc4gls0 lc4g1d4t lc4g1d4n lc4g1d3t lc4g1d3n lc4g1d2t lc4g1d2n lc4g1d1t lc 4g1d1n xxxx xxxx uuuu uuuu f35h clc4gls1 lc4g2d4t lc4g2d4n lc4g2d3t lc4g2d3n lc4g2d2t lc4g2d2n lc4g2d1t lc 4g2d1n xxxx xxxx uuuu uuuu f36h clc4gls2 lc4g3d4t lc4g3d4n lc4g3d3t lc4g3d3n lc4g3d2t lc4g3d2n lc4g3d1t lc 4g3d1n xxxx xxxx uuuu uuuu f37h clc4gls3 lc4g4d4t lc4g4d4n lc4g4d3t lc4g4d3n lc4g4d2t lc4g4d2n lc4g4d1t lc 4g4d1n xxxx xxxx uuuu uuuu table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 52 preliminary ? 2016 microchip technology inc. bank 31 ? only accessible from debug executive, unless otherwise specified cpu core registers; see table 3-2 for specifics f8ch to fe3h unimplemented fe4h (2) status_shad zd cc ---- -xxx ---- -uuu fe5h (2) wreg_shad working register normal (non-icd) shadow xxxx xxxx uuuu uuuu fe6h (2) bsr_shad bank select register normal (non-icd) shadow ---x xxxx ---u uuuu fe7h (2) pclath_shad program counter latch high register normal (non-icd) shadow -xxx xxxx -uuu uuuu fe8h (2) fsr0l_shad indirect data memory address 0 low pointer normal (non-icd) shadow xxxx xxxx uuuu uuuu fe9h (2) fsr0h_shad indirect data memory address 0 high pointer normal (non-icd) shadow xxxx xxxx uuuu uuuu feah (2) fsr1l_shad indirect data memory address 1 low pointer normal (non-icd) shadow xxxx xxxx uuuu uuuu febh (2) fsr1h_shad indirect data memory address 1 high pointer normal (non-icd) shadow xxxx xxxx uuuu uuuu fech unimplemented fedh (2) stkptr current stack pointer ---x xxxx ---1 1111 feeh (2) tosl top of stack low byte xxxx xxxx xxxx xxxx fefh (2) tosh top of stack high byte -xxx xxxx -xxx xxxx table 3-4: special function regist er summary banks 0-31 (continued) address name pic16(l)f18326 pic16(l)f18346 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets legend: x = unknown, u = unchanged, q = depends on condition, - = unimplemented, read as 0 , r = reserved. shaded locations unimplemented, read as 0 . note 1: only on pic16f18326/18346. 2: register accessible from both user and icd debugger. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 53 pic16(l)f18326/18346 3.3 pcl and pclath the program counter (pc) is 15 bits wide. the low byte comes from the pcl register, which is a readable and writable register. the high byte (pc<14:8>) is not directly readable or writable and comes from pclath. on any reset, the pc is cleared. figure 3-3 shows the five situations for the loading of the pc. figure 3-3: loading of pc in different situations 3.3.1 modifying pcl executing any instruction with the pcl register as the destination simultaneously causes the program counter pc<14:8> bits (pch) to be replaced by the contents of the pclath register. this allows the entire contents of the program counter to be changed by writing the desired upper seven bits to the pclath register. when the lower eight bits are written to the pcl register, all 15 bits of the program counter will change to the values contained in the pclath register and those being written to the pcl register. 3.3.2 computed goto a computed goto is accomplished by adding an offset to the program counter ( addwf pcl ). when performing a table read using a computed goto method, care should be exercised if the table location crosses a pcl memory boundary (each 256-byte block). refer to application note an556, implementing a table read (ds00556). 3.3.3 computed function calls a computed function call allows programs to maintain tables of functions and provide another way to execute state machines or look-up tables. when performing a table read using a computed function call , care should be exercised if the table location crosses a pcl memory boundary (each 256-byte block). if using the call instruction, the pch<2:0> and pcl registers are loaded with the operand of the call instruction. pch<6:3> is loaded with pclath<6:3>. the callw instruction enables computed calls by combining pclath and w to form the destination address. a computed callw is accomplished by loading the w register with the desired address and executing callw . the pcl register is loaded with the value of w and pch is loaded with pclath. 3.3.4 branching the branching instructions add an offset to the pc. this allows relocatable code and code that crosses page boundaries. there are two forms of branching, brw and bra . the pc will have incremented to fetch the next instruction in both cases. when using either branching instruction, a pcl memory boundary may be crossed. if using brw , load the w register with the desired unsigned address and execute brw . the entire pc will be loaded with the address pc + 1 + w . if using bra , the entire pc will be loaded with pc + 1 + the signed value of the operand of the bra instruction. pcl pch 0 14 pc pcl pch 0 14 pc alu result 8 7 6 pclath 0 instruction with pcl as destination goto, call opcode <10:0> 11 4 6 pclath 0 pcl pch 0 14 pc w 8 7 6 pclath 0 callw pcl pch 0 14 pc pc + w 15 brw pcl pch 0 14 pc pc + opcode <8:0> 15 bra downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 54 preliminary ? 2016 microchip technology inc. 3.4 stack all devices have a 16-level x 15-bit wide hardware stack (refer to figure 3-4 through figure 3-7 ). the stack space is not part of either program or data space. the pc is pushed onto the stack when call or callw instructions are executed or an interrupt causes a branch. the stack is poped in the event of a return , retlw or a retfie instruction execution. pclath is not affected by a push or pop operation. the stack operates as a circular buffer if the stvren bit is programmed to 0 (configuration words). this means that after the stack has been pushed sixteen times, the seventeenth push overwrites the value that was stored from the first push. the eighteenth push overwrites the second push (and so on). the stkovf and stkunf flag bits will be set on an overflow/underflow, regardless of whether the reset is enabled. 3.4.1 accessing the stack the stack is available through the tosh, tosl and stkptr registers. stkptr is the current value of the stack pointer. tosh:tosl register pair points to the top of the stack. both registers are read/writable. tos is split into tosh and tosl due to the 15-bit size of the pc. to access the stack, adjust the value of stkptr, which will position tosh:tosl, then read/write to tosh:tosl. stkptr is five bits to allow detection of overflow and underflow. during normal program operation, call, callw and interrupts will increment stkptr while retlw , return , and retfie will decrement stkptr. at any time, stkptr can be inspected to see how much stack is left. the stkptr always points at the currently used place on the stack. therefore, a call or callw will increment the stkptr and then write the pc, and a return will unload the pc and then decrement the stkptr. reference figure 3-4 through figure 3-7 for examples of accessing the stack. figure 3-4: accessing the stack example 1 note 1: there are no instructions/mnemonics called push or pop. these are actions that occur from the execution of the call, callw , return , retlw and retfie instructions or the vectoring to an interrupt address. note: care should be taken when modifying the stkptr while interrupts are enabled. 0x0f 0x0e 0x0d 0x0c 0x0b 0x0a 0x09 0x08 0x07 0x06 0x05 0x04 0x03 0x02 0x01 0x00 0x0000 stkptr = 0x1f initial stack configuration: after reset, the stack is empty. the empty stack is initialized so the stack pointer is pointing at 0x1f. if the stack overflow/underflow reset is enabled, the tosh/tosl registers will return 0 . if the stack overflow/underflow reset is disabled, the tosh/tosl registers will return the contents of stack address 0x0f. 0x1f stkptr = 0x1f stack reset disabled (stvren = 0 ) stack reset enabled (stvren = 1 ) tosh:tosl tosh:tosl downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 55 pic16(l)f18326/18346 figure 3-5: accessing the stack example 2 figure 3-6: accessing the stack example 3 0x0f 0x0e 0x0d 0x0c 0x0b 0x0a 0x09 0x08 0x07 0x06 0x05 0x04 0x03 0x02 0x01 return address 0x00 stkptr = 0x00 this figure shows the stack configuration after the first call or a single interrupt. if a return instruction is executed, the return addre ss will be placed in the program counter and the stack pointer decremented to the empty state (0x1f). tosh:tosl 0x0f 0x0e 0x0d 0x0c 0x0b 0x0a 0x09 0x08 0x07 return address 0x06 return address 0x05 return address 0x04 return address 0x03 return address 0x02 return address 0x01 return address 0x00 stkptr = 0x06 after seven call s or six call s and an interrupt, the stack looks like the figure on the left. a series of return instructions will repeatedly place the return addresses into the program counter and pop the stack. tosh:tosl downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 56 preliminary ? 2016 microchip technology inc. figure 3-7: accessing the stack example 4 3.4.2 overflow/underflow reset if the stvren bit in configuration words is programmed to 1 , the device will be reset if the stack is pushed beyond the sixteenth level or poped beyond the first level, setting the appropriate bits (stkovf or stkunf, respectively) in the pcon register. 0x0f 0x0e 0x0d 0x0c 0x0b 0x0a 0x09 0x08 0x07 0x06 0x05 0x04 0x03 0x02 0x01 return address 0x00 stkptr = 0x10 when the stack is full, the next call or an interrupt will set the stack pointer to 0x10. this is identical to address 0x00 so the stack will wrap and overwrite the return address at 0x00. if the stack overflow/underflow reset is enabled, a reset will occur and location 0x00 will not be overwritten. return address return address return address return address return address return address return address return address return address return address return address return address return address return address return address tosh:tosl downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 57 pic16(l)f18326/18346 3.5 indirect addressing the indfn registers are not physical registers. any instruction that accesses an indfn register actually accesses the register at the address specified by the file select registers (fsr). if the fsrn address specifies one of the two indfn registers, the read will return 0 and the write will not occur (though status bits may be affected). the fsrn register value is created by the pair fsrnh and fsrnl. the fsr registers form a 16- bit address that allows an addressing space with 65536 locations. these locations are divided into four memory regions: traditional data memory linear data memory program flash memory eeprom 3.5.1 traditional data memory the traditional data memory is a region from fsr address 0x000 to fsr address 0xfff. the addresses correspond to the absolute addresses of all sfr, gpr and common registers. figure 3-8: traditio nal data memory map indirect addressing direct addressing bank select location select 4b s r 6 0 from opcode fsrxl 70 bank select location select 00000 00001 00010 11111 0x00 0x7f bank 0 bank 1 bank 2 bank 31 0 fsrxh 70 0000 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 58 preliminary ? 2016 microchip technology inc. 3.5.2 linear data memory the linear data memory is the region from fsr address 0x2000 to fsr address 0x29af. this region is a virtual region that points back to the 80-byte blocks of gpr memory in all the banks. unimplemented memory reads as 0x00. use of the linear data memory region allows buffers to be larger than 80 bytes because incrementing the fsr beyond one bank will go directly to the gpr memory of the next bank. the 16 bytes of common memory are not included in the linear data memory region. figure 3-9: linear data memory map 3.5.3 program flash memory to make constant data access easier, the entire program flash memory is mapped to the upper half of the fsr address space. when the msb of fsrnh is set, the lower 15 bits are the address in program memory which will be accessed through indf. only the lower eight bits of each memory location are accessible via indf. writing to the program flash memory cannot be accomplished via the fsr/indf interface. all instructions that access program flash memory via the fsr/indf interface will require one additional instruction cycle to complete. figure 3-10: program flash memory map 7 0 1 7 0 0 location select 0x2000 fsrnh fsrnl 0x020 bank 0 0x06f 0x0a0 bank 1 0x0ef 0x120 bank 2 0x16f 0xf20 bank 30 0xf6f 0x29af 0 7 1 7 0 0 location select 0x8000 fsrnh fsrnl 0x0000 0x7fff 0xffff program flash memory (low 8 bits) downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 59 pic16(l)f18326/18346 4.0 device configuration device configuration consists of configuration words, code protection and device id. 4.1 configuration words there are several configuration word bits that allow different oscillator and memory protection options. these are implemented as configuration word 1 at 8007h, configuration word 2 at 8008h, configuration word 3 at 8009h, and configuration word 4 at 800ah. note: the debug bit in configuration words is managed automatically by device development tools including debuggers and programmers. for normal device operation, this bit should be maintained as a 1 . downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 60 preliminary ? 2016 microchip technology inc. 4.2 register definitions: configuration words register 4-1: configurat ion word 1: oscillators r/p-1 u-1 r/p-1 u-1 u-1 r/p-1 fcmen cswen c l k o u t e n bit 13 bit 8 u-1 r/p-1 r/p-1 r/p-1 u-1 r/p-1 r/p-1 r/p-1 rstosc2 rstosc1 rstosc0 fextosc2 fextosc1 fextosc0 bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 1 0 = bit is cleared 1 = bit is set n = value when blank or after bulk erase bit 13 fcmen: fail-safe clock monitor enable bit 1 = on fscm timer enabled 0 = off fscm timer disabled bit 12 unimplemented: read as 1 bit 11 cswen: clock switch enable bit 1 = on writing to nosc and ndiv is allowed 0 = off the nosc and ndiv bits cannot be changed by user software bit 10-9 unimplemented: read as 1 bit 8 clkouten : clock out enable bit if fextosc = ec , hs, ht or lp, then this bit is ignored; otherwise: 1 = off clkout function is disabled; i/o or oscillator function on osc2 0 = on clkout function is enabled; f osc /4 clock appears at osc2 bit 7 unimplemented: read as 1 bit 6-4 rstosc<2:0>: power-up default value for cosc bits this value is the reset default value for cosc, and selects the oscillator first used by user software 111 = ext1x extosc operating per fextosc<2:0> bits 110 = hfint1 hfintosc (1 mhz) 101 = reserved 100 = lfint lfintosc 011 = sosc sosc (32.768 khz) 010 = reserved 001 = ext4x extosc with 4x pll; extosc operating per fextosc<2:0> bits 000 = hfint32 hfintosc (32 mhz) bit 3 unimplemented: read as 1 bit 2-0 fextosc<2:0>: fextosc external oscillator mode selection bits 111 = ech ec ( external clock) above 8 mhz 110 = ecm ec ( external clock) for 100 khz to 8 mhz 101 = ecl ec ( external clock) below 100 khz 100 = off oscillator not enabled 011 = unimplemented 010 = hs hs ( crystal oscillator) above 8 mhz 001 = xt ht ( crystal oscillator) above 100 khz, below 8 mhz 000 = lp lp ( crystal oscillator) optimized for 32.768 khz downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 61 pic16(l)f18326/18346 register 4-2: co nfiguration word 2: supervisors r/p-1 r/p-1 r/p-1 u-1 r/p-1 u-1 debug stvren pps1way b o r v bit 13 bit 8 r/p-1 r/p-1 r/p-1 u-1 r/p-1 r/p-1 r/p-1 r/p-1 boren1 boren0 lpboren wdte1 wdte0 pwrte mclre bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 1 0 = bit is cleared 1 = bit is set n = value when blank or after bulk erase bit 13 debug : debugger enable bit (1) 1 = off background debugger disabled; icspclk and icspdat are general purpose i/o pins 0 = on background debugger enabled; icspclk and icspdat are dedicated to the debugger bit 12 stvren: stack overflow/underflow reset enable bit 1 = on stack overflow or underflow will cause a reset 0 = off stack overflow or underflow will not cause a reset bit 11 pps1way: ppslock one-way set enable bit 1 = on the ppslocked bit can be cleared and set only once; pps registers remain locked after one clear/set cycle 0 = off the ppslocked bit can be set and cleared repeatedly (subject to the unlock sequence) bit 10 unimplemented: read as 1 bit 9 borv: brown-out reset voltage selection bit (2) 1 = low brown-out reset voltage ( v bor ) set to 1.9v on lf, and 2.45v on f devices 0 = high brown-out reset voltage ( v bor ) set to 2.7v the higher voltage setting is recommended for operation at or above 16 mhz. bit 8 unimplemented: read as 1 bit 7-6 boren<1:0>: brown-out reset enable bits when enabled, brown-out reset voltage ( v bor ) is set by the borv bit 11 = on brown-out reset is enabled; sboren bit is ignored 10 = sleep brown-out reset is enabled while running, di sabled in sleep; sboren bit is ignored 01 = sboren brown-out reset is enabled according to sboren 00 = off brown-out reset is disabled bit 5 lpboren : low-power bor enable bit 1 = off ulpbor is disabled 0 = on ulpbor is enabled bit 4 unimplemented: read as 1 bit 3-2 wdte<1:0>: watchdog timer enable bit 11 = on wdt is enabled; swdten is ignored 10 = sleep wdt is enabled while running and disabled in sleep/idle; swdten is ignored 01 = swdten wdt is controlled by the swdten bit in the wdtcon register 00 = off wdt is disabled; swdten is ignored bit 1 pwrte : power-up timer enable bit 1 = off pwrt is disabled 0 = on pwrt is enabled bit 0 mclre: master clear (mclr ) enable bit if lvp = 1 : ra3 pin function is mclr . if lvp = 0 : 1 = on mclr pin is mclr . 0 = off mclr pin function is port-defined function. note 1: the debug bit in configuration words is managed automa tically by device development tools including debuggers and programmers. for normal device operation, this bit should be maintained as a 1 . 2: see v bor parameter for specific trip point voltages. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 62 preliminary ? 2016 microchip technology inc. register 4-3: config uration word 3: memory r/p-1 u-1 u-1 u-1 u-1 u-1 lvp (1) bit 13 bit 8 u-1 u-1 u-1 u-1 u-1 u-1 r/p-1 r/p-1 wrt1 wrt0 bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 1 0 = bit is cleared 1 = bit is set n = value when blank or after bulk erase bit 13 lvp: low-voltage programming enable bit (1) 1 = on low-voltage programming is enabled. mclr /v pp pin function is mclr . mclre configuration bit is ignored. 0 = off hv on mclr /v pp must be used for programming. bit 12-2 unimplemented: read as 1 bit 1-0 wrt<1:0>: user nvm self-write protection bits 11 = off write protection off 10 = boot 0000h to 01ffh write-protected, 0200h to 07ffh may be modified 01 = half 0000h to 1fffh write-protected, 2000h to 3fffh may be modified 00 = all 0000h to 3fffh write-protected, no addresses may be modified wrt applies only to the self-write feature of the device; writing through icsp? is never protected. note 1: the lvp bit cannot be programmed to 0 when programming mode is entered via lvp. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 63 pic16(l)f18326/18346 register 4-4: conf iguration word 4: code protection u-1 u-1 u-1 u-1 u-1 u-1 bit 13 bit 8 u-1 u-1 u-1 u-1 u-1 u-1 r/p-1 r/p-1 c p d cp bit 7 bit 0 legend: r = readable bit p = programmable bit u = unimplemented bit, read as 1 0 = bit is cleared 1 = bit is set n = value when blank or after bulk erase bit 13-2 unimplemented: read as 1 bit 1 cpd : data eeprom memory code protection bit 1 = off data eeprom code protection disabled 0 = on data eeprom code protection enabled bit 0 cp : program memory code protection bit 1 = off program memory code protection disabled 0 = on program memory code protection enabled downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 64 preliminary ? 2016 microchip technology inc. 4.3 code protection code protection allows the device to be protected from unauthorized access. program memory protection and data memory are controlled independently. internal access to the program memory is unaffected by any code protection setting. 4.3.1 program memory protection the entire program memory space is protected from external reads and writes by the cp bit in configuration words. when cp = 0 , external reads and writes of program memory are inhibited and a read will return all 0 s. the cpu can continue to read program memory, regardless of the protection bit settings. self-write writing the program memory is dependent upon the write protection setting. see section 4.4 ?write protection? for more information. 4.3.2 data memory protection the entire data eeprom is protected from external reads and writes by the cpd bit in the configuration words. when cpd = 0 , external reads and writes of eeprom memory are inhibited and a read will return all 0 s. the cpu can continue to read and write eeprom memory, regardless of the protection bit settings. 4.4 write protection write protection allows the device to be protected from unintended self-writes. applications, such as boot loader software, can be protected while allowing other regions of the program memory to be modified. the wrt<1:0> bits in configuration words define the size of the program memory block that is protected. 4.5 user id four memory locations (8000h-8003h) are designated as id locations where the user can store checksum or other code identification numbers. these locations are readable and writable during normal execution. see section 10.4.7 ?nvmreg eeprom, user id, device id and configuration word access? for more information on accessing these memory locations. for more information on checksum calculation, see the ?pic16(l)f183xx memory programming specification? (ds40001738) . 4.6 device id and revision id the 14-bit device id word is located at 8006h and the 14-bit revision id is located at 8005h. these locations are read-only and cannot be erased or modified. see section 10.4 ?nvmreg access? for more information on accessing these memory locations. development tools, such as device programmers and debuggers, may be used to read the device id and revision id. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 65 pic16(l)f18326/18346 4.7 register definitions: device and revision register 4-5: devid: device id register rrrrrr dev<13:8> bit 13 bit 8 rrrrrrrr dev<7:0> bit 7 bit 0 legend: r = readable bit 1 = bit is set 0 = bit is cleared bit 13-0 dev<13:0>: device id bits register 4-6: revid: revision id register r - 1r - 0rrrr rev<13:8> bit 13 bit 8 rrrrrrrr rev<7:0> bit 7 bit 0 legend: r = readable bit 1 = bit is set 0 = bit is cleared bit 13-0 rev<13:0>: revision id bits note: the upper two bits of the revision id register will always read 10 . device devid<13:0> values pic16f18326 11 0000 1010 0100 (30a4) pic16lf18326 11 0000 1010 0110 (30a6) pic16f18346 11 0000 1010 0101 (30a5) pic16lf18346 11 0000 1010 0111 (30a7) downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 66 preliminary ? 2016 microchip technology inc. 5.0 resets there are multiple ways to reset this device: power-on reset (por) brown-out reset (bor) low-power brown-out reset (lpbor) mclr reset wdt reset reset instruction stack overflow stack underflow programming mode exit to allow v dd to stabilize, an optional power-up timer can be enabled to extend the reset time after a bor or por event. a simplified block diagram of the on-chip reset circuit is shown in figure 5-1 . figure 5-1: simplified block di agram of on-chip reset circuit note 1: see table 5-1 for bor active conditions. device reset power-on reset wdt time-out brown-out reset lpbor reset reset instruction mclre sleep bor active (1) pwrte lfintosc v dd icsp programming mode exit stack underflow stack overlfow v pp /mclr r power-up timer rev. 10-000006a 8/14/2013 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 67 pic16(l)f18326/18346 5.1 power-on reset (por) the por circuit holds the device in reset until v dd has reached an acceptable level for minimum operation. slow rising v dd , fast operating speeds or analog performance may require greater than minimum v dd . the pwrt, bor or mclr features can be used to extend the start-up period until all device operation conditions have been met. 5.2 brown-out reset (bor) the bor circuit holds the device in reset while v dd is below a selectable minimum level. between the por and bor, complete voltage range coverage for execu- tion protection can be implemented. the brown-out reset module has four operating modes controlled by the boren<1:0> bits in configuration words. the four operating modes are: bor is always on bor is off when in sleep bor is controlled by software bor is always off refer to table 5-1 for more information. the brown-out reset voltage level is selectable by configuring the borv bit in configuration words. a v dd noise rejection filter prevents the bor from triggering on small events. if v dd falls below v bor for a duration greater than parameter t bordc , the device will reset. see figure 5-2 for more information. 5.2.1 bor is always on when the boren bits of configuration words are programmed to 11 , the bor is always on. the device start-up will be delayed until the bor is ready and v dd is higher than the bor threshold. bor protection is active during sleep. the bor does not delay wake-up from sleep. 5.2.2 bor is off in sleep when the boren bits of configuration words are programmed to 10 , the bor is on, except in sleep. the device start-up will be delayed until the bor is ready and v dd is higher than the bor threshold. bor protection is not active during sleep, but device wake-up will be delayed until the bor can determine that v dd is higher than the bor threshold. the device wake-up will be delayed until the bor is ready. table 5-1: bor operating modes boren<1:0> sboren device mode bor mode instruction execution upon: release of por or wake-up from sleep 11 x x active waits for release of bor (1) (borrdy = 1 ) 10 x awake active waits for release of bor (borrdy = 1 ) sleep disabled bor ignored when asleep 01 1 x active waits for release of bor (1) (borrdy = 1 ) 0 x disabled begins immediately (borrdy = x ) 00 x x disabled note 1: in these specific cases, release of por and wake-up from sleep, there is no delay in start-up. the bor ready flag, (borrdy = 1 ), will be set before the cpu is ready to execute instructions because the bor circuit is forced on by the boren<1:0> bits. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 68 preliminary ? 2016 microchip technology inc. 5.2.3 bor controlled by software when the boren bits of configuration words are programmed to 01 , the bor is controlled by the sboren bit of the borcon register. the device wake from sleep is not delayed by the bor ready condition or the v dd level. bor protection begins as soon as the bor circuit is ready. the status of the bor circuit is reflected in the borrdy bit of the borcon register. 5.2.3.1 bor protection is unchanged by sleep figure 5-2: brown -out situations 5.2.4 bor always off when the boren bits of configuration word 2 are programmed to 00 , the bor is always disable. in the configuration, setting the swboren bit will have no affect on bor operation. t pwrt (1) v bor v dd internal reset v bor v dd internal reset t pwrt (1) < t pwrt t pwrt (1) v bor v dd internal reset note 1: t pwrt delay only if pwrte bit is programmed to 0 . downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 69 pic16(l)f18326/18346 5.3 low-power brown-out reset (lpbor) the low-power brown-out reset (lpbor) is an essential part of the reset subsystem. refer to figure 5-1 to see how the bor interacts with other modules. the lpbor is used to monitor the external v dd pin. when too low of a voltage is detected, the device is held in reset. when this occurs, a register bit (bor ) is changed to indicate that a bor reset has occurred. the same bit is set for both the bor and the lpbor. refer to register 5-2 . 5.3.1 enabling lpbor the lpbor is controlled by the lpbor bit of configuration words. when the device is erased, the lpbor module defaults to disabled. 5.3.1.1 lpbor module output the output of the lpbor module is a signal indicating whether or not a reset is to be asserted. this signal is ord together with the reset signal of the bor module to provide the generic bor signal, which goes to the pcon register and to the power control block. 5.4 mclr the mclr is an optional external input that can reset the device. the mclr function is controlled by the mclre bit of configuration words and the lvp bit of configuration words ( table 5-2 ). 5.4.1 mclr enabled when mclr is enabled and the pin is held low, the device is held in reset. the mclr pin is connected to v dd through an internal weak pull-up. the device has a noise filter in the mclr reset path. the filter will detect and ignore small pulses. 5.4.2 mclr disabled when mclr is disabled, the pin functions as a general purpose input and the internal weak pull-up is under software control. see section 11.2 ?porta registers? for more information. 5.5 watchdog timer (wdt) reset the watchdog timer generates a reset if the firmware does not issue a clrwdt instruction within the time-out period. the to and pd bits in the status register as well as the rwdt bit in the pcon register, are changed to indicate the wdt reset. see section 9.0 ?watchdog timer (wdt)? for more information. 5.6 reset instruction a reset instruction will cause a device reset. the ri bit in the pcon register will be set to 0 . see ta b l e 5 - 4 for default conditions after a reset instruction has occurred. 5.7 stack overflow/underflow reset the device can reset when the stack overflows or underflows. the stkovf or stkunf bits of the pcon register indicate the reset condition. these resets are enabled by setting the stvren bit in configuration words. see section 3.4.2 ?overflow/underflow reset? for more information. 5.8 programming mode exit upon exit of programming mode, the device will behave as if a device reset had just occurred. 5.9 power-up timer the power-up timer provides a nominal 64 ms time-out on por or brown-out reset. the device is held in reset as long as pwrt is active. the pwrt delay allows additional time for the v dd to rise to an acceptable level. the power-up timer is enabled by clearing the pwrte bit in configuration words. the power-up timer starts after the release of the por and bor. for additional information, refer to application note an607 , power-up trouble shooting (ds00607). table 5-2: m clr configuration mclre lvp mclr 00 disabled 10 enabled x1 enabled note: a reset does not drive the mclr pin low. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 70 preliminary ? 2016 microchip technology inc. 5.10 start-up sequence upon the release of a por or bor, the following must occur before the device will begin executing: 1. power-up timer runs to completion (if enabled). 2. mclr must be released (if enabled). 3. oscillator start-up timer runs to completion (if required for oscillator source). the total time out will vary based on oscillator configuration and power-up timer configuration. see section 6.0, oscillator module (with fail-safe clock monitor) for more information. the power-up timer and oscillator start-up timer run independently of mclr reset. if mclr is kept low long enough, the power-up timer will expire. upon bringing mclr high, the device will begin execution after ten f osc cycles (see figure 5-3 ). this is useful for testing purposes or to synchronize more than one device operating in parallel. figure 5-3: reset start-up sequence t ost t mclr t pwrt v dd internal por power-up timer mclr internal reset oscillator modes oscillator start-up timer oscillator f osc internal oscillator oscillator f osc external clock (ec) clkin f osc external crystal downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 71 pic16(l)f18326/18346 5.11 determining the cause of a reset upon any reset, multiple bits in the status and pcon registers are updated to indicate the cause of the reset. ta b l e 5 - 3 and tab le 5 - 4 show the reset conditions of these registers. table 5-3: reset status bits and their significance stkovf stkunf rwdt rmclr ri por bor to pd condition 001110x1 1 power-on reset 001110x0 x illegal, to is set on por 001110xx 0 illegal, pd is set on por 00u11u01 1 brown-out reset uu0uuuu0 u wdt reset uuuuuuu0 0 wdt wake-up from sleep uuuuuuu1 0 interrupt wake-up from sleep uuu0uuuu u mclr reset during normal operation uuu0uuu1 0 mclr reset during sleep u u u u 0 u u u u reset instruction executed 1uuuuuuu u stack overflow reset (stvren = 1 ) u1uuuuuu u stack underflow reset (stvren = 1 ) table 5-4: reset condition for special registers condition program counter status register pcon0 register power-on reset 0000h ---1 1000 00-- 110x mclr reset during normal operation 0000h ---u uuuu uu-- 0uuu mclr reset during sleep 0000h ---1 0uuu uu-- 0uuu wdt reset 0000h ---0 uuuu uu-0 uuuu wdt wake-up from sleep pc + 1 ---0 0uuu uu-u uuuu brown-out reset 0000h ---1 1000 00-1 11u0 interrupt wake-up from sleep pc + 1 (1) ---1 0uuu uu-u uuuu reset instruction executed 0000h ---u uuuu uu-u u0uu stack overflow reset (stvren = 1 ) 0000h ---u uuuu 1u-u uuuu stack underflow reset (stvren = 1 ) 0000h ---u uuuu u1-u uuuu legend: u = unchanged, x = unknown, - = unimplemented bit, reads as 0 . note 1: when the wake-up is due to an interrupt and global enable bit (gie) is set, the return address is pushed on the stack and pc is loaded with the interrupt vector (0004h) after execution of pc + 1. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 72 preliminary ? 2016 microchip technology inc. 5.12 power control (pcon) register the power control (pcon) register contains flag bits to differentiate between a: power-on reset (por ) brown-out reset (bor ) reset instruction reset (ri ) mclr reset (rmclr ) watchdog timer reset (rwdt ) stack underflow reset (stkunf) stack overflow reset (stkovf) the pcon0 register bits are shown in register 5-2 . hardware will change the corresponding register bit during the reset process; if the reset was not caused by the condition, the bit remains unchanged ( ta b l e 5 - 4 ). software should reset the bit to the inactive state after the restart (hardware will not reset the bit). software may also set any pcon bit to the active state, so that user code may be tested, but no reset action will be generated. register 5-1: borco n: brown-out reset control register r/w-1/u r/w-0-0 u-0 u-0 u-0 u-0 u-0 r-q/u sboren (1) reserved borrdy 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 q = value depends on condition bit 7 sboren: software brown-out reset enable bit (1) if boren <1:0> in configuration words ? 01 : sboren is read/write, but has no effect on the bor. if boren <1:0> in configuration words = 01 : 1 =bor enabled 0 =bor disabled bit 6 reserved: bit must be maintained as 0 bit 5-1 unimplemented: read as 0 bit 0 borrdy: brown-out reset circuit ready status bit 1 = the brown-out reset circuit is active 0 = the brown-out reset circuit is inactive note 1: boren<1:0> bits are located in configuration words. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 73 pic16(l)f18326/18346 5.13 register definitions: power control register 5-2: pcon0: power control register 0 r/w/hs-0/q r/w/hs-0/q u-0 r/w/hc-1/q r/w/ hc-1/q r/w/hc-1/q r/w/hc-q/u r/w/hc-q/u stkovf stkunf rwdt rmclr ri por bor bit 7 bit 0 legend: hc = bit is cleared by hardware hs = bit is set by hardware r = readable bit w = writable bit u = unimplemented bit, read as 0 u = bit is unchanged x = bit is unknown -m/n = value at por and bor/value at all other resets 1 = bit is set 0 = bit is cleared q = value depends on condition bit 7 stkovf: stack overflow flag bit 1 = a stack overflow occurred 0 = a stack overflow has not occurred or cleared by firmware bit 6 stkunf: stack underflow flag bit 1 = a stack underflow occurred 0 = a stack underflow has not occurred or cleared by firmware bit 5 unimplemented: read as 0 bit 4 rwdt : watchdog timer reset flag bit 1 = a watchdog timer reset has not occurred or set to 1 by firmware 0 = a watchdog timer reset has occurred (cleared by hardware) bit 3 rmclr : mclr reset flag bit 1 =a mclr reset has not occurred or set to 1 by firmware 0 =a mclr reset has occurred (cleared by hardware) bit 2 ri : reset instruction flag bit 1 =a reset instruction has not been executed or set to 1 by firmware 0 =a reset instruction has been executed (cleared by hardware) bit 1 por : power-on reset status bit 1 = no power-on reset occurred 0 = a power-on reset occurred (must be set in software after a power-on reset occurs) bit 0 bor : brown-out reset status bit 1 = no brown-out reset occurred 0 = a brown-out reset occurred (must be set in software after a power-on reset or brown-out reset occurs) table 5-5: summary of registers associated with resets name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page borcon sboren borrdy 72 pcon0 stkovf stkunf r w d t rmclr ri por bor 73 status t o pd z dc c 27 wdtcon wdtps<4:0> swdten 118 legend: = unimplemented location, read as 0 . shaded cells are not used by resets. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 74 preliminary ? 2016 microchip technology inc. 6.0 oscillator module (with fail-safe clock monitor) 6.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 performance and minimizing power consumption. figure 6-1 illustrates a block diagram of the oscillator module. clock sources can be supplied from external oscillators, quartz-crystal resonators and ceramic resonators. in addition, the system clock source can be supplied from one of two internal oscillators and pll circuits, with a choice of speeds selectable via software. additional clock features include: selectable system clock source between external or internal sources via software. fail-safe clock monitor (fscm) designed to detect a failure of the external clock source (lp, xt, hs, ech, ecm, ecl) and switch automatically to the internal oscillator. oscillator start-up timer (ost) ensures stability of crystal oscillator sources. the rstosc bits of configuration word 1 determine the type of oscillator that will be used when the device is reset, including when it is first powered-up. the internal clock modes, lfintosc, hfintosc (set at 1 mhz), or hfintosc (set at 32 mhz) can be set through the rstosc bits. if an external clock source is selected, the fextosc bits of configuration word 1 must be used in conjunction with the rstosc bits to select the external clock mode. the external oscillator module can be configured in one of the following clock modes by setting the fextosc<2:0> bits of configuration word 1: 1. ecl C external clock low-power mode (below 500 khz) 2. ecm C external clock medium-power mode (500 khz to 8 mhz) 3. ech C external clock high-power mode (above 8 mhz) 4. lp C 32 khz low-power crystal mode. 5. xt C medium gain crystal or ceramic resonator oscillator mode (between 500 khz and 8 mhz) 6. hs C high gain crystal or ceramic resonator mode (above 8 mhz) the ech, ecm, and ecl clock modes rely on an external logic level signal as the device clock source. the lp, xt, and hs clock modes require an external crystal or resonator to be connected to the device. each mode is optimized for a different frequency range. the intosc internal oscillator block produces low and high-frequency clock sources, designated lfintosc and hfintosc. (see internal oscillator block, figure 6-1 ). downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 75 pic16(l)f18326/18346 figure 6-1: simplified pic ? mcu clock source block diagram rev. 10-000208e 1/22/2015 hffrq< :0> hfintosc secondary oscillator (sosc) external oscillator (extosc) clkin/ osc1 clkout/ osc2 soscin/sosci sosco 31khz oscillator 4x pll 0 000 000 0 00 00 cosc<2:0> lfintosc 1 C 32 mhz oscillator 9-bit postscaler divider 000 000000 00000 000 000 00 00 0 512 256 128 6432 16 84 2 1 cdiv<4:0> sleep idle sleep syscmd system clock peripheral clock fscm sosc_clk to peripherals to peripherals downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 76 preliminary ? 2016 microchip technology inc. 6.2 clock source types clock sources can be classified as external or internal. external clock sources rely on external circuitry for the clock source to function. examples are: oscillator modules (ech, ecm, ecl mode), quartz crystal resonators or ceramic resonators (lp, xt and hs modes). there is also a secondary oscillator block which is optimized for a 32.768 khz external clock source, which can be used as an alternate clock source. there are two internal oscillator blocks: -hfintosc -lfintosc the hfintosc can produce clock frequencies from 1-16 mhz. the lfintosc generates a 31 khz clock frequency. there is a pll that can be used by the external oscilla- tor. see section 6.2.1.4 ?4x pll? for more details. additionally, there is a pll that can be used by the hfintosc at certain frequencies. see section 6.2.2.2 ?2x pll? for more details. 6.2.1 external clock sources an external clock source can be used as the device system clock by performing one of the following actions: program the rstosc<2:0> bits in the configuration words to select an external clock source that will be used as the default system clock upon a device reset. write the nosc<2:0> and ndiv<3:0> bits in the osccon1 register to switch the system clock source. see section 6.3 ?clock switching? for more information. 6.2.1.1 ec mode the external clock (ec) mode allows an externally generated logic level signal to be the system clock source. when operating in this mode, an external clock source is connected to the clkin input. osc2/clkout is available for general purpose i/o or clkout. figure 6-2 shows the pin connections for ec mode. ec mode has three power modes to select from through configuration words: ech C high power, 8-32 mhz ecm C medium power, 0.5-8 mhz ecl C low power, 0-0.5 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 6-2: external clock (ec) mode operation osc1/clkin osc2/clkout clock from ext. system pic ? mcu f osc /4 or i/o (1) note 1: output depends upon clkouten bit of the configuration words. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 77 pic16(l)f18326/18346 6.2.1.2 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 6-3 ). the three modes select 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 designed to drive only 32.768 khz tun- ing-fork type crystals (watch 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 resona- tors with a medium drive level specification. hs oscillator mode selects the highest gain set- ting of the internal inverter-amplifier. hs mode current consumption is the highest of the three modes. this mode is best suited for resonators that require a high-drive setting. figure 6-3 and figure 6-4 show typical circuits for quartz crystal and ceramic resonators, respectively. figure 6-3: quartz crystal operation (lp, xt or hs mode) figure 6-4: 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, reference 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 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 78 preliminary ? 2016 microchip technology inc. 6.2.1.3 oscillator start-up timer (ost) if 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) or a wake-up from sleep. 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. 6.2.1.4 4x pll the oscillator module contains a pll that can be used with external clock sources to provide a system clock source. the input frequency for the pll must fall within specifications. see the pll clock timing specifications in tab l e 3 4- 9 . the pll may be enabled for use by one of two methods: 1. program the rstosc bits in the configuration word 1 to enable the extosc with 4x pll. 2. write the nosc<2:0> bits in the osccon1 register to enable the extosc with 4x pll. 6.2.1.5 secondary oscillator the secondary oscillator is a separate oscillator block that can be used as an alternate system clock source. the secondary oscillator is optimized for 32.768 khz, and can be used with an external crystal oscillator connected to the sosci and sosco device pins, or an external clock source connected to the soscin pin. the secondary oscillator can be selected during run-time using clock switching. refer to section 6.3 ?clock switching? for more information. figure 6-5: quartz crystal operation (secondary oscillator) c1 c2 32.768 khz sosci to internal logic pic ? mcu crystal sosco quartz 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, reference the following microchip application notes: an826, crystal oscillator basics and crystal selection for rfpic ? and pic ? devices (ds00826) an849, basic picmicro ? oscillator design (ds00849) an943, practical picmicro ? oscillator analysis and design (ds00943) an949, making your oscillator work (ds00949) tb097, interfacing a micro crystal ms1v-t1k 32.768 khz tuning fork crystal to a pic16f690/ss (ds91097) an1288, design practices for low-power external oscillators (ds01288) downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 79 pic16(l)f18326/18346 6.2.2 internal clock sources the device may be configured to use the internal oscillator block as the system clock by performing one of the following actions: program the rstosc<2:0> bits in configuration words to select the intosc clock source, which will be used as the default system clock upon a device reset. write the nosc<2:0> bits in the osccon1 register to switch the system clock source to the internal oscillator during run-time. see section 6.3 ?clock switching? for more information. the function of the osc2/clkout pin is determined by the clkouten bit in configuration words. the internal oscillator block has two independent oscillators that can produce two internal system clock sources. 1. the hfintosc (high-frequency internal oscillator) is factory-calibrated and operates up to 32 mhz. the frequency of hfintosc can be selected through the oscfrq frequency selection register, and fine-tuning can be done via the osctune register. 2. the lfintosc (low-frequency internal oscillator) is factory-calibrated and operates at 31 khz. 6.2.2.1 hfintosc the high-frequency internal oscillator (hfintosc) is a precision digitally-controlled internal clock source that produces a stable clock up to 32 mhz. the hfintosc can be enabled through one of the following methods: programming the rstosc<2:0> bits in configuration word 1 to 110 (1 mhz) or 000 (32 mhz) to set the oscillator upon device power-up or reset write to the nosc<2:0> bits of the osccon1 register during run-time the hfintosc frequency can be selected by setting the hffrq<3:0> bits of the oscfrq register. the ndiv<3:0> bits of the osccon1 register allow for division of the output of the selected clock source by a range between 1:1 and 1:512. 6.2.2.2 2x pll the oscillator module contains a pll that can be used with the hfintosc clock source to provide a system clock source. the input frequency to the pll is limited to 8, 12, or 16 mhz, which will yield a system clock source of 16, 24, or 32 mhz, respectively. the pll may be enabled for use by one of two methods: 1. program the rstosc bits in the configuration word 1 to 000 to enable the hfintosc (32 mhz). this setting configures the hffrq<3:0> bits to 110 (16 mhz) and activates the 2x pll. 2. write 000 to the nosc<2:0> bits in the osccon1 register to enable the 2x pll, and write the correct value into the hffrq<3:0> bits of the oscfrq register to select the desired system clock frequency. see register 6-6 for more information. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 80 preliminary ? 2016 microchip technology inc. 6.2.2.3 internal oscillator frequency adjustment the internal oscillator is factory-calibrated. this internal oscillator can be adjusted in software by writing to the osctune register ( register 6-3 ). the default value of the osctune register is 00h. the value is a 6-bit twos complement number. a value of 3fh will provide an adjustment to the maximum frequency. a value of 0h will provide an adjustment to the minimum frequency. when the osctune register is modified, the oscillator frequency will begin shifting to the new frequency. code execution continues during this shift. there is no indication that the shift has occurred. osctune does 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. 6.2.2.4 lfintosc the low-frequency internal oscillator (lfintosc) is a factory-calibrated 31 khz internal clock source. the lfintosc is the clock source for the power-up timer (pwrt), watchdog timer (wdt) and fail-safe clock monitor (fscm). the lfintosc is selected as the clock source through one of the following methods: programming the rstosc<2:0> bits of configuration word 1 to enable lfintosc. write to the nosc<2:0> bits of the osccon1 register. 6.2.2.5 oscillator status and manual enable the ready status of each oscillator is displayed in the oscstat1 register ( register 6-4 ). the oscillators can also be manually enabled through the oscen register ( register 6-5 ). manual enables make it possible to verify the operation of the extosc or sosc crystal oscillators. this can be achieved by enabling the selected oscillator, then watching the corresponding ready state of the oscillator in the oscstat1 register. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 81 pic16(l)f18326/18346 6.3 clock switching the system clock source can be switched between external and internal clock sources via software using the new oscillator source (nosc) and new divider selection request (ndiv) bits of the osccon1 register. the following clock sources can be selected: external oscillator (extosc) high-frequency internal oscillator (hfintosc) low-frequency internal oscillator (lfintosc) secondary oscillator (sosc) extosc with 4x pll hfintosc with 2x pll 6.3.1 new oscillator source (nosc) and new divider selection request (ndiv) bits the new oscillator source (nosc) and new divider selection request (ndiv) bits of the osccon1 register select the system clock source that is used for the cpu and peripherals. when the new values of nosc<2:0> and ndiv<3:0> are written to osccon1, the current oscillator selection will continue to operate as the system clock while waiting for the new source to indicate that it is stable and ready. in some cases, the newly requested source may already be in use, and is ready immediately. in the case of a divider-only change, the new and old sources are the same, so the source will be ready immediately, as well. the device may enter sleep while waiting for the switch as described in section 6.3.3 ?clock switch and sleep? . when the new oscillator is ready, the new oscillator is ready (noscr) bit of osccon3 and the clock switch interrupt flag (cswif) bit of pir3 become set (cswif = 1 ). if clock switch interrupts are enabled (clksie = 1 ), an interrupt will be generated at that time. the oscillator ready (ordy) bit of osccon3 can also be polled to determine when the oscillator is ready in lieu of an interrupt. if the clock switch hold (cswhold) bit of osccon3 is clear, the oscillator switch will occur when the new oscillator ready bit (noscr) is set and the interrupt (if enabled) will be serviced at the new oscillator setting. if cswhold is set, the oscillator switch is suspended, while execution continues using the current (old) clock source. when the noscr bit is set, software should: set cswhold = 0 so the switch can complete, or copy cosc into nosc<2:0> to abandon the switch. if doze is in effect, the switch occurs on the next clock cycle, whether or not the cpu is operating during that cycle. changing the clock post-divider without changing the clock source (i.e., changing f osc from 1 mhz to 2 mhz) is handled in the same manner as a clock source change, as described previously. the clock source will already be active, so the switch is relatively quick. cswhold must be clear (cswhold = 0 ) for the switch to complete. the current cosc and cdiv are indicated in the osccon2 register up to the moment when the switch actually occurs, at which time osccon2 is updated and ordy is set. noscr is cleared by hardware to indicate that the switch is complete. 6.3.2 pll input switch switching between the pll and any non-pll source is managed as described above. the input to the pll is established when nosc<2:0> selects the pll, and maintained by the cosc setting. when nosc<2:0> and cosc select the pll with different input sources, the system continues to run using the cosc setting, and the new source is enabled per nosc<2:0>. when the new oscillator is ready (and cswhold = 0 ), system operation is suspended while the pll input is switched and the pll acquires lock. 6.3.3 clock switch and sleep if osccon1 is written with a new value and the device is put to sleep before the switch completes, the switch will not take place and the device will enter sleep mode. when the device wakes from sleep and the cswhold bit is clear, the device will wake with the new clock active, and the clock switch interrupt flag bit (cswif) will be set. when the device wakes from sleep and the cswhold bit is set, the device will wake with the old clock active and the new clock will be requested again. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 82 preliminary ? 2016 microchip technology inc. figure 6-6: clock sw itch (cswhold = 0 ) figure 6-7: clock sw itch (cswhold = 1 ) note 1: cswif is asserted coincident with noscr; interrupt is serviced at osc#2 speed. 2: the assertion of noscr is hidden from the user because it appears only for the duration of the switch. cswhold noscr osc #2 cswif osccon1 written note 1 user clear osc #1 note 2 ordy note 1: cswif is asserted coincident with noscr, and may be cleared before or after clearing cswhold = 0 . cswhold noscr osc #1 osc #2 cswif osccon1 written note 1 ordy user clear downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 83 pic16(l)f18326/18346 figure 6-8: clo ck switch abandoned note 1: cswif may be cleared before or after rewriting osccon1; cswif is not automatically cleared. 2: ordy = 0 if osccon1 does not match osccon2; a new switch will begin. cswhold noscr osc #1 cswif osccon1 written osccon1 written note 2 ordy note 1 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 84 preliminary ? 2016 microchip technology inc. 6.4 fail-safe clock monitor the fail-safe clock monitor (fscm) allows the device to continue operating should the external oscillator fail. the fscm is enabled by setting the fcmen bit in the configuration words. the fscm is applicable to all external oscillator modes (lp, xt, hs, ec and secondary oscillator). figure 6-9: fscm block diagram 6.4.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 6-9 ). 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 external clock goes low. 6.4.2 fail-safe operation when the external clock fails, the fscm switches the device clock to the hfintosc at 1 mhz clock frequency and sets the bit flag osfif of the pir3 register. setting this flag will generate an interrupt if the osfie bit of the pie3 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, by writing to the nosc<2:0> and ndiv<3:0>bits of the osccon1 register. 6.4.3 fail-safe condition clearing the fail-safe condition is cleared after a reset, executing a sleep instruction or changing the nosc<2:0> and ndiv<3:0> bits of the osccon1 register. when switching to the external oscillator or pll, the ost is restarted. while the ost is running, the device continues to operate from the intosc selected in osccon1. when the ost times out, the fail-safe condition is cleared after successfully switching to the external clock source. the osfif bit should be cleared prior to switching to the external clock source. if the fail-safe condition still exists, the osfif flag will again become set by hardware. 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 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 85 pic16(l)f18326/18346 6.4.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 clock modes so that the external clock signal can be stopped if required. therefore, the device will always be executing code while the ost is operating. figure 6-10: 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 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 86 preliminary ? 2016 microchip technology inc. 6.5 register definitions: oscillator control register 6-1: osccon1: osci llator control register 1 u-0 r/w-f/f (1) r/w-f/f (1) r/w-f/f (1) r/w-q/q (4) r/w-q/q (4) r/w-q/q (4) r/w-q/q (4) n o s c < 2 : 0 > (2,3) ndiv<3:0> (2,3) 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 f = determined by fuse setting bit 7 unimplemented: read as 0 bit 6-4 nosc<2:0>: new oscillator source request bits the setting requests a source oscillator and pll combination per ta b l e 6 - 1 . por value = rstosc ( register 4-2 ). bit 3-0 ndiv<3:0>: new divider selection request bits the setting determines the new postscaler division ratio per ta bl e 6 - 2 . note 1: the default value (f/f) is set equal to the rstosc configuration bits. 2: if nosc is written with a reserved value ( table 6-1 ), the hfintosc will be automatically selected as the clock source. 3: when cswen = 0 , this register is read-only and cannot be changed from the por value. 4: when rstosc = 110 (hfintosc 1 mhz) the ndiv bits will default to ' 0010 ' upon reset; for all other nosc settings, the nvid bits will default to ' 0000 ' upon reset. register 6-2: osccon2: osci llator control register 2 u-0 r-q/q (1) r-q/q (1) r-q/q (1) r-q/q (1) r-q/q (1) r-q/q (1) r-q/q (1) cosc<2:0> cdiv<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 resets 1 = bit is set 0 = bit is cleared bit 7 unimplemented: read as 0 bit 6-4 cosc<2:0>: current oscillator source select bits (read-only) indicates the current source oscillator and pll combination per tab l e 6 - 1 . bit 3-0 cdiv<3:0>: current divider select bits (read-only) indicates the current postscaler division ratio per tab l e 6 - 2 . note 1: the reset value (n/n) will match the nosc<2:0>/ndiv<3:0> bits. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 87 pic16(l)f18326/18346 table 6-1: nosc/cosc bit settings nosc<2:0> cosc<2:0> clock source 111 extosc (1) 110 hfintosc (1 mhz) 101 reserved 100 lfintosc 011 sosc 010 reserved 001 extosc with 4xpll (1) 000 hfintosc with 2x pll (32 mhz) note 1: extosc configured by the fextosc bits of configuration word 1 ( register 4-1 ). table 6-2: ndiv/cdiv bit settings ndiv<3:0> cdiv<3:0> clock divider 1111 C 1010 reserved 1001 512 1000 256 0111 128 0110 64 0101 32 0100 16 0011 8 0010 4 0001 2 0000 1 register 6-3: osccon3: osci llator control register 3 r/w/hc-0/0 r/w-0/0 r/w-0/0 r-0/0 r-0/0 u-0 u-0 u-0 cswhold soscpwr soscbe ordy noscr 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 cswhold: clock switch hold bit 1 = clock switch will hold (with interrupt) when the oscillator selected by nosc is ready 0 = clock switch may proceed when the oscillator selected by nosc is ready; if this bit is set at the time that noscr becomes 1 , the switch and interrupt will occur. bit 6 soscpwr: secondary oscillator power mode select bit if soscbe = 0 1 = secondary oscillator operating in high-power mode 0 = secondary oscillator operating in low-power mode if soscbe = 1 x = bit is ignored bit 5 soscbe: secondary oscillator bypass enable bit 1 = secondary oscillator sosci is configured as an external clock input (st-buffer); sosco is not used. 0 = secondary oscillator is configured as a crystal oscillator using sosco and sosci pins. bit 4 ordy: oscillator ready bit (read-only) 1 = osccon1 = osccon2; the current system clock is the clock specified by nosc 0 = a clock switch is in progress bit 3 noscr: new oscillator is ready bit (read-only) 1 = a clock switch is in progress and the oscillator selected by nosc indicates a ready condition 0 = a clock switch is not in progress, or the nosc-selected oscillator is not yet ready bit 2-0 unimplemented: read as 0 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 88 preliminary ? 2016 microchip technology inc. register 6-4: oscstat1: oscillator status register 1 r-q/q r-q/q u-0 r-q/q r-q/q r-q/q u-0 r-q/q extor hfor l f o rs o ra d o r p l l r 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 extor: extosc (external) oscillator ready bit 1 = the oscillator is ready to be used 0 = the oscillator is not enabled, or is not yet ready to be used. bit 6 hfor: hfintosc oscillator ready bit 1 = the oscillator is ready to be used 0 = the oscillator is not enabled, or is not yet ready to be used. bit 5 unimplemented: read as 0 bit 4 lfor: lfintosc oscillator ready bit 1 = the oscillator is ready to be used 0 = the oscillator is not enabled, or is not yet ready to be used. bit 3 sor: secondary oscillator ready bit 1 = the oscillator is ready to be used 0 = the oscillator is not enabled, or is not yet ready to be used. bit 2 ador: adcrc oscillator ready bit 1 = the oscillator is ready to be used 0 = the oscillator is not enabled, or is not yet ready to be used bit 1 unimplemented: read as 0 bit 0 pllr: pll is ready bit 1 = the pll is ready to be used 0 = the pll is not enabled, the required input so urce is not ready, or the pll is not ready. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 89 pic16(l)f18326/18346 register 6-5: oscen: oscill ator manual enable register r/w-0/0 r/w-0/0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 u-0 u-0 extoen hfoen lfoen soscen adoen 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 extoen: external oscillator manual request enable bit 1 = extosc is explicitly enabled, operating as specified by fextosc 0 = extosc could be enabled by another module bit 6 hfoen: hfintosc oscillator manual request enable bit 1 = hfintosc is explicitly enabled, operating as specified by oscfrq ( register 6-6 ) 0 = hfintosc could be enabled by another module bit 5 unimplemented: read as 0 bit 4 lfoen: lfintosc (31 khz) oscillator manual request enable bit 1 = lfintosc is explicitly enabled 0 = lfintosc could be enabled by another module bit 3 soscen: secondary (timer1) oscillator manual request enable bit 1 = secondary oscillator is explicitly enabled, operating as specified by soscbe and soscpwr 0 = secondary oscillator could be enabled by another module bit 2 adoen: adosc (600 khz) oscillator manual request enable bit 1 = adosc is explicitly enabled 0 = adosc could be enabled by another module bit 1-0 unimplemented: read as 0 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 90 preliminary ? 2016 microchip technology inc. register 6-6: oscfrq: hfintosc frequency selection register u-0 u-0 u-0 u-0 r/w-0/0 r/w-1/1 r/w-1/1 r/w-0/0 hffrq<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 resets 1 = bit is set 0 = bit is cleared bit 7-4 unimplemented: read as 0 bit 3-0 hffrq<3:0>: hfintosc frequency selection bits hffrq<3:0> nominal freq. (mhz) (nosc = 110 ) 2x pll freq. (mhz) (nosc = 000 ) 0000 1 reserved 0001 2 0010 reserved 0011 4 0100 81 6 0101 12 24 0110 16 32 0111 32 reserved 1xxx 32 reserved downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 91 pic16(l)f18326/18346 register 6-7: osctune: hfintosc tuning register u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 hftun<5: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 unimplemented: read as 0 bit 5-0 hftun<5:0>: hfintosc frequency tuning bits 01 1111 = maximum frequency 01 1110 00 0001 00 0000 = center frequency. oscillator module is running at the calibrated frequency (default value). 11 1111 10 0000 = minimum frequency. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 92 preliminary ? 2016 microchip technology inc. table 6-3: summary of 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 osccon1 nosc<2:0> ndiv<3:0> 86 osccon2 cosc<2:0> cdiv<3:0> 86 osccon3 cwshold soscpwr soscbe ordy noscr 87 oscstat1 extor hfor l f o r sor ador pllr 88 oscen extoen hfoen l f o e n soscen adoen 89 oscfrq hffrq<3:0> 90 osctune h f t u n < 5 : 0 > 91 legend: = unimplemented location, read as 0 . shaded cells are not used by clock sources. table 6-4: summary of configurat ion word with clock sources name bits bit -/7 bit -/6 bit 13/5 bit 12/4 bit 11/3 bit 10/2 bit 9/1 bit 8/0 register on page config1 13:8 fcmen cswen c l k o u t e n 60 7:0 rstosc2 rstosc1 rstosc0 fextosc2 fextosc1 fextosc0 legend: = unimplemented location, read as 0 . shaded cells are not used by clock sources. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 93 pic16(l)f18326/18346 7.0 interrupts the interrupt feature allows certain events to preempt normal program flow. firmware is used to determine the source of the interrupt and act accordingly. some interrupts can be configured to wake the mcu from sleep mode. this chapter contains the following information for interrupts: operation interrupt latency interrupts during sleep int pin automatic context saving many peripherals produce interrupts. refer to the corresponding chapters for details. a block diagram of the interrupt logic is shown in figure 7-1 . figure 7-1: interrupt logic tmr0if tmr0ie intf inte iocif iocie interrupt to cpu wake-up (if in sleep mode) gie (tmr1if) pir1<0> pirn<7> peie (tmr1ie) pie1<0> peripheral interrupts pien<7> downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 94 preliminary ? 2016 microchip technology inc. 7.1 operation interrupts are disabled upon any device reset. they are enabled by setting the following bits: gie bit of the intcon register interrupt enable bit(s) for the specific interrupt event(s) peie bit of the intcon register (if the interrupt enable bit of the interrupt event is contained in the piex registers) the pir1, pir2, pir3 and pir4 registers record individual interrupts via interrupt flag bits. interrupt flag bits will be set, regardless of the status of the gie, peie and individual interrupt enable bits. the following events happen when an interrupt event occurs while the gie bit is set: current prefetched instruction is flushed gie bit is cleared current program counter (pc) is pushed onto the stack critical registers are automatically saved to the shadow registers (see ? section 7.5 ?automatic context saving? ) pc is loaded with the interrupt vector 0004h the firmware within the interrupt service routine (isr) should determine the source of the interrupt by polling the interrupt flag bits. the interrupt flag bits must be cleared before exiting the isr to avoid repeated interrupts. because the gie bit is cleared, any interrupt that occurs while executing the isr will be recorded through its interrupt flag, but will not cause the processor to redirect to the interrupt vector. the retfie instruction exits the isr by popping the previous address from the stack, restoring the saved context from the shadow registers and setting the gie bit. for additional information on a specific interrupts operation, refer to its peripheral chapter. 7.2 interrupt latency interrupt latency is defined as the time from when the interrupt event occurs to the time code execution at the interrupt vector begins. the latency for synchronous interrupts is three or four instruction cycles. for asynchronous interrupts, the latency is three to five instruction cycles, depending on when the interrupt occurs. see figure 7-2 and figure 7-3 for more details. note 1: individual interrupt flag bits are set, regardless of the state of any other enable bits. 2: all interrupts will be ignored while the gie bit is cleared. any interrupt occurring while the gie bit is clear will be serviced when the gie bit is set again. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 95 pic16(l)f18326/18346 figure 7-2: interrupt latency 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 clkr pc 0004h 0005h pc inst(0004h) nop gie q1 q2 q3 q4 q1 q2 q3 q4 1 cycle instruction at pc pc inst(0004h) nop 2 cycle instruction at pc fsr addr pc+1 pc+2 0004h 0005h pc inst(0004h) nop gie pc pc-1 3 cycle instruction at pc execute interrupt inst(pc) interrupt sampled during q1 inst(pc) pc-1 pc+1 nop pc new pc/ pc+1 0005h pc-1 pc+1/fsr addr 0004h nop interrupt gie interrupt inst(pc) nop nop fsr addr pc+1 pc+2 0004h 0005h pc inst(0004h) nop gie pc pc-1 3 cycle instruction at pc interrupt inst(pc) nop nop nop inst(0005h) execute execute execute downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 96 preliminary ? 2016 microchip technology inc. figure 7-3: int pin interrupt timing q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 osc1 clkout int pin intf gie instruction flow pc instruction fetched instruction executed interrupt latency pc pc + 1 pc + 1 0004h 0005h inst (0004h) inst (0005h) forced nop inst (pc) inst (pc + 1) inst (pc C 1) inst (0004h) forced nop inst (pc) note 1: intf flag is sampled here (every q1). 2: asynchronous interrupt latency = 3-5 t cy . synchronous latency = 3-4 t cy , where t cy = instruction cycle time. latency is the same whether inst (pc) is a single cycle or a 2-cycle instruction. 3: clkout not available in all oscillator modes. 4: for minimum width of int pulse, refer to ac specifications in section 34.0 ?electrical specifications? ? . 5: intf is enabled to be set any time during the q4-q1 cycles. (1) (2) (3) (4) (5) (1) downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 97 pic16(l)f18326/18346 7.3 interrupts during sleep some interrupts can be used to wake from sleep. to wake from sleep, the peripheral must be able to operate without the system clock. the interrupt source must have the appropriate interrupt enable bit(s) set prior to entering sleep. on waking from sleep, if the gie bit is also set, the processor will branch to the interrupt vector. otherwise, the processor will continue executing instructions after the sleep instruction. the instruction directly after the sleep instruction will always be executed before branching to the isr. refer to section 8.0 ?power-saving operation modes? for more details. 7.4 int pin the int pin can be used to generate an asynchronous edge-triggered interrupt. this interrupt is enabled by setting the inte bit of the pie0 register. the intedg bit of the intcon register determines on which edge the interrupt will occur. when the intedg bit is set, the rising edge will cause the interrupt. when the intedg bit is clear, the falling edge will cause the interrupt. the intf bit of the pir0 register will be set when a valid edge appears on the int pin. if the gie and inte bits are also set, the processor will redirect program execution to the interrupt vector. 7.5 automatic context saving upon entering an interrupt, the return pc address is saved on the stack. additionally, the following registers are automatically saved in the shadow registers: w register status register (except for to and pd ) bsr register fsr registers pclath register upon exiting the interrupt service routine, these registers are automatically restored. any modifications to these registers during the isr will be lost. if modifications to any of these registers are desired, the corresponding shadow register should be modified and the value will be restored when exiting the isr. the shadow registers are available in bank 31 and are readable and writable. depending on the users application, other registers may also need to be saved. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 98 preliminary ? 2016 microchip technology inc. 7.6 register definitions: interrupt control register 7-1: intcon: interrupt control register r/w-0/0 r/w-0/0 u-0 u-0 u-0 u-0 u-0 r-1/1 gie peie i n t e d g 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 gie: global interrupt enable bit 1 = enables all active interrupts 0 = disables all interrupts bit 6 peie: peripheral interrupt enable bit 1 = enables all active peripheral interrupts 0 = disables all peripheral interrupts bit 5-1 unimplemented: read as 0 bit 0 intedg: interrupt edge select bit 1 = interrupt on rising edge of int pin 0 = interrupt on falling edge of int pin 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, gie, of the intcon register. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 99 pic16(l)f18326/18346 register 7-2: pie0: peripheral interrupt enable register 0 u-0 u-0 r/w/hs-0/0 r/w-0/0 u-0 u-0 u-0 r/w/hs-0/0 t m r 0 i ei o c i e i n t e 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 hs = hardware set bit 7-6 unimplemented : read as 0 bit 5 tmr0ie: tmr0 overflow interrupt enable bit 1 = enables the tmr0 interrupt 0 = disables the tmr0 interrupt bit 4 iocie: interrupt-on-change interrupt enable bit 1 = enables the ioc change interrupt 0 = disables the ioc change interrupt bit 3-1 unimplemented : read as 0 bit 0 inte: int external interrupt flag bit (1) 1 = enables the int external interrupt 0 = disables the int external interrupt note 1: the external interrupt gpio pin is selected by intpps ( register 12-1 ). downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 100 preliminary ? 2016 microchip technology inc. register 7-3: pie1: peripheral interrupt enable register 1 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 tmr1gie adie rcie txie ssp1ie bcl1ie tmr2ie tmr1ie 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 tmr1gie: timer1 gate interrupt enable bit 1 = enables the timer1 gate acquisition interrupt 0 = disables the timer1 gate acquisition interrupt bit 6 adie: analog-to-digital converter (adc) interrupt enable bit 1 = enables the adc interrupt 0 = disables the adc interrupt bit 5 rcie: eusart receive interrupt enable bit 1 = enables the eusart receive interrupt 0 = disables the eusart receive interrupt bit 4 txie: eusart transmit interrupt enable bit 1 = enables the eusart transmit interrupt 0 = disables the eusart transmit interrupt bit 3 ssp1ie: synchronous serial port (mssp) interrupt enable bit 1 = enables the mssp interrupt 0 = disables the mssp interrupt bit 2 bcl1ie: mssp1 bus collision interrupt enable bit 1 = mssp bus collision interrupt enabled 0 = mssp bus collision interrupt not enabled bit 1 tmr2ie: tmr2 to pr2 match interrupt enable bit 1 = enables the timer2 to pr2 match interrupt 0 = disables the timer2 to pr2 match interrupt bit 0 tmr1ie: timer1 overflow interrupt enable bit 1 = enables the timer1 overflow interrupt 0 = disables the timer1 overflow interrupt note: bit peie of the intcon register must be set to enable any peripheral interrupt. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 101 pic16(l)f18326/18346 register 7-4: pie2: peripheral interrupt enable register 2 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 tmr6ie c2ie c1ie nvmie ssp2ie bcl2ie tmr4ie nco1ie 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 tmr6ie: tmr6 to pr6 match interrupt enable bit 1 = tmr6 to pr6 match interrupt is enabled 0 = tmr6 to pr6 match is not enabled bit 6 c2ie: comparator c2 interrupt enable bit 1 = enables the comparator c2 interrupt 0 = disables the comparator c2 interrupt bit 5 c1ie: comparator c1 interrupt enable bit 1 = enables the comparator c1 interrupt 0 = disables the comparator c1 interrupt bit 4 nvmie: nvm interrupt enable bit 1 = envm task complete interrupt enabled 0 = nvm interrupt not enabled bit 3 ssp2ie: master synchronous serial port (mssp2) interrupt enable bit 1 = enables the mssp2 interrupt 0 = disables the mssp2 interrupt bit 2 bcl2ie: mssp2 bus collision interrupt enable bit 1 = mssp bus collision interrupt enabled 0 = mssp bus collision interrupt not enabled bit 1 tmr4ie: tmr4 to pr4 match interrupt enable bit 1 = tmr4 to pr4 match interrupt is enabled 0 = tmr4 to pr4 match is not enabled bit 0 nco1ie: nco interrupt enable bit 1 = nco rollover interrupt enabled 0 = nco rollover interrupt not enabled note: bit peie of the intcon register must be set to enable any peripheral interrupt. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 102 preliminary ? 2016 microchip technology inc. register 7-5: pie3: peripheral interrupt enable register 3 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 osfie cswie tmr3gie tmr3ie clc4ie clc3ie clc2ie clc1ie 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 osfie : oscillator fail interrupt enable bit 1 = enables the oscillator fail interrupt 0 = disables the oscillator fail interrupt bit 6 cswie: clock switch complete interrupt enable bit 1 = the clock switch module interrupt is enabled 0 = the clock switch module interrupt is not enabled bit 5 tmr3gie: timer3 gate interrupt enable bit 1 = timer3 gate interrupt is enabled 0 = timer3 gate interrupt is not enabled bit 4 tmr3ie: tmr3 overflow interrupt enable bit 1 = tmr3 overflow interrupt is enabled 0 = tmr3 overflow interrupt is not enabled bit 3 clc4ie: clc4 interrupt flag bit 1 = clc4 interrupt is enabled 0 = clc4 interrupt is not enabled bit 2 clc3ie: clc3 interrupt flag bit 1 = clc3 interrupt is enabled 0 = clc3 interrupt is not enabled bit 1 clc2ie: clc2 interrupt enable bit 1 = clc2 interrupt enabled 0 = clc2 interrupt disabled bit 0 clc1ie: clc1 interrupt enable bit 1 = clc1 interrupt enabled 0 = clc1 interrupt disabled note: bit peie of the intcon register must be set to enable any peripheral interrupt. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 103 pic16(l)f18326/18346 register 7-6: pie4: peripheral interrupt enable register 4 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 cwg2ie cwg1ie tmr5gie tmr5ie ccp4ie ccp3ie ccp2ie ccp1ie 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 hs = hardware set bit 7 cwg2ie: cwg 2 interrupt enable bit 1 = cwg2 interrupt enabled 0 = cwg2 interrupt not enabled bit 6 cwg1ie: cwg 1 interrupt enable bit 1 = cwg1 interrupt enabled 0 = cwg1 interrupt not enabled bit 5 tmr5gie: timer5 gate interrupt enable bit 1 = tmr5 gate interrupt is enabled 0 = tmr5 gate interrupt is not enabled bit 4 tmr5ie: tmr5 overflow interrupt enable bit 1 = tmr5 overflow interrupt is enabled 0 = tmr5 overflow interrupt is not enabled bit 3 ccp4ie: ccp4 interrupt enable bit 1 = ccp4 interrupt is enabled 0 = ccp4 interrupt is not enabled bit 2 ccp3ie: ccp3 interrupt enable bit 1 = ccp3 interrupt is enabled 0 = ccp3 interrupt is not enabled bit 1 ccp2ie: ccp2 interrupt enable bit 1 = ccp2 interrupt is enabled 0 = ccp2 interrupt is not enabled bit 0 ccp1ie: ccp1 interrupt enable bit 1 = ccp1 interrupt is enabled 0 = ccp1 interrupt is not enabled note: bit peie of the intcon register must be set to enable any peripheral interrupt. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 104 preliminary ? 2016 microchip technology inc. register 7-7: pir0: peripheral interrupt request register 0 u-0 u-0 r/w/hs-0/0 r-0 u-0 u-0 u-0 r/w/hs-0/0 t m r 0 i fi o c i f intf (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 hs= hardware set bit 7-6 unimplemented: read as 0 bit 5 tmr0if: tmr0 overflow interrupt flag bit 1 = tmr0 register has overflowed (must be cleared in software) 0 = tmr0 register did not overflow bit 4 iocif: interrupt-on-change interrupt flag bit (read-only) 1 = an enabled edge was detected by the ioc module. one of the iocf bits is set. 0 = no enabled edge is was detected by the ioc module. none of the iocf bits is set. pins are individually masked via iocxp and iocxn. bit 3-1 unimplemented: read as 0 bit 0 intf: int external interrupt flag bit (1) 1 = the int external interrupt occurred (must be cleared in software) 0 = the int external interrupt did not occur note 1: the external interrupt gpio pin is selected by intpps ( register 12-1 ). 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, gie, of the intcon register. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 105 pic16(l)f18326/18346 register 7-8: pir1: peripheral interrupt request register 1 r/w/hs-0/0 r/w/hs-0/0 r/w-0/0 r/w-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 tmr1gif adif rcif txif ssp1if bcl1if tmr2if tmr1if 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 hs = hardware set bit 7 tmr1gif : timer1 gate interrupt flag bit 1 = the timer1 gate has gone inactive (the gate is closed) 0 = the timer1 gate has not gone inactive. bit 6 adif: analog-to-digital converter (adc) interrupt flag bit 1 = the a/d conversion completed 0 = the a/d conversion is not completed bit 5 rcif: eusart receive interrupt flag bit 1 = the eusart1 receive buffer is not empty 0 = the eusart1 receive buffer is empty bit 4 txif : eusart transmit interrupt flag bit 1 = the eusart1 receive buffer is not empty 0 = the eusart1 receive buffer is empty bit 3 ssp1if : synchronous serial port (mssp) interrupt flag bit 1 = the transmission/reception/bus condition is complete (must be cleared in software) 0 = waiting for the transmission/reception/bus condition in progress bit 2 bcl1if: mssp bus collision interrupt flag bit 1 = a bus collision was detected (must be cleared in software) 0 = no bus collision was detected bit 1 tmr2if: timer2 to pr2 interrupt flag bit 1 = tmr2 to pr2 match occurred (must be cleared in software) 0 = no tmr2 to pr2 match occurred bit 0 tmr1if: timer1 overflow interrupt flag bit 1 = tmr1 overflow occurred (must be cleared in software) 0 = no tmr1 overflow occurred 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, gie, of the intcon register. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 106 preliminary ? 2016 microchip technology inc. register 7-9: pir2: peripheral interrupt request register 2 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 tmr6if c2if c1if nvmif ssp2if bcl2if tmr4if nco1if 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 hs = hardware set bit 7 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 6 c2if: comparator c2 interrupt flag bit 1 = comparator 2 interrupt asserted 0 = comparator 2 interrupt not asserted bit 5 c1if: comparator c1 interrupt flag bit 1 = comparator 1 interrupt asserted 0 = comparator 1 interrupt not asserted bit 4 nvmif : nvm interrupt flag bit 1 = the nvm has completed a programming task 0 = nvm interrupt not asserted bit 3 ssp2if: master synchronous serial port (mssp2) interrupt flag bit 1 = the transmission/reception/bus condition is complete (must be cleared in software) 0 = waiting for the transmission/reception/bus condition in progress bit 2 bcl2if: mssp2 bus collision interrupt flag bit 1 = a bus collision was detected (must be cleared in software) 0 = no bus collision was detected bit 1 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 bit 0 nco1if: nco interrupt flag bit 1 = the nco has rolled over. 0 = no nco interrupt is asserted. 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, gie, of the intcon register. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 107 pic16(l)f18326/18346 register 7-10: pir3: peripheral interrupt request register 3 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 osfif cswif tmr3gif tmr3if clc4if clc3if clc2if clc1if 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 hs = hardware set bit 7 osfif: oscillator fail-safe interrupt flag bit 1 = oscillator fail-safe interrupt has occurred 0 = no oscillator fail-safe interrupt bit 6 cswif: clock switch complete interrupt flag bit 1 = the clock switch module indicates an interrupt condition 0 = the clock switch module does not indicate an interrupt condition bit 5 tmr3gif: timer3 gate interrupt flag bit 1 = the tmr3 gate has gone inactive (the gate is closed) 0 = the tmr3 gate has not gone inactive. bit 4 tmr3if: tmr3 overflow interrupt flag bit 1 = tmr3 overflow occurred (must be cleared in software) 0 = no tmr3 overflow occurred bit 3 clc4if: clc4 interrupt flag bit 1 = the clc4out interrupt condition has been met 0 = no clc4 interrupt bit 2 clc3if: clc3 interrupt flag bit 1 = the clc3out interrupt condition has been met 0 = no clc3 interrupt bit 1 clc2if: clc2 interrupt flag bit 1 = the clc2out interrupt condition has been met 0 = no clc2 interrupt bit 0 clc1if: clc1 interrupt flag bit 1 = the clc1out interrupt condition has been met 0 = no clc1 interrupt 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, gie, of the intcon register. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 108 preliminary ? 2016 microchip technology inc. register 7-11: pir4: peripheral interrupt request register 4 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 cwg2if cwg1if tmr5gif tmr5if ccp4if ccp3if ccp2if ccp1if 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 hs = hardware set bit 7 cwg2if: cwg 2 interrupt flag bit 1 = cwg2 has gone into shutdown 0 = cwg2 is operating normally, or interrupt cleared bit 6 cwg1if: cwg1 interrupt flag bit 1 = cwg1 has gone into shutdown 0 = cwg1 is operating normally, or interrupt cleared bit 5 tmr5gif: timer5 gate interrupt flag bit 1 = the tmr5 gate has gone inactive (the gate is closed). 0 = the tmr5 gate has not gone inactive. bit 4 tmr5if: timer5 overflow interrupt flag bit 1 = tmr5 overflow occurred (must be cleared in software) 0 = no tmr5 overflow occurred bit 3 ccp4if: ccp4 interrupt flag bit bit 2 ccp3if: ccp3 interrupt flag bit value ccpm mode capture compare pwm 1 capture occurred (must be cleared in software) compare match occurred (must be cleared in software) output trailing edge occurred (must be cleared in software) 0 capture did not occur compare match did not occur output trailing edge did not occur value ccpm mode capture compare pwm 1 capture occurred (must be cleared in software) compare match occurred (must be cleared in software) output trailing edge occurred (must be cleared in software) 0 capture did not occur compare match did not occur output trailing edge did not occur downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 109 pic16(l)f18326/18346 bit 1 ccp2if: ccp2 interrupt flag bit bit 0 ccp1if: ccp1 interrupt flag bit 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, gie, of the intcon register. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. table 7-1: summary of registers associated with interrupts name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page intcon gie peie i n t e d g 98 pie0 t m r 0 i ei o c i e i n t e 99 pie1 tmr1gie adie rcie txie ssp1ie bcl1ie tmr2ie tmr1ie 100 pie2 tmr6ie c2ie c1ie nvmie ssp2ie bcl2ie tmr4ie nco1ie 101 pie3 osfie cswie tmr3gie tmr3ie clc4ie clc3ie clc2ie clc1ie 102 pie4 cwg2ie cwg1ie tmr5gie tmr5ie ccp4ie ccp3ie ccp2ie ccp1ie 103 pir0 t m r 0 i fi o c i f i n t f 104 pir1 tmr1gif adif rcif txif ssp1if bcl1if tmr2if tmr1if 105 pir2 tmr6if c2if c1if nvmif ssp2if bcl2if tmr4if nco1if 106 pir3 osfif cswif tmr3gif tmr3if clc4if clc3if clc2if clc1if 107 pir4 cwg2if cwg1if tmr5gif tmr5if ccp4if ccp3if ccp2if ccp1if 108 legend: = unimplemented location, read as 0 . shaded cells are not used by interrupts. register 7-11: pir4: peripheral interrupt request register 4 (continued) value ccpm mode capture compare pwm 1 capture occurred (must be cleared in software) compare match occurred (must be cleared in software) output trailing edge occurred (must be cleared in software) 0 capture did not occur compare match did not occur output trailing edge did not occur value ccpm mode capture compare pwm 1 capture occurred (must be cleared in software) compare match occurred (must be cleared in software) output trailing edge occurred (must be cleared in software) 0 capture did not occur compare match did not occur output trailing edge did not occur downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 110 preliminary ? 2016 microchip technology inc. 8.0 power-saving operation modes the purpose of the power-down modes is to reduce power consumption. there are two power-down modes: doze mode and sleep mode. 8.1 doze mode doze mode allows for power savings by reducing cpu operation and program memory access, without affecting peripheral operation. doze mode differs from sleep mode because the system oscillators continue to operate, while only the cpu and program memory are affected. the reduced execution saves power by eliminating unnecessary operations within the cpu and memory. when the doze enable (dozen) bit is set (dozen = 1 ), the cpu executes only one instruction cycle out of every n cycles as defined by the doze<2:0> bits of the cpudoze register. for example, if doze<2:0> = 100 , the instruction cycle ratio is 1:32. the cpu and memory execute for one instruction cycle and then lay idle for 31 instruction cycles. during the unused cycles, the peripherals continue to operate at the system clock speed. figure 8-1: doze mode operation example sstem clock /v????]}v w?]}? cpu clock pfm ops cpu ops 1111111111111 1234 2 22222 22 2 22 22 2 2 22 2 1 11111 3 33333 4 44444 2 3333333333333 4 4 4 4 4 4 4 4 4 4 4 4 4 fetch fetch fetch fetch exec exec exec (1,2) exec exec exec push nop 0004h interrup t here (roi = 1) downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 111 pic16(l)f18326/18346 8.1.1 doze operation the doze operation is illustrated in figure 8-1 . for this example: doze enable (dozen) bit set (dozen = 1 ) doze<2:0>= 001 (1:4) ratio recover-on-interrupt (roi) bit set (roi = 1 ) as with normal operation, the program memory fetches for the next instruction cycle. the instruction clocks to the peripherals continue throughout. 8.1.2 interrupts during doze if an interrupt occurs and the recover-on-interrupt (roi) bit is clear (roi = 0 ) at the time of the interrupt, the interrupt service routine (isr) continues to exe- cute at the rate selected by doze<2:0>. interrupt latency is extended by the doze<2:0> ratio. if an interrupt occurs and the roi bit is set (roi = 1 ) at the time of the interrupt, the dozen bit is cleared and the cpu executes at full speed. the prefetched instruc- tion is executed and then the interrupt vector sequence is executed. in figure 8-1 , the interrupt occurs during the 2 nd instruction cycle of the doze period, and imme- diately brings the cpu out of doze. if the doze-on-exit (doe) bit is set (doe = 1 ) when the retfie operation is executed, dozen is set, and the cpu executes at the reduced rate based on the doze<2:0> ratio. 8.2 sleep mode sleep mode is entered by executing the sleep instruction, while the idle enable (idlen) bit of the cpudoze register is clear (idlen = 0 ). if the sleep instruction is executed while the idlen bit is set (idlen = 1 ), the cpu will enter the idle mode ( section 8.2.3 ?low-power sleep mode? ). upon entering sleep mode, the following conditions exist: 1. wdt will be cleared but keeps running if enabled for operation during sleep 2. the pd bit of the status register is cleared 3. the to bit of the status register is set 4. the cpu clock is disabled 5. 31 khz lfintosc, hfintosc and sosc are unaffected and peripherals using them may continue operation in sleep. 6. timer1 and peripherals that use it continue to operate in sleep when the timer1 clock source selected is: lfintosc t1cki secondary oscillator 7. adc is unaffected if the dedicated adcrc oscillator is selected 8. i/o ports maintain the status they had before sleep was executed (driving high, low, or high-impedance) 9. resets other than wdt are not affected by sleep mode refer to individual chapters for more details on peripheral operation during sleep. to minimize current consumption, the following conditions should be considered: - i/o pins should not be floating - external circuitry sinking current from i/o pins - internal circuitry sourcing current from i/o pins - current draw from pins with internal weak pull-ups - modules using any oscillator i/o pins that are high-impedance inputs should be pulled to v dd or v ss externally to avoid switching currents caused by floating inputs. examples of internal circuitry that might be sourcing current include modules such as the dac and fvr modules. see section 23.0 ?5-bit digital-to-analog converter (dac1) module? and section 15.0 ?fixed voltage reference (fvr)? for more information on these modules. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 112 preliminary ? 2016 microchip technology inc. 8.2.1 wake-up from sleep the device can wake-up from sleep through one of the following events: 1. external reset input on mclr pin, if enabled 2. bor reset, if enabled. 3. por reset. 4. watchdog timer, if enabled 5. any external interrupt. 6. interrupts by peripherals capable of running during sleep (see individual peripheral for more information). the first three events will cause a device reset. the last three events are considered a continuation of program execution. to determine whether a device reset or wake-up event occurred, refer to section 5.11 ?determining the cause of a reset? . when the sleep instruction is being executed, the next instruction (pc + 1) is prefetched. for the device to wake-up through an interrupt event, the corresponding interrupt enable bit must be enabled. wake-up will occur regardless of the state of the gie bit. if the gie bit is disabled, the device continues execution at the instruction after the sleep instruction. if the gie bit is enabled, the device executes the instruction after the sleep instruction, the device will then call the interrupt service routine. in cases where the execution of the instruction following sleep is not desirable, the user should have a nop after the sleep instruction. the wdt is cleared when the device wakes-up from sleep, regardless of the source of wake-up. 8.2.2 wake-up using interrupts when global interrupts are disabled (gie cleared) and any interrupt source, with the exception of the clock switch interrupt, has both its interrupt enable bit and interrupt flag bit set, one of the following will occur: if the interrupt occurs before the execution of a sleep instruction - sleep instruction will execute as a nop - wdt and wdt prescaler will not be cleared -to bit of the status register will not be set -pd bit of the status register will not be cleared if the interrupt occurs during or after the execution of a sleep instruction - sleep instruction will be completely executed - device will immediately wake-up from sleep - wdt and wdt prescaler will be cleared -to bit of the status register will be set -pd bit of the status register will be cleared even if the flag bits were checked before executing a sleep instruction, it may be possible for flag bits to become set before the sleep instruction completes. to determine whether a sleep instruction executed, test the pd bit. if the pd bit is set, the sleep instruction was executed as a nop . figure 8-2: wake-up from sleep through interrupt q1 q2 q3 q4 q1 q2 q3 q4 q1 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 clkin (1) clkout (2) interrupt flag gie bit (intcon reg.) instruction flow pc instruction fetched instruction executed pc pc + 1 pc + 2 inst(pc) = sleep inst(pc - 1) inst(pc + 1) sleep processor in sleep interrupt latency (4) inst(pc + 2) inst(pc + 1) inst(0004h) inst(0005h) inst(0004h) forced nop pc + 2 0004h 0005h forced nop t ost (3) pc + 2 note 1: external clock. high, medium, low mode assumed. 2: clkout is shown here for timing reference. 3: t ost = 1024 t osc . this delay does not apply to ec and intosc oscillator modes. 4: gie = 1 assumed. in this case after wake-up, the processor calls the isr at 0004h. if gie = 0 , execution will continue in-line. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 113 pic16(l)f18326/18346 8.2.3 low-power sleep mode the pic16f18326/18346 device contains an internal low dropout (ldo) voltage regulator, which allows the device i/o pins to operate at voltages up to 5.5v while the internal device logic operates at a lower voltage. the ldo and its associated reference circuitry must remain active when the device is in sleep mode. the pic16f18326/18346 allows the user to optimize the operating current in sleep, depending on the application requirements. low-power sleep mode can be selected by setting the vregpm bit of the vregcon register. depending on the configuration of these bits, the ldo and reference circuitry are placed in a low-power state when the device is in sleep. 8.2.3.1 sleep current vs. wake-up time in the default operating mode, the ldo and reference circuitry remain in the normal configuration while in sleep. the device is able to exit sleep mode quickly since all circuits remain active. in low-power sleep mode, when waking-up from sleep, an extra delay time is required for these circuits to return to the normal configuration and stabilize. the low-power sleep mode is beneficial for applications that stay in sleep mode for long periods of time. the normal mode is beneficial for applications that need to wake from sleep quickly and frequently. 8.2.3.2 peripheral usage in sleep some peripherals that can operate in sleep mode will not operate properly with the low-power sleep mode selected. the low-power sleep mode is intended for use with these peripherals: brown-out reset (bor) watchdog timer (wdt) external interrupt pin/interrupt-on-change pins timer 1 (with external clock source) it is the responsibility of the end user to determine what is acceptable for their application when setting the vregpm settings in order to ensure operation in sleep. 8.2.4 idle mode when the idle enable (idlen) bit is clear (idlen = 0 ), the sleep instruction will put the device into full sleep mode (see section 8.2 ?sleep mode? ). when idlen is set (idlen = 1 ), the sleep instruction will put the device into idle mode. in idle mode, the cpu and mem- ory operations are halted, but the peripheral clocks continue to run. this mode is similar to doze mode, except that in idle both the cpu and program memory are shut off. 8.2.4.1 idle and interrupts idle mode ends when an interrupt occurs (even if gie = 0 ), but idlen is not changed. the device can re-enter idle by executing the sleep instruction. if recover-on-interrupt is enabled (roi = 1 ), the interrupt that brings the device out of idle also restores full-speed cpu execution when doze is also enabled. 8.2.4.2 idle and wdt when in idle, the wdt reset is blocked and will instead wake the device. the wdt wake-up is not an interrupt, therefore roi does not apply. note: the pic16lf18326/18346 does not have a configurable low-power sleep mode. pic16lf18326/18346 is an unregulated device and is always in the lowest power state when in sleep, with no wake-up time penalty. this device has a lower maximum v dd and i/o voltage than the pic16f18326/18346. see section 34.0 ?electrical specifications? for more information. note: peripherals using f osc will continue running while in idle (but not in sleep). note: if clkout is enabled (clkout = 0 , configuration word 1), the output will continue operating while in idle. note: the wdt can bring the device out of idle, in the same way it brings the device out of sleep. the dozen bit is not affected. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 114 preliminary ? 2016 microchip technology inc. 8.3 register definitions: voltage regulator control register 8-1: vregcon: voltag e regulator control register (1) u-0 u-0 u-0 u-0 u-0 u-0 r/w-0/0 r/w-1/1 vregpm reserved 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-2 unimplemented: read as 0 bit 1 vregpm: voltage regulator power mode selection bit 1 = low-power sleep mode enabled in sleep (2) ; draws lowest current in sleep, slower wake-up 0 = normal-power sleep mode enabled in sleep (2) ; draws higher current in sleep, faster wake-up bit 0 reserved: read as 1 . maintain this bit set. note 1: pic16f18326/18346 only. 2: see section 34.0 ?electrical specifications? . register 8-2: cpudoze: doze and idle register r/w-0/u r/w/hc/hs-0/0 r/w-0/0 r/w-0/0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 idlen dozen (1,2) roi doe doze<2: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 idlen: idle enable bit 1 =a sleep instruction inhibits the cpu cloc k, but not the peripheral clock(s) 0 =a sleep instruction places the device into full-sleep mode bit 6 dozen: doze enable bit (1,2) 1 = the cpu executes instruction cycles according to doze setting 0 = the cpu executes all instruction cycles (fastest, highest power operation) bit 5 roi: recover-on-interrupt bit 1 = entering the interrupt service routine (isr) makes dozen = 0 bit, bringing the cpu to full-speed operation. 0 = interrupt entry does not change dozen bit 4 doe: doze-on-exit bit 1 = executing retfie makes dozen = 1 , bringing the cpu to reduced speed operation. 0 = retfie does not change dozen bit 3 unimplemented: read as 0 bit 2-0 doze<2:0>: ratio of cpu instruction cycles to peripheral instruction cycles 111 = 1:256 110 =1:128 101 =1:64 100 =1:32 011 =1:16 010 =1:8 001 =1:4 000 =1:2 note 1: when roi = 1 or doe = 1 , dozen is changed by hardware interrupt entry and/or exit. 2: entering icd overrides dozen, returning the cpu to full execution speed; this bit is not affected. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 115 pic16(l)f18326/18346 table 8-1: summary of registers as sociated with power-down mode name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page intcon gie peie i n t e d g 98 pie0 t m r 0 i ei o c i e i n t e 99 pie1 tmr1gie adie rcie txie ssp1ie bcl1ie tmr2ie tmr1ie 100 pie2 tmr6ie c2ie c1ie nvmie ssp2ie bcl2ie tmr4ie nco1ie 101 pie3 osfie cswie tmr3gie tmr3ie clc4ie clc3ie clc2ie clc1ie 102 pie4 cwg2ie cwg1ie tmr5gie tmr5ie ccp4ie ccp3ie ccp2ie ccp1ie 103 pir0 t m r 0 i fi o c i f intf 104 pir1 tmr1gif adif rcif txif ssp1if bcl1if tmr2if tmr1if 105 pir2 tmr6if c2if c1if nvmif ssp2if bcl2if tmr4if nco1if 106 pir3 osfif cswif tmr3gif tmr3if clc4if clc3if clc2if clc1if 107 pir4 cwg2if cwg1if tmr5gif tmr5if ccp4if ccp3if ccp2if ccp1if 108 iocap iocap5 iocap4 iocap3 iocap2 iocap1 iocap0 172 iocan iocan5 iocan4 iocan3 iocan2 iocan1 iocan0 172 iocaf iocaf5 iocaf4 iocaf3 iocaf2 iocaf1 iocaf0 173 iocbp (1) iocbp7 iocbp6 iocbp5 iocbp4 173 iocbn (1) iocbn7 iocbn6 iocbn5 iocbn4 174 iocbf (1) iocbf7 iocbf6 iocbf5 iocbf4 174 ioccp ioccp7 (1) ioccp6 (1) ioccp5 ioccp4 ioccp3 ioccp2 ioccp1 ioccp0 175 ioccn ioccn7 (1) ioccn6 (1) ioccn5 ioccn4 ioccn3 ioccn2 ioccn1 ioccn0 175 ioccf ioccf7 (1) ioccf6 (1) ioccf5 ioccf4 ioccf3 ioccf2 ioccf1 ioccf0 176 status t o pd zd c c 27 vregcon (2) v r e g p m 114 cpudoze idlen dozen roi doe doze<2:0> 114 wdtcon w d t p s < 4 : 0 >s w d t e n 118 legend: = unimplemented location, read as 0 . shaded cells are not used in power-down mode. note 1: pic16(l)f18346 only. 2: pic16f18326/18346 only. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 116 preliminary ? 2016 microchip technology inc. 9.0 watchdog timer (wdt) the watchdog timer is a system timer that generates a reset if the firmware does not issue a clrwdt instruction within the time-out period. the watchdog timer is typically used to recover the system from unexpected events. the wdt has the following features: independent clock source multiple operating modes - wdt is always on - wdt is off when in sleep - wdt is controlled by software - wdt is always off configurable time-out period is from 1 ms to 256 seconds (nominal) multiple reset conditions operation during sleep figure 9-1: watchdog ti mer block diagram lfintosc 23-bit programmable prescaler wdt wdt time-out wdtps<4:0> swdten sleep wdte<1:0> = 11 wdte<1:0> = 01 wdte<1:0> = 10 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 117 pic16(l)f18326/18346 9.1 independent clock source the wdt derives its time base from the 31 khz lfintosc internal oscillator. time intervals in this chapter are based on a nominal interval of 1 ms. see table 34-8 for the lfintosc specification. 9.2 wdt operating modes the watchdog timer module has four operating modes controlled by the wdte<1:0> bits in configuration words (see tab le 9 - 1 ). 9.2.1 wdt is always on when the wdte bits of configuration words are set to 11 , the wdt is always on. wdt protection is active during sleep. 9.2.2 wdt is off in sleep when the wdte bits of configuration words are set to 10 , the wdt is on, except in sleep. wdt protection is not active during sleep. 9.2.3 wdt controlled by software when the wdte bits of configuration words are set to 01 , the wdt is controlled by the swdten bit of the wdtcon register. wdt protection is unchanged by sleep. see table 9-1 for more details. 9.3 time-out period the wdtps<4:0> bits of the wdtcon register set the time-out period from 1 ms to 256 seconds (nominal). after a reset, the default time-out period is two seconds. 9.4 clearing the wdt the wdt is cleared when any of the following conditions occur: any reset clrwdt instruction is executed device enters sleep device wakes up from sleep oscillator fail wdt is disabled oscillator start-up timer (ost) is running see table 9-2 for more information. 9.5 operation during sleep when the device enters sleep, the wdt is cleared. if the wdt is enabled during sleep, the wdt resumes counting. when the device exits sleep, the wdt is cleared again. the wdt remains clear until the ost, if enabled, completes. see section 6.0 ?oscillator module (with fail-safe clock monitor)? for more information on the ost. when a wdt time-out occurs while the device is in sleep, no reset is generated. instead, the device wakes up and resumes operation. the to and pd bits in the status register are changed to indicate the event. see status register ( register 3-1 ) for more information. table 9-1: wdt operating modes wdte<1:0> swdten device mode wdt mode 11 x xa c t i v e 10 x awake active sleep disabled 01 1 x active 0 disabled 00 x x disabled table 9-2: wdt clearing conditions conditions wdt wdte = 00 cleared and disabled wdte = 01 and swdten = 0 exit sleep due to a reset + system clock = xt, hs, lp cleared until the end of ost exit sleep due to a reset + system clock = hfintosc, lfintosc, ec, sosc exit sleep due to an interrupt cleared enter sleep clrwdt command oscillator failure (see section 6.4 ?fail-safe clock monitor? ) system reset any clock switch or divider change (see section 6.3 ?clock switching? )u n a f f e c t e d downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 118 preliminary ? 2016 microchip technology inc. 9.6 register definitions: watchdog control register 9-1: wdtcon: wat chdog timer control register u-0 u-0 r/w-0/0 r/w-1/1 r/w-0/0 r/w-1/1 r/w-1/1 r/w-0/0 wdtps<4:0> (1) swdten 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 -m/n = value at por and bor/value at all other resets 1 = bit is set 0 = bit is cleared bit 7-6 unimplemented: read as 0 bit 5-1 wdtps<4:0>: watchdog timer period select bits (1) bit value = prescale rate 11111 = reserved. results in minimum interval (1:32) 10011 = reserved. results in minimum interval (1:32) 10010 = 1:8388608 (2 23 ) (interval 256s nominal) 10001 = 1:4194304 (2 22 ) (interval 128s nominal) 10000 = 1:2097152 (2 21 ) (interval 64s nominal) 01111 = 1:1048576 (2 20 ) (interval 32s nominal) 01110 = 1:524288 (2 19 ) (interval 16s nominal) 01101 = 1:262144 (2 18 ) (interval 8s nominal) 01100 = 1:131072 (2 17 ) (interval 4s nominal) 01011 = 1:65536 (interval 2s nominal) (reset value) 01010 = 1:32768 (interval 1s nominal) 01001 = 1:16384 (interval 512 ms nominal) 01000 = 1:8192 (interval 256 ms nominal) 00111 = 1:4096 (interval 128 ms nominal) 00110 = 1:2048 (interval 64 ms nominal) 00101 = 1:1024 (interval 32 ms nominal) 00100 = 1:512 (interval 16 ms nominal) 00011 = 1:256 (interval 8 ms nominal) 00010 = 1:128 (interval 4 ms nominal) 00001 = 1:64 (interval 2 ms nominal) 00000 = 1:32 (interval 1 ms nominal) bit 0 swdten: software enable/disable for watchdog timer bit if wdte<1:0> = 1x : this bit is ignored. if wdte<1:0> = 01 : 1 = wdt is turned on 0 = wdt is turned off if wdte<1:0> = 00 : this bit is ignored. note 1: times are approximate. wdt time is based on 31 khz lfintosc. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 119 pic16(l)f18326/18346 table 9-3: summary of registers associated with watchdog timer name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page s t a t u s t o pd zd cc 27 wdtcon wdtps<4:0> swdten 118 legend: x = unknown, u = unchanged, C = unimplemented locations read as 0 . shaded cells are not used by watchdog timer. table 9-4: summary of configurat ion word with watchdog timer name bits bit -/7 bit -/6 bit 13/5 bit 12/4 bit 11/3 bit 10/2 bit 9/1 bit 8/0 register on page config2 13:8 debug stvren pps1way borv 61 7:0 boren1 boren0 lpboren w d t e 1w d t e 0 pwrte mclre legend: = unimplemented location, read as 0 . shaded cells are not used by watchdog timer. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 120 preliminary ? 2016 microchip technology inc. 10.0 nonvolatile memory (nvm) control nvm is separated into two types: program flash memory and data eeprom. nvm is accessible by using both the fsr and indf registers, or through the nvmreg register interface. the write time is controlled by an on-chip timer. the write/erase voltages are generated by an on-chip charge pump rated to operate over the operating voltage range of the device. nvm can be protected in two ways; by either code protection or write protection. code protection (cp and cpd bits in configuration word 4) disables access, reading and writing, to both the program flash memory and eeprom via external device programmers. code protection does not affect the self-write and erase functionality. code protection can only be reset by a device programmer performing a bulk erase to the device, clearing all nonvolatile memory, configuration bits, and user ids. write protection prohibits self-write and erase to a portion or all of the program flash memory, as defined by the wrt<1:0> bits of configuration word 3. write protection does not affect a device programmers ability to read, write, or erase the device. 10.1 program flash memory program flash memory consists of 8192 14-bit words as user memory, with additional words for user id information, configuration words, and interrupt vectors. program flash memory provides storage locations for: user program instructions user defined data program flash memory data can be read and/or written to through: cpu instruction fetch (read-only) fsr/indf indirect access (read-only) ( section 10.3 ?fsr and indf access? ) nvmreg access ( section 10.4 ?nvmreg access? in-circuit serial programming? (icsp?) read operations return a single word of memory. when write and erase operations are done on a row basis, the row size is defined in table 10-1 . program flash memory will erase to a logic 1 and program to a logic 0 . it is important to understand the program flash memory structure for erase and programming operations. program flash memory is arranged in rows. a row consists of 32 14-bit program memory words. a row is the minimum size that can be erased by user software. after a row has been erased, all or a portion of this row can be programmed. data to be written into the program memory row is written to 14-bit wide data write latches. these latches are not directly accessible to the user, but may be loaded via sequential writes to the nvmdath:nvmdatl register pair. 10.1.1 program memory voltages the program flash memory is readable and writable during normal operation over the full v dd range. 10.1.1.1 programming externally the program memory cell and control logic support write and bulk erase operations down to the minimum device operating voltage. special bor operation is enabled during bulk erase ( section 5.2.3.1 ?bor protection is unchanged by sleep? ). 10.1.1.2 self-programming the program memory cell and control logic will support write and row erase operations across the entire v dd range. bulk erase is not supported when self-programming. table 10-1: flash memory organization by device device row erase (words) write latches (words) pic16(l)f18326 32 32 pic16(l)f18346 note: to modify only a portion of a previously programmed row, then the contents of the entire row must be read and saved in ram prior to the erase. then, the new data and retained data can be written into the write latches to reprogram the row of program flash memory. however, any unprogrammed locations can be written without first erasing the row. in this case, it is not necessary to save and rewrite the other previously programmed locations downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 121 pic16(l)f18326/18346 10.2 data eeprom data eeprom consists of 256 bytes of user data memory. the eeprom provides storage locations for 8-bit user defined data. eeprom can be read and/or written through: fsr/indf indirect access ( section 10.3 ?fsr and indf access? ) nvmreg access ( section 10.4 ?nvmreg access? ) in-circuit serial programming (icsp) unlike program flash memory, which must be written to by row, eeprom can be written to word by word. 10.3 fsr and indf access the fsr and indf registers allow indirect access to the program flash memory or eeprom. 10.3.1 fsr read with the intended address loaded into an fsr register, a moviw instruction or read of indf will read data from the program flash memory or eeprom. reading from nvm requires one instruction cycle. the cpu operation is suspended during the read, and resumes immediately after. read operations return a single word of memory. 10.3.2 fsr write writing/erasing the nvm through the fsr registers (ex. movwi instruction) is not supported in the pic16(l)f18326/18346 devices. 10.4 nvmreg access the nvmreg interface allows read/write access to all the locations accessible by fsrs, and also read/write access to the user id locations, and read-only access to the device identification, revision, and configuration data. reading, writing, or erasing of nvm via the nvmreg interface is prevented when the device is code-protected. 10.4.1 nvmreg read operation to read a nvm location using the nvmreg interface, the user must: 1. clear the nvmregs bit of the nvmcon1 register if the user intends to access program flash memory locations, or set nvmregs if the user intends to access user id, configuration, or eeprom locations. 2. write the desired address into the nvmadrh:nvmadrl register pair ( table 10-2 ). 3. set the rd bit of the nvmcon1 register to initiate the read. once the read control bit is set, the cpu operation is suspended during the read, and resumes immediately after. the data is available in the very next cycle, in the nvmdath:nvmdatl register pair; therefore, it can be read as two bytes in the following instructions. nvmdath:nvmdatl register pair will hold this value until another read or until it is written to by the user. upon completion, the rd bit is cleared by hardware. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 122 preliminary ? 2016 microchip technology inc. figure 10-1: program flash memory read flowchart example 10-1: program flash memory read end read operation select memory: program flash memory, eeprom, config. words, user id (nvmregs) select word address (nvmadrh:nvmadrl) start read operation initiate read operation (rd = 1 ) data read now in nvmdath:nvmdatl * this code block will read 1 word of program * memory at the memory address: prog_addr_hi : prog_addr_lo * data will be returned in the variables; * prog_data_hi, prog_data_lo banksel nvmadrl ; select bank for nvmcon registers movlw prog_addr_lo ; movwf nvmadrl ; store lsb of address movlw prog_addr_hi ; movwf nvmadrh ; store msb of address bcf nvmcon1,nvmregs ; do not select configuration space bsf nvmcon1,rd ; initiate read movf nvmdatl,w ; get lsb of word movwf prog_data_lo ; store in user location movf nvmdath,w ; get msb of word movwf prog_data_hi ; store in user location downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 123 pic16(l)f18326/18346 10.4.2 nvm unlock sequence the unlock sequence is a mechanism that protects the nvm from unintended self-write programming or eras- ing. the sequence must be executed and completed without interruption to successfully complete any of the following operations: program flash memory row erase load of program flash memory write latches write of program flash memory write latches to program flash memory memory write of program flash memory write latches to user ids write to eeprom the unlock sequence consists of the following steps and must be completed in order: write 55h to nvmcon2 write aah to nmvcon2 set the wr bit of nvmcon1 once the wr bit is set, the processor will stall internal operations until the operation is complete and then resume with the next instruction. since the unlock sequence must not be interrupted, global interrupts should be disabled prior to the unlock sequence and re-enabled after the unlock sequence is completed. figure 10-2: nvm unlock sequence flowchart example 10-2: nvm unlock sequence note: the two nop instructions after setting the wr bit, which were required in previous devices, are not required for pic16(l)f18326/18346 devices. see figure 10-2 . end unlock operation write 55h to nvmcon2 write aah to nvmcon2 initiate write or erase operation (wr = 1 ) nop instruction (not required for pic16(l)f18326/18346 devices) nop instruction (not required for pic16(l)f18326/18346 devices) start unlock sequence note 1: sequence begins when nvmcon2 is written; steps 1-4 must occur in the cy cle-accurate order shown. 2: opcodes shown are illustrative; any instruction that has the indicated eff ect may be used. banksel nvmcon1 bsf nvmcon1,wren ; enable write/erase movlw 55h ; load 55h bcf intcon,gie ; recommended so sequence is not interrupted movwf nvmcon2 ; step 1: load 55h into nvmcon2 movlw aah ; step 2: load w with aah movwf nvmcon2 ; step 3: load aah into nvmcon2 bsf nvmcon1,wr ; step 4: set wr bit to begin write/erase bsf intcon,gie ; re-enable interrupts downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 124 preliminary ? 2016 microchip technology inc. 10.4.3 nvmreg write to eeprom writing to the eeprom is accomplished by the following steps: 1. set the nvmregs and wren bits of the nvmcon1 register. 2. write the desired address (address +7000h) into the nvmadrh:nvmadrl register pair ( ta b l e 1 0 - 2 ). 3. perform the unlock sequence as described in section 10.4.2 ?nvm unlock sequence? . a single eeprom word is written with nvmdata. the operation includes an implicit erase cycle for that word (it is not necessary to set the free bit), and requires many instruction cycles to finish. cpu execution continues in parallel and, when complete, wr is cleared by hardware, nvmif is set, and an interrupt will occur if nvmie is also set. software must poll the wr bit to determine when writing is complete, or wait for the interrupt to occur. wren will remain unchanged. once the eeprom write operation begins, clearing the wr bit will have no effect; the operation will continue to run to completion. 10.4.4 nvmreg erase of program flash memory before writing to program flash memory, the word(s) to be written must be erased or previously unwritten. pro- gram flash memory can only be erased one row at a time. no automatic erase occurs upon the initiation of the write to program flash memory. to erase a program flash memory row: 1. clear the nvmregs bit of the nvmcon1 register to erase program flash memory locations, or set the nvmregs bit to erase user id locations. 2. write the desired address into the nvmadrh:nvmadrl register pair ( ta b l e 1 0 - 2 ). 3. set the free and wren bits of the nvmcon1 register. 4. perform the unlock sequence as described in section 10.4.2 ?nvm unlock sequence? . if the program flash memory address is write-pro- tected, the wr bit will be cleared and the erase opera- tion will not take place. while erasing program flash memory, cpu operation is suspended, and resumes when the operation is complete. upon completion, the nvmif is set, and an interrupt will occur if the nvmie bit is also set. write latch data is not affected by erase operations, and wren will remain unchanged. figure 10-3: nvm erase flowchart unlock sequence ( figure 10-2 ) end erase operation select memory: program flash memory, config. words, user id (nvmregs) disable write/erase operation (wren = 0 ) select word address (nvmadrh:nvmadrl) start erase operation cpu stalls while erase operation completes (2 ms typical) select erase operation (free = 1 ) disable interrupts (gie = 0 ) enable interrupts (gie = 1 ) enable write/erase operation (wren = 1 ) downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 125 pic16(l)f18326/18346 example 10-3: erasing one row of program flash memory ; this sample row erase routine assumes the following: ; 1.a valid address within the erase row is loaded in variables addrh:addrl ; 2.addrh and addrl are located in common ram (locations 0x70 - 0x7f) banksel nvmadrl movf addrl,w movwf nvmadrl ; load lower 8 bits of erase address boundary movf addrh,w movwf nvmadrh ; load upper 6 bits of erase address boundary bcf nvmcon1,nvmregs ; choose program flash memory area bsf nvmcon1,free ; specify an erase operation bsf nvmcon1,wren ; enable writes bcf intcon,gie ; disable interrupts during unlock sequence ; -------------------------------required unlock sequence:------------------------------ movlw 55h ; load 55h to get ready for unlock sequence movwf nvmcon2 ; first step is to load 55h into nvmcon2 movlw aah ; second step is to load aah into w movwf nvmcon2 ; third step is to load aah into nvmcon2 bsf nvmcon1,wr ; final step is to set wr bit ; -------------------------------------------------------------------------------------- bsf intcon,gie ; re-enable interrupts, erase is complete bcf nvmcon1,wren ; disable writes table 10-2: nvm organization and access information master values nvmreg access fsr access memory function program counter (pc), icsp? address memory type nvmregs bit (nvmcon1) nvmadr <14:0> allowed operations fsr address fsr programming address reset vector 0000h program flash memory 0 0000h read write 8000h read-only user memory 0001h 0 0001h 8001h 0003h 0003h 8003h int vector 0004h 0 0004h 8004h user memory 0005h 0 0005h 8005h 17ffh 17ffh ffffh user id no pc address program flash memory 1 0000h read no access 0003h reserved 0004h rev id program flash memory 1 0005h read device id 1 0006h config1 1 0007h config2 1 0008h config3 1 0009h config4 000ah user memory eeprom 1 7000h read f000h read-only 70ffh write f0ffh downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 126 preliminary ? 2016 microchip technology inc. 10.4.5 nvmreg write to program flash memory program memory is programmed using the following steps: 1. load the address of the row to be programmed into nvmadrh:nvmadrl. 2. load each write latch with data. 3. initiate a programming operation. 4. repeat steps 1 through 3 until all data is written. before writing to program memory, the word(s) to be written must be erased or previously unwritten. program memory can only be erased one row at a time. no automatic erase occurs upon the initiation of the write. program memory can be written one or more words at a time. the maximum number of words written at one time is equal to the number of write latches. see figure 10-4 (row writes to program memory with 32 write latches) for more details. the write latches are aligned to the flash row address boundary defined by the upper ten bits of nvmadrh:nvmadrl, (nvmadrh<6:0>:nvmadrl<7:5>) with the lower five bits of nvmadrl, (nvmadrl<4:0>) determining the write latch being loaded. write opera- tions do not cross these boundaries. at the completion of a program memory write operation, the data in the write latches is reset to contain 0x3fff. the following steps should be completed to load the write latches and program a row of program memory. these steps are divided into two parts. first, each write latch is loaded with data from the nvmdath:nvmdatl using the unlock sequence with lwlo = 1 . when the last word to be loaded into the write latch is ready, the lwlo bit is cleared and the unlock sequence executed. this initiates the programming operation, writing all the latches into flash program memory. 1. set the wren bit of the nvmcon1 register. 2. clear the nvmregs bit of the nvmcon1 register. 3. set the lwlo bit of the nvmcon1 register. when the lwlo bit of the nvmcon1 register is 1 , the write sequence will only load the write latches and will not initiate the write to flash program memory. 4. load the nvmadrh:nvmadrl register pair with the address of the location to be written. 5. load the nvmdath:nvmdatl register pair with the program memory data to be written. 6. execute the unlock sequence ( section 10.4.2 ?nvm unlock sequence? ). the write latch is now loaded. 7. increment the nvmadrh:nvmadrl register pair to point to the next location. 8. repeat steps 5 through 7 until all but the last write latch has been loaded. 9. clear the lwlo bit of the nvmcon1 register. when the lwlo bit of the nvmcon1 register is 0 , the write sequence will initiate the write to flash program memory. 10. load the nvmdath:nvmdatl register pair with the program memory data to be written. 11. execute the unlock sequence ( section 10.4.2 ?nvm unlock sequence? ). the entire program memory latch content is now written to flash program memory. an example of the complete write sequence is shown in example 10-4 . the initial address is loaded into the nvmadrh:nvmadrl register pair; the data is loaded using indirect addressing. note: the special unlock sequence is required to load a write latch with data or initiate a flash programming operation. if the unlock sequence is interrupted, writing to the latches or program memory will not be initiated. note: the program memory write latches are reset to the blank state (0x3fff) at the completion of every write or erase operation. as a result, it is not necessary to load all the program memory write latches. unloaded latches will remain in the blank state. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 127 pic16(l)f18326/18346 figure 10-4: block writes to program flash memory with 32 write latches 6 8 14 14 14 write latch #31 1fh 14 14 program memory write latches 14 14 14 nvmadrh<6:0> nvmadrl<7:5> program flash memory row row address decode addr write latch #30 1eh write latch #1 01h write latch #0 00h addr addr addr 000h 001fh 001eh 0000h 0001h 001h 003fh 003eh 0020h 0021h 002h 005fh 005eh 0040h 0041h 3feh 7fdfh 7fdeh 7fc0h 7fc1h 3ffh 7fffh 7ffeh 7fe0h 7fe1h 14 nvmadrl<4:0> 400h 800bh - 801fh 8000h - 8003h configuration words user id 0 - 3 8007h C 800ah 8005h -8006h device id dev / rev reserved reserved configuration memory nvmregs = 0 nvmregs = 1 nvmadrh nvmadrl 7 6 0 7 5 4 0 c4 c3 c2 c1 c0 r9 r8 r7 r6 r5 r4 r3 - r1 r0 r2 5 10 nvmdath nvmdatl 7 5 0 7 0 - - 8004h downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 128 preliminary ? 2016 microchip technology inc. figure 10-5: program flash memory write flowchart start write operation determine number of words to be written into pfm or configuration memory. the number of words cannot exceed the number of words per row (word_cnt) select pfm or config. memory (nvmregs) select row address (nvmadrh:nvmadrl) select write operation (free = 0 ) load write latches only (lwlo = 1 ) disable interrupts (gie = 0 ) enable write/erase operation (wren = 1 ) load the value to write (nvmdath:nvmdatl) update the word counter (word_cnt--) last word to write? unlock sequence no delay when writing to pfm latches re-enable interrupts (gie = 1 ) write latches to pfm (lwlo = 0 ) disable interrupts (gie = 0 ) cpu stalls while write operation completes (2 ms typical) disable write/erase operation (wren = 0 ) end write operation increment address (nvmadrh:nvmadrl++) unlock sequence re-enable interrupts (gie = 1 ) yes no ( figure 10-2 ) ( figure 10-2 ) downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 129 pic16(l)f18326/18346 example 10-4: writing to program flash memory ; this write routine assumes the following: ; 1. 64 bytes of data are loaded, starting at the address in data_addr ; 2. each word of data to be written is made up of two adjacent bytes in data_addr, ; stored in little endian format ; 3. a valid starting address (the least significant bits = 00000) is loaded in addrh:addrl ; 4. addrh and addrl are located in common ram (locations 0x70 - 0x7f) ; 5. nvm interrupts are not taken into account banksel nvmadrh movf addrh,w movwf nvmadrh ; load initial address movf addrl,w movwf nvmadrl movlw low data_addr ; load initial data address movwf fsr0l movlw high data_addr movwf fsr0h bcf nvmcon1,nvmregs ; set program flash memory as write location bsf nvmcon1,wren ; enable writes bsf nvmcon1,lwlo ; load only write latches loop moviw fsr0++ movwf nvmdatl ; load first data byte moviw fsr0++ movwf nvmdath ; load second data byte movf nvmadrl,w xorlw 0x1f ; check if lower bits of address are 00000 andlw 0x1f ; and if on last of 32 addresses btfsc status,z ; last of 32 words? goto start_write ; if so, go write latches into memory call unlock_seq ; if not, go load latch incf nvmadrl,f ; increment address goto loop start_write bcf nvmcon1,lwlo ; latch writes complete, now write memory call unlock_seq ; perform required unlock sequence bcf nvmcon1,wren ; disable writes unlock_seq movlw 55h bcf intcon,gie ; disable interrupts movwf nvmcon2 ; begin unlock sequence movlw aah movwf nvmcon2 bsf nvmcon1,wr bsf intcon,gie ; unlock sequence complete, re-enable interrupts return downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 130 preliminary ? 2016 microchip technology inc. 10.4.6 modifying program flash memory when modifying existing data in a program memory row, and data within that row must be preserved, it must first be read and saved in a ram image. program memory is modified using the following steps: 1. load the starting address of the row to be modified. 2. read the existing data from the row into a ram image. 3. modify the ram image to contain the new data to be written into program memory. 4. load the starting address of the row to be rewritten. 5. erase the program memory row. 6. load the write latches with data from the ram image. 7. initiate a programming operation. figure 10-6: program flash memory modify flowchart start modify operation read operation (figure x.x) erase operation (figure x.x) modify image the words to be modified are changed in the ram image end modify operation write operation use ram image (figure x.x) an image of the entire row read must be stored in ram figure 10-1 figure 10-3 figure 10-5 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 131 pic16(l)f18326/18346 10.4.7 nvmreg eeprom, user id, device id and configuration word access instead of accessing program flash memory, the eeprom, the user ids, device id/revision id and configuration words can be accessed when nvmregs = 1 in the nvmcon1 register. this is the region that would be pointed to by pc<15> = 1 , but not all addresses are accessible. different access may exist for reads and writes. refer to tab l e 1 0- 3 . when read access is initiated on an address outside the parameters listed in table 10-3 , the nvmdath: nvmdatl register pair is cleared, reading back 0 s. table 10-3: eeprom, user id, dev/re v id and configuration word access (nvmregs = 1 ) address function read access write access 8000h-8003h user ids yes yes 8005h-8006h device id/revision id yes no 8007h-800ah configuration words 1-4 yes no f000h-f0ffh eeprom yes yes downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 132 preliminary ? 2016 microchip technology inc. example 10-5: device id access ; this write routine assumes the following: ; 1. 64 bytes of data are loaded, starting at the address in data_addr ; 2. each word of data to be written is made up of two adjacent bytes in data_addr, ; stored in little endian format ; 3. a valid starting address (the least significant bits = 00000) is loaded in addrh:addrl ; 4. addrh and addrl are located in common ram (locations 0x70 - 0x7f) ; 5. nvm interrupts are not taken into account banksel nvmadrh movf addrh,w movwf nvmadrh ; load initial address movf addrl,w movwf nvmadrl movlw low data_addr ; load initial data address movwf fsr0l movlw high data_addr movwf fsr0h bcf nvmcon1,nvmregs ; set program flash memory as write location bsf nvmcon1,wren ; enable writes bsf nvmcon1,lwlo ; load only write latches loop moviw fsr0++ movwf nvmdatl ; load first data byte moviw fsr0++ movwf nvmdath ; load second data byte movf nvmadrl,w xorlw 0x1f ; check if lower bits of address are 00000 andlw 0x1f ; and if on last of 32 addresses btfsc status,z ; last of 32 words? goto start_write ; if so, go write latches into memory call unlock_seq ; if not, go load latch incf nvmadrl,f ; increment address goto loop start_write bcf nvmcon1,lwlo ; latch writes complete, now write memory call unlock_seq ; perform required unlock sequence bcf nvmcon1,wren ; disable writes unlock_seq movlw 55h bcf intcon,gie ; disable interrupts movwf nvmcon2 ; begin unlock sequence movlw aah movwf nvmcon2 bsf nvmcon1,wr bsf intcon,gie ; unlock sequence complete, re-enable interrupts return downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 133 pic16(l)f18326/18346 10.4.8 write verify it is considered good programming practice to verify that program memory writes agree with the intended value. since program memory is stored as a full page then the stored program memory contents are compared with the intended data stored in ram after the last write is complete. figure 10-7: program flash memory verify flowchart start verify operation read operation end verify operation this routine assumes that the last row of data written was from an image saved in ram. this image will be used to verify the data currently stored in flash program memory. nvmdat = ram image? last word? fail verify operation no yes yes no figure 10-1 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 134 preliminary ? 2016 microchip technology inc. 10.4.9 wrerr bit the wrerr bit can be used to determine if a write error occurred. wrerr will be set if one of the following conditions occurs: if wr is set while the nvmadrh:nmvadrl points to a write-protected address a reset occurs while a self-write operation was in progress an unlock sequence was interrupted the wrerr bit is normally set by hardware, but can be set by the user for test purposes. once set, wrerr must be cleared in software. table 10-4: actions for progra m flash memory when wr = 1 free lwlo actions for program flash memory when wr = 1 comments 00 write the write latch data to program flash memory row. see section 10.4.4 ?nvmreg erase of pro- gram flash memory? if wp is enabled, wr is cleared and wrerr is set write latches are reset to 3ffh nvmdath:nvmdatl is ignored 01 copy nvmdath:nvmdatl to the write latch corre- sponding to nvmadr lsbs. see section 10.4.4 ?nvmreg erase of program flash memory? write protection is ignored no memory access occurs 1x erase the 32-word row of nvmadrh:nvmadrl location. see section 10.4.3 ?nvmreg write to eeprom? if wp is enabled, wr is cleared and wrerr is set all 32 words are erased nvmdath:nvmdatl is ignored downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 135 pic16(l)f18326/18346 10.5 register definitions: program flash memory control register 10-1: nvmdatl: nonvolatil e memory data low byte register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 nvmdat<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 bit 7-0 nvmdat<7:0> : read/write value for least signifi cant bits of program memory register 10-2: nvmdath: nonvolatil e memory data high byte register u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 nvmdat<13:8> 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 unimplemented: read as 0 bit 5-0 nvmdat<13:8> : read/write value for most signi ficant bits of program memory register 10-3: nvmadrl: nonvolatile memory address low byte register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 nvmadr<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 bit 7-0 nvmadr<7:0> : specifies the least significant bits for program memory address register 10-4: nvmadrh: nonvolatile memory address high byte register u-1 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 nvmadr<14:8> 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 1 bit 6-0 nvmadr<14:8> : specifies the most significant bits for program memory address downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 136 preliminary ? 2016 microchip technology inc. register 10-5: nvmcon1: nonvolat ile memory control 1 register u-0 r/w-0/0 r/w-0/0 r/w/hc-0/0 r/w/hc-x/q r/w-0/0 r/s/hc-0/0 r/s/hc-0/0 nvmregs lwlo free wrerr wren wr rd bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 s = bit can only be set 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 unimplemented: read as 0 bit 6 nvmregs: configuration select bit 1 = access eeprom, configuration, user id and device id registers 0 = access program flash memory bit 5 lwlo: load write latches only bit when free = 0 : 1 = the next wr command updates the write latch for this word within the row; no memory op eration is initiated. 0 = the next wr command writes data or erases otherwise: the bit is ignored. bit 4 free: program flash memory erase enable bit when nvmregs:nvmadr points to a program flash memory location: 1 = performs an erase operation with the next wr command; the 32-word pseudo-row containing the indicated address is erased (to all 1s) to prepare for writing. 0 = all write operations have completed normally bit 3 wrerr: program/erase error flag bit (1,2,3) this bit is normally set by hardware. 1 = a write operation was interrupted by a reset, interrupted unlock sequence, or wr was written to one while nvmadr points to a write-protected address. 0 = the program or erase operation completed normally bit 2 wren: program/erase enable bit 1 = allows program/erase cycles 0 = inhibits programming/erasing of program flash bit 1 wr: write control bit (4,5,6) when nvmreg:nvmadr points to a eeprom location: 1 = initiates an erase/program cycle at the corresponding eeprom location 0 = nvm program/erase operation is complete and inactive when nvmreg:nvmadr points to a program flash memory location: 1 = initiates the operation indicated by table 10-5 0 = nvm program/erase operation is complete and inactive otherwise: this bit is ignored. bit 0 rd: read control bit (7) 1 = initiates a read at address = nvmadr1, and loads data to nvmdat read takes one instruction cycle and the bit is cleared when the operation is complete. the bit can only be set (not cleared) in software. 0 = nvm read operation is complete and inactive. note 1: bit is undefined while wr = 1 (during the eeprom write operation it may be 0 or 1 ). 2: bit must be cleared by software; hardware will not clear this bit. 3: bit may be written to 1 by software in order to implement test sequences. 4: this bit can only be set by following the unlock sequence of section 10.4.2 ?nvm unlock sequence? . 5: operations are self-timed, and the wr bit is cleared by hardware when complete. 6: once a write operation is initiated, setting this bit to zero will have no effect. 7: reading from eeprom loads only nvmdatl<7:0> ( register 10-1 ). downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 137 pic16(l)f18326/18346 register 10-6: nvmcon2: nonvolat ile memory control 2 register w-0/0 w-0/0 w-0/0 w-0/0 w-0/0 w-0/0 w-0/0 w-0/0 nvmcon2 bit 7 bit 0 legend: r = readable bit w = writable bit u = unimplemented bit, read as 0 s = bit can only be set 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-0 nvmcon2<7:0>: flash memory unlock pattern bits to unlock writes, a 55h must be written first, followed by an aah, before setting the wr bit of the nvmcon1 register. the value written to this register is used to unlock the writes. table 10-5: summary of registers associ ated with nonvolat ile memory (nvm) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page intcon gie peie intedg 98 pir2 tmr6if c2if c1if nvmif ssp2if blc2if tmr4if nco1if 106 pie2 tmr6ie c2ie c1ie nvmie ssp2ie blc2ie tmr4ie nco1ie 101 nvmcon1 nvmregs lwlo free wrerr wren wr rd 136 nvmcon2 nvmcon2 137 nvmadrl nvmadr<7:0> 135 nvmadrh (1) nvmadr<14:8> 135 nvmdatl nvmdat<7:0> 135 nvmdath nvmdat<13:8> 135 legend: ? = unimplemented location, read as 0 . shaded cells are not used by nvm. note 1: unimplemented, read as 1 . table 10-6: summary of co nfiguration word with no nvolatile memory (nvm) name bits bit -/7 bit -/6 bit 13/5 bit 12/4 bit 11/3 bit 10/2 bit 9/1 bit 8/0 register on page config3 13:8 lvp 62 7:0 w r t < 1 : 0 > config4 13:8 63 7:0 c p d cp legend: ? = unimplemented location, read as 0 . shaded cells are not used by nvm. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 138 preliminary ? 2016 microchip technology inc. 11.0 i/o ports each port has ten standard registers for its operation. these registers are: portx registers (reads the levels on the pins of the device) latx registers (output latch) trisx registers (data direction) anselx registers (analog select) wpux registers (weak pull-up) inlvlx (input level control) slrconx registers (slew rate) odconx registers (open-drain) most port pins share functions with device peripherals, both analog and digital. in general, when a peripheral is enabled on a port pin, that pin cannot be used as a general purpose output; however, the pin can still be read. the data latch (latx registers) is useful for read-modify-write operations on the value that the i/o pins are driving. a write operation to the latx register has the same effect as a write to the corresponding portx register. a read of the latx register reads of the values held in the i/o port latches, while a read of the portx register reads the actual i/o pin value. ports that support analog inputs have an associated anselx register. when an ansel bit is set, the digital input buffer associated with that bit is disabled. disabling the input buffer prevents analog signal levels on the pin between a logic high and low from causing excessive current in the logic input circuitry. a simplified model of a generic i/o port, without the interfaces to other peripherals, is shown in figure 11-1. figure 11-1: generic i/o port operation 11.1 i/o priorities each pin defaults to the port data latch after reset. other functions are selected with the peripheral pin select logic. see section 12.0 ?peripheral pin select (pps) module? for more information. analog input functions, such as adc and comparator inputs, are not shown in the peripheral pin select lists. these inputs are active when the i/o pin is set for analog mode using the anselx register. digital output functions may continue to control the pin when it is in analog mode. analog outputs, when enabled, take priority over the digital outputs and force the digital output driver to the high-impedance state. table 11-1: port availability per device device porta portb portc pic16(l)f18326 pic16(l)f18346 q d ck write latx data register i/o pin read portx write portx trisx read latx data bus to digital peripherals anselx v dd v ss to analog peripherals downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 139 pic16(l)f18326/18346 11.2 porta registers 11.2.1 data register porta is a 6-bit wide, bidirectional port. the corresponding data direction register is trisa ( register 11-2 ). 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., enables output driver and puts the contents of the output latch on the selected pin). the exception is ra3, which is input-only and its tris bit will always read as 1 . example 11-1 shows how to initialize porta. reading the porta register ( register 11-1 ) reads the status of the pins, whereas writing to it will write to the port latch. all write operations are read-modify-write operations. therefore, a write to a port implies that the port pins are read, this value is modified and then written to the port data latch (lata). the port data latch lata ( register 11-3 ) holds the output port data, and contains the latest value of a lata or porta write. example 11-1: initializing porta 11.2.2 direction control the trisa register ( register 11-2 ) controls the porta pin output drivers, 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. i/o pins configured as analog inputs always read 0 . 11.2.3 open-drain control the odcona register ( register 11-6 ) controls the open-drain feature of the port. open-drain operation is independently selected for each pin. when an odcona bit is set, the corresponding port output becomes an open-drain driver capable of sinking current only. when an odcona bit is cleared, the corresponding port output pin is the standard push-pull drive capable of sourcing and sinking current. 11.2.4 slew rate control the slrcona register ( register 11-7 ) controls the slew rate option for each port pin. slew rate control is independently selectable for each port pin. when an slrcona bit is set, the corresponding port pin drive is slew rate limited. when an slrcona bit is cleared, the corresponding port pin drive slews at the maximum rate possible. 11.2.5 input threshold control the inlvla register ( register 11-8 ) controls the input voltage threshold for each of the available porta input pins. a selection between the schmitt trigger cmos or the ttl compatible thresholds is available. the input threshold is important in determining the value of a read of the porta register and also the level at which an interrupt-on-change occurs, if that feature is enabled. see table 34-4 for more information on threshold levels. ; this code example illustrates ; initializing the porta register. the ; other ports are initialized in the same ; manner. banksel porta ; clrf porta ;init porta banksel lata ;data latch clrf lata ; banksel ansela ; clrf ansela ;digital i/o banksel trisa ; movlw b'00111000' ;set ra<5:3> as inputs movwf trisa ;and set ra<2:0> as ;outputs note: it is not necessary to set open-drain control when using the pin for i 2 c; the i 2 c module controls the pin and makes the pin open-drain. note: changing the input threshold selection should be performed while all peripheral modules are disabled. changing the threshold level during the time a module is active may inadvertently generate a transition associated with an input pin, regardless of the actual voltage level on that pin. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 140 preliminary ? 2016 microchip technology inc. 11.2.6 analog control the ansela register ( register 11-4 ) is used to configure the input mode of an i/o pin to analog. setting the appropriate ansela bit high will cause all digital reads on the pin to be read as 0 and allow analog functions on the pin to operate correctly. the state of the ansela bits has no effect on digital output functions. a pin with tris clear and ansel set will still operate as a digital output, but the input mode will be analog. this can cause unexpected behavior when executing read-modify-write instructions on the affected port. 11.2.7 weak pull-up control the wpua register ( register 11-5 ) controls the individual weak pull-ups for each port pin. porta pin ra3 includes the mclr /v pp input. the m clr input allows the device to be reset, and can be disabled by the mclre bit of configuration word 2. a weak pull-up is present on the ra3 port pin. this weak pull-up is enabled when mclr is enabled (mclre = 1 ) or the wpua3 bit is set. the weak pull-up is disabled when the mclr is disabled and the wpua3 bit is clear. 11.2.8 porta functions and output priorities each porta pin is multiplexed with other functions. each pin defaults to the port latch data after reset. other output functions are selected with the peripheral pin select logic. see section 12.0 ?peripheral pin select (pps) module? for more information. analog input functions, such as adc and comparator inputs are not shown in the peripheral pin select lists. digital output functions may continue to control the pin when it is in analog mode. note: the ansela bits default to the analog mode after reset. to use any pins as digital general purpose or peripheral inputs, the corresponding ansel bits must be initialized to 0 by user software. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 141 pic16(l)f18326/18346 11.3 register definitions: porta register 11-1: porta: porta register u-0 u-0 r/w-x/u r/w-x/u r-x/u r/w-x/u r/w-x/u r/w-x/u r a 5r a 4r a 3 (2) ra2 ra1 ra0 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 unimplemented: read as 0 bit 5-0 ra<5:0> : porta i/o value bits (1) 1 = port pin is > v ih 0 = port pin is < v il note 1: writes to porta are actually written to corresponding lata register. reads from porta register is return of actual i/o pin values. 2: bit ra3 is read-only, and will read 1 when mclre = 1 (master clear enabled). register 11-2: trisa: porta tri-state register u-0 u-0 r/w-1/1 r/w-1/1 u-1 r/w-1/1 r/w-1/1 r/w-1/1 trisa5 trisa4 trisa2 trisa1 trisa0 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 unimplemented: read as 0 bit 5-4 trisa<5:4>: porta tri-state control bit 1 = porta pin configured as an input (tri-stated) 0 = porta pin configured as an output bit 3 unimplemented: read as 1 bit 2-0 trisa<2:0>: porta tri-state control bit 1 = porta pin configured as an input (tri-stated) 0 = porta pin configured as an output downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 142 preliminary ? 2016 microchip technology inc. register 11-3: lata: porta data latch register u-0 u-0 r/w-x/u r/w-x/u u-0 r/w-x/u r/w-x/u r/w-x/u l a t a 5l a t a 4 lata2 lata1 lata0 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 unimplemented: read as 0 bit 5-4 lata<5:4> : ra<5:4> output latch value bits (1) bit 3 unimplemented: read as 0 bit 2-0 lata<2:0> : ra<2:0> output latch value bits (1) note 1: writes to porta are actually written to corresponding lata register. reads from porta register is return of actual i/o pin values. register 11-4: ansela: porta analog select register u-0 u-0 r/w-1/1 r/w-1/1 u-0 r/w-1/1 r/w-1/1 r/w-1/1 ansa5 ansa4 ansa2 ansa1 ansa0 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 unimplemented: read as 0 bit 5-4 ansa<5:4> : analog select between analog or digital function on pins ra<5:4>, respectively 1 = analog input. pin is assigned as analog input (1) . digital input buffer disabled. 0 = digital i/o. pin is assigned to port or digital special function. bit 3 unimplemented: read as 0 bit 2-0 ansa<2:0> : analog select between analog or digital function on pins ra<2:0>, respectively 1 = analog input. pin is assigned as analog input (1) . digital input buffer disabled. 0 = digital i/o. pin is assigned to port or digital special function. note 1: when setting a pin to an analog input, the corresponding tris bit must be set to input mode in order to allow external control of the voltage on the pin. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 143 pic16(l)f18326/18346 register 11-5: wpua: weak pull-up porta register u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 wpua5 wpua4 wpua3 (1) wpua2 wpua1 wpua0 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 unimplemented: read as 0 bit 5-0 wpua<5:0> : weak pull-up register bits (2) 1 = pull-up enabled 0 = pull-up disabled note 1: if mclre = 1 , the weak pull-up in ra3 is always enabled; bit wpua3 is not affected. 2: the weak pull-up device is automatically disabled if the pin is configured as an output. register 11-6: odcona: porta open-drain control register u-0 u-0 r/w-0/0 r/w-0/0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 odca5 odca4 odca2 odca1 odca0 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 unimplemented: read as 0 bit 5-4 odca<5:4>: porta open-drain enable bits for ra<5:4> pins, respectively 1 = port pin operates as open-drain drive (sink current only) 0 = port pin operates as standard push-pull drive (source and sink current) bit 3 unimplemented: read as 0 bit 2-0 odca<2:0>: porta open-drain enable bits for ra<2:0> pins, respectively 1 = port pin operates as open-drain drive (sink current only) 0 = port pin operates as standard push-pull drive (source and sink current) downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 144 preliminary ? 2016 microchip technology inc. register 11-7: slrcona: port a slew rate control register u-0 u-0 r/w-1/1 r/w-1/1 u-0 r/w-1/1 r/w-1/1 r/w-1/1 slra5 slra4 slra2 slra1 slra0 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 unimplemented: read as 0 bit 5-4 slra<5:4>: porta slew rate enable bits for ra<5:4> pins, respectively 1 = port pin slew rate is limited 0 = port pin slews at maximum rate bit 3 unimplemented: read as 0 bit 2-0 slra<2:0>: porta slew rate enable bits for ra<2:0> pins, respectively 1 = port pin slew rate is limited 0 = port pin slews at maximum rate register 11-8: inlvla: porta input level control register u-0 u-0 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 inlvla5 inlvla4 inlvla3 inlvla2 inlvla1 inlvla0 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 unimplemented: read as 0 bit 5-0 inlvla<5:0>: porta input level select bits for ra<5:0> pins, respectively 1 = st input used for port reads and interrupt-on-change 0 = ttl input used for port reads and interrupt-on-change downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 145 pic16(l)f18326/18346 table 11-3: summary of conf iguration word with porta table 11-2: summary of 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 porta ra5 ra4 ra3 ra2 ra1 ra0 141 trisa trisa5 trisa4 trisa2 trisa1 trisa0 141 lata l a t a 5l a t a 4 lata2 lata1 lata0 142 ansela ansa5 ansa4 ansa2 ansa1 ansa0 142 wpua wpua5 wpua4 wpua3 wpua2 wpua1 wpua0 143 odcona odca5 odca4 odca2 odca1 odca0 143 slrcona slra5 slra4 slra2 slra1 slra0 144 inlvla inlvla5 inlvla4 inlvla3 inlvla2 inlvla1 inlvla0 144 legend: x = unknown, u = unchanged, C = unimplemented locations read as 0 . shaded cells are not used by porta. name bits bit -/7 bit -/6 bit 13/5 bit 12/4 bit 11/3 bit 10/2 bit 9/1 bit 8/0 register on page config2 13:8 debug stvren pps1way b o r v 61 7:0 boren1 boren0 lpboren wdte1 wdte0 pwrte mclre legend: = unimplemented location, read as 0 . shaded cells are not used by porta. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 146 preliminary ? 2016 microchip technology inc. 11.4 portb registers (pic16(l)f18346 only) 11.4.1 data register portb is a 4-bit wide bidirectional port and is only available in the pic16(l)f18346 devices. the corresponding data direction register is trisb ( register 11-10 ). setting a trisb bit (= 1 ) will make the corresponding portb pin an input (i.e., put the corresponding output driver in a high-impedance mode). 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). example 11-1 shows how to initialize an i/o port. reading the portb register ( register 11-9 ) reads the status of the pins, whereas writing to it will write to the port latch. all write operations are read-modify-write operations. therefore, a write to a port implies that the port pins are read; this value is modified and then written to the port data latch (latb). the port data latch latb ( register 11-11 ) holds the output port data, and contains the latest value of a latb or portb write. 11.4.2 direction control the trisb register ( register 11-10 ) controls the portb pin output drivers, even when they are being used as analog inputs. the user should ensure the bits in the trisb register are maintained set when using them as analog inputs. i/o pins configured as analog inputs always read 0 . 11.4.3 input threshold control the inlvlb register ( register 11-16 ) controls the input voltage threshold for each of the available portb input pins. a selection between the schmitt trigger cmos or the ttl compatible thresholds is available. the input threshold is important in determining the value of a read of the portb register and also the level at which an interrupt-on-change occurs, if that feature is enabled. see table 34-4 for more information on threshold levels. 11.4.4 open-drain control the odconb register ( register 11-14 ) controls the open-drain feature of the port. open-drain operation is independently selected for each pin. when an odconb bit is set, the corresponding port output becomes an open-drain driver capable of sinking current only. when an odconb bit is cleared, the corresponding port output pin is the standard push-pull drive capable of sourcing and sinking current. 11.4.5 slew rate control the slrconb register ( register 11-15 ) controls the slew rate option for each port pin. slew rate control is independently selectable for each port pin. when an slrconb bit is set, the corresponding port pin drive is slew rate limited. when an slrconb bit is cleared, the corresponding port pin drive slews at the maximum rate possible. 11.4.6 analog control the anselb register ( register 11-12 ) is used to configure the input mode of an i/o pin to analog. setting the appropriate anselb bit high will cause all digital reads on the pin to be read as 0 and allow analog functions on the pin to operate correctly. the state of the anselb bits has no effect on digital output functions. a pin with tris clear and anselb set will still operate as a digital output, but the input mode will be analog. this can cause unexpected behavior when executing read-modify-write instructions on the affected port. 11.4.7 weak pull-up control the wpub register ( register 11-13 ) controls the individual weak pull-ups for each port pin. 11.4.8 portb functions and output priorities each pin defaults to the port latch data after reset. other output functions are selected with the peripheral pin select logic. see section 12.0 ?peripheral pin select (pps) module? for more information. analog input functions, such as adc and comparator inputs, are not shown in the peripheral pin select lists. digital output functions may continue to control the pin when it is in analog mode. note: changing the input threshold selection should be performed while all peripheral modules are disabled. changing the threshold level during the time a module is active may inadvertently generate a transition associated with an input pin, regardless of the actual voltage level on that pin. note: it is not necessary to set open-drain control when using the pin for i 2 c; the i 2 c module controls the pin and makes the pin open-drain. note: the anselb bits default to the analog mode after reset. to use any pins as digital general purpose or peripheral inputs, the corresponding ansel bits must be initialized to 0 by user software. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 147 pic16(l)f18326/18346 11.5 register definitions: portb register 11-9: portb: portb register r/w-x/x r/w-x/x r/w-x/x r/w-x/x u-0 u-0 u-0 u-0 rb7 rb6 rb5 rb4 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 rb<7:4> : portb i/o value bits (1) 1 = port pin is > v ih 0 = port pin is < v il bit 3-0 unimplemented: read as 0 note 1: writes to portb are actually written to corresponding latb register. reads from portb register return actual i/o pin values. register 11-10: trisb: portb tri-state register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 u-0 u-0 u-0 u-0 trisb7 trisb6 trisb5 trisb4 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 trisb<7:4>: portb i/o tri-state control bits 1 = portb pin configured as an output 0 = portb pin configured as an input (tri-stated) bit 3-0 unimplemented: read as 0 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 148 preliminary ? 2016 microchip technology inc. register 11-11: latb: portb data latch register r/w-x/u r/w-x/u r/w-x/u r/w-x/u u-0 u-0 u-0 u-0 latb7 latb6 latb5 latb4 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 latb<7:4> : rb<5:4> output latch value bits (1) bit 3-0 unimplemented: read as 0 note 1: writes to latb are equivalent with writes to the corresponding portb register.reads from latb register return register values, not i/o pin values. register 11-12: anselb: portb analog select register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 u-0 u-0 u-0 u-0 ansb7 ansb6 ansb5 ansb4 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 ansb<7:4> : analog select between analog or digital function 1 = analog input. pin is assigned as analog input (1) . digital input buffer disabled. 0 = digital i/o. pin is assigned to port or digital special function. bit 3-0 unimplemented: read as 0 note 1: setting ansb[n] = 1 disables the digital input circuitry. weak pull-ups, if available, are unaffected. the corresponding tris bit must be set to input mode by the user to allow external control of the voltage on the pin. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 149 pic16(l)f18326/18346 register 11-13: wpub: weak pull-up portb register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 u-0 u-0 u-0 u-0 wpub7 wpub6 wpub5 wpub4 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 wpub<7:4> : weak pull-up register bits 1 = weak pull-up enabled 0 = weak pull-up disabled bit 3-0 unimplemented: read as 0 register 11-14: odco nb: portb open-drain control register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 u-0 u-0 u-0 u-0 odcb7 odcb6 odcb5 odcb4 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 odcb<7:4>: portb open-drain configuration bits for rb<7:4> pins, respectively: 1 = port pin operates as open-drain drive (sink current only) 0 = port pin operates as standard push-pull drive (source and sink current) bit 3-0 unimplemented: read as 0 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 150 preliminary ? 2016 microchip technology inc. register 11-15: slrconb: port b slew rate control register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 u-0 u-0 u-0 u-0 slrb7 slrb6 slrb5 slrb4 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 slrb<7:4>: portb slew rate control on pins rb<7:4>, respectively 1 = slew rate enabled 0 = slew rate disabled bit 3-0 unimplemented: read as 0 register 11-16: inlvlb: portb input level control register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 u-0 u-0 u-0 u-0 inlvlb7 inlvlb6 inlvlb5 inlvlb4 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 inlvlb<7:4>: portb input level select on pins rb<7:4>, respectively 1 = st input used for port reads 0 = ttl input used for port reads bit 3-0 unimplemented: read as 0 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 151 pic16(l)f18326/18346 table 11-4: summary of 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 portb rb7 rb6 rb5 rb4 147 trisb trisb7 trisb6 trisb5 trisb4 147 latb latb7 latb6 latb5 latb4 148 anselb ansb7 ansb6 ansb5 ansb4 148 wpub wpub7 wpu6 wpub5 wpub4 149 odconb odcb7 odcb6 odcb5 odcb4 149 slrconb slrb7 slrb6 slrb5 slrb4 150 inlvlb inlvlb7 inlvlb6 inlvlb5 inlvlb4 150 legend: x = unknown, u = unchanged, C = unimplemented locations read as 0 . shaded cells are not used by portb. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 152 preliminary ? 2016 microchip technology inc. 11.6 portc registers 11.6.1 data register portc is a bidirectional port that is either 6-bit wide (pic16(l)f18326) or 8-bit wide (pic16(l)f18346). the corresponding data direction register is trisc ( register 11-18 ). setting a trisc bit (= 1 ) will make the corresponding portc pin an input (i.e., put the corresponding output driver in a high-impedance mode). 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). example 11-1 shows how to initialize an i/o port. reading the portc register ( register 11-17 ) reads the status of the pins, whereas writing to it will write to the port latch. all write operations are read-modify-write operations. therefore, a write to a port implies that the port pins are read, this value is modified and then written to the port data latch (latc). the port data latch latc ( register 11-19 ) holds the output port data, and contains the latest value of a latc or portc write. 11.6.2 direction control the trisc register ( register 11-18 ) controls the portc pin output drivers, even when they are being used as analog inputs. the user should ensure the bits in the trisc register are maintained set when using them as analog inputs. i/o pins configured as analog inputs always read 0 . 11.6.3 input threshold control the inlvlc register ( register 11-24 ) controls the input voltage threshold for each of the available portc input pins. a selection between the schmitt trigger cmos or the ttl compatible thresholds is available. the input threshold is important in determining the value of a read of the portc register and also the level at which an interrupt-on-change occurs, if that feature is enabled. see table 34-4 for more information on threshold levels. 11.6.4 open-drain control the odconc register ( register 11-22 ) controls the open-drain feature of the port. open-drain operation is independently selected for each pin. when an odconc bit is set, the corresponding port output becomes an open-drain driver capable of sinking current only. when an odconc bit is cleared, the corresponding port output pin is the standard push-pull drive capable of sourcing and sinking current. 11.6.5 slew rate control the slrconc register ( register 11-23 ) controls the slew rate option for each port pin. slew rate control is independently selectable for each port pin. when an slrconc bit is set, the corresponding port pin drive is slew rate limited. when an slrconc bit is cleared, the corresponding port pin drive slews at the maximum rate possible. 11.6.6 analog control the anselc register ( register 11-20 ) is used to configure the input mode of an i/o pin to analog. setting the appropriate anselc bit high will cause all digital reads on the pin to be read as 0 and allow analog functions on the pin to operate correctly. the state of the anselc bits has no effect on digital out- put functions. a pin with tris clear and anselc set will still operate as a digital output, but the input mode will be analog. this can cause unexpected behavior when exe- cuting read-modify-write instructions on the affected port. 11.6.7 weak pull-up control the wpuc register ( register 11-21 ) controls the individual weak pull-ups for each port pin. 11.6.8 portc functions and output priorities each pin defaults to the port latch data after reset. other functions are selected with the peripheral pin select logic. see section 12.0 ?peripheral pin select (pps) module? for more information. analog output functions, such as adc and comparator inputs, are not shown in the peripheral pin select lists. digital output functions may continue to control the pin when it is in analog mode. note: changing the input threshold selection should be performed while all peripheral modules are disabled. changing the threshold level during the time a module is active may inadvertently generate a transition associated with an input pin, regardless of the actual voltage level on that pin. note: it is not necessary to set open-drain control when using the pin for i 2 c; the i 2 c module controls the pin and makes the pin open-drain. note: the anselc bits default to the analog mode after reset. to use any pins as digital general purpose or peripheral inputs, the corresponding ansel bits must be initialized to 0 by user software. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 153 pic16(l)f18326/18346 11.7 register definitions: portc register 11-17: portc: portc register 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 rc7 (1) rc6 (1) rc5 rc4 rc3 rc2 rc1 rc0 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 rc<7:6> : portc i/o value bits (1,2) 1 = port pin is > v ih 0 = port pin is < v il bit 5-0 rc<5:0> : portc general purpose i/o pin bits (2) 1 = port pin is > v ih 0 = port pin is < v il note 1: pic16(l)f18346 only; otherwise read as 0 . 2: writes to portc are actually written to corresponding latc register. reads from portc register is return of actual i/o pin values. register 11-18: trisc: portc tri-state register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 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 trisc<7:6> : portc tri-state control bits (1) 1 = portc pin configured as an input (tri-stated) 0 = portc pin configured as an output bit 5-0 trisc<5:0>: portc tri-state control bits 1 = portc pin configured as an input (tri-stated) 0 = portc pin configured as an output note 1: pic16(l)f18346 only; otherwise read as 0 . downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 154 preliminary ? 2016 microchip technology inc. register 11-19: latc: portc data latch register 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 latc7 (1) latc6 (1) latc5 latc4 latc3 latc2 latc1 latc0 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 latc<7:6> : portc output latch value bits (1) bit 5-0 latc<5:0> : portc output latch value bits note 1: pic16(l)f18346 only; otherwise read as 0 . downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 155 pic16(l)f18326/18346 register 11-20: anselc: portc analog select register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 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 ansc<7:6> : analog select between analog or digital function on pins rc<7:6>, respectively (1) 1 = analog input. pin is assigned as analog input (2) . digital input buffer disabled. 0 = digital i/o. pin is assigned to port or digital special function. bit 5-0 ansc<5:0> : analog select between analog or digital function on pins rc<5:0>, respectively 1 = analog input. pin is assigned as analog input (2) . digital input buffer disabled. 0 = digital i/o. pin is assigned to port or digital special function. note 1: pic16(l)f18346 only; otherwise read as 0 . 2: when setting a pin to an analog input, the corresponding tris bit must be set to input mode in order to allow external control of the voltage on the pin. register 11-21: wpuc: weak pull-up portc register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 wpuc7 (1) wpuc6 (1) wpuc5 wpuc4 wpuc3 wpuc2 wpuc1 wpuc0 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 wpuc<7:6> (1) : weak pull-up register bits (2) 1 = pull-up enabled 0 = pull-up disabled bit 5-0 wpuc<5:0> : weak pull-up register bits (2) 1 = pull-up enabled 0 = pull-up disabled note 1: pic16(l)f18346 only; otherwise read as 0 . 2: the weak pull-up device is automatically disabled if the pin is configured as an output. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 156 preliminary ? 2016 microchip technology inc. register 11-22: odconc: portc open-drain control register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 odcc7 (1) odcc6 (1) odcc5 odcc4 odcc3 odcc2 odcc1 odcc0 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 odcc<7:6> : portc open-drain enable bits (1) for rc<7:6> pins, respectively 1 = port pin operates as open-drain drive (sink current only) 0 = port pin operates as standard push-pull drive (source and sink current) bit 5-0 odcc<5:0>: portc open-drain enable bits for rc<5:0> pins, respectively 1 = port pin operates as open-drain drive (sink current only) 0 = port pin operates as standard push-pull drive (source and sink current) note 1: pic16(l)f18346 only; otherwise read as 0 . register 11-23: slrconc: port c slew rate control register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 slrc7 (1) slrc6 (1) slrc5 slrc4 slrc3 slrc2 slrc1 slrc0 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 slrc<7:6>: portc slew rate enable bits (1) for rc<7:6> pins, respectively 1 = port pin slew rate is limited 0 = port pin slews at maximum rate bit 5-0 slrc<5:0>: portc slew rate enable bits for rc<5:0> pins, respectively 1 = port pin slew rate is limited 0 = port pin slews at maximum rate note 1: pic16(l)f18346 only; otherwise read as 0 . downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 157 pic16(l)f18326/18346 register 11-24: inlvlc: portc input level control register r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 r/w-1/1 inlvlc7 (1) inlvlc6 (1) inlvlc5 inlvlc4 inlvlc3 inlvlc2 inlvlc1 inlvlc0 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 inlvlc<7:6>: portc input level select bits (1) for rc<7:6> pins, respectively 1 = st input used for port reads and interrupt-on-change 0 = ttl input used for port reads and interrupt-on-change bit 5-0 inlvlc<5:0>: portc input level select bits for rc<5:0> pins, respectively 1 = st input used for port reads and interrupt-on-change 0 = ttl input used for port reads and interrupt-on-change note 1: pic16(l)f18346 only; otherwise read as 0 . table 11-5: summary of regist ers associated with portc name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page portc rc7 (1) rc6 (1) rc5 rc4 rc3 rc2 rc1 rc0 153 trisc trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 153 latc latc7 (1) latc6 (1) latc5 latc4 latc3 latc2 latc1 latc0 154 anselc ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 155 wpuc wpuc7 (1) wpuc6 (1) wpuc5 wpuc4 wpuc3 wpuc2 wpuc1 wpuc0 155 odconc odcc7 (1) odcc6 (1) odcc5 odcc4 odcc3 odcc2 odcc1 odcc0 156 slrconc slrc7 (1) slrc6 (1) slrc5 slrc4 slrc3 slrc2 slrc1 slrc0 156 inlvlc inlvlc7 (1) inlvlc6 (1) inlvlc5 inlvlc4 inlvlc3 inlvlc2 inlvlc1 inlvlc0 157 note 1: pic16(l)f18346 only. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 158 preliminary ? 2016 microchip technology inc. 12.0 peripheral pin select (pps) module the peripheral pin select (pps) module connects peripheral inputs and outputs to the device i/o pins. only digital signals are included in the selections. all analog inputs and outputs remain fixed to their assigned pins. input and output selections are independent as shown in the simplified block diagram figure 12-1 . 12.1 pps inputs each peripheral has a pps register with which the inputs to the peripheral are selected. inputs include the device pins. although every peripheral has its own pps input selec- tion register, the selections are identical for every peripheral as shown in register 12-1 . 12.2 pps outputs each i/o pin has a pps register with which the pin output source is selected. with few exceptions, the port tris control associated with that pin retains control over the pin output driver. peripherals that control the pin output driver as part of the peripheral operation will override the tris control as needed. these peripherals include: eusart1 (synchronous operation) mssp (i 2 c) although every pin has its own pps peripheral selection register, the selections are identical for every pin as shown in register 12-2 . figure 12-1: simplified pps block diagram note: the notation xxx in the register name is a place holder for the peripheral identifier. for example, clc1pps. note: the notation rxy is a place holder for the pin identifier. for example, ra0pps. ra0 rxy ra0pps rxypps rc7 (1) rc7pps (1) pps outputs pps inputs peripheral abc peripheral xyz abcpps xyzpps ra0 rc7 (1) note 1: rb<7:4> and rc<7:6> are available on pic16(l)f18346 only. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 159 pic16(l)f18326/18346 12.3 bidirectional pins pps selections for peripherals with bidirectional signals on a single pin must be made so that the pps input and pps output select the same pin. this requires configuring both the appropriate xxxpps input and rxypps output registers. for example, if the scl1 line is routed to pin rc0, the ssp1sclpps input register would be set to ' 10000 ' (routes to rc0) and the rc0pps output register would be set to ' 11000 ' (routes the scl1 internal connection to rc0). peripherals that have bidirectional signals include: eusart1 (synchronous operation) mssp (i 2 c) 12.4 pps lock the pps includes a mode in which all input and output selections can be locked to prevent inadvertent changes. pps selections are locked by setting the ppslocked bit of the ppslock register. setting and clearing this bit requires a special sequence as an extra precaution against inadvertent changes. examples of setting and clearing the ppslocked bit are shown in example 12-1 . example 12-1: pps lock/unlock sequence 12.5 pps permanent lock the pps can be permanently locked by setting the pps1way configuration bit. when this bit is set, the ppslocked bit can only be cleared and set one time after a device reset. this allows for clearing the ppslocked bit so that the input and output selections can be made during initialization. when the ppslocked bit is set after all selections have been made, it will remain set and cannot be cleared until after the next device reset event. 12.6 operation during sleep pps input and output selections are unaffected by sleep. 12.7 effects of a reset a device power-on-reset (por) clears all pps input and output selections to their default values. all other resets leave the selections unchanged. default input selections are shown in pin allocation table 2 and tab l e 3 . note: the i 2 c default input pins are i 2 c and smbus compatible and are the only pins on the pic16(l)f18326 with this compati- bility. for the pic16(l)f18346, in addition to the default pins as described above, rc0, rc1, rc4, and rc5 are also i 2 c and smbus compatible. clock and data signals can be routed to any pin, however pins without i 2 c compatibility will operate at standard ttl/st logic levels as selected by the invlv register. ; suspend interrupts bcf intcon,gie ; banksel ppslock ; set bank ; required sequence, next 5 instructions movlw 0x55 movwf ppslock movlw 0xaa movwf ppslock ; set ppslocked bit to disable writes or ; clear ppslocked bit to enable writes bsf ppslock,ppslocked ; restore interrupts bsf intcon,gie downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 160 preliminary ? 2016 microchip technology inc. 12.8 register definitions: pps input selection register 12-1: xxxpps: peripheral xxx input selection u-0 u-0 u-0 r/w-q/u r/w-q/u r/w-q/u r/w-q/u r/w-q/u xxxpps<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 q = value depends on peripheral bit 7-5 unimplemented: read as 0 bit 4-0 xxxpps<4:0>: peripheral xxx input selection bits 11xxx = reserved. do not use. 10111 = peripheral input is rc7 (1) 10110 = peripheral input is rc6 (1) 10101 = peripheral input is rc5 10100 = peripheral input is rc4 10011 = peripheral input is rc3 10010 = peripheral input is rc2 10001 = peripheral input is rc1 10000 = peripheral input is rc0 ...01111 = peripheral input is rb7 (1) 01110 = peripheral input is rb6 (1) 01101 = peripheral input is rb5 (1) 01100 = peripheral input is rb4 (1) ...0011x = reserved. do not use. 00101 = peripheral input is ra5 00100 = peripheral input is ra4 00011 = peripheral input is ra3 00010 = peripheral input is ra2 00001 = peripheral input is ra1 00000 = peripheral input is ra0 note 1: pic16(l)f18346 only. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 161 pic16(l)f18326/18346 register 12-2: rxypps: pin rxy output source selection register u-0 u-0 u-0 r/w-0/u r/w-0/u r/w-0/u r/w-0/u r/w-0/u rxypps<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 rxypps<4:0>: pin rxy output source selection bits 11111 = rxy source is dsm 11110 = rxy source is clkr 11101 = rxy source is nco1 11100 = rxy source is tmr0 11011 = rxy source is sdo2/sda2 (1) 11010 = rxy source is sck2/scl2 (1) 11001 = rxy source is sdo1/sda1 11000 = rxy source is sck1/scl1 (1) 10111 = rxy source is c2 10110 = rxy source is c1 10101 = rxy source is dt (1) 10100 = rxy source is tx/ck (1) 10011 = rxy source is cwg2d (1) 10010 = rxy source is cwg2c (1) 10001 = rxy source is cwg2b (1) 10000 = rxy source is cwg2a (1) 01111 = rxy source is ccp4 01110 = rxy source is ccp3 01101 = rxy source is ccp2 01100 = rxy source is ccp1 01011 = rxy source is cwg1d (1) 01010 = rxy source is cwg1c (1) 01001 = rxy source is cwg1b (1) 01000 = rxy source is cwg1a (1) 00111 = rxy source is clc4out 00110 = rxy source is clc3out 00101 = rxy source is clc2out 00100 = rxy source is clc1out 00011 = rxy source is pwm6 00010 = rxy source is pwm5 00001 = reserved 00000 = rxy source is latxy note 1: tris control is overridden by the peripheral as required. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 162 preliminary ? 2016 microchip technology inc. register 12-3: ppslo ck: pps lock register u-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-0/0 ppslocked 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 unimplemented: read as 0 bit 0 ppslocked: pps locked bit 1 = pps is locked. pps selections can not be changed. 0 = pps is not locked. pps selections can be changed. table 12-1: summary of registers associated with the pps module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ppslock ppslocked 162 intpps intpps<4:0> 160 t0ckipps t0ckipps<4:0> 160 t1ckipps t1ckipps<4:0> 160 t1gpps t1gpps<4:0> 160 t3ckipps t3ckipps<4:0> 160 t3gpps t3gpps<4:0> 160 t5ckipps t5ckipps<4:0> 160 t5gpps t5gpps<4:0> 160 ccp1pps ccp1pps<4:0> 160 ccp2pps ccp2pps<4:0> 160 ccp3pps ccp3pps<4:0> 160 ccp4pps ccp4pps<4:0> 160 cwg1pps cwg1pps<4:0> 160 cwg2pps cwg2pps<4:0> 160 mdcin1pps mdcin1pps<4:0> 160 mdcin2pps mdcin2pps<4:0> 160 mdminpps mdminpps<4:0> 160 ssp1clkpps ssp1clkpps<4:0> 160 ssp1datpps ssp1datpps<4:0> 160 ssp1sspps ssp1sspps<4:0> 160 ssp2clkpps ssp2clkpps<4:0> 160 ssp2datpps ssp2datpps<4:0> 160 ssp2sspps ssp2sspps<4:0> 160 legend: = unimplemented, read as 0 . shaded cells are unused by the pps module. note 1: pic16(l)f18346 only. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 163 pic16(l)f18326/18346 rxpps rxpps<4:0> 160 txpps txpps<4:0> 160 clcin0pps clcin0pps<4:0> 160 clcin1pps clcin1pps<4:0> 160 clcin2pps clcin2pps<4:0> 160 clcin3pps clcin3pps<4:0> 160 ra0pps ra0pps<4:0> 161 ra1pps ra1pps<4:0> 161 ra2pps ra2pps<4:0> 161 ra4pps ra4pps<4:0> 161 ra5pps ra5pps<4:0> 161 rb4pps (1) rb4pps<4:0> 161 rb5pps (1) rb5pps<4:0> 161 rb6pps (1) rb6pps<4:0> 161 rb7pps (1) rb7pps<4:0> 161 rc0pps rc0pps<4:0> 161 rc1pps rc1pps<4:0> 161 rc2pps rc2pps<4:0> 161 rc3pps rc3pps<4:0> 161 rc4pps rc4pps<4:0> 161 rc5pps rc5pps<4:0> 161 rc6pps (1) rc6pps<4:0> 161 rc7pps (1) rc7pps<4:0> 161 table 12-1: summary of registers associated with the pps module (continued) name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page legend: = unimplemented, read as 0 . shaded cells are unused by the pps module. note 1: pic16(l)f18346 only. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 164 preliminary ? 2016 microchip technology inc. 13.0 peripheral module disable the pic16(l)f18326/18346 provides the ability to disable selected modules, placing them into the lowest possible power mode. for legacy reasons, all modules are on by default following any reset. 13.1 disabling a module disabling a module has the following effects: all clock and control inputs to the module are suspended; there are no logic transitions, and the module will not function. the module is held in reset. any sfrs become unimplemented - writing is disabled - reading returns 00h module outputs are disabled; i/o goes to the next module according to pin priority. 13.2 enabling a module when the register bit is cleared, the module is re- enabled and will be in its reset state; sfr data will reflect the por reset values. depending on the module, it may take up to one full instruction cycle for the module to become active. there should be no interaction with the module (e.g., writing to registers) for at least one instruction after it has been re-enabled. 13.3 disabling a module when a module is disabled, any and all associated input selection registers (isms) are also disabled. 13.4 system clock disable setting syscmd (pmd0, register 13-1 ) disables the system clock (f osc ) distribution network to the peripherals. not all peripherals make use of sysclk, so not all peripherals are affected. refer to the specific peripheral description to see if it will be affected by this bit. register 13-1: pmd0: pmd control register 0 r/w-0/0 r/w-0/0 u-0 u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 syscmd fvrmd nvmmd clkrmd iocmd 7 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 q = value depends on condition bit 7 syscmd: disable peripheral system clock network bit see description in section 13.4 ?system clock disable? . 1 = system clock network disabled (a.k.a. f osc ) 0 = system clock network enabled bit 6 fvrmd: disable fixed voltage reference fvr bit 1 = fvr module disabled 0 = fvr module enabled bit 5-3 unimplemented: read as 0 bit 2 nvmmd: nvm module disable bit (1) 1 = data eeprom (a.k.a. user memory, eeprom) reading and writing is disabled; nvmcon registers cannot be written; fsr access to eeprom returns zero. 0 = nvm module enabled bit 1 clkrmd: disable clock reference clkr bit 1 = clkr module disabled 0 = clkr module enabled bit 0 iocmd: disable interrupt-on-change bit, all ports 1 = ioc module(s) disabled 0 = ioc module(s) enabled note 1: when enabling nvm, a delay of up to 1 s may be required before accessing data. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 165 pic16(l)f18326/18346 register 13-2: pmd1: pmd control register 1 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 ncomd tmr6md tmr5md tmr4md tmr3md tmr2md tmr1md tmr0md 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 q = value depends on condition bit 7 ncomd: disable numerically control oscillator bit 1 = nco1 module disabled 0 = nco1 module enabled bit 6 tmr6md: disable timer tmr6 bit 1 = tmr6 module disabled 0 = tmr6 module enabled bit 5 tmr5md: disable timer tmr5 bit 1 = tmr5 module disabled 0 = tmr5 module enabled bit 4 tmr4md: disable timer tmr4 bit 1 = tmr4 module disabled 0 = tmr4 module enabled bit 3 tmr3md: disable timer tmr3 bit 1 = tmr3 module disabled 0 = tmr3 module enabled bit 2 tmr2md: disable timer tmr2 bit 1 = tmr2 module disabled 0 = tmr2 module enabled bit 1 tmr1md: disable timer tmr1 bit 1 = tmr1 module disabled 0 = tmr1 module enabled bit 0 tmr0md: disable timer tmr0 bit 1 = tmr0 module disabled 0 = tmr0 module enabled downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 166 preliminary ? 2016 microchip technology inc. register 13-3: pmd2: pmd control register 2 u-0 r/w-0/0 r/w-0/0 u-0 u-0 r/w-0/0 r/w-0/0 u-0 dacmd adcmd cmp2md cmp1md 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 q = value depends on condition bit 7 unimplemented: read as 0 bit 6 dacmd: disable dac bit 1 = dac module disabled 0 = dac module enabled bit 5 adcmd: disable adc bit 1 = adc module disabled 0 = adc module enabled bit 4-3 unimplemented: read as 0 bit 2 cmp2md: disable comparator c2 bit 1 = c2 module disabled 0 = c2 module enabled bit 1 cmp1md: disable comparator c1 bit 1 = c1 module disabled 0 = c1 module enabled bit 0 unimplemented: read as 0 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 167 pic16(l)f18326/18346 register 13-4: pmd3: pmd control register 3 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 cwg2md cwg1md pwm6md pwm5md ccp4md ccp3md ccp2md ccp1md 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 q = value depends on condition bit 7 cwg2md: disable cwg2 bit 1 = cwg2 module disabled 0 = cwg2 module enabled bit 6 cwg1md: disable cwg1 bit 1 = cwg1 module disabled 0 = cwg1 module enabled bit 5 pwm6md: disable pulse-width modulator pwm6 bit 1 = pwm6 module disabled 0 = pwm6 module enabled bit 4 pwm5md: disable pulse-width modulator pwm5 bit 1 = pwm5 module disabled 0 = pwm5 module enabled bit 3 ccp4md: disable pulse-width modulator ccp4 bit 1 = ccp4 module disabled 0 = ccp4 module enabled bit 2 ccp3md: disable pulse-width modulator ccp3 bit 1 = ccp3 module disabled 0 = ccp3 module enabled bit 1 ccp2md: disable pulse-width modulator ccp2 bit 1 = ccp2 module disabled 0 = ccp2 module enabled bit 0 ccp1md: disable pulse-width modulator ccp1 bit 1 = ccp1 module disabled 0 = ccp1 module enabled downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 168 preliminary ? 2016 microchip technology inc. register 13-5: pmd4: pmd control register 4 u-0 u-0 r/w-0/0 u-0 u-0 r/w-0/0 r/w-0/0 u-0 u a r t 1 m d mssp2md mssp1md 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 q = value depends on condition bit 7-6 unimplemented: read as 0 bit 5 uart1md: disable eusart1 bit 1 = eusart1 module disabled 0 = eusart1 module enabled bit 4-3 unimplemented: read as 0 bit 2 mssp2md: disable mssp2 bit 1 = mssp2 module disabled 0 = mssp2 module enabled bit 1 mssp1md: disable mssp1 bit 1 = mssp1 module disabled 0 = mssp1 module enabled bit 0 unimplemented: read as 0 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 169 pic16(l)f18326/18346 register 13-6: pmd5: pmd control register 5 u-0 u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 clc4md clc3md clc2md clc1md dsmmd 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 q = value depends on condition bit 7-5 unimplemented: read as 0 bit 4 clc4md: disable clc4 bit 1 = clc4 module disabled 0 = clc4 module enabled bit 3 clc3md: disable clc3 bit 1 = clc3 module disabled 0 = clc3 module enabled bit 2 clc2md: disable clc2 bit 1 = clc2 module disabled 0 = clc2 module enabled bit 1 clc1md: disable clc1 bit 1 = clc1 module disabled 0 = clc1 module enabled bit 0 dsmmd: disable data signal modulator bit 1 = dsm module disabled 0 = dsm module enabled downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 170 preliminary ? 2016 microchip technology inc. 14.0 interrupt-on-change all pins on all ports can be configured to operate as interrupt-on-change (ioc) pins. an interrupt can be generated by detecting a signal that has either a rising edge or a falling edge. any individual pin, or combination of pins, can be configured to generate an interrupt. the interrupt-on-change module has the following features: interrupt-on-change enable (master switch) individual pin configuration rising and falling edge detection individual pin interrupt flags figure 14-1 is a block diagram of the ioc module. 14.1 enabling the module to allow individual pins to generate an interrupt, the iocie bit of the pie0 register must be set. if the iocie bit is disabled, the edge detection on the pin will still occur, but an interrupt will not be generated. 14.2 individual pin configuration for each pin, a rising edge detector and a falling edge detector are present. to enable a pin to detect a rising edge, the associated bit of the iocxp register is set. to enable a pin to detect a falling edge, the associated bit of the iocxn register is set. a pin can be configured to detect rising and falling edges simultaneously by setting the associated bits in both of the iocxp and iocxn registers. 14.3 interrupt flags the bits located in the iocxf registers are status flags that correspond to the interrupt-on-change pins of each port. if an expected edge is detected on an appropriately enabled pin, then the status flag for that pin will be set, and an interrupt will be generated if the iocie bit is set. the iocif bit of the pir0 register reflects the status of all iocxf bits. 14.4 clearing interrupt flags the individual status flags, (iocxf register bits), can be cleared by resetting them to zero. if another edge is detected during this clearing operation, the associated status flag will be set at the end of the sequence, regardless of the value actually being written. in order to ensure that no detected edge is lost while clearing flags, only and operations masking out known changed bits should be performed. the following sequence is an example of what should be performed. example 14-1: clearing interrupt flags (porta example) 14.5 operation in sleep the interrupt-on-change interrupt sequence will wake the device from sleep mode, if the iocie bit is set. if an edge is detected while in sleep mode, the affected iocxf register will be updated prior to the first instruction executed out of sleep. movlw 0xff xorwf iocaf, w andwf iocaf, f downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 171 pic16(l)f18326/18346 figure 14-1: interrupt-on-change bl ock diagram (porta example) iocanx iocapx q2 q4q1 data bus = 0or1 write iocafx iocie to data bus iocafx edge detect ioc interrupt to cpu core from all other iocnfx individual pin detectors dqr s dq r dq r rax q1q2 q3 q4 q4q1 q1 q2 q3 q4 q1 q2 q3 q4 q4q1 q4q1 q4q1 f osc rev. 10-000037a 7/30/2013 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 172 preliminary ? 2016 microchip technology inc. 14.6 register definitions: interrupt-on-change control register 14-1: iocap: interrupt-on-c hange porta positive edge register u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 iocap5 iocap4 iocap3 iocap2 iocap1 iocap0 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 unimplemented: read as 0 bit 5-0 iocap<5:0>: interrupt-on-change porta positive edge enable bits 1 = interrupt-on-change enabled on the pin for a positive going edge. iocafx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin register 14-2: iocan: interrupt-on-change porta negative edge register u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 iocan5 iocan4 iocan3 iocan2 iocan1 iocan0 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 unimplemented: read as 0 bit 5-0 iocan<5:0>: interrupt-on-change porta negative edge enable bits 1 = interrupt-on-change enabled on the pin for a negative going edge. iocafx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 173 pic16(l)f18326/18346 register 14-3: iocaf: interrupt- on-change porta flag register u-0 u-0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 iocaf5 iocaf4 iocaf3 iocaf2 iocaf1 iocaf0 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 hs - bit is set in hardware bit 7-6 unimplemented: read as 0 bit 5-0 iocaf<5:0>: interrupt-on-change porta flag bits 1 = an enabled change was detected on the associated pin set when iocapx = 1 and a rising edge was detected on rax, or when iocanx = 1 and a falling edge was detected on rax. 0 = no change was detected, or the user cleared the detected change. register 14-4: iocbp: interrupt-on-c hange portb positive edge register (1) r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 u-0 u-0 u-0 u-0 iocbp7 iocbp6 iocbp5 iocbp4 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 iocbp<7:4>: interrupt-on-change portb positive edge enable bits 1 = interrupt-on-change enabled on the pin for a positive going edge. iocafx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin bit 3-0 unimplemented: read as 0 note 1: pic16(l)f18346 only. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 174 preliminary ? 2016 microchip technology inc. register 14-5: iocbn: interrupt-on-change portb negative edge register (1) r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 u-0 u-0 u-0 u-0 iocbn7 iocbn6 iocbn5 iocbn4 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 iocbn<7:4>: interrupt-on-change portb negative edge enable bits 1 = interrupt-on-change enabled on the pin for a negative going edge. iocafx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin bit 3-0 unimplemented: read as 0 note 1: pic16(l)f18346 only. register 14-6: iocbf: interrupt- on-change portb flag register (1) r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 u-0 u-0 u-0 u-0 iocbf7 iocbf6 iocbf5 iocbf4 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 hs - bit is set in hardware bit 7-4 iocbf<7:4>: interrupt-on-change portb flag bits 1 = an enabled change was detected on the associated pin set when iocbpx = 1 and a rising edge was detected on rbx, or when iocbnx = 1 and a falling edge was detected on rbx. 0 = no change was detected, or the user cleared the detected change. bit 3-0 unimplemented: read as 0 note 1: pic16(l)f18346 only. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 175 pic16(l)f18326/18346 register 14-7: ioccp: interrupt-on-c hange portc positive edge register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 ioccp7 (1) ioccp6 (1) ioccp5 ioccp4 ioccp3 ioccp2 ioccp1 ioccp0 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 ioccp<7:6>: interrupt-on-change portc positive edge enable bits (1) 1 = interrupt-on-change enabled on the pin for a positive going edge. ioccfx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin bit 5-0 ioccp<5:0>: interrupt-on-change portc positive edge enable bits 1 = interrupt-on-change enabled on the pin for a positive going edge. ioccfx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin note 1: pic16(l)f18346 only. register 14-8: ioccn: interrupt-on-change portc negative edge register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 ioccn7 (1) ioccn6 (1) ioccn5 ioccn4 ioccn3 ioccn2 ioccn1 ioccn0 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 ioccn<7:6>: interrupt-on-change portc negative edge enable bits (1) 1 = interrupt-on-change enabled on the pin for a negative going edge. ioccfx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin bit 5-0 ioccn<5:0>: interrupt-on-change portc negative edge enable bits 1 = interrupt-on-change enabled on the pin for a negative going edge. ioccfx bit and iocif flag will be set upon detecting an edge. 0 = interrupt-on-change disabled for the associated pin note 1: pic16(l)f18346 only. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 176 preliminary ? 2016 microchip technology inc. register 14-9: ioccf: interrupt- on-change portc flag register r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 r/w/hs-0/0 ioccf7 (1) ioccf6 (1) ioccf5 ioccf4 ioccf3 ioccf2 ioccf1 ioccf0 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 hs - bit is set in hardware bit 7-6 i occf<7:6>: interrupt-on-change portc flag bits 1 = an enabled change was detected on the associated pin. set when ioccpx = 1 and a rising edge was detected on rcx, or when ioccnx = 1 and a falling edge was detected on rcx. 0 = no change was detected, or the user cleared the detected change. bit 5-0 ioccf<5:0>: interrupt-on-change portc flag bits 1 = an enabled change was detected on the associated pin. set when ioccpx = 1 and a rising edge was detected on rcx, or when ioccnx = 1 and a falling edge was detected on rcx. 0 = no change was detected, or the user cleared the detected change. note 1: pic16(l)f18346 only. table 14-1: summary of registers as sociated with interrupt-on-change name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ansela ansa4 ansa4 ansa2 ansa1 ansa0 142 anselb (1) ansb7 ansb6 ansb5 ansb4 148 anselc ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 155 trisa trisa5 trisa4 (2) trisa2 trisa1 trisa0 141 trisb (1) trisb7 trisb6 trisb5 trisb4 147 trisc trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 153 intcon gie peie i n t e d g 98 pie0 tmr0ie iocie inte 99 iocap iocap5 iocap4 iocap3 iocap2 iocap1 iocap0 172 iocan iocan5 iocan4 iocan3 iocan2 iocan1 iocan0 172 iocaf iocaf5 iocaf4 iocaf3 iocaf2 iocaf1 iocaf0 173 iocbp (1) iocbp7 iocbp6 iocbp5 iocbp4 173 iocbn (1) iocbn7 iocbn6 iocbn5 iocbn4 174 iocbf (1) iocbf7 iocbf6 iocbf5 iiocbf4 174 ioccp ioccp7 (1) ioccp6 (1) ioccp5 ioccp4 ioccp3 ioccp2 ioccp1 ioccp0 175 ioccn ioccn7 (1) ioccn6 (1) ioccn5 ioccn4 ioccn3 ioccn2 ioccn1 ioccn0 175 ioccf ioccf7 (1) ioccf6 (1) ioccf5 ioccf4 ioccf3 ioccf2 ioccf1 ioccf0 176 legend: = unimplemented location, read as 0 . shaded cells are not used by interrupt-on-change. note 1: pic16(l)f18346 only. 2: unimplemented, read as 1 . downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 177 pic16(l)f18326/18346 15.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 fvrcon register. 15.1 independent gain amplifiers the output of the fvr, which is supplied to the adc, comparators and dac, is routed through two independent programmable gain amplifiers. each amplifier can be programmed for a gain of 1x, 2x or 4x, to produce the three possible voltage levels. the adfvr<1:0> bits of the fvrcon register are used to enable and configure the gain amplifier settings for the reference supplied to the adc module. reference section 21.0 ?analog-to-digital converter (adc) module? for additional information. the cdafvr<1:0> bits of the fvrcon register are used to enable and configure the gain amplifier settings for the reference supplied to the dac and comparator module. reference section 23.0 ?5-bit digital-to-analog converter (dac1) module? and section 17.0 ?comparator module? for additional information. 15.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 fvrrdy bit of the fvrcon register will be set. figure 15-1: voltage reference block diagram note: fixed voltage reference output cannot exceed v dd . 1x 2x 4x 1x 2x 4x adfvr<1:0> cdafvr<1:0> fvr_buffer1 (to adc module) fvr_buffer2 (to comparators and dac) + _ fvren fvrrdy note 1 2 2 rev. 10 -000 053c 12 /9/201 3 note: any peripheral requiring the fixed reference (see table 15-1 ). downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 178 preliminary ? 2016 microchip technology inc. 15.3 register definitions: fvr control register 15-1: fvrcon: fixed voltage reference control register r/w-0/0 r-q/q r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 fvren fvrrdy (1) tsen (3) tsrng (3) cdafvr<1:0> adfvr<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 q = value depends on condition bit 7 fvren: fixed voltage reference enable bit 1 = fixed voltage reference is enabled 0 = fixed voltage reference is disabled bit 6 fvrrdy: fixed voltage reference ready flag bit (1) 1 = fixed voltage reference output is ready for use 0 = fixed voltage reference output is not ready or not enabled bit 5 tsen: temperature indicator enable bit (3) 1 = temperature indicator is enabled 0 = temperature indicator is disabled bit 4 tsrng: temperature indicator range selection bit (3) 1 =v out = v dd - 4v t (high range) 0 =v out = v dd - 2v t (low range) bit 3-2 cdafvr<1:0>: comparator fvr buffer gain selection bits 11 = comparator fvr buffer gain is 4x, (4.096v) (2) 10 = comparator fvr buffer gain is 2x, (2.048v) (2) 01 = comparator fvr buffer gain is 1x, (1.024v) 00 = comparator fvr buffer is off bit 1-0 adfvr<1:0>: adc fvr buffer gain selection bit 11 = adc fvr buffer gain is 4x, (4.096v) (2) 10 = adc fvr buffer gain is 2x, (2.048v) (2) 01 = adc fvr buffer gain is 1x, (1.024v) 00 = adc fvr buffer is off note 1: fvrrdy is always 1 . 2: fixed voltage reference output cannot exceed v dd . 3: see section 16.0 ?temperature indicator module? for additional information. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 179 pic16(l)f18326/18346 table 15-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 fvrcon fvren fvrrdy tsen tsrng cdafvr<1:0> adfvr<1:0> 178 adcon0 chs<5:0> go/done adon 242 adcon1 adfm adcs<2:0> adnref adpref<1:0> 243 cmxcon1 cxintp cxintn cxpch<2:0> cxnch<2:0> 189 dac1con0 dac1en dac1oe dac1pps<1:0> dac1nss 261 legend: shaded cells are not used with the fixed voltage reference. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 180 preliminary ? 2016 microchip technology inc. 16.0 temperature indicator module this family of devices is equipped with a temperature circuit designed to measure the operating temperature of the silicon die. the circuits range of operating temperature falls between -40c and +85c. the output is a voltage that is proportional to the device temperature. the output of the temperature indicator is internally connected to the device adc. the circuit may be used as a temperature threshold detector or a more accurate temperature indicator, depending on the level of calibration performed. a one- point calibration allows the circuit to indicate a temperature closely surrounding that point. a two-point calibration allows the circuit to sense the entire range of temperature more accurately. reference application note an1333, use and calibration of the internal temperature indicator (ds01333) for more details regarding the calibration process. 16.1 circuit operation figure 16-1 shows a simplified block diagram of the temperature circuit. the proportional voltage output is achieved by measuring the forward voltage drop across multiple silicon junctions. equation 16-1 describes the output characteristics of the temperature indicator. equation 16-1: v out ranges the temperature sense circuit is integrated with the fixed voltage reference (fvr) module. see section 15.0 ?fixed voltage reference (fvr)? for more information. the circuit is enabled by setting the tsen bit of the fvrcon register. when disabled, the circuit draws no current. the circuit operates in either high or low range. the high range, selected by setting the tsrng bit of the fvrcon register, provides a wider output voltage. this provides more resolution over the temperature range, but may be less consistent from part to part. this range requires a higher bias voltage to operate and thus, a higher v dd is needed. the low range is selected by clearing the tsrng bit of the fvrcon register. the low range generates a lower voltage drop and thus, a lower bias voltage is needed to operate the circuit. the low range is provided for low voltage operation. figure 16-1: temperature circuit diagram 16.2 minimum operating v dd when the temperature circuit is operated in low range, the device may be operated at any operating voltage that is within specifications. when the temperature circuit is operated in high range, the device operating voltage, v dd , must be high enough to ensure that the temperature circuit is correctly biased. table 16-1 shows the recommended minimum v dd vs. range setting. 16.3 temperature output the output of the circuit is measured using the internal analog-to-digital converter. a channel is reserved for the temperature circuit output. refer to section 21.0 ?analog-to-digital converter (adc) module? for detailed information. high range: v out = v dd - 4v t low range: v out = v dd - 2v t table 16-1: recommended v dd vs. range min. v dd , tsrng = 1 min. v dd , tsrng = 0 3.6v 1.8v tsen tsrng v dd v out to a d c temp. indicator downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 181 pic16(l)f18326/18346 16.4 adc acquisition time to ensure accurate temperature measurements, the user must wait at least 200 ? s after the adc input multiplexer is connected to the temperature indicator output before the conversion is performed. in addition, the user must wait 200 ? s between consecutive conversions of the temperature indicator output. table 16-2: summary of registers asso ciated with the temperature indicator name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page fvrcon fvren fvrrdy tsen tsrng cdafvr<1:0> adfvr<1:0> 178 legend: shaded cells are unused by the temperature indicator module. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 182 preliminary ? 2016 microchip technology inc. 17.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. comparators are very useful mixed signal building blocks because they provide analog functionality independent of program execution. the analog comparator module includes the following features: programmable input selection programmable output polarity rising/falling output edge interrupts wake-up from sleep programmable speed/power optimization cwg auto-shutdown source selectable voltage reference 17.1 comparator overview a single comparator is shown in figure 17-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. the comparators available for this device are located in table 17-1 . figure 17-1: single comparator table 17-1: available comparators device c1 c2 pic16(l)f18326 pic16(l)f18346 C + 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. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 183 pic16(l)f18326/18346 figure 17-2: comparat or module simplified block diagram note 1: when cxon = 0 , the comparator will produce a 0 at the output. 2: when cxon = 0 , all multiplexer inputs are disconnected. mux cx cxon (1) cxnch<2:0> 3 01 c x pch<2:0> c x in1- c x in2- c x in3- c x in+ mux - + cxvn cxvp q1 den q set cxif 01 c x sync c x out dq dac_ o utput fvr buffer 2 c x in0- 2 cxsp cxhys det interrupt det interrupt cxintn cxintp 3 3 agnd tris bit cxon (2) (2) from timer1 tmr1_clk fvr buffer 2 0 1 2 3 4 5 6 7 agnd 45 6 7 reserved reserved reserved reserved reserved sync_cxout to ti m e r 1 to cm x con0 (c x out) and cm2con1 (mc x out) c x pol 01 cxzlf zlf async_cxout reserved downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 184 preliminary ? 2016 microchip technology inc. 17.2 comparator control each comparator has two control registers: cmxcon0 and cmxcon1. the cmxcon0 register (see register 17-1 ) contains control and status bits for the following: enable output output polarity speed/power selection hysteresis enable timer1 output synchronization the cmxcon1 register (see register 17-2 ) contains control bits for the following: interrupt on positive/negative edge enables positive input channel selection negative input channel selection 17.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. 17.2.2 comparator output the output of the comparator can be monitored by reading either the cxout bit of the cmxcon0 register or the mcxout bit of the cmout register. the comparator output can also be routed to an external pin through the rxypps register ( register 12-2 ). the corresponding tris bit must be clear to enable the pin as an output. 17.2.3 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 17-2 shows the output state versus input conditions, including polarity control. 17.3 comparator hysteresis a selectable amount of separation voltage can be added to the input pins of each comparator to provide a hysteresis function to the overall operation. hysteresis is enabled by setting the cxhys bit of the cmxcon0 register. see comparator specifications in table 34-14 for more information. 17.4 timer1 gate operation the output resulting from a comparator operation can be used as a source for gate control of timer1. see section 26.6 ?timer1 gate? for more information. this feature is useful for timing the duration or interval of an analog event. it is recommended that the comparator output be synchronized to timer1. this ensures that timer1 does not increment while a change in the comparator is occurring. 17.4.1 comparator output synchronization the output from a comparator can be synchronized with timer1 by setting the cxsync bit of the cmxcon0 register. once enabled, the comparator output is latched on the falling edge of the timer1 source clock. if a prescaler is used with timer1, the comparator output is latched after the prescaling function. to prevent a race condition, the comparator output is latched on the falling edge of the timer1 clock source and timer1 increments on the rising edge of its clock source. see the comparator block diagram ( figure 17-2 ) and the timer1 block diagram ( figure 26-1 ) for more information. note 1: the internal output of the comparator is latched with each instruction cycle. unless otherwise specified, external outputs are not latched. table 17-2: comparator output state vs. input conditions input condition cxpol cxout cxv n > cxv p 00 cxv n < cxv p 01 cxv n > cxv p 11 cxv n < cxv p 10 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 185 pic16(l)f18326/18346 17.5 comparator interrupt an interrupt can be generated upon a change in the output value of the comparator for each comparator, a rising edge detector and a falling edge detector are present. when either edge detector is triggered and its associ- ated enable bit is set (cxintp and/or cxintn bits of the cmxcon1 register), the corresponding interrupt flag bit (cxif bit of the pir2 register) will be set. to enable the interrupt, you must set the following bits: cxon, cxpol and cxsp bits of the cmxcon0 register cxie bit of the pie2 register cxintp bit of the cmxcon1 register (for a rising edge detection) cxintn bit of the cmxcon1 register (for a falling edge detection) peie and gie bits of the intcon register the associated interrupt flag bit, cxif bit of the pir2 register, must be cleared in software. if another edge is detected while this flag is being cleared, the flag will still be set at the end of the sequence. 17.6 comparator positive input selection configuring the cxpch<2:0> bits of the cmxcon1 register directs an internal voltage reference or an analog pin to the non-inverting input of the comparator: cxin0+ analog pin dac output fvr (fixed voltage reference) v ss (ground) see section 15.0 ?fixed voltage reference (fvr)? for more information on the fixed voltage reference module. see section 23.0 ?5-bit digital-to-analog converter (dac1) module? for more information on the dac input signal. any time the comparator is disabled (cxon = 0 ), all comparator inputs are disabled. 17.7 comparator negative input selection the cxnch<2:0> bits of the cmxcon1 register direct an analog input pin and internal reference voltage or analog ground to the inverting input of the comparator: cxin- pin fvr (fixed voltage reference) analog ground some inverting input selections share a pin with the operational amplifier output function. enabling both functions at the same time will direct the operational amplifier output to the comparator inverting input. 17.8 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 table 34-14 for more details. note: although a comparator is disabled, an interrupt can be generated by changing the output polarity with the cxpol bit of the cmxcon0 register, or by switching the comparator on or off with the cxon bit of the cmxcon0 register. note: to use cxiny+ and cxiny- pins as analog input, the appropriate bits must be set in the ansel register and the corresponding tris bits must also be set to disable the output drivers. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 186 preliminary ? 2016 microchip technology inc. 17.9 analog input connection considerations a simplified circuit for an analog input is shown in figure 17-3 . 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, may have very little leakage current to minimize inaccuracies introduced. figure 17-3: 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 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 to comparator note 1: see i/o ports in table 34-4 . analog input pin downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 187 pic16(l)f18326/18346 17.10 cwg auto-shutdown source the output of the comparator module can be used as an auto-shutdown source for the cwg module. when the output of the comparator is active and the corresponding asxe is enabled, the cwg operation will be suspended immediately ( section 19.7.1.2 ?external input source shutdown? ). 17.11 operation in sleep mode the comparator module can operate during sleep. the comparator clock source is based on the timer1 clock source. if the timer1 clock source is either the system clock (f osc ) or the instruction clock (f osc /4), timer1 will not operate during sleep, and synchronized comparator outputs will not operate. a comparator interrupt will wake the device from sleep. the cxie bits of the pie2 register must be set to enable comparator interrupts. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 188 preliminary ? 2016 microchip technology inc. 17.12 register definitions: comparator control register 17-1: cmxcon0: comparator cx control register 0 r/w-0/0 r-0/0 u-0 r/w-0/0 u-0 r/w-1/1 r/w-0/0 r/w-0/0 cxon cxout cxpol cxsp cxhys cxsync 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 cxon: comparator enable bit 1 = comparator is enabled 0 = comparator is disabled and consumes no active power bit 6 cxout: comparator output bit if cxpol = 1 (inverted polarity): 1 = cxvp < cxvn 0 = cxvp > cxvn if cxpol = 0 (non-inverted polarity): 1 = cxvp > cxvn 0 = cxvp < cxvn bit 5 unimplemented: read as 0 bit 4 cxpol: comparator output polarity select bit 1 = comparator output is inverted 0 = comparator output is not inverted bit 3 unimplemented: read as 0 bit 2 cxsp: comparator speed/power select bit 1 = comparator operates in normal-power, high-speed mode 0 = reserved. (do not use) bit 1 cxhys: comparator hysteresis enable bit 1 = comparator hysteresis enabled 0 = comparator hysteresis disabled bit 0 cxsync: comparator output synchronous mode bit 1 = comparator output to timer1 and i/o pin is synchronous to changes on timer1 clock source. output updated on the falling edge of timer1 clock source. 0 = comparator output to timer1 and i/o pin is asynchronous downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 189 pic16(l)f18326/18346 register 17-2: cmxcon1: comparator cx control register 1 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 cxintp cxintn cxpch<2:0> cxnch<2: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 cxintp: comparator interrupt on positive going edge enable bits 1 = the cxif interrupt flag will be set upon a positive going edge of the cxout bit 0 = no interrupt flag will be set on a positive going edge of the cxout bit bit 6 cxintn: comparator interrupt on negative going edge enable bits 1 = the cxif interrupt flag will be set upon a negative going edge of the cxout bit 0 = no interrupt flag will be set on a negative going edge of the cxout bit bit 5-3 cxpch<2:0>: comparator positive input channel select bits 111 = cxvp connects to av ss 110 = cxvp connects to fvr buffer 2 101 = cxvp connects to dac output 100 = cxvp unconnected 011 = cxvp unconnected 010 = cxvp unconnected 001 = cxvn unconnected 000 = cxvp connects to cxin0+ pin bit 2-0 cxnch<2:0>: comparator negative input channel select bits 111 = cxvn connects to av ss 110 = cxvn connects to fvr buffer 2 101 = cxvn unconnected 100 = cxvn unconnected 011 = cxvn connects to cxin3- pin 010 = cxvn connects to cxin2- pin 001 = cxvn connects to cxin1- pin 000 = cxvn connects to cxin0- pin downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 190 preliminary ? 2016 microchip technology inc. register 17-3: cmout: comparator output register u-0 u-0 u-0 u-0 u-0 u-0 r-0/0 r-0/0 mc2out mc1out 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-2 unimplemented: read as 0 bit 1 mc2out: mirror copy of c2out bit bit 0 mc1out: mirror copy of c1out bit table 17-3: summary of registers as sociated with co mparator module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page ansela D D ansa5 ansa4 D ansa2 ansa1 ansa0 142 anselb (1) ansb7 ansb6 ansb5 ansb4 D D D D 148 anselc ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 155 trisa D D trisa5 trisa4 D trisa2 trisa1 trisa0 141 trisb (1) trisb7 trisb6 trisb5 trisb4 D D D D 147 trisc trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 153 cmxcon0 cxon cxout D cxpol D cxsp cxhys cxsync 188 cmxcon1 cxintp cxintn cxpch<2:0> cxnch<2:0> 189 cmout D D D D D D mc2out mc1out 190 fvrcon fvren fvrrdy tsen tsrng cdafvr<1:0> adfvr<1:0> 178 daccon0 dac1en D dac1oe D dac1pss<1:0> D dac1nss 261 daccon1 D D D dac1r<4:0> 262 intcon gie peie D D D D D intedg 98 pie2 tmr6ie c2ie c1ie nvmie ssp2ie blc2ie tmr4ie nco1ie 101 pir2 tmr6if c2if c1if nvmif ssp2if blc2if tmr4if nco1if 106 cmpxpps D D D cmpxpps<4:0> 160 clcinxpps D D D clcinxpps<4:0> 160 mdminpps D D D mdminpps<4:0> 160 t1gpps D D D t1gpps<4:0> 160 cwgxas1 D D D as4e as3e as2e as1e as0e 216 legend: = unimplemented location, read as 0 . shaded cells are unused by the comparator module. note 1: pic16(l)f18346 only. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 191 pic16(l)f18326/18346 18.0 pulse-width modulation (pwm) the pwmx modules generate pulse-width modulated (pwm) signals of varying frequency and duty cycle. in addition to the ccp modules, the pic16(l)f18326/18346 devices contain two pwm modules. these modules are essentially the same as the ccp modules without the capture or compare functionality. 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 (pulse width), and the low portion of the signal is considered the off state. 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. 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. figure 18-1 shows a typical waveform of the pwm signal. figure 18-1: pwm output 18.1 standard pwm mode the standard pwm mode generates a pulse-width modulation (pwm) signal on the pwmx pin with up to ten bits of resolution. the period, duty cycle, and resolution are controlled by the following registers: tmr2, tmr4 or tmr6 registers pr2, pr4 or pr6 registers pwmxcon registers pwmxdch registers pwmxdcl registers figure 28-2 shows a simplified block diagram of the pwm operation. if pwmpol = 0 , the default state of the output is 0 . if pwmpol = 1 , the default state is 1 . if pwmen = 0 , the output will be the default state. period pulse width tmr2 = 0 tmr2 = pwmdc tmr2 = pr2 note: the corresponding tris bit must be cleared to enable the pwm output on the pwmx pin note: the formulas and text refer to tmr2 and pr2, for simplicity. the same formulas and text apply to tmr4/6 and pr4/6. the timer sources can be selected in register 18-4 . for additional information on tmr2/4/6, please refer to section 27.0 ?timer 2/4/6 module? downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 192 preliminary ? 2016 microchip technology inc. figure 18-2: simplified pwm block diagram 18.1.1 pwm period referring to figure 18-1 , the pwm output has a period and a pulse width. the frequency of the pwm is the inverse of the period (1/period). the pwm period is specified by writing to the pr2 register. the pwm period can be calculated using the following formula: equation 18-1: pwm period when tmr2 is equal to pr2, the following three events occur on the next increment cycle: tmr2 is cleared the pwmx pin is set (exception: if the pwm duty cycle = 0%, the pin will not be set.) the pwm pulse width is latched from pwmxdc. 18.1.2 pwm duty cycle the pwm duty cycle is specified by writing a 10-bit value to the pwmxdc register. the pwmxdch contains the eight msbs and bits <7:6> of the pwmxdcl register contain the two lsbs. the pwmdc register is double-buffered and can be updated at any time. this double buffering is essential for glitch-free pwm operation. new values take effect when tmr2 = pr2. note that pwmdc is left-justified. the 8-bit timer tmr2 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 timer2 prescaler is set to 1:1. equation 18-2 is used to calculate the pwm pulse width. equation 18-3 is used to calculate the pwm duty cycle ratio. equation 18-2: pulse width equation 18-3: duty cycle ratio r s qq dut ccle reg isters pwmdch pwmdcl<7:6> comparator comparator tmr2 pr2 output polarit (pwmpol) pwmx r note: if the pulse-width value is greater than the period, the assigned pwm pin(s) will remain unchanged. pwm period pr 2 ?? 1+ ?? 4t osc ? ? ? = (tmr2 prescale value) note: t osc = 1/f osc pulse width pwmxdc ?? t osc ? ? = ? (tmr2 prescale value) duty cycle ratio pwmxdc ?? 4 pr 21 + ?? ------------------------------ - = downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 193 pic16(l)f18326/18346 18.1.3 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 pr2 is 255. the resolution is a function of the pr2 register value as shown by equation 18-4 . equation 18-4: pwm resolution 18.1.4 operation in sleep mode in sleep mode, the tmr2 register will not increment and the state of the module will not change. if the pwmx pin is driving a value, it will continue to drive that value. when the device wakes up, tmr2 will continue from its previous state. 18.1.5 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 6.0, oscillator module (with fail-safe clock monitor) for additional details. 18.1.6 effects of reset any reset will force all ports to input mode and the pwmx registers to their reset states. 18.1.7 setup for pwm operation the following steps will be taken when configuring the module for using the pwmx outputs: 1. disable the pwmx pin output driver(s) by setting the associated tris bit(s). 2. configure the pwm output polarity by configuring the pwmxpol bit of the pwmxcon register. 3. load the pr2 register with the pwm period value, as determined by equation 18-1 . 4. load the pwmxdch register and bits <7:6> of the pwmxdcl register with the pwm duty cycle value, as determined by equation 18-2 . 5. configure and start timer2: clear the tmr2if interrupt flag bit of the pir1 register. select the timer2 prescale value by configuring the t2ckps bit of the t2con register. enable timer2 by setting the tmr2on bit of the t2con register. 6. wait until the tmr2if is set. 7. when the tmr2if flag bit is set: clear the associated tris bit(s) to enable the output driver. route the signal to the desired pin by configuring the rxypps register. enable the pwmx module by setting the pwmxen bit of the pwmxcon register. in order to send a complete duty cycle and period on the first pwm output, the above steps must be followed in the order given. if it is not critical to start with a complete pwm signal, then the pwm module can be enabled during step 2 by setting the pwmxen bit of the pwmxcon register. note: if the pulse-width value is greater than the period, the assigned pwm pin(s) will remain unchanged. resolution 4 pr 21 + ?? ?? log 2 ?? log ----------------------------------------- bits = downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 194 preliminary ? 2016 microchip technology inc. 18.2 register definitions: pwm control register 18-1: pwmxcon: pwm control register r/w-0/0 u-0 r-0 r/w-0/0 u-0 u-0 u-0 u-0 pwmxen pwmxout pwmxpol 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 pwmxen: pwm module enable bit 1 = pwm module is enabled 0 = pwm module is disabled bit 6 unimplemented: read as 0 bit 5 pwmxout: pwm module output level when bit is read. bit 4 pwmxpol: pwmx output polarity select bit 1 = pwm output is active-low. 0 = pwm output is active-high. bit 3-0 unimplemented: read as 0 register 18-2: pwmxdch: pwm duty cycle high bits 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 pwmxdc<9:2> 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-0 pwmxdc<9:2>: pwm duty cycle most significant bits these bits are the msbs of the pwm duty cycle. the two lsbs are found in pwmxdcl register. register 18-3: pwmxdcl: pwm duty cycle low bits r/w-x/u r/w-x/u u-0 u-0 u-0 u-0 u-0 u-0 pwmxdc<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 pwmxdc<1:0>: pwm duty cycle least significant bits these bits are the lsbs of the pwm duty cycle. the msbs are found in pwmx dch register. bit 5-0 unimplemented: read as 0 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 195 pic16(l)f18326/18346 table 18-1: 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 16 4 1 1 1 1 pr2 value 0xff 0xff 0xff 0x3f 0x1f 0x17 maximum resolution (bits) 10 10 10 8 7 6.6 table 18-2: 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 t i m e r p r e s c a l e 1 641111 pr2 value 0xff 0xff 0xff 0x3f 0x1f 0x17 maximum resolution (bits) 10 10 10 8 7 6.6 register 18-4: pwmtmrs: pw m timers control register u-0 u-0 u-0 u-0 r/w-0/0 r/w-1/1 r/w-0/0 r/w-1/1 p6tsel<1:0> p5tsel<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 p6tsel<1:0>: pwm6 mode timer selection bits 11 = pwm6 is based on tmr6 10 = pwm6 is based on tmr4 01 = pwm6 is based on tmr2 00 = reserved bit 1-0 p5tsel<1:0>: pwm5 mode timer selection bits 11 = pwm5 is based on tmr6 10 = pwm5 is based on tmr4 01 = pwm5 is based on tmr2 00 = reserved downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 196 preliminary ? 2016 microchip technology inc. table 18-3: summary of registers associated with pwmx name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page trisa trisa5 trisa4 (2) trisa2 trisa1 trisa0 141 ansela ansa5 ansa4 ansa2 ansa1 ansa0 142 trisb (1) trisb7 trisb6 trisb5 trisb4 147 anselb (1) ansb7 ansb6 ansb5 ansb4 148 trisc trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 153 anselc ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 155 pwm5con pwm5en p w m 5 o u t p w m 5 p o l 194 pwm5dch pwm5dc<9:2> 194 pwm5dcl pwm5dc<1:0> 194 pwm6con pwm6en p w m 6 o u t p w m 6 p o l 194 pwm6dch pwm6dc<9:2> 194 pwm6dcl pwm6dc<1:0> 194 pwmtmrs p6tsel<1:0> p5tsel<1:0> 195 intcon gie peie i n t e d g 98 pir1 tmr1gif adif rcif txif ssp1if bcl1if tmr2if tmr1if 105 pir2 tmr6if c2if c1if nvmif ssp2if bcl2if tmr4if nco1if 106 pie1 tmr1gie adie rcie txie ssp1ie bcl1ie tmr2ie tmr1ie 100 pie2 tmr6ie c2ie c1ie nvmie ssp2ie bcl2ie tmr4ie nco1ie 101 t2con t2outps<3:0> tmr2on t2ckps<1:0> 296 t4con t4outps<3:0> tmr4on t4ckps<1:0> 290 t6con t6outps<3:0> tmr6on t6ckps<1:0> 290 tmr2 tmr2<7:0> 297 tmr4 tmr4<7:0> 297 tmr6 tmr6<7:0> 297 pr2 pr2<7:0> 297 pr4 pr4<7:0> 297 pr6 pr6<7:0> 297 rxypps rxypps<4:0> 161 cwgxdat d a t < 3 : 0 > 213 clcxsely l c x d y s < 5 : 0 > 227 mdsrc mdms<3:0> 270 mdcarh mdchpol mdchsync mdch<3:0> 271 mdcarl mdclpol mdclsync mdcl<3:0> 272 legend: - = unimplemented locations, read as 0 . shaded cells are not used by the pwm module. note 1: pic16(l)f18346 only. 2: unimplemented, read as 1 . downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 197 pic16(l)f18326/18346 19.0 complementary waveform generator (cwg) module the complementary waveform generator (cwgx) produces complementary waveforms with dead-band delay from a selection of input sources. the cwgx module has the following features: selectable dead-band clock source control selectable input sources output enable control output polarity control dead-band control with independent 6-bit rising and falling edge dead-band counters auto-shutdown control with: - selectable shutdown sources - auto-restart enable - auto-shutdown pin override control 19.1 fundamental operation the cwg generates two output waveforms from the selected input source. the off-to-on transition of each output can be delayed from the on-to-off transition of the other output, thereby, creating a time delay immediately where neither output is driven. this is referred to as dead time and is covered in section 19.6 ?dead-band control? . it may be necessary to guard against the possibility of circuit faults or a feedback event arriving too late or not at all. in this case, the active drive must be terminated before the fault condition causes damage. this is referred to as auto-shutdown and is covered in section 19.7 ?auto-shutdown control? . 19.2 operating modes the cwgx module can operate in six different modes, as specified by the mode<2:0> bits of the cwgxcon0 register: half-bridge mode push-pull mode asynchronous steering mode synchronous steering mode full-bridge mode, forward full-bridge mode, reverse all modes accept a single pulse data input, and provide up to four outputs as described in the following sections. all modes include auto-shutdown control as described in section 19.11 ?register definitions: cwg control? 19.2.1 half-bridge mode in half-bridge mode, two output signals are generated as true and inverted versions of the input as illustrated in figure 19-1 . a non-overlap (dead-band) time is inserted between the two outputs to prevent shoot- through current in various power supply applications. dead-band control is described in section 19.6 ?dead-band control? . steering modes are not used in half-bridge mode. the unused outputs, cwgxc and cwgxd, drive similar signals with polarity independently controlled by polc and pold, respectively. figure 19-1: cwgx half- bridge mode operation note: except as noted for full-bridge mode ( section 19.2.4 ?full-bridge modes? ), mode changes may only be performed while en = 0 ( register 19-1 ). cwg [ clock input source cwg [ a cwg [ b falling event dead band rising event dead band rising event dead band falling event dead band falling event dead band rising event dead band downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 198 preliminary ? 2016 microchip technology inc. 19.2.2 push-pull mode in push-pull mode, two output signals are generated, alternating copies of the input as illustrated in figure 19-2 . this alternation creates the push-pull effect required for driving some transformer based power supply designs. dead-band control is not used in push-pull mode. steering modes are not used in push-pull mode. the push-pull sequencer is reset whenever en = 0 or if an auto-shutdown event occurs. the sequencer is clocked by the first input pulse, and the first output appears on cwgxa. the unused outputs cwgxc and cwgxd drive copies of cwgxa and cwgxb, respectively, but with polarity controlled by polc and pold. figure 19-2: cwgx push -pull mode operation 19.2.3 steering modes in both synchronous and asynchronous steering modes, the modulated input signal can be steered to any combination of four cwg outputs and a fixed-value will be presented on all the outputs not used for the pwm output. each output has independent polarity, steering, and shutdown options. dead-band control is not used in either steering mode. when stry = 0 ( register 19-5 ), the corresponding pin is held at the level defined by sdaty ( register 19-5 ). when stry = 1 , the pin is driven by the modulated input signal. the poly bits ( register 19-2 ) control the signal polarity only when stry = 1 . the cwg auto-shutdown operation also applies to steering modes as described in section 19.11 ?register definitions: cwg control? . cwg [ clock cwg [ a cwg [ b input source note: only the wgstry bits are synchronized; the wgsdaty (data) bits are not synchronized. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 199 pic16(l)f18326/18346 19.2.3.1 synchronous steering mode in synchronous steering mode (mode<2:0> bits = 001 , register 19-1 ), changes to steering selection registers take effect on the next rising edge of the modulated data input ( figure 19-3) . in synchronous steering mode, the output will always produce a complete waveform. figure 19-3: example of synchrono us steering (mode<2:0> = 001 ) 19.2.3.2 asynchronous steering mode in asynchronous mode (mode<2:0> bits = 000 , register 19-1 ), steering takes effect at the end of the instruction cycle that writes to wgxstr. in asynchronous steering mode, the output signal may be an incomplete waveform ( register 19-4 ). this operation may be useful when the user firmware needs to immediately remove a signal from the output pin. figure 19-4: example of asynchro nous steering (mode<2:0> = 000 ) 19.2.3.3 start-up considerations the application hardware must use the proper external pull-up and/or pull-down resistors on the cwg output pins. this is required because all i/o pins are forced to high-impedance at reset. the poly bits ( register 19-2 ) allow the user to choose whether the output signals are active-high or active- low. rising edge of input rising edge of input cwg [ a follows cwg [ input cwg [ input cwg [ a wgstra cwg [ input cwg [ a wgstra end of instruction cycle end of instruction cycle cwg [ a follows cwg [ input downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 200 preliminary ? 2016 microchip technology inc. 19.2.4 full-bridge modes in forward and reverse full-bridge modes, three out- puts drive static values while the fourth is modulated by the data input. dead-band control is described in section 19.2.3 ?steering modes? and section 19.6 ?dead-band control? . steering modes are not used with either of the full-bridge modes. the mode selection may be toggled between forward and reverse (changing mode<2:0>) without clearing en. when connected as shown in figure 19-5 , the outputs are appropriate for a full-bridge motor driver. each cwg output signal has independent polarity control, so the circuit can be adapted to high-active and low-active drivers. figure 19-5: example of full-bridge application v+ v- fet driver fet driver fet driver fet driver cwg [ a cwg [ b cwg [ c cwg [ d load qaqb qc qd downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 201 pic16(l)f18326/18346 19.2.4.1 full-bridge forward mode in full-bridge forward mode (mode<2:0> = 010 ), cwgxa is driven to its active state and cwgxd is modulated while cwgxb and cwgxc are driven to their inactive state, as illustrated at the top of figure 19-6. 19.2.4.2 full-bridge reverse mode in full-bridge reverse mode (mode<2:0> = 011 ), cwgxc is driven to its active state and cwgxb is modulated while cwgxa and cwgxd are driven to their inactive state, as illustrated at the bottom of figure 19-6. figure 19-6: example of full-bridge output 19.2.4.3 direction change in full-bridge mode in full-bridge mode, changing mode<2:0> controls the forward/reverse direction. changes to mode<2:0> change to the new direction on the next rising edge of the modulated input. a direction change is initiated in software by changing the mode<2:0> bits of the wgxcon0 register. the sequence is illustrated in figure 19-7 . the associated active output cwgxa and the inactive output cwgxc are switched to drive in the opposite direction. the previously modulated output cwgxd is switched to the inactive state, and the previously inactive output cwgxb begins to modulate. cwg modulation resumes after the direction-switch dead band has elapsed. note 1: a rising cwg data input creates a rising event on the modulated output. 2: output signals shown as active-high; all poly bits are clear. cwg1a (2) cwg1b (2) cwg1c (2) cwg1d (2) period pulse width (1) (1) forward mode pulse width period reverse mode cwg1a (2) cwg1b (2) cwg1c (2) cwg1d (2) (1) (1) note 1: a rising cwg1 data input creates a rising event on the modulated output. 2: output signals shown as active-hi gh; all wgpoly bits are clear. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 202 preliminary ? 2016 microchip technology inc. 19.2.4.4 dead-band delay in full-bridge mode dead-band delay is important when either of the following conditions is true: 1. the direction of the cwg output changes when the duty cycle of the data input is at or near 100%, or 2. the turn-off time of the power switch, including the power device and driver circuit, is greater than the turn-on time. the dead-band delay is inserted only when changing directions, and only the modulated output is affected. the statically-configured outputs (cwgxa and cwgxc) are not afforded dead band, and switch essentially simultaneously. figure 19-7 shows an example of the cwg outputs changing directions from forward to reverse, at near 100% duty cycle. in this example, at time t1, the output of cwgxa and cwgxd become inactive, while output cwgxc 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 for the duration of t. the same phenomenon will occur to power devices qa and qb for the cwg direction change from reverse to forward. if changing the cwg direction at high duty cycle is required for an application, two possible solutions for eliminating the shoot-through current are: 1. reduce the cwg duty cycle for one period before changing directions. 2. use switch drivers that can drive the switches off faster than they can drive them on. figure 19-7: example of pwm direct ion change at near 100% duty cycle forward period reverse period t1 pulse width pulse width t on t off t = t off - t on cwg1a cwg1b cwg1c cwg1d external switch c external switch d potential shoot- through current downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 203 pic16(l)f18326/18346 figure 19-8: simplified cwgx block diagram (half-bridge mode, mode<2:0> = 100 ) rev. 10-000209a 10/16/2014 10 10 10 10 0011 10 01 0011 10 01 0011 10 01 0011 10 01 lsac<1:0> lsbd<1:0> lsac<1:0> lsbd<1:0> clock data in data out clock data in data out e d q as0e c1out as1e as2e c2out clc2 as3e shutdown = 1 ren shutdown = 0 s r q pola polb polc pold cwg data freeze dq cwg data cwgxd cwgxc cwgxb cwgxa 1 1 0 1 1 0 0 0 high-z high-z high-z high-z rising dead-band block falling dead-band block cwg data a cwg data b en auto- shutdown source shutdown cwgxpps clc4 as4e cwg data input cwg clock cwg data downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 204 preliminary ? 2016 microchip technology inc. figure 19-9: simplified cwg block diagram (push-pull mode, mode <2:0> = 101 ) rev. 10-000210a 10/16/2014 10 10 10 10 0011 10 01 0011 10 01 0011 10 01 0011 10 01 lsac<1:0> lsbd<1:0> lsac<1:0> lsbd<1:0> cwg data cwg data input e d q shutdown = 1 ren shutdown = 0 s r q pola polb polc pold cwg data freeze dq cwg data cwgxd cwgxc cwgxb cwgxa 1 1 0 1 1 0 0 0 high-z high-z high-z high-z dq q cwg data a cwg data b en auto- shutdown source shutdown as0e c1out as1e as2e c2out clc2 as3e cwgxpps clc4 as4e downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 205 pic16(l)f18326/18346 figure 19-10: simplified cwg block diagram (output steering modes) rev. 10-000211a 10/16/2014 10 10 10 10 0011 10 01 0011 10 01 0011 10 01 0011 10 01 lsac<1:0> lsbd<1:0> lsac<1:0> lsbd<1:0> cwg data input e d q shutdown = 1 ren shutdown = 0 s r q pola polb polc pold cwg data freeze dq cwgxd cwgxc cwgxb cwgxa 1 1 0 1 1 0 0 0 high-z high-z high-z high-z 10 datd strd 10 datc strc 10 datb strb 10 data stra cwg data shutdown auto- shutdown source cwg data d cwg data c cwg data b cwg data a en mode<2:0> = 000: asynchronous mode<2:0> = 001: synchronous as0e c1out as1e as2e c2out clc2 as3e cwgxpps clc4 as4e downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 206 preliminary ? 2016 microchip technology inc. figure 19-11: simplified cwg block di agram (forward and reverse full-bridge modes) rev. 10-000212a 10/16/2014 10 10 10 10 0011 10 01 0011 10 01 0011 10 01 0011 10 01 lsac<1:0> lsbd<1:0> lsac<1:0> lsbd<1:0> cwg data cwg clock clock signal in signal out clock signal in signal out cwgx data input e d q shutdown = 1 ren shutdown = 0 s r q pola polb polc pold cwg data freeze dq cwg data cwgxd cwgxc cwgxb cwgxa 1 1 0 1 1 0 0 0 high-z high-z high-z high-z rising dead-band block falling dead-band block cwg data a cwg data b en auto- shutdown source shutdown mode<2:0> = 010: forward mode<2:0> = 011: reverse cwg clock mode<2:0> cwg data cwg data cwg data c cwg data d dq q as0e c1out as1e as2e c2out clc2 as3e cwgxpps clc4 as4e downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 207 pic16(l)f18326/18346 19.3 clock source the clock source is used to drive the dead-band timing circuits. the cwgx module allows the following clock sources to be selected: f osc (system clock) hfintosc (16 mhz only) when the hfintosc is selected the hfintosc will be kept running during sleep. therefore, cwg modes requiring dead band can operate in sleep provided that the cwg data input is also active during sleep. the clock sources are selected using the cs bit of the cwgxclkcon register ( register 19-3 ). 19.4 selectable input sources the cwg generates the output waveforms from the input sources in tab le 1 9- 1 . the input sources are selected using the dat<3:0> bits in the cwgxdat register ( register 19-4 ). 19.5 output control immediately after the cwg module is enabled, the complementary drive is configured with all output drives cleared. 19.5.1 cwgx outputs each cwg output can be routed to a peripheral pin select (pps) output via the rxypps register (see section 12.0 ?peripheral pin select (pps) module? ). 19.5.2 polarity control the polarity of each cwg output can be selected independently. when the output polarity bit is set, the corresponding output is active-low. clearing the output polarity bit configures the corresponding output as active-high. however, polarity does not affect the override levels. output polarity is selected with the poly bits of the cwgxcon1 register. 19.6 dead-band control dead-band control provides for non-overlapping output signals to prevent current shoot-through in power switches. the cwgx modules contain two 6-bit dead-band counters. these counters can be loaded with values that will determine the length of the dead band initiated on either the rising or falling edges of the input source. dead-band control is used in either half- bridge or full-bridge modes. the rising-edge dead-band delay is determined by the rising dead-band count register ( register 19-8, cwgxdbr) and the falling-edge dead-band delay is determined by the falling dead-band count register ( register 19-9, cwgxdbf). dead-band duration is established by counting the cwg clock periods from zero up to the value loaded into either the rising or fall- ing dead-band counter registers. the dead-band counters are incremented on every rising edge of the cwg clock source. 19.6.1 rising edge and reverse dead band in half-bridge mode, the rising edge dead band delays the turn-on of the cwgxa output after the rising edge of the cwg data input. in full-bridge mode, the reverse dead-band delay is only inserted when changing directions from forward mode to reverse mode, and only the modulated output cwgxb is affected. the cwgxdbr register determines the duration of the dead-band interval on the rising edge of the input source signal. this duration is from 0 to 64 periods of the cwg clock. dead band is always initiated on the edge of the input source signal. a count of zero indicates that no dead band is present. if the input source signal reverses polarity before the dead-band count is completed then no output will be seen on the respective output. the cwgxdbr register value is double-buffered. if en = 0 ( register 19-1 ), the buffer is loaded when cwgxdbr is written. if en = 1 , then the buffer will be loaded at the rising edge, following the first falling edge of the data input after the ld bit ( register 19-1 ) is set. table 19-1: selectable input sources source peripheral signal name cwgxpps cwg pps input connection c1out comparator 1 output c2out comparator 2 output ccp1 capture/compare/pwm output ccp2 capture/compare/pwm output ccp3 capture/compare/pwm output ccp4 capture/compare/pwm output pwm5 pwm5 output pwm6 pwm6 output nco1 numerically controlled oscillator (nco) output clc1 configurable logic cell 1 output clc2 configurable logic cell 2 output clc3 configurable logic cell 3 output clc4 configurable logic cell 4 output downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 208 preliminary ? 2016 microchip technology inc. 19.6.2 falling edge and forward dead band in half-bridge mode, the falling edge dead band delays the turn-on of the cwgxb output at the falling edge of the cwgx data input. in full-bridge mode, the forward dead-band delay is only inserted when changing direc- tions from reverse mode to forward mode, and only the modulated output cwgxd is affected. the cwgxdbf register determines the duration of the dead-band interval on the falling edge of the input source signal. this duration is from zero to 64 periods of the cwg clock. dead band is always initiated on the edge of the input source signal. a count of zero indicates that no dead band is present. if the input source signal reverses polarity before the dead-band count is completed, then no output will be seen on the respective output. the cwgxdbf register value is double-buffered. when en = 0 ( register 19-1 ), the buffer is loaded when cwgxdbf is written. if en = 1 , then the buffer will be loaded at the rising edge following the first falling edge of the data input after the ld ( register 19-1 ) is set. 19.6.3 dead-band jitter the cwg input data signal may be asynchronous to the cwg input clock, so some jitter may occur in the observed dead band in each cycle. the maximum jitter is equal to one cwg clock period. see equation 19-1 for details and an example. equation 19-1: dead-band delay time calculation t dead band_min ? 1 f cwg_clock -------------------------------- -dbx4:0> ? ? = t dead band_max ? 1 f cwg_clock -------------------------------- -dbx4:0> 1 + ? ? = t jitter t dead band_max ? t dead band_min ? ? = t jitter 1 f cwg_clock -------------------------------- - = t dead band_max ? t dead band_min ? t jitter + = example: dbr 4:0> 0x0a 10 == ? f cwg_clock 8 mhz = t jitter 1 8 mhz ----------------- = t dead band_min ? 125 ns 10 1.25 ? s = ? = t dead band_max ? 1.25 ? s0.125 ? s1.37 ? s = + = downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 209 pic16(l)f18326/18346 19.7 auto-shutdown control auto-shutdown is a method to immediately override the cwg output levels with specific overrides that allow for safe shutdown of the circuit. the shutdown state can be either cleared automatically or held until cleared by software. 19.7.1 shutdown the shutdown state can be entered by either of the following two methods: software generated external input the shutdown bit indicates when a shutdown condition exists. the bit may be set or cleared in software or by hardware. 19.7.1.1 software-generated shutdown setting the shutdown bit of the cwgxas0 register will force the cwg into the shutdown state. when auto-restart is disabled, the shutdown state will persist as long as the shutdown bit is set. when auto-restart is enabled, the shutdown bit will clear automatically and resume operation on the next rising edge event. 19.7.1.2 external input source shutdown any of the auto-shutdown external inputs can be selected to suspend the cwg operation. these sources are individually enabled by the asxe bits of the cwgxas1 register ( register 19-7 ). when any of the selected inputs goes active (pins are active-low), the cwg outputs will immediately switch to the override levels selected by the lsbd<1:0> and lsac<1:0> bits without any software delay ( section 19.7.1.3 ?pin override levels? ). any of the following external input sources can be selected to cause a shutdown condition: comparator c1 comparator c2 clc2 cwgxpps 19.7.1.3 pin override levels the levels driven to the cwg outputs during an auto- shutdown event are controlled by the lsbd<1:0> and lsac<1:0> bits of the cwgxas0 register ( register 19-6 ). the lsbd<1:0> bits control cwgxb/d output levels, while the lsac<1:0> bits control the cwgxa/c output levels. 19.7.1.4 auto-shutdown interrupts when an auto-shutdown event occurs, either by software or hardware setting shutdown, the cwgxif flag bit of the pir4 register is set ( register 7-11 ). 19.8 auto-shutdown restart after an auto-shutdown event has occurred, there are two ways to resume operation: software controlled auto-restart in either case, the shutdown source must be cleared before the restart can take place. that is, either the shutdown condition must be removed, or the corresponding wgasxe bit must be cleared. 19.8.1 software-controlled restart if the ren bit of the cwgxasd0 register is clear (ren = 0 ), the cwgx module must be restarted after an auto-shutdown event through software. once all auto-shutdown conditions are removed, the software must clear shutdown. once shutdown is cleared, the cwg module will resume operation upon the first rising edge of the cwg data input. 19.8.2 auto-restart if the ren bit of the cwgxasd0 register is set (ren = 1 ), the cwgx module will restart from the shutdown state automatically. once all auto-shutdown conditions are removed, the hardware will automatically clear shutdown. once shutdown is cleared, the cwg module will resume operation upon the first rising edge of the cwg data input. note: shutdown inputs are level sensitive, not edge sensitive. the shutdown state cannot be cleared, except by disabling auto-shutdown, as long as the shutdown input level persists. note: shutdown bit cannot be cleared in software if the auto-shutdown condition is still present. note: shutdown bit cannot be cleared in software if the auto-shutdown condition is still present. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 210 preliminary ? 2016 microchip technology inc. 19.9 operation during sleep the cwgx module will operate during sleep, provided that the input sources remain active. if the hfintosc is selected as the module clock source, dead-band generation will remain active. this will have a direct effect on the sleep mode current. 19.10 configuring the cwg 1. ensure that the tris control bits corresponding to cwg outputs are set so that all are configured as inputs, ensuring that the outputs are inactive during setup. external hardware may ensure that pin levels are held to safe levels. 2. clear the en bit, if not already cleared. 3. configure the mode<2:0> bits of the cwgxcon0 register to set the output operating mode. 4. configure the poly bits of the cwgxcon1 register to set the output polarities. 5. configure the dat<3:0> bits of the cwgxdat register to select the data input source. 6. if a steering mode is selected, configure the stry bits to select the desired output on the cwg outputs. 7. configure the lsbd<1:0> and lsac<1:0> bits of the cwgxas0 register to select the auto- shutdown output override states (this is necessary even if not using auto-shutdown because start-up will be from a shutdown state). 8. if auto-restart is desired, set the ren bit of cwgxas0. 9. if auto-shutdown is desired, configure the asxe bits of the cwgxas1 register to select the shutdown source. 10. set the desired rising and falling dead-band times with the cwgxdbr and cwgxdbf registers. 11. select the clock source in the cwgxclkcon register. 12. set the en bit to enable the module. 13. clear the tris bits that correspond to the cwg outputs to set them as outputs. 14. if auto-restart is to be used, set the ren bit and the shutdown bit will be cleared automatically. otherwise, clear the shutdown bit in software to start the cwg. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 211 pic16(l)f18326/18346 19.11 register definitions: cwg control register 19-1: cwgxcon0: cw gx control register 0 r/w-0/0 r/w/hc-0/0 u-0 u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 en ld (1) m o d e < 2 : 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 hs/hc = bit is set/cleared by hardware bit 7 en: cwgx enable bit 1 = cwgx is enabled 0 = cwgx is disabled bit 6 ld: cwg load buffers bit (1) 1 = dead-band count buffers to be loaded on cwg data rising edge following first falling edge after this bit is set. 0 = buffers remain unchanged bit 5-3 unimplemented : read as 0 bit 2-0 mode<2:0> : cwgx mode bits 111 = reserved 110 = reserved 101 = cwg outputs operate in push-pull mode 100 = cwg outputs operate in half-bridge mode 011 = cwg outputs operate in reverse full-bridge mode 010 = cwg outputs operate in forward full-bridge mode 001 = cwg outputs operate in synchronous steering mode 000 = cwg outputs operate in asynchronous steering mode note 1: this bit can only be set after en = 1 ; it cannot be set in the same cycle when en is set. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 212 preliminary ? 2016 microchip technology inc. register 19-2: cwgxcon1: cw gx control register 1 u-0 u-0 r-x u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 in pold polc polb pola 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 q = value depends on condition bit 7-6 unimplemented: read as 0 bit 5 in: cwgx data input signal (read-only) bit 4 unimplemented: read as 0 bit 3 pold: wgxd output polarity bit 1 = signal output is inverted polarity 0 = signal output is normal polarity bit 2 polc: wgxc output polarity bit 1 = signal output is inverted polarity 0 = signal output is normal polarity bit 1 polb: wgxb output polarity bit 1 = signal output is inverted polarity 0 = signal output is normal polarity bit 0 pola: wgxa output polarity bit 1 = signal output is inverted polarity 0 = signal output is normal polarity register 19-3: cwgxclkcon: cwgx clock input selection register u-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-0/0 cs 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 q = value depends on condition bit 7-1 unimplemented: read as 0 bit 0 cs: cwg clock source selection select bits wgclk clock source 0 f osc 1 hfintosc (remains operating during sleep) downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 213 pic16(l)f18326/18346 register 19-4: cwgxdat: cwgx data input selection register u-0 u-0 u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 dat<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 resets 1 = bit is set 0 = bit is cleared q = value depends on condition bit 7-4 unimplemented: read as 0 bit 3-0 dat<3:0>: cwg data input selection bits wgdat data source 0000 cwgxpps 0001 c1out 0010 c2out 0011 ccp1 0100 ccp2 0101 ccp3 0110 ccp4 0111 pwm5 1000 pwm6 1001 nco1 1010 clc1 1011 clc2 1100 clc3 1101 clc4 1110 reserved 1111 reserved downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 214 preliminary ? 2016 microchip technology inc. register 19-5: cwgxstr (1) : cwg steering control register r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 ovrd ovrc ovrb ovra strd (2) strc (2) strb (2) stra (2) 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 q = value depends on condition bit 7 ovrd: steering data d bit bit 6 ovrc: steering data c bit bit 5 ovrb: steering data b bit bit 4 ovra: steering data a bit bit 3 strd: steering enable bit d (2) 1 = cwgxd output has the cwgx data input waveform with polarity control from pold bit 0 = cwgxd output is assigned to value of ovrd bit bit 2 strc: steering enable bit c (2) 1 = cwgxc output has the cwgx data input waveform with polarity control from polc bit 0 = cwgxc output is assigned to value of ovrc bit bit 1 strb: steering enable bit b (2) 1 = cwgxb output has the cwgx data input waveform with polarity control from polb bit 0 = cwgxb output is assigned to value of ovrb bit bit 0 stra: steering enable bit a (2) 1 = cwgxa output has the cwgx data input waveform with polarity control from pola bit 0 = cwgxa output is assigned to value of ovra bit note 1: the bits in this register apply only when mode<2:0> = 00x ( register 19-1 , steering modes). 2: this bit is double-buffered when mode<2:0> = 001 . downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 215 pic16(l)f18326/18346 register 19-6: cwgxas0: cwg auto -shutdown control register 0 r/w/hs/sc-0/0 r/w-0/0 r/w-0/0 r/w-1/1 r/w-0/0 r/w-1/1 u-0 u-0 shutdown ren lsbd<1:0> lsac<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 q = value depends on condition bit 7 shutdown: auto-shutdown event status bit (1,2) 1 = an auto-shutdown state is in effect 0 = no auto-shutdown event has occurred bit 6 ren: auto-restart enable bit 1 = auto-restart is enabled 0 = auto-restart is disabled bit 5-4 lsbd<1:0>: cwgxb and cwgxd auto-shutdown state control bits 11 =a logic 1 is placed on cwgxb/d when an auto-shutdown event occurs. 10 =a logic 0 is placed on cwgxb/d when an auto-shutdown event occurs. 01 = pin is tri-stated on cwgxb/d when an auto-shutdown event occurs. 00 = the inactive state of the pin, including polarity, is placed on cwgxb/d after the required dead-band interval when an auto-shutdown event occurs. bit 3-2 lsac<1:0>: cwgxa and cwgxc auto-shutdown state control bits 11 =a logic 1 is placed on cwgxa/c when an auto-shutdown event occurs. 10 =a logic 0 is placed on cwgxa/c when an auto-shutdown event occurs. 01 = pin is tri-stated on cwg1a/c when an auto-shutdown event occurs. 00 = the inactive state of the pin, including polarity, is placed on cwgxa/c after the required dead-band interval when an auto-shutdown event occurs. bit 1-0 unimplemented: read as 0 note 1: this bit may be written while en = 0 ( register 19-1 ), to place the outputs into the shutdown configuration. 2: the outputs will remain in auto-shutdown state until the next rising edge of the cwg data input after th is bit is cleared. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 216 preliminary ? 2016 microchip technology inc. register 19-7: cwgxas1: cwg auto -shutdown control register 1 u-0 u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 as4e as3e as2e as1e as0e 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 q = value depends on condition bit 7-5 unimplemented: read as 0 bit 4 as4e: cwg auto-shutdown source 4 (clc4) enable bit 1 = auto-shutdown for clc4 is enabled 0 = auto-shutdown for clc4 is disabled bit 3 as3e: cwg auto-shutdown source 3 (clc2) enable bit 1 = auto-shutdown from clc2 is enabled 0 = auto-shutdown from clc2 is disabled bit 2 as2e: cwg auto-shutdown source 2 (c2) enable bit 1 = auto-shutdown from comparator 2 is enabled 0 = auto-shutdown from comparator 2 is disabled bit 1 as1e: cwg auto-shutdown source 1 (c1) enable bit 1 = auto-shutdown from comparator 1 is enabled 0 = auto-shutdown from comparator 1 is disabled bit 0 as0e: cwg auto-shutdown source 0 (cwgxpps) enable bit 1 = auto-shutdown from cwgxpps is enabled 0 = auto-shutdown from cwgxpps is disabled register 19-8: cwgxdbr: cwgx rising dead-band count register u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 d b r < 5 : 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 q = value depends on condition bit 7-6 unimplemented: read as 0 bit 5-0 dbr<5:0>: cwg rising edge triggered dead-band count bits 11 1111 = 63-64 cwg clock periods 11 1110 = 62-63 cwg clock periods .. . 00 0010 = 2-3 cwg clock periods 00 0001 = 1-2 cwg clock periods 00 0000 = 0 cwg clock periods. dead-band generation is bypassed. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 217 pic16(l)f18326/18346 register 19-9: cwgxdbf: cwgx falling dead-band count register u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 d b f < 5 : 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 q = value depends on condition bit 7-6 unimplemented: read as 0 bit 5-0 dbf<5:0>: cwg falling edge triggered dead-band count bits 11 1111 = 63-64 cwg clock periods 11 1110 = 62-63 cwg clock periods .. . 00 0010 = 2-3 cwg clock periods 00 0001 = 1-2 cwg clock periods 00 0000 = 0 cwg clock periods. dead-band generation is bypassed. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 218 preliminary ? 2016 microchip technology inc. table 19-2: summary of registers associated with cwgx name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page trisa D D trisa5 trisa4 D (2) trisa2 trisa1 trisa0 141 ansela D D ansa5 ansa4 D ansa2 ansa1 ansa0 142 trisb (1) trisb7 trisb6 trisb5 trisb4 D D D D 147 anselb (1) ansb7 ansb6 ansb5 ansb4 D D D D 148 trisc trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 153 anselc ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 155 pir4 cwg2if cwg1if tmr5gif tmr5if ccp4if ccp3if ccp2if ccp1if 108 pie4 cwg2ie cwg1ie tmr5gie tmr5ie ccp4ie ccp3ie ccp2ie ccp1ie 103 cwg1con0 en ld D D D mode<2:0> 211 cwg1con1 D D in D pold polc polb pola 212 cwg1clkcon D D D D D D D cs 212 cwg1dat D D D D dat<3:0> 213 cwg1str ovrd ovrc ovrb ovra strd strc strb stra 214 cwg1as0 shutdown ren lsbd<1:0> lsac<1:0> D D 215 cwg1as1 D D D as4e as3e as2e as1e as0e 216 cwg1dbr D D dbr<5:0> 216 cwg1dbf D D dbf<5:0> 217 cwg1pps D D D cwg1pps<4:0> 160 cwg2con0 en ld D D D mode<2:0> 211 cwg2con1 D D in D pold polc polb pola 212 cwg2clkcon D D D D D D D cs 212 cwg2dat D D D D dat<3:0> 213 cwg2str ovrd ovrc ovrb ovra strd strc strb stra 214 cwg2as0 shutdown ren lsbd<1:0> lsac<1:0> D D 215 cwg2as1 D D D as4e as3e as2e as1e as0e 216 cwg2dbr D D dbr<5:0> 216 cwg2dbf D D dbf<5:0> 217 cwg2pps D D D cwg2pps<4:0> 160 rxypps D D D rxypps<4:0> 161 note 1: pic16(l)f18346 only. 2: unimplemented, read as 0 . downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 219 pic16(l)f18326/18346 20.0 configurable logic cell (clc) the configurable logic cell (clcx) provides programmable logic that operates outside the speed limitations of software execution. the logic cell takes up to 32 input signals and, through the use of configurable gates, reduces the 32 inputs to four logic lines that drive one of eight selectable single-output logic functions. input sources are a combination of the following: i/o pins internal clocks peripherals register bits the output can be directed internally to peripherals and to an output pin. refer to figure 20-1 for a simplified diagram showing signal flow through the clcx. possible configurations include: combinatorial logic -and -nand - and-or - and-or-invert -or-xor -or-xnor latches -s-r - clocked d with set and reset - transparent d with set and reset - clocked j-k with reset figure 20-1: clcx simplifi ed block diagram note 1: see figure 20-2 . 2: see figure 20-3 . input data selection gates (1) logic function (2) lcxg2 lcxg1 lcxg3 lcxg4 lcxmode<2:0> lcxq lcxen lcxpol det interrupt det interrupt set bit clcxif lcxintn lcxintp clcx to peripherals q1 lcx_out lcxout mlcxout dq pps module lcx_in[0] lcx_in[1] lcx_in[2] lcx_in[29] lcx_in[30] lcx_in[35] .. . rev. 10-000025c 3/6/2014 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 220 preliminary ? 2016 microchip technology inc. 20.1 clcx setup programming the clcx module is performed by configuring the four stages in the logic signal flow. the four stages are: data selection data gating logic function selection output polarity each stage is setup at run time by writing to the corresponding clcx special function registers. this has the added advantage of permitting logic reconfiguration on-the-fly during program execution. 20.1.1 data selection there are 36 signals available as inputs to the configurable logic. four 36-input multiplexers are used to select the inputs to pass on to the next stage. data selection is through four multiplexers as indicated on the left side of figure 20-2 . data inputs in the figure are identified by a generic numbered input name. table 20-1 correlates the generic input name to the actual signal for each clc module. the column labeled lcxdys<5:0> value indicates the mux selection code for the selected data input. lcxdys is an abbreviation for the mux select input codes: lcxd1s<5:0> through lcxd4s<5:0>. data inputs are selected with clcxsel0 through clcxsel3 registers ( register 20-3 through register 20-6 ). table 20-1: clcx data input selection note: data selections are undefined at power-up. lcxdys<5:0> value clcx input source 100011 [35] tmr6/pr6 match 100010 [34] tmr5 overflow 100001 [33] tmr4/pr4 match 100000 [32] tmr3 overflow 11111 [31] f osc 11110 [30] hfintosc 11101 [29] lfintosc 11100 [28] adcrc 11011 [27] iocif int flag bit 11010 [26] tmr2/pr2 match 11001 [25] tmr1 overflow 11000 [24] tmr0 overflow 10111 [23] eusart1 (dt) output 10110 [22] eusart1 (tx/ck) output 10101 [21] sda2 10100 [20] scl2 10011 [19] sda1 10010 [18] scl1 10001 [17] pwm6 output 10000 [16] pwm5 output 01111 [15] ccp4 output 01110 [14] ccp3 output 01101 [13] ccp2 output 01100 [12] ccp1 output 01011 [11] clkr output 01010 [10] dsm output 01001 [9] c2 output 01000 [8] c1 output 00111 [7] clc4 output 00110 [6] clc3 output 00101 [5] clc2 output 00100 [4] clc1 output 00011 [3] clcin3pps 00010 [2] clcin2pps 00001 [1] clcin1pps 00000 [0] clcin0pps downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 221 pic16(l)f18326/18346 20.1.2 data gating outputs from the input multiplexers are directed to the desired logic function input through the data gating stage. each data gate can direct any combination of the four selected inputs. the gate stage is more than just signal direction. the gate can be configured to direct each input signal as inverted or non-inverted data. directed signals are anded together in each gate. the output of each gate can be inverted before going on to the logic function stage. the gating is in essence a 1-to-4 input and/nand/or/nor gate. when every input is inverted and the output is inverted, the gate is an or of all enabled data inputs. when the inputs and output are not inverted, the gate is an and or all enabled inputs. table 20-2 summarizes the basic logic that can be obtained in gate 1 by using the gate logic select bits. the table shows the logic of four input variables, but each gate can be configured to use less than four. if no inputs are selected, the output will be zero or one, depending on the gate output polarity bit. it is possible (but not recommended) to select both the true and negated values of an input. when this is done, the gate output is zero, regardless of the other inputs, but may emit logic glitches (transient-induced pulses). if the output of the channel must be zero or one, the recommended method is to set all gate bits to zero and use the gate polarity bit to set the desired level. data gating is configured with the logic gate select registers as follows: gate 1: clcxgls0 ( register 20-7 ) gate 2: clcxgls1 ( register 20-8 ) gate 3: clcxgls2 ( register 20-9 ) gate 4: clcxgls3 ( register 20-10 ) register number suffixes are different than the gate numbers because other variations of this module have multiple gate selections in the same register. data gating is indicated in the right side of figure 20-2 . only one gate is shown in detail. the remaining three gates are configured identically with the exception that the data enables correspond to the enables for that gate. 20.1.3 logic function there are eight available logic functions, including: and-or or-xor and s-r latch d flip-flop with set and reset d flip-flop with reset j-k flip-flop with reset transparent latch with set and reset logic functions are shown in figure 20-3 . each logic function has four inputs and one output. the four inputs are the four data gate outputs of the previous stage. the output is fed to the inversion stage and from there to other peripherals, an output pin, and back to the clcx itself. 20.1.4 output polarity the last stage in the configurable logic cell is the output polarity. setting the lcxpol bit of the clcxpol register inverts the output signal from the logic stage. changing the polarity while the interrupts are enabled will cause an interrupt for the resulting output transition. note: data gating is undefined at power-up. table 20-2: data gating logic clcxglsy lcxgypol gate logic 0x55 1 and 0x55 0 nand 0xaa 1 nor 0xaa 0 or 0x00 0 logic 0 0x00 1 logic 1 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 222 preliminary ? 2016 microchip technology inc. 20.2 clcx interrupts an interrupt will be generated upon a change in the output value of the clcx when the appropriate interrupt enables are set. a rising edge detector and a falling edge detector are present in each clc for this purpose. the clcxif bit of the associated pir3 register will be set when either edge detector is triggered and its associated enable bit is set. the lcxintp bit enables rising edge interrupts and the lcxintn bit enables fall- ing edge interrupts. both are located in the clcxcon register. to fully enable the interrupt, set the following bits: clcxie bit of the pie3 register lcxintp bit of the clcxcon register (for a rising edge detection) lcxintn bit of the clcxcon register (for a falling edge detection) peie and gie bits of the intcon register the clcxif bit of the pir3 register, must be cleared in software as part of the interrupt service. if another edge is detected while this flag is being cleared, the flag will still be set at the end of the sequence. 20.3 output mirror copies mirror copies of all lcxcon output bits are contained in the clcdata register. reading this register reads the outputs of all clcs simultaneously. this prevents any reading skew introduced by testing or reading the lcxout bits in the individual clcxcon registers. 20.4 effects of a reset the clcxcon register is cleared to zero as the result of a reset. all other selection and gating values remain unchanged. 20.5 operation during sleep the clc module operates independently from the system clock and will continue to run during sleep, provided that the input sources selected remain active. the hfintosc remains active during sleep when the clc module is enabled and the hfintosc is selected as an input source, regardless of the system clock source selected. in other words, if the hfintosc is simultaneously selected as the system clock and as a clc input source, when the clc is enabled, the cpu will go idle during sleep, but the clc will continue to operate and the hfintosc will remain active. this will have a direct effect on the sleep mode current. 20.6 clcx setup steps the following steps will be followed when setting up the clcx: disable clcx by clearing the lcxen bit. select desired inputs using clcxsel0 through clcxsel3 registers (see table 20-1 ). clear any associated ansel bits. set all tris bits associated with inputs. clear all tris bits associated with outputs. enable the chosen inputs through the four gates using clcxgls0, clcxgls1, clcxgls2, and clcxgls3 registers. select the gate output polarities with the lcxgypol bits of the clcxpol register. select the desired logic function with the lcxmode<2:0> bits of the clcxcon register. select the desired polarity of the logic output with the lcxpol bit of the clcxpol register. (this step may be combined with the previous gate out- put polarity step). if driving a device pin, set the desired pin pps control register and also clear the tris bit corresponding to that output. if interrupts are desired, configure the following bits: - set the lcxintp bit in the clcxcon register for rising event. - set the lcxintn bit in the clcxcon register for falling event. - set the clcxie bit of the pie3 register. - set the gie and peie bits of the intcon register. enable the clcx by setting the lcxen bit of the clcxcon register. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 223 pic16(l)f18326/18346 figure 20-2: input data selection and gating lcxg1 lcxg1pol data gate 1 lcxd1g1t lcxg2 lcxg3 lcxg4 data gate 2 data gate 3 data gate 4 lcxd1g1n lcxd2g1t lcxd2g1n lcxd3g1t lcxd3g1n lcxd4g1t lcxd4g1n lcxd1s<5:0> lcxd2s<5:0> lcxd3s<5:0> lcxd4s<5:0> lcx_in[0] lcx_in[35] 000000 100011 data selection note: all controls are undefined at power-up. lcxd1t lcxd1n lcxd2t lcxd2n lcxd3t lcxd3n lcxd4t lcxd4n (same as data gate 1) (same as data gate 1) (same as data gate 1) lcx_in[0] lcx_in[35] 000000 100011 lcx_in[0] lcx_in[35] 000000 100011 lcx_in[0] lcx_in[35] 000000 100011 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 224 preliminary ? 2016 microchip technology inc. figure 20-3: programm able logic functions lcxg1lcxg2 lcxg3 lcxg4 lcxq and-or or-xor lcxmode<2:0> = 000 lcxmode<2:0> = 001 4-input and s-r latch lcxmode<2:0> = 010 lcxmode<2:0> = 011 lcxg1lcxg2 lcxg3 lcxg4 lcxq s r q lcxq lcxg1lcxg2 lcxg3 lcxg4 lcxg1lcxg2 lcxg3 lcxg4 lcxq 1-input d flip-flop with s and r 2-input d flip-flop with r j-k flip-flop with r 1-input transparent latch with s and r lcxmode<2:0> = 100 lcxmode<2:0> = 101 lcxmode<2:0> = 110 lcxmode<2:0> = 111 d r q lcxq lcxg1 lcxg2 lcxg3 lcxg4 d r q s lcxg1 lcxg2lcxg3 lcxg4 lcxq j r q k lcxg1 lcxg2lcxg3 lcxg4 lcxq d r q s le lcxq lcxg1 lcxg2lcxg3 lcxg4 rev. 10-000122a 7/30/2013 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 225 pic16(l)f18326/18346 20.7 register definitions: clc control register 20-1: clcxcon: configurable logic cell control register r/w-0/0 u-0 r-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 lcxen lcxout lcxintp lcxintn lcxmode<2: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 lcxen: configurable logic cell enable bit 1 = configurable logic cell is enabled and mixing input signals 0 = configurable logic cell is disabled and has logic zero output bit 6 unimplemented: read as 0 bit 5 lcxout: configurable logic cell data output bit read-only: logic cell output data, after lcpol; sampled from clcxout. bit 4 lcxintp: configurable logic cell positive edge going interrupt enable bit 1 = clcxif will be set when a rising edge occurs on clcxout 0 = clcxif will not be set bit 3 lcxintn: configurable logic cell negative edge going interrupt enable bit 1 = clcxif will be set when a falling edge occurs on clcxout 0 = clcxif will not be set bit 2-0 lcxmode<2:0>: configurable logic cell functional mode bits 111 = cell is 1-input transparent latch with s and r 110 = cell is j-k flip-flop with r 101 = cell is 2-input d flip-flop with r 100 = cell is 1-input d flip-flop with s and r 011 = cell is s-r latch 010 = cell is 4-input and 001 = cell is or-xor 000 = cell is and-or downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 226 preliminary ? 2016 microchip technology inc. register 20-2: clcxpol: signal polarity control register r/w-0/0 u-0 u-0 u-0 r/w-x/u r/w-x/u r/w-x/u r/w-x/u lcxpol lcxg4pol lcxg3pol lcxg2pol lcxg1pol 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 lcxpol: clcxout output polarity control bit 1 = the output of the logic cell is inverted 0 = the output of the logic cell is not inverted bit 6-4 unimplemented: read as 0 bit 3 lcxg4pol: gate 3 output polarity control bit 1 = the output of gate 3 is inverted when applied to the logic cell 0 = the output of gate 3 is not inverted bit 2 lcxg3pol: gate 2 output polarity control bit 1 = the output of gate 2 is inverted when applied to the logic cell 0 = the output of gate 2 is not inverted bit 1 lcxg2pol: gate 1 output polarity control bit 1 = the output of gate 1 is inverted when applied to the logic cell 0 = the output of gate 1 is not inverted bit 0 lcxg1pol: gate 0 output polarity control bit 1 = the output of gate 0 is inverted when applied to the logic cell 0 = the output of gate 0 is not inverted downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 227 pic16(l)f18326/18346 register 20-3: clcxsel0: generic clcx data 0 select register u-0 u-0 r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u lcxd1s<5: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 unimplemented: read as 0 bit 5-0 lcxd1s<5:0>: clcx data1 input selection bits see table 20-1 . register 20-4: clcxsel1: generic clcx data 1 select register u-0 u-0 r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u lcxd2s<5: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 unimplemented: read as 0 bit 5-0 lcxd2s<5:0>: clcx data 2 input selection bits see table 20-1 . register 20-5: clcxsel2: generic clcx data 2 select register u-0 u-0 r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u lcxd3s<5: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 unimplemented: read as 0 bit 5-0 lcxd3s<5:0>: clcx data 3 input selection bits see table 20-1 . downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 228 preliminary ? 2016 microchip technology inc. register 20-6: clcxsel3: generic clcx data 3 select register u-0 u-0 r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u r/w-x/u lcxd4s<5: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 unimplemented: read as 0 bit 5-0 lcxd4s<5:0>: clcx data 4 input selection bits see table 20-1 . register 20-7: clcxgls0: gate 0 logic select register 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 lcxg1d4t lcxg1d4n lcxg1d3t lcxg1d3n lcxg1d2t lcxg1d2n lcxg1d1t lcxg1d1n 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 lcxg1d4t: gate 0 data 4 true (non-inverted) bit 1 = clcin3 (true) is gated into clcx gate 0 0 = clcin3 (true) is not gated into clcx gate 0 bit 6 lcxg1d4n: gate 0 data 4 negated (inverted) bit 1 = clcin3 (inverted) is gated into clcx gate 0 0 = clcin3 (inverted) is not gated into clcx gate 0 bit 5 lcxg1d3t: gate 0 data 3 true (non-inverted) bit 1 = clcin2 (true) is gated into clcx gate 0 0 = clcin2 (true) is not gated into clcx gate 0 bit 4 lcxg1d3n: gate 0 data 3 negated (inverted) bit 1 = clcin2 (inverted) is gated into clcx gate 0 0 = clcin2 (inverted) is not gated into clcx gate 0 bit 3 lcxg1d2t: gate 0 data 2 true (non-inverted) bit 1 = clcin1 (true) is gated into clcx gate 0 0 = clcin1 (true) is not gated into clcx gate 0 bit 2 lcxg1d2n: gate 0 data 2 negated (inverted) bit 1 = clcin1 (inverted) is gated into clcx gate 0 0 = clcin1 (inverted) is not gated into clcx gate 0 bit 1 lcxg1d1t: gate 0 data 1 true (non-inverted) bit 1 = clcin0 (true) is gated into clcx gate 0 0 = clcin0 (true) is not gated into clcx gate 0 bit 0 lcxg1d1n: gate 0 data 1 negated (inverted) bit 1 = clcin0 (inverted) is gated into clcx gate 0 0 = clcin0 (inverted) is not gated into clcx gate 0 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 229 pic16(l)f18326/18346 register 20-8: clcxgls1: gate 1 logic select register 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 lcxg2d4t lcxg2d4n lcxg2d3t lcxg2d3n lcxg2d2t lcxg2d2n lcxg2d1t lcxg2d1n 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 lcxg2d4t: gate 1 data 4 true (non-inverted) bit 1 = clcin3 (true) is gated into clcx gate 1 0 = clcin3 (true) is not gated into clcx gate 1 bit 6 lcxg2d4n: gate 1 data 4 negated (inverted) bit 1 = clcin3 (inverted) is gated into clcx gate 1 0 = clcin3 (inverted) is not gated into clcx gate 1 bit 5 lcxg2d3t: gate 1 data 3 true (non-inverted) bit 1 = clcin2 (true) is gated into clcx gate 1 0 = clcin2 (true) is not gated into clcx gate 1 bit 4 lcxg2d3n: gate 1 data 3 negated (inverted) bit 1 = clcin2 (inverted) is gated into clcx gate 1 0 = clcin2 (inverted) is not gated into clcx gate 1 bit 3 lcxg2d2t: gate 1 data 2 true (non-inverted) bit 1 = clcin1 (true) is gated into clcx gate 1 0 = clcin1 (true) is not gated into clcx gate 1 bit 2 lcxg2d2n: gate 1 data 2 negated (inverted) bit 1 = clcin1 (inverted) is gated into clcx gate 1 0 = clcin1 (inverted) is not gated into clcx gate 1 bit 1 lcxg2d1t: gate 1 data 1 true (non-inverted) bit 1 = clcin0 (true) is gated into clcx gate 1 0 = clcin0 (true) is not gated into clcx gate 1 bit 0 lcxg2d1n: gate 1 data 1 negated (inverted) bit 1 = clcin0 (inverted) is gated into clcx gate 1 0 = clcin0 (inverted) is not gated into clcx gate 1 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 230 preliminary ? 2016 microchip technology inc. register 20-9: clcxgls2: gate 2 logic select register 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 lcxg3d4t lcxg3d4n lcxg3d3t lcxg3d3n lcxg3d2t lcxg3d2n lcxg3d1t lcxg3d1n 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 lcxg3d4t: gate 2 data 4 true (non-inverted) bit 1 = clcin3 (true) is gated into clcx gate 2 0 = clcin3 (true) is not gated into clcx gate 2 bit 6 lcxg3d4n: gate 2 data 4 negated (inverted) bit 1 = clcin3 (inverted) is gated into clcx gate 2 0 = clcin3 (inverted) is not gated into clcx gate 2 bit 5 lcxg3d3t: gate 2 data 3 true (non-inverted) bit 1 = clcin2 (true) is gated into clcx gate 2 0 = clcin2 (true) is not gated into clcx gate 2 bit 4 lcxg3d3n: gate 2 data 3 negated (inverted) bit 1 = clcin2 (inverted) is gated into clcx gate 2 0 = clcin2 (inverted) is not gated into clcx gate 2 bit 3 lcxg3d2t: gate 2 data 2 true (non-inverted) bit 1 = clcin1 (true) is gated into clcx gate 2 0 = clcin1 (true) is not gated into clcx gate 2 bit 2 lcxg3d2n: gate 2 data 2 negated (inverted) bit 1 = clcin1 (inverted) is gated into clcx gate 2 0 = clcin1 (inverted) is not gated into clcx gate 2 bit 1 lcxg3d1t: gate 2 data 1 true (non-inverted) bit 1 = clcin0 (true) is gated into clcx gate 2 0 = clcin0 (true) is not gated into clcx gate 2 bit 0 lcxg3d1n: gate 2 data 1 negated (inverted) bit 1 = clcin0 (inverted) is gated into clcx gate 2 0 = clcin0 (inverted) is not gated into clcx gate 2 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 231 pic16(l)f18326/18346 register 20-10: clcxgls3: gate 3 logic select register 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 lcxg4d4t lcxg4d4n lcxg4d3t lcxg4d3n lcxg4d2t lcxg4d2n lcxg4d1t lcxg4d1n 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 lcxg4d4t: gate 3 data 4 true (non-inverted) bit 1 = clcin3 (true) is gated into clcx gate 3 0 = clcin3 (true) is not gated into clcx gate 3 bit 6 lcxg4d4n: gate 3 data 4 negated (inverted) bit 1 = clcin3 (inverted) is gated into clcx gate 3 0 = clcin3 (inverted) is not gated into clcx gate 3 bit 5 lcxg4d3t: gate 3 data 3 true (non-inverted) bit 1 = clcin2 (true) is gated into clcx gate 3 0 = clcin2 (true) is not gated into clcx gate 3 bit 4 lcxg4d3n: gate 3 data 3 negated (inverted) bit 1 = clcin2 (inverted) is gated into clcx gate 3 0 = clcin2 (inverted) is not gated into clcx gate 3 bit 3 lcxg4d2t: gate 3 data 2 true (non-inverted) bit 1 = clcin1 (true) is gated into clcx gate 3 0 = clcin1 (true) is not gated into clcx gate 3 bit 2 lcxg4d2n: gate 3 data 2 negated (inverted) bit 1 = clcin1 (inverted) is gated into clcx gate 3 0 = clcin1 (inverted) is not gated into clcx gate 3 bit 1 lcxg4d1t: gate 3 data 1 true (non-inverted) bit 1 = clcin0 (true) is gated into clcx gate 3 0 = clcin0 (true) is not gated into clcx gate 3 bit 0 lcxg4d1n: gate 3 data 1 negated (inverted) bit 1 = clcin0 (inverted) is gated into clcx gate 3 0 = clcin0 (inverted) is not gated into clcx gate 3 downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 232 preliminary ? 2016 microchip technology inc. register 20-11: clcdata: clc data output u-0 u-0 u-0 u-0 r-0 r-0 r-0 r-0 mlc4out mlc3out mlc2out mlc1out 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 mlc4out: mirror copy of lc4out bit bit 2 mlc3out: mirror copy of lc3out bit bit 1 mlc2out: mirror copy of lc2out bit bit 0 mlc1out: mirror copy of lc1out bit table 20-3: summary of registers associated with clcx name bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 register on page ansela D D ansa5 ansa4 D ansa2 ansa1 ansa0 142 trisa D D trisa5 trisa4 D (2) trisa2 trisa1 trisa0 141 anselb (1) ansb7 ansb6 ansb5 ansb4 D D D D 148 trisb (1) trisb7 trisb6 trisb5 trisb4 D D D D 147 anselc ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 155 trisc trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 153 intcon gie peie D D D D D intedg 98 pir3 osfif cswif tmr3gif tmr3if clc4if clc3if clc2if clc1if 107 pie3 osfie cswie tmr3gie tmr3ie clc4ie clc3ie clc2ie clc1ie 102 clc1con lc1en D lc1out lc1intp lc1intn lc1mode<2:0> 225 clc1pol lc1pol D D D lc1g4pol lc1g3pol lc1g2pol lc1g1pol 226 clc1sel0 D D lc1d1s<5:0> 227 clc1sel1 D D lc1d2s<5:0> 227 clc1sel2 D D lc1d3s<5:0> 227 clc1sel3 D D lc1d4s<5:0> 228 clc1gls0 lc1g1d4t lc1g1d4n lc1g1d3t lc1g1d3n lc1g1d2t lc1g1d2n lc1g1d1t lc1g1d1n 228 clc1gls1 lc1g2d4t lc1g2d4n lc1g2d3t lc1g2d3n lc1g2d2t lc1g2d2n lc1g2d1t lc1g2d1n 229 clc1gls2 lc1g3d4t lc1g3d4n lc1g3d3t lc1g3d3n lc1g3d2t lc1g3d2n lc1g3d1t lc1g3d1n 230 clc1gls3 lc1g4d4t lc1g4d4n lc1g4d3t lc1g4d3n lc1g4d2t lc1g4d2n lc1g4d1t lc1g4d1n 231 clc2con lc2en D lc2out lc2intp lc2intn lc2mode<2:0> 225 clc2pol lc2pol D D D lc2g4pol lc2g3pol lc2g2pol lc2g1pol 226 clc2sel0 D D lc2d1s<5:0> 227 clc2sel1 D D lc2d2s<5:0> 227 clc2sel2 D D lc2d3s<5:0> 227 clc2sel3 D D lc2d4s<5:0> 228 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 233 pic16(l)f18326/18346 clc2gls0 lc2g1d4t lc2g1d4n lc2g1d3t lc2g1d3n lc2g1d2t lc2g1d2n lc2g1d1t lc2g1d1n 228 clc2gls1 lc2g2d4t lc2g2d4n lc2g2d3t lc2g2d3n lc2g2d2t lc2g2d2n lc2g2d1t lc2g2d1n 229 clc2gls2 lc2g3d4t lc2g3d4n lc2g3d3t lc2g3d3n lc2g3d2t lc2g3d2n lc2g3d1t lc2g3d1n 230 clc2gls3 lc2g4d4t lc2g4d4n lc2g4d3t lc2g4d3n lc2g4d2t lc2g4d2n lc2g4d1t lc2g4d1n 231 clc3con lc3en D lc3out lc3intp lc3intn lc3mode<2:0> 225 clc3pol lc3pol D D D lc3g4pol lc3g3pol lc3g2pol lc3g1pol 226 clc3sel0 D D lc3d1s<5:0> 227 clc3sel1 D D lc3d2s<5:0> 227 clc3sel2 D D lc3d3s<5:0> 227 clc3sel3 D D lc3d4s<5:0> 228 clc3gls0 lc3g1d4t lc3g1d4n lc3g1d3t lc3g1d3n lc3g1d2t lc3g1d2n lc3g1d1t lc3g1d1n 228 clc3gls1 lc3g2d4t lc3g2d4n lc3g2d3t lc3g2d3n lc3g2d2t lc3g2d2n lc3g2d1t lc3g2d1n 229 clc3gls2 lc3g3d4t lc3g3d4n lc3g3d3t lc3g3d3n lc3g3d2t lc3g3d2n lc3g3d1t lc3g3d1n 230 clc3gls3 lc3g4d4t lc3g4d4n lc3g4d3t lc3g4d3n lc3g4d2t lc3g4d2n lc3g4d1t lc3g4d1n 231 clc4con lc4en D lc4out lc4intp lc4intn lc4mode<2:0> 225 clc4pol lc4pol D D D lc4g4pol lc4g3pol lc4g2pol lc4g1pol 226 clc4sel0 D D lc4d1s<5:0> 227 clc4sel1 D D lc4d2s<5:0> 227 clc4sel2 D D lc4d3s<5:0> 227 clc4sel3 D D lc4d4s<5:0> 228 clc4gls0 lc4g1d4t lc4g1d4n lc4g1d3t lc4g1d3n lc4g1d2t lc4g1d2n lc4g1d1t lc4g1d1n 228 clc4gls1 lc4g2d4t lc4g2d4n lc4g2d3t lc4g2d3n lc4g2d2t lc4g2d2n lc4g2d1t lc4g2d1n 229 clc4gls2 lc4g3d4t lc4g3d4n lc4g3d3t lc4g3d3n lc4g3d2t lc4g3d2n lc4g3d1t lc4g3d1n 230 clc4gls3 lc4g4d4t lc4g4d4n lc4g4d3t lc4g4d3n lc4g4d2t lc4g4d2n lc4g4d1t lc4g4d1n 231 clcdata D D D D mlc4out mlc3out mlc2out mlc1out 232 clcin0pps D D D clcin0pps<4:0> 160 clcin1pps D D D clcin1pps<4:0> 160 clcin2pps D D D clcin2pps<4:0> 160 clcin3pps D D D clcin3pps<4:0> 160 clc1outpps D D D clc1outpps<4:0> 160 clc2outpps D D D clc2outpps<4:0> 160 clc3outpps D D D clc3outpps<4:0> 160 clc4outpps D D D clc4outpps<4:0> 160 table 20-3: summary of registers as sociated with clcx (continued) name bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 register on page downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 234 preliminary ? 2016 microchip technology inc. 21.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 (adresh:adresl register pair). figure 21-1 shows the block diagram of the adc. the adc voltage reference is software selectable to be either internally generated or externally supplied. the adc can generate an interrupt upon completion of a conversion. this interrupt can be used to wake-up the device from sleep. figure 21-1: adc block diagram v rpos v rneg enable dacx_output fvr_buffer1 temp indicator chs<4:0> external channel inputs go/done complete start adc sample circuit write to bit go/done v ss v dd v ref + pin v dd adpref 10-bit result adresh adresl 16 adfm 10 internal channel inputs .. . an0ana anz set bit adif v ss adon sampled input q1 q2 q4 fosc divider f osc f osc /n f rc adc clock select adc_clk adcs<2:0> f rc adc clock source trigger select trigger sources ... trigsel<3:0> auto conversion trigger positive reference select rev. 10-000033a 7/30/2013 downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 235 pic16(l)f18326/18346 21.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 result formatting 21.1.1 port configuration the adc can be used to convert both analog and digital signals. when converting analog signals, the i/o pin will be configured for analog by setting the associated tris and ansel bits. refer to section 11.0 ?i/o ports? for more information. 21.1.2 channel selection there are several channel selections available: five porta pins (ra0-ra2, ra4-ra5) four portb pins (rb4-rb7, pic16(l)f18346 only) six portc pins (rc0-rc5, pic16(l)f18326) eight portc pins (rc0-rc7, pic16(l)f18346 only) temperature indicator dac output fixed voltage reference (fvr) av ss (ground) the chs<5:0> bits of the adcon0 register ( register 21-1 ) 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 21.2 ?adc operation? for more information. 21.1.3 adc voltage reference the adpref<1:0> bits of the adcon1 register provides control of the positive voltage reference. the positive voltage reference can be: v ref + pin v dd fvr 2.048v fvr 4.096v (not available on lf devices) the adnref bit of the adcon1 register provides control of the negative voltage reference. the negative voltage reference can be: v ref - pin v ss see section 21.0 ?analog-to-digital converter (adc) module? for more details on the fixed voltage reference. 21.1.4 conversion clock the source of the conversion clock is software selectable via the adcs<2:0> bits of the adcon1 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 adcrc (dedicated rc oscillator) the time to complete one bit conversion is defined as t ad . one full 10-bit conversion requires 11.5 t ad periods as shown in figure 21-2 . for correct conversion, the appropriate t ad specification must be met. refer to table 34-13 for more information. table 21-1 gives examples of appropriate adc clock selections. note: analog voltages on any pin that is defined as a digital input may cause the input buffer to conduct excess current. note: unless using the adcrc, any changes in the system clock frequency will change the adc clock frequency, which may adversely affect the adc result. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 236 preliminary ? 2016 microchip technology inc. figure 21-2: analog-to-dig ital conversion t ad cycles table 21-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> 32 mhz 20 mhz 16 mhz 8 mhz 4 mhz 1 mhz f osc /2 000 62.5ns (2) 100 ns (2) 125 ns (2) 250 ns (2) 500 ns (2) 2.0 ? s f osc /4 100 125 ns (2) 200 ns (2) 250 ns (2) 500 ns (2) 1.0 ? s4 . 0 ? s f osc /8 001 0.5 ? s (2) 400 ns (2) 0.5 ? s (2) 1.0 ? s2 . 0 ? s 8.0 ? s (3) f osc /16 101 800 ns 800 ns 1.0 ? s2 . 0 ? s4 . 0 ? s 16.0 ? s (3) f osc /32 010 1.0 ? s1 . 6 ? s2 . 0 ? s4 . 0 ? s 8.0 ? s (3) 32.0 ? s (2) f osc /64 110 2.0 ? s3 . 2 ? s4 . 0 ? s 8.0 ? s (3) 16.0 ? s (2) 64.0 ? s (2) adcrc x11 1.0-6.0 ? s (1,4) 1.0-6.0 ? s (1,4) 1.0-6.0 ? s (1,4) 1.0-6.0 ? s (1,4) 1.0-6.0 ? s (1,4) 1.0-6.0 ? s (1,4) legend: shaded cells are outside of recommended range. note 1: see t ad parameter for adcrc source typical t ad value. 2: these values violate the required t ad time. 3: outside the recommended t ad time. 4: the adc clock period (t ad ) and total adc conversion time can be minimized when the adc clock is de rived from the system clock f osc . however, the adcrc oscillator source must be used when conversions are t o be performed with the device in sleep mode. 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 9 t ad 10 t ad 11 set go bit conversion starts holding capacitor disconnected from analog input (thcd). on the following cycle: adresh:adresl is loaded, go bit is cleared, adif bit is set, holding capacitor is reconnected to analog input. b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 enable adc (adon bit) and select channel (acs bits) t hcd t acq downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 237 pic16(l)f18326/18346 21.1.5 interrupts the adc module allows for the ability to generate an interrupt upon completion of an analog-to-digital conversion. the adc interrupt flag is the adif bit in the pir1 register. the adc interrupt enable is the adie bit in the pie1 register. the adif bit must be cleared in 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 adie bit of the pie1 register and the peie bit of the intcon register must both be set and the gie bit of the intcon register must be cleared. if all three of these bits are set, the execution will switch to the interrupt service routine (isr). 21.1.6 result formatting the 10-bit adc conversion result can be supplied in two formats, left justified or right justified. the adfm bit of the adcon1 register controls the output format. figure 21-3 shows the two output formats. note 1: the adif bit is set at the completion of every conversion, regardless of whether or not the adc interrupt is enabled. 2: the adc operates during sleep only when the adcrc oscillator is selected. figure 21-3: 10-bit adc conversion result format adresh adresl (adfm = 0 )m s b l s b bit 7 bit 0 bit 7 bit 0 10-bit adc result unimplemented: read as 0 (adfm = 1 ) msb lsb bit 7 bit 0 bit 7 bit 0 unimplemented: read as 0 10-bit adc result downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 238 preliminary ? 2016 microchip technology inc. 21.2 adc operation 21.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 start the analog-to-digital conversion. 21.2.2 completion of a conversion when the conversion is complete, the adc module will: clear the go/done bit set the adif interrupt flag bit update the adresh and adresl registers with new conversion result 21.2.3 terminating a conversion if a conversion must be terminated before completion, the go/done bit can be cleared in software. the adresh and adresl registers will be updated with the partially complete analog-to-digital conversion sample. incomplete bits will match the last bit converted. 21.2.4 adc operation during sleep the adc module can operate during sleep. this requires the adc clock source to be set to the adcrc option. when the adcrc oscillator 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 adcrc, a sleep instruction causes the present conversion to be aborted and the adc module is turned off, although the adon bit remains set. 21.2.5 auto-conversion trigger the auto-conversion trigger allows periodic adc measurements without software intervention. when a rising edge of the selected source occurs, the go/done bit is set by hardware. the auto-conversion trigger source is selected with the adact<3:0> bits of the adact register. using the auto-conversion trigger does not assure proper adc timing. it is the users responsibility to ensure that the adc timing requirements are met. see table 21-2 for auto-conversion sources. note: the go/done bit will not be set in the same instruction that turns on the adc. refer to section 21.2.6 ?adc conver- sion procedure? . note: a device reset forces all registers to their reset state. thus, the adc module is turned off and any pending conversion is terminated. table 21-2: adc auto-conversion table source peripheral description tmr0 timer0 overflow condition tmr1 timer1 overflow condition tmr3 timer3 overflow condition tmr5 timer5 overflow condition tmr2 match between timer2 and pr2 tmr4 match between timer4 and pr4 tmr6 match between timer6 and pr6 c1 comparator c1 output c2 comparator c2 output clc1 clc1 output clc2 clc2 output clc3 clc3 output clc4 clc4 output ccp1 ccp1 output ccp2 ccp2 output ccp3 ccp3 output ccp4 ccp4 output downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 239 pic16(l)f18326/18346 21.2.6 adc 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 (refer to the tris register) configure pin as analog (refer to the ansel register) 2. configure the adc module: select adc conversion clock configure voltage reference select adc input channel 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 21-1: adc 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: refer to section 21.3 ?adc acquisi- tion requirements? . ;this code block configures the adc ;for polling, vdd and vss references, adcrc ;oscillator and an0 input. ; ;conversion start & polling for completion ; are included. ; banksel adcon1 ; movlw b11110000 ;right justify, adcrc ;oscillator movwf adcon1 ;vdd and vss vref banksel trisa ; bsf trisa,0 ;set ra0 to input banksel ansel ; bsf ansel,0 ;set ra0 to analog banksel adcon0 ; movlw b00000001 ;select channel an0 movwf adcon0 ;turn adc on call sampletime ;acquisiton delay bsf adcon0,adgo ;start conversion btfsc adcon0,adgo ;is conversion done? goto $-1 ;no, test again banksel adresh ; movf adresh,w ;read upper 2 bits movwf resulthi ;store in gpr space banksel adresl ; movf adresl,w ;read lower 8 bits movwf resultlo ;store in gpr space downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 240 preliminary ? 2016 microchip technology inc. 21.3 adc 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 21-4 . 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 ), refer to figure 21-4 . 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 adc acquisition must be done before the conversion can be started. to calculate the minimum acquisition time, equation 21-1 may be used. this equation assumes that 1/2 lsb error is used (1,024 steps for the adc). the 1/2 lsb error is the maximum error allowed for the adc to meet its specified resolution. equation 21-1: acquisition time example 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 not discharged after each conversion. 3: the maximum recommended impedance for analog sources is 10 k ? . this is required to meet the pin leakage specification. t acq amplifier settling time hold capacitor charging time temperature coefficient ++ = t amp t c t coff ++ = 2s t c temperature - 25c ?? 0.05s/c ?? ?? ++ = t c c hold r ic r ss r s ++ ?? ln(1/2047) ? = 10pf 1k ? 7k ? 10k ? ++ ?? ? ln(0.0004885) = 1.37 = s v applied 1e tc ? rc --------- ? ?? ?? ?? v applied 1 1 2 n1 + ?? 1 ? -------------------------- ? ?? ?? = v applied 1 1 2 n1 + ?? 1 ? -------------------------- ? ?? ?? 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 ? 5.0v v dd = assumptions: note: where n = number of bits of the adc. t acq 2s 892ns 50c- 25c ?? 0.05 s/c ?? ?? ++ = 4.62s = downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 241 pic16(l)f18326/18346 figure 21-4: analog input model figure 21-5: adc transfer function c pin va rs analog 5 pf v dd v t ? 0.6v v t ? 0.6v i leakage (1) r ic ? 1k sampling switch ss rss c hold = 10 pf ref- 6v sampling switch 5v4v 3v 2v 567891011 (k ? ) v dd legend: c pin v t i leakage r ic ss c hold = input capacitance = threshold voltage = leakage current at the pin due to = interconnect resistance = sampling switch = sample/hold capacitance various junctions r ss note 1: refer to table 34-4 (parameter d060). r ss = resistance of sampling switch input pin 3ffh 3feh adc output code 3fdh 3fch 03h02h 01h 00h full-scale 3fbh 0.5 lsb ref- zero-scale transition ref+ transition 1.5 lsb full-scale range analog input voltage downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 242 preliminary ? 2016 microchip technology inc. 21.4 register definitions: adc control register 21-1: adcon0: ad c control register 0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 chs<5:0> go/done adon 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-2 chs<5:0>: analog channel select bits 111111 = fvr (fixed voltage reference) (2) 111110 = dac1 output (1) 111101 = temperature indicator (3) 111100 =av ss (analog ground) 111011 = reserved. no channel connected. 010111 =anc7 (4) 010110 =anc6 (4) 010101 =anc5 010100 =anc4 010011 =anc3 010010 =anc2 010001 =anc1 010000 =anc0 001111 =anb7 (4) 001110 =anb6 (4) 001101 =anb5 (4) 001100 =anb4 (4) 001011 = reserved. no channel connected. 000101 =ana5 000100 =ana4 000011 = reserved. no channel connected. 000010 =ana2 000001 =ana1 000000 =ana0 bit 1 go/done : adc conversion status bit 1 = adc conversion cycle in progress. setting this bit starts an adc conversion cycle. this bit is automatically cleared by hardware when the adc conversion has completed. 0 = adc 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: see section 23.0 ?5-bit digital-to-analog converter (dac1) module? for more information. 2: see section 15.0 ?fixed voltage reference (fvr)? for more information. 3: see section 16.0 ?temperature indicator module? for more information. 4: pic16(l)f18346 only. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 243 pic16(l)f18326/18346 register 21-2: adcon1: ad c control register 1 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 adfm adcs<2:0> adnref adpref<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 adfm: adc result format select bit 1 = right justified. six most significant bits of adresh are set to 0 when the conversion result is loaded. 0 = left justified. six least significant bits of adresl are set to 0 when the conversion result is loaded. bit 6-4 adcs<2:0>: adc conversion clock select bits 111 = adcrc (dedicated rc oscillator) 110 =f osc /64 101 =f osc /16 100 =f osc /4 011 = adcrc (dedicated rc oscillator) 010 =f osc /32 001 =f osc /8 000 =f osc /2 bit 3 unimplemented: read as 0 bit 2 adnref: a/d negative voltage reference configuration bit when adon = 0 , all multiplexer inputs are disconnected. 0 =v ref - is connected to av ss 1 =v ref - is connected to external v ref - bit 1-0 adpref<1:0>: adc positive voltage reference configuration bits 11 =v ref + is connected to internal fixed voltage reference (fvr) module (1) 10 =v ref + is connected to external v ref + pin (1) 01 = reserved 00 =v ref + is connected to v dd note 1: when selecting the v ref + pin as the source of the positive reference, be aware that a minimum voltage specification exists. see table 34-13 for details. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 244 preliminary ? 2016 microchip technology inc. register 21-3: adact: a/d auto-conversion trigger u-0 u-0 u-0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 r/w-0/0 adact<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 resets 1 = bit is set 0 = bit is cleared bit 7-4 unimplemented: read as 0 bit 3-0 adact<3:0>: auto-conversion trigger selection bits (1) 1111 = ccp4 1110 = ccp3 1101 = ccp2 1100 = ccp1 1011 =clc4 1010 =clc3 1001 =clc2 1000 =clc1 0111 = comparator c2 0110 = comparator c1 0101 = timer2-pr2 match 0100 = timer1 overflow (2) 0011 = timer0 overflow (2) 0010 = timer6-pr6 match 0001 = timer4-pr4 match 0000 = no auto-conversion trigger selected note 1: this is a rising edge sensitive input for all sources. 2: trigger corresponds to when the peripherals interrupt flag is set. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 245 pic16(l)f18326/18346 register 21-4: adresh: adc result register high (adresh) adfm = 0 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 adres<9:2> 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-0 adres<9:2> : adc result register bits upper eight bits of 10-bit conversion result register 21-5: adresl: adc result register low (adresl) adfm = 0 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 adres<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 adres<1:0> : adc result register bits lower two bits of 10-bit conversion result bit 5-0 reserved : do not use. downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 246 preliminary ? 2016 microchip technology inc. register 21-6: adresh: adc result register high (adresh) adfm = 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 adres<9:8> 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-2 reserved : do not use. bit 1-0 adres<9:8> : adc result register bits upper two bits of 10-bit conversion result register 21-7: adresl: adc result register low (adresl) adfm = 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 adres<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 bit 7-0 adres<7:0> : adc result register bits lower eight bits of 10-bit conversion result downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 247 pic16(l)f18326/18346 table 21-3: summary of registers associated with adc name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 register on page intcon gie peie i n t e d g 98 pie1 tmr1gie adie rcie txie ssp1ie bcl1ie tmr2ie tmr1ie 100 pir1 tmr1gif adif rcif txif ssp1if bcl1if tmr2if tmr1if 105 trisa trisa5 trisa4 (2) trisa2 trisa1 trisa0 141 trisb (1) trisb7 trisb6 trisb5 trisb4 147 trisc trisc7 (1) trisc6 (1) trisc5 trisc4 trisc3 trisc2 trisc1 trisc0 153 ansela ansa5 ansa4 ansa2 ansa1 ansa0 142 anselb (1) ansb7 ansb6 ansb5 ansb4 148 anselc ansc7 (1) ansc6 (1) ansc5 ansc4 ansc3 ansc2 ansc1 ansc0 155 adcon0 chs<5:0> go/done adon 242 adcon1 adfm adcs<2:0> adnref adpref<1:0> 243 adact adact<3:0> 244 adresh adresh<7:0> 245 adresl adresl<7:0> 245 fvrcon fvren fvrrdy tsen tsrng cdafvr<1:0> adfvr<1:0> 178 dac1con1 d a c 1 r < 4 : 0 > 262 oscstat1 extor hfor lfor sor ador pllr 88 legend: = unimplemented read as 0 . shaded cells are not used for the adc module. note 1: pic16(l)f18346 only. 2: unimplemented, read as 1 . downloaded from: http:/// pic16(l)f18326/18346 ds40001839a-page 248 preliminary ? 2016 microchip technology inc. 22.0 numerically controlled oscillator (nco1) module the numerically controlled oscillator (nco1) module is a timer that uses the overflow from the addition of an increment value to divide the input frequency. the advantage of the addition method over simple counter-driven timer is that the output frequency resolution does not vary with the divider value. the nco1 is most useful for applications that require frequency accuracy and fine resolution at a fixed duty cycle. features of the nco1 include: 20-bit increment function fixed duty cycle (fdc) mode pulse frequency (pf) mode output pulse-width control multiple clock input sources output polarity control interrupt capability figure 22-1 is a simplified block diagram of the nco1 module. downloaded from: http:/// ? 2016 microchip technology inc. preliminary ds40001839a-page 249 pic16(l)f18326/18346 figure 22-1: numerically controlled oscilla tor module simplified block diagram 0011 10 01 000011 010 001100 101 110 111 1 0 r s qq d qq 1 & |