Note the following details of the code protection feature on Microchip devices:
•Microchip products meet the specification contained in their particular Microchip Data Sheet.
•Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
•There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
•Microchip is willing to work with the customer who is concerned about the integrity of their code.
•Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digit al Millennium Copyright Act. If suc h a c t s
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device
applications and the like is provided only for your convenience
and may be superseded by updates. It is your responsibility to
ensure that your application meets with your specifications.
MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED,
WRITTEN OR ORAL, STATUTORY OR OTHERWISE,
RELATED TO THE INFORMATION, INCLUDING BUT NOT
LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE,
MERCHANTABILITY OR FITNESS FOR PURPOSE.
Microchip disclaims all liability arising from this information and
its use. Use of M icrochip’s prod ucts as critical components in
life support systems is not authorized except with express
written approval by Microchip. No licenses are conveyed,
implicitly or otherwise, under any Microchip intellectual property
rights.
Trademarks
The Microchip name and logo, the Microchip logo, Accuron,
dsPIC, K
EELOQ, microID, MPLAB, PIC, PICmicro, PICSTART,
PRO MATE, PowerSmart, rfPIC, and SmartShunt are
registered trademarks of Microchip Technology Incorporated
in the U.S.A. and other countries.
AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB,
PICMASTER, SEEVAL, SmartSensor and The Embedded
Control Solutions Company are registered trademarks of
Microchip Technology Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, dsPICDEM,
dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR,
FanSense, FlexROM, fuzzyLAB, In-Circuit Serial
Programming, ICSP, ICEPIC, Linear Active Thermistor,
MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM,
PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo,
PowerMate, PowerTool, rfLAB, rfPICD EM, Select Mode,
Smart Serial, SmartTel, Total Endurance and WiperLock are
trademarks of Microchip Technology Incorporated in the
U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
Microchip received ISO/TS-16949:2002 quality system certification for
its worldwide headquarters, design and wafer fabrication facilities in
Chandler and Tempe, Arizona and Mountain View, California in
October 2003. The Company’s quality system processes and
procedures are for its PICmicro
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
13.0 Data EEPROM and Flash Program Memory Control............................................................................................................... 153
16.0 Special Features of the CPU.................................................... ..................... ........................................................................... 185
17.0 Instruction Set Summary.......................................................................................................................................................... 205
18.0 Development Support............................................................................................................................................................... 215
20.0 DC and AC Characteristics Graphs and Tables....................................................................................................................... 247
Appendix A: Data Sheet Revision History.......................................................................................................................................... 259
Appendix B: Migrating From Other PICmicro® Devices .................................................................................................................... 259
Index .................................................................................................................................................................................................. 261
Systems Information and Upgrade Hot Line...................................................................................................................................... 269
Product Identification System ............................................................................................................................................................ 271
TO OUR VALUED CUSTOMERS
It is our intention to provide our valued customers with the best documentation possible to ensure successful use of your Microchip
products. To this end, we will continue to improve our publications to better suit your needs. Our publications will be refined and
enhanced as new volumes and updates are introduced.
If you have any questions or c omm ents regarding t his publication, p lease c ontact the M arket ing Co mmunications Department via
E-mail at docerrors@microchip.com or fax the Reader Response Form in the back of this data sheet to (480) 792-4150. We
welcome your feedback.
Most Current Data Sheet
To obtain the most up-to-date version of this data sheet, please register at our Worldwide Web site at:
http://www.microchip.com
You can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page.
The last character of the literature number is the version number, (e.g., DS30000A is version A of document DS30000).
Errata
An errata sheet, describing minor operational differences from the data sheet and recommended workarounds, may exist for current
devices. As device/documentation issues become known to us, we will publish an errata sheet. The errata will specify the revision
of silicon and revision of document to which it applies.
To determine if an errata sheet exists for a particular device, please check with one of the following:
• Microchip’s Worldwide Web site; http://www.microchip.com
• Your local Microchip sales office (see last page)
When contacting a sales office, please specify which device, revision of silicon and data sheet (include literature number) you are
using.
Customer Notification System
Register on our web site at www.microchip.com to receive the most current information on all of our products.
This documen t conta i ns dev ic e spec if i c in for m at i on fo r
the PIC16F91X. Addition al informatio n may be found i n
the “PICmicro® Mid-Range MCU Family Reference
Manual” (DS33023), downloaded from the Microchip
web site. The Reference Ma nu al should be consi dered
a complementary document to this data sheet and is
highly recommended reading for a better
understanding of the dev ice arc hitecture and operatio n
of the peripheral modules.
The PIC16F91X devices are covered by this data
sheet. It is available in 28/40/44-pin packages.
The PIC16F917/916/914/913 has a 13-bit program
counter capable of addressing a 4k x 14 program
memory space for the PIC16F913/914 (0000h-0FFFh)
and an 8k x 14 program memory space for the
PIC16F916/917 (0000h-1FFFh). Accessing a location
above the memory boundaries for the PIC16F913 and
PIC16F914 will cause a wrap around within the first 4k x
14 space. The Reset vector is at 0000h and the interrupt
vector is at 0004h.
The data memory is partitioned into multiple banks
which contain the General Purpose Registers (GPRs)
and the Special Function Registers (SFRs). Bits RP0
and RP1 are bank select bits.
RP0RP1 (STATUS<6:5>)
= 00: → Bank 0
= 01: → Bank 1
= 10: → Bank 2
= 11: → Bank 3
Each bank extends up to 7Fh (128 bytes). The lower
locations of each bank are reserved for the Special
Function Registers. Above the Special Function
Registers are the General Purpose Registers,
implemented as static RAM. All implemented banks
contain Special Function Registers. Some frequently
used Special Function Registers from one bank are
mirrored in another bank for code reduction and
quicker access.
2.2.1GENERAL PURPOSE REGISTER
FILE
The register file is organized as 256x 8 in the
PIC16F913/914 and 352 x 8 in the PIC16F916/917.
Each register is accessed either directly or indirectly
through the File Select Register (FSR) (see Section 2.5“Indirect Addressing, INDF and FSR Registers”).
2.2.2SPECIAL FUNCTION REGISTERS
The Special Function Registers are registers used by
the CPU and peripheral functions for controlling the
desired operation of the device (see Tables 2-1,
2-2, 2-3 and 2-4). These registers are static RAM.
The special re gisters can be classifi ed into two sets:
core and peripheral. The Special Function Registers
associated with the “c ore” are des cribed in this sect ion.
Those related to the operation of the peripheral
features are described in the section of that peripheral
feature.
TABLE 2-1:PIC16F917/916/914/913 SPECIAL REGISTERS SUMMARY BANK 0
AddrNameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2 Bit 1Bit 0
Bank 0
00hINDFAddressing this location uses contents of FSR to address data memory (not a physical register)xxxx xxxxxxxx xxxx
01hTMR0Timer0 Module Registerxxxx xxxx uuuu uuuu
02hPCLProgram Counter’s (PC) Least Significant Byte0000 0000 0000 0000
03hSTATUSIRPRP1RP0TO
04hFSRIndirect Data Memory Address Pointerxxxx xxxx uuuu uuuu
05hPORTARA7RA6RA5RA4RA3RA2RA1RA0xxxx xxxx uuuu uuuu
06hPORTBRB7RB6RB5RB4RB3RB2RB1RB0xxxx xxxx uuuu uuuu
07hPORTCRC7RC6RC5RC4RC3RC2RC1RC0xxxx xxxx uuuu uuuu
08hPORTD
09hPORTE
0AhPCLATH
0BhINTCONGIE PEIET0IEINTERBIET0IFINTFRBIF0000 000x 0000 000x
0ChPIR1
0DhPIR2
0EhTMR1LHolding Register for the Least Significant Byte of the 16-bit TMR1xxxx xxxx uuuu uuuu
0FhTMR1HHolding Register for the Most Significant Byte of the 16-bit TMR1xxxx xxxx uuuu uuuu
10hT1CONT1GINVT1GET1CKPS1 T1CKPS0 T1OSCEN T1SYNC
11hTMR2
12hT2CON
13hSSPBUFSynchron ous Seri al Port Receive Buffer/Transmit Registerxxxx xxxx uuuu uuuu
14hSSPCONWCOLSSPOVSSPENCKPS SPM3SSPM2SSPM1SSPM00000 0000 0000 0000
15hCCPR1LCapture/Compare/PWM Register 1 (LSB)xxxx xxxx uuuu uuuu
16hCCPR1HCapture/Compare/PWM Register 1 (MSB)xxxx xxxx uuuu uuuu
17hCCP1CON
18hRCSTASPENRX9SRENCRENADDENFERROERRRX9D0000 000x 0000 000x
19hTXREGUSART Transmit Data Register0000 0000 0000 0000
1AhRCREGUSART Receive Data Register0000 0000 0000 0000
(2)
1Bh
(2)
1Ch
(2)
1Dh
1EhADRESHA/D Result Register High Bytexxxx xxxx uuuu uuuu
1FhADCON0ADFMVCFG1V CFG0CHS2CHS1CHS0GO/DONE
Legend:- = Unimplemented locations read as ‘0’, u = unchanged, x = unknown, q = value depends on condition, shaded = unimplemented
Note 1:Other (non Power-up) Resets include MCLR
(2)
RD7RD6RD5RD4RD3RD2RD1RD0xxxx xxxx uuuu uuuu
————RE3RE2
———Write Buffer for upper 5 bits of Program Counter---0 0000 ---0 0000
TABLE 2-2:PIC16F917/916/914/913 SPECIAL FUNCTION REGISTERS SUMMARY BANK 1
AddrNameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2 Bit 1Bit 0
Bank 1
80hINDFAddressing this location uses contents of FSR to address data memory (not a physical
81hOPTION_REGRBPU
82hPCLProgram Counter’s (PC) Least Significant Byte0000 00000000 0000
83hS TATUSIRPRP1RP0TO
84hFSRIndirect Data Memory Address Pointerxxxx xxxxuuuu uuuu
85hTRISATRISA7TRISA6TRISA5TRISA4TRISA3TRISA2TRISA1TRISA01111 11111111 1111
86hTRISBTRISB7TRISB6TRISB5TRISB4TRISB3TRISB2TRISB1TRISB01111 11111111 1111
87hTRISCTRISC7TRISC6TRISC5TRISC4TRISC3TRISC2TRISC1TRISC01111 11111111 1111
88hTRISD
89hTRISE
8AhPCLATH
8BhINTCONGIE PEIE T0IEINTERBIET0IFINTFRBIF0000 000x0000 000x
8ChPIE1EEIEADIERCIETXIESSPIECCP1IETMR2IETMR1IE0000 00000000 0000
8DhPIE2OSFIEC2IEC1IELCDIE
8EhPCON
8FhOSCCON
90hOSCTUNE
91hANSELANS7
92hPR2Timer2 Peri od Regi ster1111 11111111 1111
93hSSPADDSynchronous Serial Port (I
94hSSPSTATSMPCKED/A
95hWPUBWPUB7WPUB6WPUB5WPUB4WPUB3WPUB2WPUB1WPUB01111 11111111 1111
96hIOCBIOCB7IOCB6IOCB5IOCB4
97hCMCON1
98hTXSTACSRCTX9TXENSYNC
99hSPBRGSPBRG7 SPBRG6 SPBRG5 SPBRG4 SPBRG3 SPBRG2 SPBRG1 SPBRG00000 00000000 0000
9Ah—Unimplemented——
9Bh—Unimplemented——
9ChCMCON0C2OUTC1OUTC2INVC1INVCISCM2CM1CM00000 00000000 0000
9DhVRCONVREN
9EhADRESLA/D Result Register Low Bytexxxx xxxxuuuu uuuu
9FhADCON1
Legend:- = Unimplemented locations read as ‘0’, u = unchanged, x = unknown, q = value depends on condition, shaded = unimplemented
Note 1:Other (non Power-up) Resets include MCLR
(2)
2:PIC 16F 91 4/9 17 only.
3:PIC 16F 91 4/9 17 only, forced ‘0’ on PIC16F91 3/9 16.
4:The value of the OSTS bit is dependent on the value of the Configuration Word (CONFIG) of the device. See Section 4.0 “Clock
Legend:- = Unimplemented locations read as ‘0’, u = unchanged, x = unknown, q = value depends on condition, shaded = unimplemented
Note 1:Other (non Power-up) Resets include MCLR
(3)
(3)
(2,3)
2:PIC16F914/917 only.
3:This register is only initialized by a POR or BOR reset and is unchanged by other Resets.
Legend:- = Unimplemented locations read as ‘0’, u = unchanged, x = unknown, q = value depends on condition, shaded = unimplemented
Note 1:Other (non Power-up) Resets include MCLR
The Status register, shown in Register 2-1, contains:
• the arithmetic status of the ALU
• the Reset status
• the bank select bits for data memory (SRAM)
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 bit s are set or cleared ac cording to the
device logic. Furthermore, the TO
writable. Therefore, the result of an instruction with the
Status register as destination may be different than
intended.
and PD bits are not
For example, CLRF STATUS will clear the upper three
bits and set the Z bit. Thi s leav es the Status register as
‘000u u1uu’ (where u = unchanged).
It is recommended, therefore, that only BCF, BSF,SWAPF and MOVWF instructions are used to alter the
Stat us register , beca use these instru ctions do not af fect
any Status bits. For other instructions not affecting any
Status bits (see Section 17.0 “Instruction Set
Summary”).
Note 1: The C and DC bits operate as a Borrow
and Digit Borrow out bit, respectively, in
subtraction. See the SUBLW and SUBWF
instructions for examples.
REGISTER 2-1:STATUS – STATUS REGISTER (ADDRESS: 03h, 83h, 103h OR 183h)
R/W-0R/W-0R/W-0R-1R-1R/W-xR/W-xR/W-x
IRPRP1RP0TOPDZDCC
bit 7bit 0
bit 7
bit 6-5
bit 4
bit 3
bit 2
bit 1
bit 0
IRP: Register Bank Select bit (used for indirect addressing)
1 = Bank 2, 3 (100h-1FFh)
0 = Bank 0, 1 (00h-FFh)
RP<1:0>: Register Bank Select bits (used for direct addressing)
00 = Bank 0 (00h-7Fh)
01 = Bank 1 (80h-FFh)
10 = Bank 2 (100h-17Fh)
11 = Bank 3 (180h-1FFh)
TO: Time-out bit
1 = After power-up, CLRWDT instruction or SLEEP instruction
0 = A WDT time-out occurred
PD: Power-down bit
1 = After power-up or by the CLRWDT instruction
0 = By execution of the SLEEP instruction
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
DC: Digit Carry/Borrow bit (ADDWF, ADDLW,SUBLW,SUBWF instructions)
1 = A carry-out from the 4th low-order bit of the result occurred
0 = No carry-out from the 4th low-order bit of the resultC: Carry/Borrow bit (ADDWF, ADDLW, SUBLW, SUBWF instructions)
1 = A carry-out from the Most Significant bit of the result occurred
0 = No carry-out from the Most Significant bit of the result occurred
Note 1: For Borrow,
complement of the second operand. For rotate (RRF, RLF) instructions, this bit is
loaded with either the high or low-order bit of the source register.
the polarity is reversed. A subtraction is executed by adding the two’s
(1)
(1)
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
The INTCON register is a readable and writable
register , which c ontains the various en able and fl ag bit s
for TMR0 register overflow, PORTB change and
external RB0/INT/SEG0 pin interrupts.
Note:Interrupt flag bits are set when an interr upt
condition occurs, regard less of the st ate of
its corresponding enable bit or the global
enable bit, GIE (INTCON<7>). User
software should ensure the appropriate
interrupt flag bits are clear prior to
enabling an interrupt.
REGISTER 2-3:INTCON – INTERRUPT CONTROL REGISTER (ADDRESS: 0Bh, 8Bh, 10Bh OR
18Bh)
R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-x
GIEPEIET0IEINTERBIET0IFINTFRBIF
bit 7bit 0
bit 7GIE: Global Interrupt Enable bit
1 = Enables all unmasked interrupts
0 = Disables all interrupts
bit 6PEIE: Peripheral Interrupt Enable bit
1 = Enables all unmasked peripheral interrupts
0 = Disables all peripheral interrupts
bit 5T0IE: TMR0 Overflow Interrupt Enable bit
1 = Enables the TMR0 interrupt
0 = Disables the TMR0 in terrupt
bit 4INTE: RB0/INT/SEG0 External Interrupt Enable bit
1 = Enables the RB0/INT/SEG0 external interrupt
0 = Disables the RB0/INT/SEF0 external interrupt
bit 3RBIE: PORTB Change Interrupt Enable bit
1 = Enables the PORTB change interrupt
0 = Disables the PORTB change interrupt
bit 2T0IF: TMR0 Overflow Interrupt Flag bit
1 = TMR0 regis ter has overflowed (must be cleared in software)
0 = TMR0 register did not overflow
bit 1INTF: RB0/INT/SEG0 External Interrupt Flag bit
1 = The RB0/INT/SEG0 external interrupt occurred (must be cleared in software)
0 = The RB0/INT/SEG0 external interrupt did not occur
bit 0RBIF: PORTB Change Interrupt Flag bit
1 = When at least one of the PORTB <5:0> pins changed state (must be cleared in software)
0 = None of the PORTB <7:4> pins have changed state
Note 1: IOCB register must also be enabled.
2: T0IF bit is set when Timer0 rolls over. Timer0 is unchanged on Reset and should
be initialized before clearing T0IF bit.
(1)
(2)
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
1 = The write operation completed (must be cleared in software)
0 = The write operation has not completed or has not started
bit 6ADIF: A/D Converter Interrupt Flag bit
1 = The A/D conversion completed (must be cleared in software)
0 = The A/D conversion is not complete
bit 5RCIF: USART Receive Interrupt Flag bit
1 = The USART receive buffer is full (cleared by reading RCREG)
0 = The USART receive buffer is not full
bit 4TXIF: USART Transmit Interrupt Flag bit
1 = The USART transmit buffer is empty (cleared by writing to TXREG)
0 = The USART transmit buffer is full
bit 3SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit
1 = The Transmission/Reception is complete (must be cleared in software)
0 = Waiting to Transmit/Receive
bit 2CCP1IF: CCP1 Interrupt Flag bit
Capture Mode
1 = A TMR1 register capture occurred (must be cleared in software)
0 = No TMR1 register capture occurred
Compare Mode
1 = A TMR1 register compare match occurred (must be cleared in software)
0 = No TMR1 register compare match occurred
PWM mode
Unused in this mode
bit 1TMR2IF: TMR2 to PR2 Interrupt Flag bit
1 = A TMR2 to PR2 match occurred (must be cleared in software)
0 = No TMR2 to PR2 match occurred
bit 0TMR1IF: TMR1 Overflow Interrupt Flag bit
1 = The TMR 1 register overflowed (must be cleared in software)
0 = The TMR1 register did not overflow
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 (INTCON<7>). User
software should ensure the appropriate
interrupt flag bits are clear prior to enabling
an interrupt.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
1 = System oscillator failed, clock input ha s changed to INT OSC (must be c leared in s oftware)
0 = System clock operating
bit 6C2IF: Comparator 2 Interrupt Flag bit
1 = Comparator output (C2OUT bit) has changed (must be cleared in software)
0 = Comparator output (C2OUT bit) has not changed
bit 5C1IF: Comparator 1 Interrupt Flag bit
1 = Comparator output (C1OUT bit) has changed (must be cleared in software)
0 = Comparator output (C1OUT bit) has not changed
bit 4LCDIF: LCD Module Interrupt bit
1 = LCD has generated an interrupt
0 = LCD has not generated an int errupt
bit 3Unimplemented: Read as ‘0’
bit 2LVDIF: Low Voltage Detect Interrupt Flag bit
1 = LVD has generated an interrupt
0 = LVD has not generated an interrupt
bit 1Unimplemented: Read as ‘0’
bit 0CCP2IF: CCP2 Interrupt Flag bit (only available in 16F914/917)
Capture Mode
1 = A TMR1 register capture occurred (must be cleared in software)
0 = No TMR1 register capture occurred
Compare Mode
1 = A TMR1 register compare match occurred (must be cleared in software)
0 = No TMR1 register compare match occurred
PWM mode
Unused in this mode
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 (INTCON<7>). User
software should ensure the appropriate
interrupt flag bits are clear prior to enabling
an interrupt.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
The Program Counter ( PC) is 13 bits wide. The low
byte comes from the PCL register, which is a readable
and writable register. The high byte (PC<12:8>) is not
directly readable or writable and comes from
PCLATH. On any Reset, the PC is cleared. Figure 2-5
shows the two situations for the loading of the PC. The
upper example in Figure 2-5 shows how the PC is
loaded on a write to PCL (PCLATH<4:0> → PCH).
The lower example in Figure 2-5 shows how the PC is
loaded during a CALL or GOTO instruction
(PCLATH<4:3> → PCH).
FIGURE 2-5:LOADING OF PC IN
DIFFERENT SITUATIONS
PCHPCL
128 70
PC
PCLATH<4:0>
5
PCLATH
PCHPCL
12 11 100
PC
2
87
PCLATH<4:3>
PCLATH
8
11
2.3.1COMPUTED GOTO
A computed GOTO is accomplish ed by adding an offset
to the program counter (ADDWF PCL). When performing a table read using a computed GOTO method, care
should be exercise d i f the t able loca tio n cros ses a PCL
memory boundary (each 256-byte block). Refer to the
Application Note AN556, “Implementing a Table Read”
(DS00556).
2.3.2STACK
The PIC16F917/916/914/913 family has an
8-level x 13-bit wide hardware stack (see Figures 2-1
and 2-2). The stack space is not pa r t of eit her pro gra m
or data space and the Stack Pointer is not readable or
writable. The PC is PUSHed onto the stack when a
CALL instruction is executed or an interrupt causes a
branch. The stack is POPed in the event of a RETURN,RETLW or a RETFIE instruction ex ecuti on. PC LATH is
not affected by a PUSH or POP operat ion.
The stack opera tes as a circular buf fer . This means that
after the stack has been PUSHed eight ti mes, the nin th
PUSH overwrites the value that was stored from the
first PUSH. The tenth PUSH overwrites the second
PUSH (and so on).
Instruction wit
PCL a
Destinatio
ALU Result
GOTO, CALL
OPCODE<10:0>
PIC16F917/916/914/913
Note 1: There are no Status bits to indicate stack
overflow or stack underflow conditions.
2: There are no instructions/mnemonics
called PUSH or POP. These are actions
that occur from the exec ution of the CALL,RETURN, RETLW and RETFIE instructions or the vectoring to an interrupt
address.
2.4Program Memory Paging
All PIC16F917/916/914/913 devices are capable of
addressing a continuous 8K word block of program
memory. The CALL and GOTO instructions pro vide only
1 1 bits of addres s to allow branching within a ny 2K program memory page. When doing a CALL or GOTO
instruction, the upper 2 bits of the addres s are provided
by PCLA TH<4:3>. Wh en doing a CALL or GOTO instruction, the user must ensu re tha t the p age select bits are
programmed so that the desired program memory
page is addressed. If a return from a CALL instruction
(or interrupt) is executed, the entire 13-b it PC is POPed
off the stack. Therefore, manipulation of the
PCLATH<4:3> bits is not required for the RETURN
instructions (which POPs the address from the stack).
Note:The contents of the PCLATH register are
unchanged after a RETURN or RETFIE
instruction is executed. The user must
rewrite the contents of the PCLATH register for any subsequent subroutine calls or
GOTO instructions.
Example 2-1 shows the calling of a subroutine in
page 1 of the prog ram memory. This example as sumes
that PCLATH is saved and restored by the Interrupt
Service Routine
The INDF register is not a physi cal register. Addressing
the INDF register will cause indirect addressing.
Indirect addressing is possible by using the INDF
register. Any instruction using the INDF register
actually accesses data pointed to by the File Select
EXAMPLE 2-2:INDIRECT ADDRESSING
MOVLW0x20;initialize pointer
MOVWFFSR;to RAM
NEXTCLRFINDF;clear INDF register
INCFFSR;inc pointer
BTFSSFSR,4;all done?
GOTONEXT;no clear next
CONTINUE;yes continue
Register (FSR). Reading INDF itself indirectly will
produce 00h. Writing to the INDF register indirectly
results in a no operation (although Status bits may be
affected). An effective 9-bit address is obtained by
concatenating the 8-bit FSR register and the IRP bit
(STATUS<7>), as shown in Figure 2-6.
A simple program to clear RA M location 20h -2Fh using
indirect addressing is shown in Example 2-2.
This device includ es four 8 -bi t port registers along with
their corresponding TRIS registers and one four bit
port:
• PORTA and TRISA
• PORTB and TRISB
• PORTC and TRISC
• PORTD and TRISD
• PORTE and TRISE
PORTA, PORTB, PORTC and RE3/MCLR
implemente d on al l de vi ce s. PO RTD an d RE <2: 0 > ar e
implemented only on the PIC16F914 and PIC16F917.
3.1PORTA and TRISA Registers
PORTA is a 8-bit wide, bidirectional port. The
corresponding data direction register is TRISA
(Register 3-2). Setting a TRISA bit (= 1) will make the
corresponding PORTA pin an input (i.e., put the
corresponding output driver in a High-impedance mode).
Clearing a TRISA bit (= 0) will make the corr esponding
PORTA pin an output (i.e., put the contents of the output
latch on the selected pin). Example 3-1 shows how to
initialize PORTA.
Five of the p ins o f PORTA can be c onfig ured a s an alog
inputs. These pins, RA5 and RA<3:0>, are configured
as analog inputs on device power-up and must be
reconfigured by the user to be used as I/O’s. This is
done by writing the a ppropri ate v alues to th e CMCO N0
and ANSEL registers (see Example 3-1).
Reading the PORTA register (Register 3-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 a nd then written
to the port data latch.
The TRISA register controls the direction of the
PORT A pins, even when they are being us ed as analog
inputs. The user must ensure the bits in the TRISA
register are maint ai ned set when using the m as an alog
inputs. I/O pin s co nfigure d as analo g inpu t alw ays rea d
‘0’.
/VPP are
EXAMPLE 3-1:INITIALIZING PORTA
BCFSTATUS,RP0;Bank 0
BCFSTATUS,RP1;
CLRFPORTA;Init PORTA
BSFSTATUS,RP0;Bank 1
BCFSTATUS,RP1;
MOVLW07h;Set RA<2:0> to
MOVWFCMCON0;digital I/O
CLFANSEL;Make all PORTA I/O
MOVLWF0h;Set RA<7:4> as inputs
MOVWFTRISA;and set RA<3:0>
; as outputs
BCFSTATUS,RP0;Bank 0
BCFSTATUS,RP1;
Note 1: The CMCON0 (9Ch) register must be
initialized to configure an analog channel
as a digital input. Pins configured as
analog inputs will read ‘0’.
2: Analog lines that carry LCD signals
(i.e., SEGx, COMy, where x and y are
segment and common identifiers) are
shown as direct connectio ns to the device
pins. The signals are outputs from the
LCD module and may be tri-stated,
depending on the configuration of the
LCD module.
Each PORTA pin is multiplexed with other functions. The
pins and their combined functions are briefly described
here. For specific information about individual function s,
refer to the appropriate section in this data sheet.
3.1.1.1RA0/AN0/C1-/SEG12
Figure 3-1 shows the diagram for this pin. The
RA0/AN0/C1-/SEG12 pin is configurable to function as
one of the following:
Legend:x = unknown, u = unchanged, - = unimplemented locations read as ‘0’. Shaded cells are not used by PORTA.
Note 1:This register is only initialized by a POR or BOR reset and is unchanged by other Resets.
PORTB is a general purpose I/O port with similar
functionality as the PIC16F77. All PORTB pins can have
a weak pull-up feature, and PORTB<7:4> implements an
interrupt-on-input change function.
PORTB is also used for the Serial Flash programming
interface.
Note:Analog lines that carry LCD signals
(i.e., SEGx, COMy , whe re x and y are se gment and common identifiers) are shown
as direct connections to the device pins.
The signals are outputs from the LCD
module and may be tri-stated, depending
on the configuration of the LCD module.
RB<7:6> are used as data and clock signals, respectively,
for both serial programming and the in-circuit debugger
features on the device. Also, RB0 can be configured as an
external interrupt input.
3.3.1WEAK PULL-UPS
Each of the PORTB pins has an indiv idually configurable
internal weak pull-up. Control bits WPUB<7:0> enable or
disable each pull-up. Refer to Register 3-6. Each weak
pull-up is automatically turned off when the port pin is
configured as an output. The pull-ups are disabled on a
Power-on Reset by the RBPU
3.3.2INTERRUPT-ON-CHANGE
Four of the PORTB pins are individually configurable
as an interrupt-on-change pin. Control bits IOCB<7:4>
enable or disable the interrupt function for each pin.
Refer to Register 3-5. The interrupt-on-change feature
is disabled on a Power-on Reset.
For enabled interrupt-on-change pins, the values are
compared with the old value la tched on the last rea d of
PORTB. The ‘mismatch’ outputs of the last read are
OR’d together to set the PORTB Change Interrupt flag
bit (RBIF) in the INTCON register (Register 2-3).
This interrupt can wake the device from Sleep. The user ,
in the Interrupt Service Routine, clears the interrupt by:
a) Any read or write of PORTB. This will end the
mismatch condition.
b) Clear the flag bit RBIF.
A mismatch c ond it i on w i ll co nt i n ue t o s et f l ag bi t R BI F.
Reading or writing PORTB will end the mismatch condition and allow flag bit RBIF to be cleared. The latch
holding the last read value is not affected by a MCLR
nor Brown-out Reset. After these Re sets, the RBIF flag
will continue to be set if a mismatch is present.
bit (OPTION_REG<7>).
Note:If a change on the I/O pin should occur
when the read operation is being executed
(start of the Q2 cycle), then the RBIF
interrupt flag may not get set. Furthermore,
since a read or write on a port affects all bits
of that port, care must be taken when using
multiple pins in Interrupt-on-change mode.
Changes on one pin may not be seen while
servicing changes on another pin.
Each PORTB pin is multiplexed with other functions. The
pins and their combined functions are briefly described
here. For specific information about i ndividual functions
such as the LCD or interrupts, refer to the a ppropriate
section in this data sheet.
3.3.3.1RB0/INT/SEG0
Figure 3-9 shows the diagram for this pin. The
RB0/INT/SEG0 pin is configu rable to function as one of
the following:
• a general purpose I/O
• an external edge triggered interrupt
• an analog output for the LCD
3.3.3.2RB1/SEG1
Figure 3-9 shows the diagram for this pin. The
RB1/SEG1 pin is c onf igu r abl e to f unc tio n as one of the
following:
• a general purpose I/O
• an analog output for the LCD
3.3.3.3RB2/SEG2
Figure 3-9 shows the diagram for this pin. The
RB2/SEG2 pin is c onf igu r abl e to f unc tio n as one of the
following:
• a general purpose I/O
• an analog output for the LCD
3.3.3.4RB3/SEG3
Figure 3-9 shows the diagram for this pin. The
RB3/SEG3 pin is c onf igu r abl e to f unc tio n as one of the
following:
Legend:x = unknown, u = unchanged, - = unimplemented locations read as ‘0’. Shaded cells are not used by PORTB.
Note 1:This register is only initialized by a POR or BOR reset and is unchanged by other Resets.
PORTC is an 8-bit bidirectional port. PORTC is
multiplexed with several peripheral functions. PORTC
pins have Schmitt Trigger input buffers.
All PORTC pins have latch bits (PORTC register).
They, when written, will modify the contents of the
PORTC latch; thus, modifying the value driven out on
a pin if the corresponding TRISC bit is configured for
output.
Note:Analog lines that carry LCD signals
(i.e., SEGx, VLCDy, where x and y are
segment and LCD bias v oltage identifiers)
are shown as direct connections to the
device pins. The signals are outputs from
the LCD module and may be tri-stated,
depending on the co nfiguration of th e LCD
module.
Each PORTC pin is multiplexed with other functions. The
pins and their combined functions are briefly described
here. For specific information about i ndividual functions
such as the LCD or SSP, refer to the appropriat e section
in this data sheet.
3.4.1.1RC0/VLCD1
Figure 3-14 shows the diagram for this pin. The
RC0/VLCD1 pin is configurable to function as one of
the following:
• a general purpose I/O
• an analog input for the LCD bias voltage
3.4.1.2RC1/VLCD2
Figure 3-15 shows the diagram for this pin. The
RC1/VLCD2 pin is configurable to function as one of
the following:
• a general purpose I/O
• an analog input for the LCD bias voltage
FIGURE 3-14:BLOCK DIAGRAM OF RC0/VLCD1
3.4.1.3RC2/VLCD3
Figure 3-16 shows the diagram for this pin. The
RC2/VLCD3 pin is configurable to function as one of
the following:
Legend:x = unknown, u = unchanged, - = unimplemented locations read as ‘0’. Shaded cells are not used by PORTC.
Note 1:This register is only initialized by a POR or BOR reset and is unchanged by other Resets.
PORTD is an 8-bit port with Schmitt Trigge r input buffers.
Each pin is individually configured as an input or outpu t.
PORTD is only available on the PIC16F914 and
PIC16F917.
Note:Analog lines that carry LCD signals
(i.e., SEGx, COMy , whe re x and y are se gment and common identifiers) are shown
as direct connections to the device pins.
The signals are outputs from the LCD
module and may be tri-stated, depending
on the configuration of the LCD module.
Each PORTD pin is multiplexed with other functions. The
pins and their combined functions are briefly described
here. For specific information about i ndividual functions
such as the comparator or the A/D, refer to the
appropriate section in this data sheet.
3.5.1.1RD0/COM3
Figure 3-22 shows the diagram for this pin. The
RD0/COM3 pin is con figurable to func tion as one of the
following:
• a general purpose I/O
• an analog input for the A/D
3.5.1.2RD1
Figure 3-23 shows the diagram for this pin. The RD1
pin is configurable to function as one of the following:
• a general purpose I/O
3.5.1.3RD2/CCP2
Figure 3-24 shows the diagram for this pin. The
RD2/CCP2 pin is con figurab le to fun ction a s one of th e
following:
• a general purpose I/O
• a Capture input, Compare output or PWM output
3.5.1.7RD6/SEG19
Figure 3-25 shows the diagram for this pin. The
RD6/SEG19 pin is configurable to function as one of
the following:
• a general purpose I/O
• an analog output for the LCD
3.5.1.8RD7/SEG20
Figure 3-25 shows the diagram for this pin. The
RD7/SEG20 pin is configurable to function as one of
the following:
• a general purpose I/O
• an analog output for the LCD
3.5.1.4RD3/SEG16
Figure 3-25 shows the diagram for this pin. The
RD3/SEG16 pin is configurable to function as one of
the following:
• a general purpose I/O
• an analog output for the LCD
3.5.1.5RD4/SEG17
Figure 3-25 shows the diagram for this pin. The
RD4/SEG17 pin is configurable to function as one of
the following:
• a general purpose I/O
• an analog output for the LCD
3.5.1.6RD5/SEG18
Figure 3-25 shows the diagram for this pin. The
RD5/SEG18 pin is configurable to function as one of
the following:
Legend:x = unknown, u = unchanged, - = unimplemented locations read as ‘0’. Shaded cells are not used by PORTC.
Note 1:This register is only initialized by a POR or BOR reset and is unchanged by other Resets.
PORTE is a 4-bit port with Schm itt Trigg er input buffers .
RE<2:0> are individually configured as inputs or outputs. RE3 is only available as an input if MCLRE is ‘0’
in Configu ration Word (Register 16-1).
RE<2:0> are only available on the PIC16F914 and
PIC16F917.
PORTE and TRISE Registers
Note:Analog lines that carry LCD signals
(i.e., SEGx, where x are segment identifiers) are shown as direct connections to
the device pins. The signals are outputs
from the LCD module and may be
tri-stated, depending on the configuration
of the LCD mo dule.
EXAMPLE 3-5:INITIALIZING PORTE
BCFSTATUS,RP0;Bank 0
BCFSTATUS,RP1;
CLRFPORTE;Init PORTE
BSFSTATUS,RP0;Bank 1
BCFSTATUS,RP1;
MOVLW0Fh;Set RE<3:0> as inputs
MOVWFTRISE;
CLRFANSEL;Make RE<2:0> as I/O’s
BCFSTATUS,RP0;Bank 0
BCFSTATUS,RP1;
REGISTER 3-11:PORTE – PORTE REGISTER (ADDRESS: 09h)
U-0U-0U-0U-0R/W-xR/W-xR/W-xR/W-x
————RE3RE2RE1RE0
bit 7bit 0
bit 7-4Unimplemented: Read as ‘0’
bit 3-0RE<3:0>: PORTE I/O Pin bits
1 = Port pin is >V
0 = Port pin is <VIL
IH
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
REGISTER 3-12:TRISE – PORTE TRI-STATE REGISTER (ADDRESS: 89h)
U-0U-0U-0U-0R-1R/W-1R/W-1R/W-1
————TRISE3TRISE2TRISE1TRISE0
bit 7bit 0
bit 7-4Unimplemented: Read as ‘0’
bit 3TRISE3: Data Direction bit. RE3 is always an input, so this bit always reads as a ‘1’
bit 2-0TRISE<2:0>: Data Direction bits
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
Each PORTE pin is multiplexed with other functions. The
pins and their combined functions are briefly described
here. For specific information about i ndividual functions
such as the comparator or the A/D, refer to the
appropriate section in this data sheet.
3.6.1.1RE0/AN5/SEG21
Figure 3-26 shows the diagram for this pin. The
RE0/AN5/SEG21pin is configurable to function as one
of the following:
• a general purpose I/O
• an analog input for the A/D
• an analog output for the LCD
3.6.1.2RE1/AN6/SEG22
Figure 3-26 shows the diagram for this pin. The
RE1/AN6/SEG22 pin is configur abl e to funct ion as one
of the following:
• a general purpose I/O
• an analog input for the A/D
• an analog output for the LCD
3.6.1.3RE2/AN7/SEG23
Figure 3-26 shows the diagram for this pin. The
RE2/AN7/SEG23 pin is confi gurabl e to function as one
of the following:
• a general purpose I/O
• an analog input for the A/D
• an analog output for the LCD
3.6.1.4RE3/MCLR/VPP
Figure 3-27 shows the diagram for this pin. The
RE3/MCLR
of the following:
Legend:x = unknown, u = unchanged, - = unimplemented locations read as ‘0’. Shaded cells are not used by PORTC.
Note 1:This register is only initialized by a POR or BOR reset and is unchanged by other Resets.
2:PIC16F914/917 only.
3:Bit is read-only; TRISE = 1 always.
The PIC16F917/916/914/913 has a wide variety of
clock sources and selection features to allow it to be
used in a wide range of applications while maximizing
performance and minimizing power consumption.
Figure 4-1 illustrates a block diagram of the
PIC16F917/916/914/913 clock sources.
Clock sources can be configured from external oscillators,
quartz crystal resonators, ceramic resonators, and
Resistor-Capacitor (RC) circuits. In add ition, the system
clock source can be configured from one of two in ternal
oscillators, with a choice of speeds selectable via
software. Additional clock features include:
• Selectable system clock source between external
or internal via softwa re.
• Two-Speed Clock Start-up mode, which
minimizes latency between external oscillator
start-up and code execu t io n.
• Fail-Safe Clock Monitor (FSCM) designed to
detect a failure of the external clock source (LP,
XT, HS, EC or RC modes) and switch to the
Internal Oscillator.
The PIC16F917/916/914/913 can be configured in on e
of eight clock modes.
1.EC – External clock with I/O on RA6.
2.LP – Low-gain Crystal or Ceramic Resonator
Oscillator mode.
3. XT – Medium-gain Crystal or Ceramic Resonator
Oscillator mode.
4.HS – High-gain Crystal or Ceramic Resonator
mode.
5.RC – External Resistor-Capacitor (RC) with
OSC/4 output on RA6.
F
6.RCIO – External Resistor-Capacitor with I/O on
RA6.
7.INTOSC – Internal oscillator with F
on RA6 and I/O on RA7.
8.INTOSCIO – Internal oscillator with I/O on RA6
and RA7.
Clock source m odes are configu red by t he FOSC<2:0 >
bits in the Configuration Word register (see
Section 16.0 “Special Features of the CPU”). The
internal clock can be generated by two oscillators. The
HFINTOSC is a high-frequency calibrated oscillator.
The LFINTOSC is a low-frequency uncalibrated
oscillator.
OSC/4 output
FIGURE 4-1:PIC16F917/916/914/913 SYSTEM CLOCK BLOCK DIAGRAM
bit 3OSTS: Oscillator Start-up Time-out Status bit
1 = Device is running from the external system clock defined by FOSC<2:0>
0 = Device is running from the internal system clock (HFINTOSC or LFINTOSC)
bit 2HTS: HFINTOSC (High Frequency – 8MHz to 125 kHz) Status bit
1 =HFINTOSC is stable
0 = HFINTOSC is not stable
bit 1LTS: LFINTOSC (Low Frequency – 31 kHz) Stable bit
1 = LFINTOSC is stable
0 = LFINTOSC is not stable
bit 0SCS: System Clock Select bit
1 = Internal oscillator is used for system clock
0 = Clock source defined by FOSC<2:0>
(1)
HTSLTSSCS
Note 1: The value of the OSTS bit on device power-up is dependent on the value of the
Configuration Word (CONFIG) of the device. The value of the OSTS bit will be ‘0’
on a device Power- on Reset ( POR) or any automatic clock swi tch, which may occur
from Two-Sp ee d Start-up or Fail-Safe Cloc k Mo nit or, if the following conditio ns are
true:
OSTS = 0 if:
FOSC<2:0> = 000 (LP) or 001 (XT) or 010 (HS)
and
IESO = 1 or FSCM = 1
(IESO will be enabled automatically if FSCM is enabled)
If any of the above conditions are not met, the value of the OSTS bit will be ‘1’ on
a device POR. See Section 4.6 “Two-Speed Clock Start-up Mode” and
Section 4.7 “Fail-Safe Clock Monitor” for more details.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
q = value depends on condition
Clock source modes can be classified as external or
internal.
• External clock modes rely on external circuitry for
the clock source. Exam ples are oscillator modules
(EC mode), quartz crystal resonators or ceramic
resonators (LP, XT and HS modes), and
Resistor-Capacitor (RC mode) circuits.
• Internal clock sources are contained internally
within the PIC16F917/916/914/913. The
PIC16F917/916/914/913 has two internal oscillators: the 8 MHz High-Frequency Internal Oscillator (HFINTOSC) and 31 kHz Low-Frequency
Internal Oscillator (LFINTOSC).
The system clock can be selected between external or
internal cloc k sourc es via the S ystem Clo ck Selecti on
(SCS) bit (see Section 4.5 “Clock Switching”).
4.3External Clock Modes
4.3.1O SCILLA TOR START-UP TIMER
(OST)
If the PIC16F917/916/914/913 is configured for LP, XT
or HS modes, the Oscillator Start-up Timer (OST)
counts 1024 osci llations from the OSC1 pin, fo llowing a
Power-on Reset (POR), and the Power-up Timer
(PWRT) has expired (if configured), or a wake-up from
Sleep. During this time, the program counter does not
increment and program execution is suspended. The
OST ensures that the oscillator circuit, using a quartz
crystal reson ator or ce ramic res onator, has started and
is providing a stable system clock to the
PIC16F917/916/914/913. When switching between
clock sources a de lay is required to all ow the new cl ock
to stabilize. These oscillator delays are shown in
Table 4-1.
4.3.1.1Special Case
An exception to this is when the device is put to Sleep
while the following condition s are true:
• LP is the selected primary oscillator mode.
• T1OSCEN = 1 (Timer1 oscillator is enabled).
•SCS = 0 (oscillator mode is defined by
FOSC<2:0>).
• OSTS = 1 (device is running from primary system
clock).
For this case, the OST is no t necessary after a wak e-up
from Sleep, since T im er1 conti nues to r un during Sleep
and uses the same LP oscillator circuit as its clock
source. For these devices, this case is typically seen
when the LCD module is running during Sleep.
In applications where the OSC TUNE register is used to
shift the F
expect the F
In this case, the frequency may shift gradually toward
the new value. The time for this frequency shift is less
than eight cycles of the base frequency.
Note:When the OST is invoked, the WDOG is
T abl e 4-1 shows example s where the oscilla tor delay is
invoked.
In order to minimize laten cy between externa l oscillator
start-up and code execution, the Two-Speed Clock
Start-up mode can be selected (see Section 4.6“Two-Speed Clock Start-up Mode”).
INTOSC frequency, the application should not
INTOSC frequency to stabilize immediately.
held in Reset, because the WDOG ripple
counter is used by th e OST to perform the
oscillator delay count. When the OST
count has expired, the WDOG will begin
counting (if enabled).
LFIOSC31 kHzSleep10 μs internal delay Following a wake-up from Sleep mode
HFIOSC125 kHz-8 MHzSleep10 μs internal delay Following a wake-up from Sleep mode
XT or HS4-20 MHzINTOSC or Sleep1024 clock cycles Following a change from INTOSC, an
LP32 kHzINTOSC or Sleep1024 clock cycles Following a change from INTOSC, an
LP with T1OSC
enabled
EC, RC0-20 MHzSleep10 μs internal delay Following a wake-up from Sleep mode
EC, RC0-20 MHzLFIOSC10 μs internal delay Following a switch from a LFIOSC or
FrequencySwitching From
32 kHzSleep10 μs internal delay Following a wake-up from Sleep mod e,
Oscillator Delay
(T
OST)
Comments
or POR, an internal delay is invoked to
allow the memory bias to stabilize
before program execution can begin.
or POR, an internal delay is invoked to
allow the memory bias to stabilize
before program execution can begin.
OST of 1024 cycles must occur.
OST of 1024 cycles must occur. See
Section 4.3.1.1 “Special Case” for
special case conditions.
an internal delay is invoked to allow the
memory bias to stabilize before
program execution can begin. See
Section 4.3.1.1 “Special Case” for
details about this special case.
or POR, an internal delay is invoked to
allow the memory bias to stabilize
before program execution can begin.
POR, an internal delay is invoked to
allow the memory bias to stabilize
before program execution can begin.
The External Clock (EC) mode allows an externally
generated logic level as the system clock source.
When operating in this mode, an external clock source
is connected to the OSC1 pin and the RA6 pin is
available for general purpose I/ O. Figure 4-2 shows the
pin connections for EC mode.
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 PIC16F917/916/914/913
design is full y static, stoppi ng the extern al clock input
will have the ef fect of halting th e device while leaving all
data intact. Upon restarting the external clock, the
device will resum e ope ration as if no ti me ha d elap se d.
FIGURE 4-2:EXTERNAL CLOCK (EC)
MODE OPERATION
PIC16F917/916/914/913
Clock
(External
System)
RA6
OSC1/
CLKIN
RA6/OSC2/CLKO/T1OSO
FOSC
FOSC<2:0> = 011
Internal
Clock
4.3.3LP, XT, HS MODES
The LP, XT and HS modes support the use of quartz
crystal resonators or ceramic resonators connected to
the OSC1 and OSC2 pins (Figures 4-3 and 4-4). The
mode selects a low, medium or high gain setting of the
internal inverter-amplifier to support various resonator
types and speed.
LP Oscillator mode selects the lowest gain setting of the
internal inverter-amplifier . LP mode current consum ption
is the least of the three modes. This mode is best suited
to drive resonators with a low drive level specification, for
example, tuning fork type crystals.
Note:In the past, the sources for the LP oscilla-
tor and Timer1 oscillator have been separate circuits. In this family of devices, the
LP oscillator and Timer1 oscillator use the
same oscillator circuitry. When using a
device configured for the LP os cilla tor and
with T1OSCEN = 1, the source of the
clock for each function comes from the
same oscillator block.
XT Oscillator mode selects the intermediate gain
setting of the internal inverter-amplifier. XT mode
current consumption is the mediu m of the three mo des.
This mode is best suited to drive resonators with a
medium drive level specification, for example,
low-frequency/AT-cut quartz crystal resonators.
HS Oscillator mode selects the highest gain setting 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, for example, high-frequency/AT-cut
quartz crystal resonators or ceramic re sonators.
Figures 4-3 and 4-4 show typical circuits for quartz
crystal and ceramic resonators, respectively.
The External Resistor-Capacitor (RC) modes support
the use of an external RC circuit. This allows the
designer maximum flexibility in frequency choice while
keeping costs to a minimum when clock accuracy is not
required. There are two modes, RC and RCIO.
In RC mode, the RC circuit connects to the OSC1 pin.
The OSC2/CLKO pin outputs the RC oscillator
frequency divided by 4. This signal may be used to
provide a clock for external circuitry, synchronization,
calibration, test or other application requirements.
Figure 4-5 shows the RC mode connections.
FIGURE 4-5:RC MODE
VDD
PIC16F917/916/914/913
REXT
OSC1
CEXT
VSS
FOSC/4
Recommended values: 3 kΩ ≤ REXT ≤ 100 kΩ
In RCIO mode, the RC circuit is connected to the OSC1
pin. The OSC2 pin becomes an additional general
purpose I/O pin. The I/O pin becomes bit 4 of PORTA
(RA4). Figure 4-6 shows the RCIO mode connections.
OSC2/CLKO
C
EXT > 20 pF
Internal
Clock
FIGURE 4-6:RCIO MODE
VDD
PIC16F917/916/914/913
REXT
OSC1
CEXT
VSS
RA6
Recommended values :3 kΩ ≤ REXT ≤ 100 kΩ
The RC oscillator frequency is a function of the supply
voltage, the resistor (R
values and the operating temperature. In addition to
this, the oscillator frequency will vary from unit to unit
due to normal threshold voltage. Furthermore, the
difference in le ad fram e c apacitance between pac kag e
types will also a ffe ct the oscil lation freque ncy o r for l ow
EXT values. The user also needs to take into account
C
variation due to tolerance of external RC components
used.
I/O (OSC2)
EXT > 20 pF
C
EXT) and capacitor (CEXT)
Internal
Clock
4.4Internal Clock Modes
The PIC16F917/916/914/913 has two independent,
internal oscillators that can be configured or selected
as the system clock source.
1.The HFINTOSC (High-Frequency Internal
Oscillator) is factory calibrated and operates at
8 MHz. The frequency of t he HFINT OSC can be
user adjusted ±12% via software using the
OSCTUNE register (Register 4-2).
2.The LFINTOSC (Low-Frequency Internal
Oscillator) is uncalibrated and operates at
approximately 31kHz.
The system cloc k speed ca n be selec ted via sof tware
using the Internal Oscillator Frequency Select (IRCF)
bits.
The system clock ca n be se lec ted betw ee n external or
internal cloc k sourc es via th e System Cl ock Select ion
(SCS) bit (see Section 4 .5 “Clock Switching”).
4.4.1INTOSC AND INTOSCIO MODES
The INTOSC and INTOSCIO modes configure the
internal oscillators as the system clock source wh en the
device is programmed using the Oscillator Selection
(FOSC) bits in the Configuration Word register
(Register 16-1).
In INTOSC mode, the OSC1 pin is available for ge neral
purpose I/O. The OSC2/CLKO pin outputs the selected
internal oscillator frequency divided by 4. The CLKO
signal may be used to provide a clock for external
circuitry, synchronization, calibration, test or other
application requirements.
In INTOSCIO mode, the OSC1 and OSC2 pins are
available for general purpose I/O.
4.4.2HFINTOSC
The High-Frequency Int ernal Oscillato r (HFINT OSC) is
a factory calibrated 8 MHz internal clock source. The
frequency of the HFINTOSC can be altered
approximately ±12% via software using the OSC TUNE
register (Register 4-2).
The output of the HFINTOSC connects to a postscaler
and multiplexer (see Figure 4-1). One of seven
frequencies can be selected via software using the
IRCF bits (see Section 4.4.4 “Frequency Select Bits(IRCF)”).
The HFINTOSC is enabled by selecting any frequency
between 8 MHz and 125 kHz (IRCF ≠ 000) as the
System Clock S ource (S CS = 1), or when Two-Speed
Start-up is enabled (IESO = 1 and IRCF ≠ 000).
The HF Internal Oscillator (HTS) bit (OSCCON<2>)
indicates whether the HFINTOSC is stable or not.
The HFINTOSC is factory calibrated but can be
adjusted in software by writing to the OSCTUNE
register (Register4-2).
The OSCTUNE register has a tuning range of ±12%.
The default value of the OSCTUNE register is ‘0’. The
value is a 5-bit two’s complement number. Due to
process variatio n, the m onoton icity and freq uency step
cannot be specified.
When the OSCTUNE register is modified, the HFINTOSC
frequency will begin shifting to the new freque ncy. The
HFINTOSC clock will stabilize within 1 ms. 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 af fected by the
change in frequency.
The Low-Frequency Internal Oscillator (LFINTOSC) is
an uncalibrated (approximate) 31 kHz internal clock
source.
The output of the LFINTOSC connects to a postscaler
and multiplexer (see Figure4-1). 31 kHz can be
selected via software using the IRCF bits (see
Section 4.4.4 “Frequency Select Bits (IRCF)”). The
LFINTOSC is also the frequency for the Power-up
Timer (PWRT), Watchdog Timer (WDT) and Fail-Safe
Clock Monitor (FSCM).
The LFINTOSC is enabled by selecting 31 kHz
(IRCF = 000) as the Sys tem Cloc k So urce ( SCS = 1),
or when any of the following are enabled:
• Two-Speed Start-up (IESO = 1 and IRCF = 000)
• Power-up Timer (PWRT)
• Watchdog Timer (WDT)
• Fail-Safe Clock Monitor (FSCM)
• Selected as LCD module clock sourc e
The LF Internal Oscillator (LTS) bit (OSCCON<1>)
indicates whether the LFINTOSC is stable or not.
4.4.4FREQUENCY SELECT BITS (IRCF)
The output of the 8 MHz HFINTOSC and 31 kHz
LFINTOSC connect to a postscaler and multiplexer
(see Figure 4-1). The Internal Oscillator Frequency
select bits, IRCF<2:0> (OSCCON<6:4>), select the
frequency output of the internal oscillators. One of eight
frequencies can be selected via software:
•8 MHz
• 4 MHz (Default after Reset)
•2 MHz
•1 MHz
• 500 kHz
• 250 kHz
• 125 kHz
•31 kHz
Note:Following any Reset, t he IRCF bit s are set
to ‘110’ and the frequency selection is set
to 4 MHz. The user can modify the IRCF
bits to select a different frequency.
4.4.5HF AND LF INTOSC CLOCK
SWITCH TIMING
When switching between the LFINTOSC and the
HFINTOSC, the new oscillator may already be shut
down to save power. If this is the case, there is a 10 μs
delay after the IRCF bits are modified before the
frequency selection takes place. The LTS/HTS bits will
reflect the current active status of the LFINTOSC and
the HFINTOSC oscillators. The timing of a frequency
selection is as follows:
1.IRCF bits are modified.
2.If the new clock is shut down, a 10 μs clock
start-up delay is started.
3.Clock switch circuitry waits for a falling edge of
the current clock.
4.CLKO is held low and the clock switch circuitry
waits for a rising edge in the new clock.
5.CLKO is now connected with the new clock.
HTS/LTS bits are updated as required.
6.Clock switch is complete.
If the internal oscillator speed selected is between
8 MHz and 125 kHz, there is no start-up delay before
the new frequency is selected. This is because the old
and the new frequencies are derived from the
HFINTOSC via the postscaler and multiplexer.
4.5Clock Switching
The system clock source can be switched between
external and internal clock sources via software using
the System Clock Select (SCS) bit.
4.5.1SYS TEM CLOCK SELECT (SCS) BIT
The System Clock Select (SCS) bit (OSCCON<0>)
selects the system clock source that is used for the
CPU and peripherals.
• When SCS = 0, the system clock source is
determined by configuration of the FOSC<2:0>
bits in the Configuration Word register (CONFIG).
• When SCS = 1, the system clock source is
chosen by the internal oscillator frequency
selected by the IRCF bits. After a Reset, SCS is
always cleared.
Note:Any automatic clock switch, which may
occur from Two-Speed Start-up or
Fail-Safe Clock Monitor, does not update
the SCS bit. The user can monitor the
OSTS (OSCCON<3>) to determine the
current syste m clock source.
The Oscillator Start-up Time-out Status (OSTS) bit
(OSCCON<3>) indicates whether the system clock is
running from the external clock source, as defined by
the FOSC bits, or from the internal clock source. In
particular, OSTS indicates that the Oscillator Start-up
Timer (OST) has timed out for LP, XT or HS modes.
4.6Two-Speed Clock Start-up Mode
Two-Speed Start-up mode provides additional power
savings by minimizing the latency between external
oscillator start-up and code execution. In applications
that make heavy us e of the Sleep mode, Two-Speed
Start-up will remove the external oscillator start-up
time from the time spent awake and can reduce the
overall power consumption of the device.
This mode allows the application to wake-up from
Sleep, perform a few instructions using the INTOSC
as the clock source and go back to Sleep without
waiting for the primary oscillator to become stable.
Note:Executing a SLEEP instruction will abort
the oscillator start-up time and will cause
the OSTS bit (OSCCON<3>) to remain
clear.
When the PIC16F917/916/914/913 is configured for
LP, XT or HS modes, the Oscillator Start-up Timer
(OST) is enabled (see Section 4.3.1 “OscillatorStart-up Timer (OST)”). The OST timer will suspend
program execution until 1024 oscillations are counted.
Two-Spe ed Start-up mode minimize s the del ay in cod e
execution by operating from the internal oscillator as
the OST is counting. When the OST count reaches
1024 and the OSTS bit (OSC CO N<3 >) is se t, program
execution switches to the external oscillator.
4.6.2TWO-SPEED START-UP
SEQUENCE
1.Wake-up from Power-on Reset or Sleep.
2.Instructions begin execution by the internal
oscillator at the frequency set in the IRCF bits
(OSCCON<6:4>).
3.OST enabled to count 1024 clock cycles.
4.OST timed out, wait for falling edge of the
internal oscillator.
5.OSTS is set.
6.System clock held low until the next fal ling edg e
of new clock (LP, XT or HS mode).
7.System clock is switched to external clock
source.
4.6.3CHE CKING EXTERNAL/I NTERNAL
CLOCK STATUS
Checking the state of the OSTS bit (OSCCON<3>) will
confirm if the PIC16F917/916/914/913 is running from
the extern al c lo c k so urc e as d e f in ed by t h e FO SC b its
in the Configuration Word (CONFIG) or the internal
oscillator.
4.6.1TWO-SPEED START-UP MODE
CONFIGURATION
Two-Speed Start-up mode is configured by the
following settings:
• IESO = 1 (CONFIG<10>) Internal/External
Switchover bit.
•SCS = 0.
• FOSC configured for LP, XT or HS mode.
Two-Speed Start-up mode is entered after:
• Power-on Reset (PO R) and, if enabled, after
PWRT has expired, or
• Wake-up from Sleep.
If the external clock oscillator is configured to be anything
other than LP, XT or HS mode, then Two-Speed Start-up
is disabled. This is because the external clock oscillator
does not require any stabilization time aft er POR or an
exit from Sleep.
The Fail-Safe Clock Monitor (FSCM) is designed to
allow the device to continue to operate in the event of
an oscillator failure. The FSCM can detect oscillator
failure at any point after the device has exited a Reset
or Sleep condition and the Oscillator Start-up Timer
(OST) has expired.
FIGURE 4-8:FSCM BLOCK DIAGRAM
The frequency of the internal oscillator will depend upon
the value contained in the IRCF bits (OSCCON<6:4>).
Upon entering the Fail-Safe condition, the OSTS bit
(OSCCON<3>) is automatically cleared to reflect that
the internal oscillator is active and the WDT is cleared.
The SCS bit (OSCCON<0>) is not updated. Enabling
FSCM does not affect the LTS bit.
The FSCM sample clock is generated by dividing the
INTOSC clock by 64. This will allow enough time
between FSCM sample clocks for a system clock edge
to occur. Figure 4-8 shows the FSCM block diagram.
On the rising edge of the sample clock, a monitoring
latch (CM = 0) will be cleared. On a falling edge of the
primary system clock, the monitoring latch will be set
(CM = 1). In the event that a fal lin g e dge o f th e s am pl e
clock occurs, and the m onitoring latch is n ot set, a clock
failure has been detected. The assigned internal
oscillator is enabled when FSCM is enabled as
reflected by the IRCF.
The FSCM function is enabled by setting the FCMEN
bit in the Config uration W ord (CO NFIG). It is applic able
to all external clock options (LP, XT, HS, EC or RC
modes).
In the event of an external clock failure, the FSCM will
set the OSFIF bit (PIR2< 7>) and g enerate an os cilla tor
fail interrupt if the OSFIE bit (PIE2<7>) is set. The
device will then switch the system clock to the internal
oscillator. The system clock will continue to come from
the internal oscill ator unless the external clock recovers
and the Fail-Safe condition is exited.
The Fail-Sa fe conditi on is cle ared afte r a Reset, t he
execution of a SLEEP instruction, or a modi fication of
the SCS bit. While in Fail-Safe condition, the
PIC16F91X uses the internal oscillator as the system
without exiting the Fail-Safe condition.
The Fail-Safe condition must be cleared before the
OSFIF flag can be cleared.
FIGURE 4-9:FSCM TIMING DIAGRAM
Sample Clock
System
Clock
Oscillator
Failure
Output
CM Output
(Q)
Failure
Detected
OSCFIF
CM Test
CM T estCM Test
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.
4.7.2RESET OR WAKE-UP FROM SL EE P
The FSCM is designed to detect oscillator failure at
any point after the device has exited a Reset or Sleep
condition and the Oscillator Start-up Timer (OST) has
expired. If the external clock is EC or RC mode,
monitoring will begin immediately following these
events.
For LP, XT or HS mode the external oscillator may
require a start-up time considerably longer than the
FSCM sample clock time, a false clock failure may be
detected (see Figure 4-9). To prevent this, the internal
oscillator is automatically configured as the system
clock and functions until the external clock is stable
(the OST has timed out). This is identical to
Two-Speed Start-up mode. Once the external
oscillator is stabl e, the LF INTOSC returns to its role as
the FSCM source.
Note:Due to the wide range of oscill ator st art-up
times, the Fail-Safe circuit is not active
during oscillator start-up (i.e., after exiting
Reset or Sleep). After an appropriate
amount of time, the u se r sho uld check the
OSTS bit (OSCCON<3>) to verify the
oscillator start-up and system clock
switchover has successfully completed.
TABLE 4-2:SUMMARY OF REGISTERS ASSOCIATED WITH CLOCK SOURCES
AddrNameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
8FhOSCCON
90hOSCTUNE
(1)
2007h
Legend:x = unknown, u = unchanged,
Note 1:See Register 16-1 for operation of all Configuration Word bits.
- = unimplemented locations read as ‘0’. Shaded cells are not used by oscillators.
(2)
HTSLTSSCS-110 q000 -110 x000
Val ue on:
POR, BOR
Value on
all other
Resets
PIC16F917/916/914/913
5.0TIMER0 MODULE
The Timer0 module timer/counter has the following
features:
• 8-bit timer/counter
• Readable and writable
• 8-bit software programmable prescaler
• Internal or external clock select
• Interrupt on overflow from FFh to 00h
• Edge select for external clock
Figure 5-1 is a block diagram of th e Ti mer0 module and
the prescaler shared with the WDT.
Note:Additional information on the Timer0
module is available in the “PICmicro
Mid-Range MCU Family Reference
Manual” (DS33023).
5.1Timer0 Operation
Timer mode is selected by clearing the T0CS bit
(OPTION_REG<5>). In Timer mode, the Timer0
module will increment every instruction cycle (without
prescaler). If TMR0 is written , the increment is inhibited
for the following two instruction cycles. The user can
work around this by writing an adjusted value to the
TMR0 register.
Counter mode is selected by setting the T0CS bit
(OPTION_REG<5>). In this mode, the Timer0 module
will increment either on every rising or falling edge of pin
RA4/C1OUT/T0CKI/SEG4. The incrementing edge is
determined by the source edge (T0SE) control bit
(OPTION_REG<4>). Clearing the T0SE bit selects the
rising edge.
Note:Counter mode has specific external clock
requirements. Additional information on
these requirements is available in the
®
“PICmicro
Mid-Range MCU Family
Reference Manual” (DS33023).
5.2Timer0 Interrupt
®
A Timer0 interrupt is generated when the TMR0
register timer/counter overflows from FFh to 00h. This
overflow sets the T0IF bit (INTCON<2>). The interrupt
can be masked by clearing the T0IE bit (INTCO N<5 >).
The T0IF bit must be cleared in softwa re by the T i mer0
module Interrupt Service Routine before re-enabling
this interrupt. The Timer0 interrupt cannot wake the
processor from Sleep, si nc e th e timer is shut off during
Sleep.
FIGURE 5-1:BLOCK DIAGRAM OF THE TIMER0/WDT PRESCALER
CLKO
(= FOSC/4)
0
1
1
T0CKI
pin
T0SE
WDTE
SWDTEN
31 kHz
INTOSC
Note:T0SE, T0CS, PSA and PS<2:0> are bits in the Option register; WDTPS<3:0> are bits in the WDTCON register.
When no prescaler is used, the external clock input is the
same as the prescaler output. The synchronization of
T0CKI, with the internal phase clocks, is accomplished by
sampling the prescaler output on the Q2 and Q4 cycles of
the internal phase clocks. Therefore, it is necessar y for
T0CKI to be high for at least 2 T
of 20 ns) and low for at least 2 T
of 20 ns). Refer to the electrical specification of the
desired device.
REGISTER 5-1:OPTION_REG – OPTION REGISTER (ADDRESS: 81h OR 181h)
OSC (and a small RC delay
OSC (and a small RC delay
R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1
RBPU
bit 7bit 0
INTEDGT0CST0SEPSAPS2PS1PS0
bit 7RBPU
bit 6INTEDG: Interrupt Edge Select bit
bit 5T0CS: TMR0 Clock Source Select bit
bit 4T0SE: TMR0 Source Edge Select bit
bit 3PSA: Prescaler Assignment bit
bit 2-0PS<2:0>: Prescaler Rate Select bits
: PORTB Pull-up Enable bit
1 = PORTB pull-ups are disabled
0 = PORTB pull-ups are enabled by individual port latch values in WPUA register
1 = Interrupt on rising edge of RB0/INT/SEG0 pin
0 = Interrupt on falling edge of RB0/INT/SEG0 pin
An 8-bit counter is available as a prescaler for the
Timer0 module, or as a postscaler for the Watchdog
Timer. For simplicity, this counter will be referred to as
“prescaler” throughout this data sheet. The prescaler
assignment is controlled in software by the control bit
PSA (OPTION_REG<3>). Clearing the PSA bit will
assign the prescaler to Timer0. Prescale values are
selectable via the PS<2:0> bits (OPTION_REG<2:0>).
The prescaler is not readable or writable. When
assigned to the Timer0 module, all instructions writing
to the TMR0 register (e.g., CLRF 1, MOVWF 1,
BSF 1, x....etc.) will clear the prescaler. When
assigned to WDT, a CLRWDT instruction will clear the
prescaler along with the Watchdog Timer.
5.4.1SWITCHING PRESCALER
ASSIGNMENT
The prescaler assignment is fully under software control
(i.e., it can be changed “on-the-fly” during program
execution). To avoid an unintended device Reset, the
following instruction sequence (Example 5-1 and
Example 5-2) must be executed when changing the
prescaler assignment from Timer0 to WDT.
EXAMPLE 5-1:CHANGING PRESCALER
(TIMER0 → WDT)
BCFSTATUS,RP0;Bank 0
CLRWDT;Clear WDT
CLRFTMR0;Clear TMR0 and
; prescaler
BSFSTATUS,RP0;Bank 1
MOVLWb’00101111’;Required if desired
MOVWFOPTION_REG; PS2:PS0 is
CLRWDT; 000 or 001
;
MOVLWb’00101xxx’;Set postscaler to
MOVWFOPTION_REG; desired WDT rate
BCFSTATUS,RP0;Bank 0
To change prescaler from the WDT to the TMR0
module, use the se quence sh own in Examp le 5-2. This
precaution must be t aken even if the WDT is disabled.
• 16-bit asynchronous counter
In Timer mode, Timer1 is incremented on every
instruction cycle. In Counter mode, Timer1 is incremented
on the rising edge of the external clock input T1CKI. In
addition, the Counter mode clock can be synchronized to
the microcontroller system clock or run asynchronously .
In the Timer1 modu le, the m odule cl ock can b e gated
by the T imer1 gate, which can be select ed as eithe r the
pin or Comparator 2 output.
T1G
If an external clock oscillator is needed (and the
microcontroller is using the INTOSC without CLKO),
Timer1 can use the LP oscillator as a clock source.
Note:In Counter mode, a falling edge must be
registered by the counter prior to the first
incrementing rising edge.
6.2Timer1 Interrupt
The Timer1 register pair (TMR1H:TMR1L) increments
to FFFFh and rolls over to 0000h. When Timer1 rolls
over, the Timer1 Interrupt Flag bit (PIR1<0>) is set. To
enable the inte rrupt on rollo ver , you must set these bits :
• Timer1 Interrupt Enable bit (PIE1<0>)
• PEIE bit (INTCON<6>)
• GIE bit (INTCON<7>)
The interrupt is cleared by clearing the TMR1IF bit in
the Interrupt Service Routine.
Note:The TMR1H:TTMR1L register p air an d the
TMR1IF bit should be cleared before
enabling interrupts.
6.3Timer1 Prescaler
Timer1 has four prescaler options allowing 1, 2, 4 or 8
divisions of the clock input. The T1CKPS bits
(T1CON<5:4>) control the prescale counter. The
prescale counter is not directly readable or writable;
however, the prescaler counter is cleared upon a write
to TMR1H or TMR1L.
6.4Timer1 Gate
Timer1 gate source is software configurable to be the
T1G
pin or the output of Comp ara t or 2. This all ows th e
device to directly time external events using T1G
analog events using Comparator 2. See CMCON1
(Register 8-2) for selecting the Timer1 gate source.
This feature can si mplify the softwa re for a Delta-Si gma
A/D converter and many other applications. For more
information on Delta-Sigma A/D converters, see the
Microchip web site (www.microchip.com).
Note:T1GE bit (T1CON<6>) must be set to use
either T1G
source. See Register 8-2 for more
information on selecting the Timer1 gate
source.
Timer1 gate can be inverted using the T1GINV bit
(T1CON<7>), whether it origin ates fro m the T1G
Comparator 2 output. This configures Timer1 to
measure either the active-high or active-low time
between events.
or C2OUT as the Timer1 gate
or
pin or
FIGURE 6-2:TIMER1 INCREMENTING EDGE
T1CKI = 1
when TMR1
Enabled
T1CKI = 0
when TMR1
Enabled
Note 1:Arrows indicate counter increments.
2: In Counter mode, a falling edge must be registered by the counter prior to the first incrementing
If control bit T1SYNC (T1CON<2>) is set, the external
clock input is not synchronized. The timer continues to
increment asynchronous to the internal phase clocks.
The timer will continue to run during Sleep and can
generate an interrupt-on-overflow, which will wake-up
the processor. However, special precautions in
software are needed to read/write the timer (see
Section 6.5.1 “Reading and Writing Timer1 in
Asynchronous Counter Mode”).
Note:The ANSEL (91h) and CMCON0 (9Ch)
registers must be initia lized to configu re an
analog channel as a digital input. Pins
configured as analog inputs will read ‘0’.
6.5.1READING AND WRITING TIMER1 IN
ASYNCHRONOUS COUNTER
MODE
Reading TMR1H or TMR1L, while the timer is running
from an external asynchronous clock, will ensure a
valid read (taken care of in hardware). However, the
user should keep i n mind that r eadin g the 16-bit timer
in two 8-bit values it self, poses certain problems, since
the timer may overflow between the reads.
For writes, it is recomm ended that the us er simply stop
the timer and write the desired values. A write
contention may occur by writing to the timer registers,
while the register is incrementi ng. This may pro duce an
unpredictable value in the timer r egister.
Reading the 16-bit value requires some care.
Examples in the “PICmicroReference Manual” (DS33023) show how to read and
write Ti mer1 wh en it i s runni ng in Async hronou s mode.
®
Mid-Range MCU Family
6.6TIMER1 OSCILLATOR
To minimize the multiplexing of peripherals on the I/O
ports, the dedicated TMR1 oscillator, which is normally
used for TMR1 real-time clock applications, is eliminated.
Instead, the TMR1 module can enable the LP oscillator.
If the microcontroller is programmed to run from
INTOSC with no CLKO or LP oscillator:
1.Setting the T1OSCEN and TMR1CS bits to ‘1’
will enable the LP oscilla tor to clock TMR 1 while
the microcontroller is clocked from either the
INTOSC or LP oscillator. Note that the T1OSC
and LP oscillators share the same circuitry.
Therefore, when LP oscillator is selected and
T1OSC is enabled, both the micro contro lle r and
the Timer1 module share the same clock
source.
2.Sleep mode does not shut off the LP oscillator
operation (i.e., if the INTOSC oscillator runs the
microcontroller, and T1OSCEN = 1 (TMR1 is
running from the LP oscillator), then the LP
oscillator will continue to run during Sleep mode.
In all oscillator modes except for INTOSC with no
CLKOUT and LP, the T1OSC enable option is unavailable and is ignored.
Note:When INTOSC without CLKO oscillator is
selected and T1OSCEN = 1, the LP
oscillator will run continuously independent
of the TMR1ON bit.
6.7Resetting Timer1 Using a CCP
Trigger Output
If the CCP1 or CCP2 module is config ured in C omp are
mode to generate a “special event trigger”
(CCP1M<3:0> = 1011), this signal will reset Timer1.
Note:The special event triggers from the CCP1
and CCP2 modules will not set interrupt
flag bit, TMR1IF (PIR1<0>).
Timer1 mu st be confi gured for eit her T ime r or Synchronized Counter mode to take advantage of this feature.
If Timer1 is running in Asynchronous Counter mode,
this Reset operation may not work.
In the event that a write to Timer1 coincides with a
special event trigger from CCP1 or CCP2, the write will
take precedence.
In this mode of operation, the CCPRxH:CCPRxL register
pair effectively becomes the period register for Timer1.
6.8Resetting of Timer1 Register Pair
(TMR1H, TMR1L)
TMR1H and TMR1L registers are not rese t to 00h on a
POR, or any other Reset, except by the CCP1 and
CCP2 special event triggers.
T1CON register is reset to 00h on a Power-on Reset,
or a Brown-out Reset, which shuts off the timer and
leaves a 1:1 prescale. In all other Resets, the register
is unaffected.
6.9Timer1 Operation During Sleep
Timer1 can only operate during Sleep when setup in
Asynchronous Count er mode. In this mode, a n external
crystal or clock source can be used to increment the
counter. To set up the timer to wake the device:
• Timer1 must be on (T1CON<0>)
• TMR1IE bit (PIE1<0>) must be set
• PEIE bit (INTCON<6>) must be set
The device will wake-up on an overflow. If the GIE bit
(INTCON<7>) is set, the device will wake-up and jump
to the Interrupt Service Routine (0004h) on an overflow .
If the GIE bit i s clear, execution wi ll continue w ith the
next instruction.
PIC16F917/916/914/913
TABLE 6-1:REGISTERS ASSOCIATED WITH TIMER1
AddrNameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
0Bh/
INTCONGIEPEIE
8Bh
0ChPIR1
0EhTMR1LHolding Register for the Least Significant Byte of the 16-bit TMR1 Registerxxxx xxxx uuuu uuuu
0FhTMR1HHolding Register for the Most Significant Byte of the 16-bit TMR1 Registerxxxx xxxx uuuu uuuu
10hT1CONT1GINVT1GET1CKPS1 T1CKPS0 T1OSCEN T1SYNC
1AhCMCON1
8ChPIE1
Legend: x = unknown, u = unchanged,
• Interrupt on TMR2 match with PR2
Timer2 has a control register shown in Register 7-1.
TMR2 can be shut-off by clearing control bit TMR2ON
(T2CON<2>) to minimize power consumption.
Figure 7-1 is a simplified block diagram of the Timer2
module. The prescaler and postscaler selection of
Timer2 are controlled by this register.
7.1Timer2 Operation
Timer2 can be used as the PWM time base for the
PWM mode of the CCP module. The TMR2 register is
readable and writable, and is cleared on any device
Reset. The in put cl ock (F
of 1:1, 1:4 or 1:16, selected by control bits T2CKPSx
(T2CON<1:0>). The match output of TMR2 goes
through a 4-bit postscaler (which gives a 1:1 to 1:16
scaling inclusive) t o generate a TMR 2 interrupt (latc hed
in flag bit TMR2IF, (PIR1<1>)).
The prescaler and postscaler counters are cleared
when any of the following occurs:
• A write to the TMR2 register
• A write to the T2CON register
• Any device Reset (Power-on Reset, MCLR
Watchdog Timer Reset, or Brown-out Reset)
TMR2 is not cleared when T2CON is written.
OSC/4) has a prescale option
REGISTER 7-1:T2CON – TIMER2 CONTROL REGISTER (ADDRESS: 12h)
The Timer2 module has an 8-bit period register, PR2.
Timer2 increments from 00h until it matches PR2 and
then resets to 00h on the next increment cycle. PR2 is
a readable and writable register. The PR2 register is
initialized to FFh upon Reset.
FIGURE 7-1:TIMER2 BLOCK DIAGRAM
OSC/4
F
Prescaler
1:1, 1:4, 1:16
2
T2CKPS<1:0>
TMR2
Comparator
PR2
7.3Timer2 Output
The output of TMR2 (before the post scaler) is fed to the
SSP module, which optionally uses it to generate the
shift clock.
Sets Flag
bit TMR2IF
Reset
Postscaler
1:1 to 1:16
EQ
TOUTPS<3:0>
TMR2
Output
4
(1)
Note 1: TMR2 register output can be software s elec ted by the SSP module as a baud cloc k.
TABLE 7-1:REGISTERS ASSOCIATED WITH TIMER2
AddrNameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
0Bh/
INTCONGIEPEIE
8Bh
0ChPIR1
11hTMR2Holding Register for the 8-bit TMR2 Register0000 0000 0000 0000
12hT2CON
8ChPIE1
92hPR2Timer2 Period Register1111 1111 1111 1111
Legend:x = unknown, u = unchanged, - = unimplemented, read as ‘0’. Shaded c ells are not used by the Timer2 module.
The CMCON0 register (Register 8-1) controls the
comparator input and output multiplexers. A block
The comparator module contains two analog
comparators. The inputs to the comparators are
diagram of the various comparator configurations is
shown in Figure 8-3.
multiplexed with I/O port pins RA<3:0>, while the outputs
are multiplexed to pins RA<5:4>. An on-chip Comparator
Voltage Reference (CVRE F) can also be applied to the
inputs of the comparators.
REGISTER 8-1:CMCON0 – COMP ARA TO R CONFIGURA TION REGISTER (ADDRESS: 9Ch)
R-0R-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0
C2OUTC1OUTC2INVC1INVCISCM2CM1CM0
bit 7bit 0
bit 7C2OUT: Comparator 2 Output bit
When C2INV
1 =C2 V
0 =C2 V
When C2INV
0 =C2 V
1 =C2 V
bit 6C1OUT: Comparator 1 Output bit
When C1INV = 0:
1 =C1 V
0 =C1 V
When C1INV
0 =C1 V
1 =C1 V
bit 5C2INV: Comparator 2 Output Inversion bit
1 = C2 Output inverted
0 = C2 Output not inverted
bit 4C1INV: Comparator 1 Output Inversion bit
1 = C1 Output inverted
0 = C1 Output not inverted
bit 3CIS: Comparator Input Switch bit
When CM<2:0>
1 =C1 V
0 =C1 V
When CM<2:0>
1 =C1 V
0 =C1 V
When CM<2:0>
1 =C2 V
0 =C2 V
bit 2-0CM<2:0>: Comparator Mode bits
See Figure 8-3 for comparator modes and CM<2:0> bit settings.
Note 1: Setting a pin to an analog input automatically disables the digital input circuitry,
= 0:
IN+ > C2 VIN-
IN+ < C2 VIN-
= 1:
IN+ > C2 VIN-
IN+ < C2 VIN-
IN+ > C1 VIN-
IN+ < C1 VIN-
= 1:
IN+ > C1 VIN-
IN+ < C1 VIN-
= 010:
IN- connects to RA3/AN3/C1+/VREF+/SEG15
C2 V
IN- connects to RA2/AN2/C2+/VREF-/COM2
IN- connects to RA0/AN0/C1-/SEG12
IN- connects to RA1/AN1/C2-/SEG7
C2 V
= 001:
IN- connects to RA3/AN3/C1+/VREF+/SEG15
IN- connects to RA0/AN0/C1-/SEG12
= 101:
IN+ connects to internal 0.6V reference
IN+ connects to RA2/AN2/C2+/VREF-/COM2
(1)
weak pull-ups, and interrupt-on-change if available. The corresponding TRIS bit
must be set to Input mod e in order to allow exte rnal control of the volt age on the pin.
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
A single comparator is shown in Figure 8-1 along with
the relationship between the analog input levels and
the digital output . When the analo g input at V
than the analog input V
IN-, the output of the compara tor
is a digital low level. When the analog input at V
greater than the analog input V
IN-, the output of the
comparator is a digital high level. The shaded areas of
the output of the comparator in Figure 8-1 represent
the uncertainty due to input offsets and response time.
Note:To use CIN+ and CIN- pins as analog
inputs, the appropriate bits must be
programmed in the CMCON0 (9Ch)
register.
The polarity of the comparator output can be inverted
by setting the CxINV bits (CMCON0<5:4>). Clearing
CxINV results in a non-inverted output. A complete
table showing the output state versus input conditions
and the polarity bit is shown in Table 8-1.
TABLE 8-1:OUTPUT STATE VS. INPUT
CONDITIONS
Input ConditionsCINVCxOUT
IN- > VIN+00
V
VIN- < VIN+01
VIN- > VIN+11
IN- < VIN+10
V
IN+ is less
IN+ is
FIGURE 8-1:SINGLE COMPARATOR
VIN+
VIN-
VIN-
V
IN–
VIN+
V
IN+
Output
utput
+
Output
–
8.2Analog Input Connection
Considerations
A simplified circuit for an analog input is shown in
Figure 8-2. Since the analog pins are connected to a
digital output, they have reverse biased diodes to VDD
and VSS. The analog input, th erefore, must be b etween
SS and VDD. If the input voltage deviates from this
V
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 i mpedance of 10 k Ω is recommended
for the analog sources. Any external component
connected to an analog inpu t pin , suc h as a capaci tor
or a Zener diode, should have very little leakage.
FIGURE 8-2:ANALOG INPUT MODEL
Rs < 10K
A
IN
VA
Legend: CPIN = Input Capacitance
V
LEAKAGE= Leakage Current at the pin due to various junctions
There are eight modes of operation for the comparators.
The CMCON0 register is used to select these modes.
If the Comparator mode is changed, the comparator
output level may not be valid for the specified mode
change delay shown in Section 19.0 “ElectricalSpecifications”.
Figure 8-3 shows the eight possible modes.
Note:Compara tor in terru pts shou ld be disab led
bit 7-2:Unimplemented: Read as ‘0’
bit 1T1GSS: Timer1 Gate Source Select bit
1 = Timer1 gate source is T1G
0 = Timer1 gate source is Comparator 2 Output
bit 0C2SYNC: Comparator 2 Synchronize bit
1 = C2 output synchronized with falling edge of Timer1 clock
0 = C2 output not synchronized with Timer1 clock
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
- n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
pin (RC4 must be configured as digital input)
8.4Comparator Outputs
The comparator outputs are read through the
CMCON0 register. These bits are read-only. The
comparator outputs may also be directly output to the
RA4 and RA5 I/O pins. When enabled, multiplexers in
the output path of the RA4 and RA5 pins wi ll switch
and the output of each pin will be the unsynchronized
output of the com parator. The uncertainty of each of
the comparators is related to t he input offset vo ltage
and the response time given in the specifications.
Figure 8-4 and Figure 8-5 show the output block
diagram for Comparator 1 and 2.
The TRIS bits will still function as an output
enable/disable for the RA4 and RA5 pins while in this
mode.
The polarity of the comparator outputs can be changed
using the C1INV and C2INV bits (CMCON0<5:4>).
Timer1 gate source can be configured to use the T1G
pin or Comparator 2 output as selected by the T1GSS
bit (CMCON1<1>). This feature can be used to time
the duration or interval of analog events. The output of
Comparator 2 can also be synchronized with Timer1
by setting the C2SYNC bit (CMCON1<0>). When
enabled, the output of Comparator 2 is latched on the
falling edge of Timer1 clock source. If a prescaler is
used with Timer1, Comparator 2 is latched after the
prescaler. To prevent a race condition, the Comparator
2 output is latched on the falling edge of the Timer1
clock source and Timer1 increments on the rising edge
of its clock source. See (Figure 8-5), Comparator 2
Block Diagram and (Figure 6-1), Timer1 Block
Diagram for more information.
It is recommended to synchronize Comparator 2 with
Timer1 by setting the C2SYNC bit when Comparator 2
is used as the Timer1 gate source. This ensures Timer1
does not mi ss an inc rement if Comparator 2 changes
during an increment.
8.5 Comparator Interrupts
The comparator interrupt flags are set whenever there is
a change in the outp ut value of it s respective c omparator .
Software will need to maintain information about the
status of the output bi ts, as re ad from C MCON0< 7:6>, to
determine the a ctual chan ge that has occurred. The CxIF
bits, PIR2<6:5>, ar e the Comp arat or In terrupt flags. Th is
bit must be reset in softwar e by clearing it to ‘0’. Sinc e it
is also possibl e t o w rit e a ‘1’ to this register, a simulated
interrupt may be initiated.
The CxIE bits (PIE2<6:5>) and the PEIE bit
(INTCON<6>) must be set to enable the interrupts. In
addition, the GIE bit must also be set. If any of these
bits are cleared, th e interrupt is n ot enabled, th ough the
CxIF bits will still be se t if an interru pt condi tion occ urs.
The user , in the Interru pt Service Routi ne, can cle ar the
interrupt in the following manner:
a) Any read or write of CMCON0. This will end the
mismatch condition.
b) Clear flag bit CxIF
A mismatch condition will continue to set flag bit CxIF.
Reading CMCON0 w ill end the m ismatch co ndition and
allow flag bits CxIF to be cleared.
Note:If a change in the CMCON0 register
(CxOUT) should occur when a read
operation is bein g executed (st art of the Q2
cycle), then the CxIF (PI R2<6:5>) interru pt
flag may not get set.
The comparato r m od ule al so allows the selection of an
internally generated voltage reference for one of the
comparator inputs. The VRCON register, Register 8-3,
controls the voltage reference module shown in
Figure 8-6.
8.6.1CONFIGURING THE VOLTAGE
REFERENCE
The voltage reference can output 32 distinct voltage
levels; 16 in a high range and 16 in a low range.
The following equation determines the output volt ages:
The full range of VSS to VDD cannot be realized due to
the construction of the module. The transistors on the
top and bottom of the resistor ladder network
(Figure 8-6) keep CV
DD. The exception is when the module is disabled by
V
clearing the VREN bit (VRCON< 7>). When disabled,
the reference voltage is VSS when VR<3:0> = 0000.
This allows the comparators to detect a zero-crossing
and not consume CV
The voltage referen ce is V
CV
REF output changes with fluctuations in VDD. The
tested absolute accuracy of the comparator voltage
reference can be found in Section 19.0 “ElectricalSpecifications”.
REF from approaching VSS or
REF module current.
DD derived and theref ore, the
FIGURE 8-6:COMPARATOR VOLTAGE REFERENCE BLOCK DIAGRAM