- Software selectable frequency range of
8 MHz to 32 kHz
- Software tunable
- Two-Speed Start-up mode
- Crystal fail detect for critical applications
- Clock mode switching during operation for
power savings
• Power-Saving Sleep mode
• Wide Operating Voltage Range (2.0V-5.5V)
• Industrial and Extend ed Temperature Range
• Power-on Reset (POR)
• Power-up Timer (PWRTE) and Oscillator Start-up
Timer (OST)
• Brown-out Reset (BOR) with Software Control
Option
• Enhanced Low-Current Watchdog Timer (WDT)
with On-Chip Oscillator (Sof t wa re sel ec t ab le
nominal 268 Seconds with Full Prescaler) with
Software Enable
• Multiplexed Master Clear/Input Pin
• Programmable Code Protection
• High Endurance Flash/EEPROM Cell:
- 100,000 write Flash endurance
- 1,000,000 write EEPROM endurance
- Flash/Data EEPROM retention: > 40 years
• Enhanced USART Module:
- Supports RS-485, RS-232 and LIN 2.0
- Auto-Baud Detect
- Auto-wake-up on Start bit
Low-Power Features
• Standby Current:
- 50 nA @ 2.0V, typical
• Operating Current:
-11A @ 32 kHz, 2.0V, typical
-220A @ 4 MHz, 2.0V, typical
• Watchdog Timer Current:
-<1A @ 2.0V, typical
Peripheral Features
• 17 I/O Pins and 1 Input-Only Pin:
- High current source/sink for direct LED drive
- Interrupt-on-Change pin
- Individually programmable weak pull-ups
- Ultra Low-Power Wake-up (ULPWU)
• Analog Compar ator Module with:
- Two analog comparators
- Programmable on-chip voltage reference
REF) module (% of VDD)
(CV
- Comparator inputs and outputs externally
accessible
- SR Latch mode
- Timer 1 Gate Sync Latch
- Fixed 0.6V V
• A/D Converter:
- 10-bit resolution and 12 channels
• Timer0: 8-Bit Timer/Counter with 8-Bit
Programmable Prescaler
• Enhanced Timer1:
- 16-bit timer/counter with prescaler
- External Timer1 Gate (count enable)
- Option to use OSC1 and OSC2 in LP mode
as Timer1 oscillator if INTOSC mode
selected
• Timer2: 8-Bit Timer/Counter with 8-Bit Period
Register, Prescaler and Postscaler
10.0 Data EEPROM and Flash Program Memory Control........ ...... ................. ................. ........ ................. ...................................... 117
14.0 Special Features of th e CPU...................................................... ....................................... ....................................................... 193
15.0 Instruction Set Summary.......................................................................................................................................................... 212
16.0 Development Support............................................................................................................................................................... 221
18.0 DC and AC Characteristics Graphs and Tables............................................................................... ........................................258
The Microchip Web Site.................. ........................................ ........................................................................................................... 295
Customer Change Notification Service .............................................................................................................................................. 295
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 comments regarding this publication, please contact the Marketing Communications Department via Email 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 Web site at:
http://www.microchip.com
You can determine the version of a data sheet by e xamining 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., DS30000000A is version A of document DS30000000).
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 o f
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.
DS40001262F-page 8 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
Power-up
Timer
Oscillator
Start-up Timer
Power-on
Reset
Watchdog
Timer
Brown-out
Reset
Flash
Program
Memory
13
Data Bus
8
14
Program
Bus
Instruction Reg
Program Counter
RAM
File
Registers
Direct Addr
7
RAM Addr
9
Addr MUX
Indirect
Addr
FSR Reg
STATUS Reg
MUX
ALU
W Reg
Instruction
Decode and
Control
Timing
Generation
OSC1/CLKI
OSC2/CLKO
8
8
8
3
8-Level Stack (13-bit)
64 bytes
1K x 14
VDD
INT
Configuration
Internal
Oscillator
MCLR
Block
VSS
2
Timer0Timer1
Analog Comparators
C1IN- C1IN+ C1OUT
and Reference
8
C2IN- C2IN+ C2OUT
T1G T1CKIT0CKI
Data
EEPROM
128 Bytes
EEDAT
EEADR
RB4
RB5
RB6
RB7
PORTA
RA0
RA1
RA2
RA3
RA4
RA5
PORTC
RC0
RC1
RC2
RC3
RC4
RC5
RC6
RC7
PORTB
Ultra Low-Power
Wake-up
ULPWU
1.0DEVICE OVERVIEW
The PIC16F631/677/685/687/689/690 devices are
covered by this dat a sheet. The y are availab le in 20-pin
PDIP, SOIC, TSSOP and QFN packages.
FIGURE 1-1:PIC16F631 BLOCK DIAGRAM
Block Diagrams and pinout descriptions of the devices
are as follows:
The PIC16F631/677/685/687/689/690 has a 13-bit
program counter capable of addressing an 8K x 14
program memory sp ac e. Onl y th e fi rst 1 Kx 14 (0000h03FFh) is physically implemented for the PIC16F631,
the first 2K x 14 (0000h-07FFh) for the PIC16F677/
PIC16F687, and the first 4K x 14 (0000h-0FFFh) for
the PIC16F685/PIC16F689/PIC16F690. Accessing a
location above these boundaries will cause a wraparound. The Reset vector is at 0000h and the interrupt
vector is at 0004h (see Figures 2-1 through 2-3).
FIGURE 2-1:PROGRAM MEMORY MAP
AND STACK FOR THE
PIC16F631
FIGURE 2-2:PROGRAM MEMORY MAP
AND STACK FOR THE
PIC16F685/689/690
FIGURE 2-3:PROGRAM MEMORY MAP
AND STACK FOR THE
PIC16F677/PIC16F687
DS40001262F-page 24 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
2.2 Data Memory Organization
The data memory (see Figures 2-6 through 2-8) is
partitioned into four banks which contain the General
Purpose Registers (GPR) and the Special Function
Registers (SFR). The Special Function Registers are
located in the first 32 locations of each bank. The
General Purpose Registers, implemented as static
RAM, are located in the l ast 96 locat ion s of ea ch Bank .
Register locations F0h-FFh in Bank 1, 170h-17Fh in
Bank 2 and 1F0h-1FFh in Bank 3 point to addresses
70h-7Fh in Bank 0. The actual number of General
Purpose Resisters (GPR) in eac h Bank depends on the
device. Details are shown in Figures 2-4 through 2-8.
All other RAM is unimplemented and returns ‘0’ when
read. RP<1:0> of the STATUS register are the bank
select bits:
RP1
RP0
00Bank 0 is selected
01Bank 1 is selected
10Bank 2 is selected
11Bank 3 is selected
2.2.1GENERAL PURPOSE REGISTER
FILE
The register file is organized as 128 x 8 in the
PIC16F687 and 256 x 8 in the PIC16F685/PIC16F689/
PIC16F690. Each register is accessed, either directly or
indirectly, through the File Select Register (FSR) (see
Section 2.4 “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
through 2-4). These re g is t e rs ar e s tatic 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 sectio n.
Registers related to the operati on of peripheral features
are described in the section of that peri phe ral feature .
2: Address 93h also accesses the SSP Mask (SSPMSK) register under certain conditions.
See Registers 13-2 and 13-3 for more details.
FIGURE 2-8:PI C16F69 0 SPECIA L FUNCTION REGISTERS
DS40001262F-page 30 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
T ABLE 2-1:PIC16F631/677/685/687/689/690 SPECIAL FUNCTION 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 xxxx43,200
01hTMR0Timer0 Module Registerxxxx xxxx79,200
02hPCLProgram Counter’s (PC) Least Significant Byte0000 000043,200
03hSTATUSIRPRP1RP0TO
———Write Buffer for upper 5 bits of Program Counter---0 000043,200
0Bh INTCONGIE PEIET0IEINTERABIET0IFINTFRABIF
0Ch PIR1
0Dh PIR2
—ADIF
(4)
OSFIFC2IFC1IFEEIF————0000 ----41,200
RCIF
(2)
TXIF
(2)
SSPIF
(5)
CCP1IF
(3)
TMR2IF
(3)
TMR1IF -000 000040,200
0Eh TMR1LHolding Register for the Least Significant Byte of the 16-bit TMR1 Registerxxxx xxxx85,200
0FhTMR1HHolding Register for the Most Significant Byte of the 16-bit TMR1 Registerxxxx xxxx85,200
Synchronous Serial Port Receive Buffer/Transmit Registerxxxx xxxx178,200
WCOLSSPOVSSPENCKPSSPM3SSPM2SSPM1SSPM0 0000 0000177,200
Capture/Compare/PW M Regi st er 1 (LSB )xxxx xxxx126,200
Capture/Compare/PW M Regi st er 1 (MSB )xxxx xxxx126,200
2:PIC16F685/PIC16F689/PIC16F690 only.
3:P IC16 F6 77 / PIC 16F 68 5/PI C1 6F 68 7/P IC1 6F 689 /P IC16 F690 only.
4:Port pins with analog functions controlled by the ANSEL and ANSELH registers will read ‘0’ immediately after a Reset even though the
data latches are either undefined (POR) or unchanged (other Resets).
TABLE 2-4:PIC16F631/677/685/687/689/690 SPECIAL FUNCTION REGISTERS SUMMARY BANK 3
(1)
Value on
POR, BOR
0000 000x
Page
36,200
37,200
AddrNameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2 Bit 1Bit 0
Bank 3
180hINDFAddressing this location uses contents of FSR to address data memory (not a physical register) xxxx xxxx43,200
181hOPTION_REGRABPU
182hPCLProgram Counter’s (PC) Least Significant Byte0000 000043,200
183hSTATUSIRPRP1RP0TO
184h FSRIndirect Data Memory Address Pointerxxxx xxxx43,200
185hTRISA
186hTRISBTRISB7TRISB6TRISB5TRISB4
187hTRISCTRISC7TRISC6TRISC5TRISC4TRISC3TRISC2TRISC1TRISC0 1111 111174,201
188h—Unimplemented——
189h—Unimplemented——
18Ah PCLATH
18Bh INTCONGIEPEIET0IEINTERABIET0IFINTFRABIF
18Ch EECON1EEPGD
18Dh EECON2EEPROM Control Register 2 (not a physical register)---- ----117,201
18Eh—Unimplemented——
18Fh—Unimplemented——
190h—Unimplemented——
191h—Unimplemented——
192h—Unimplemented——
193h—Unimplemented——
194h—Unimplemented——
195h—Unimplemented——
196h—Unimplemented——
197h—Unimplemented——
198h—Unimplemented——
199h—Unimplemented——
19Ah—Unimplemented——
19Bh—Unimplemented——
19Ch—Unimplemented——
19Dh PSTRCON
19Eh SRCONSR1SR0C1SENC2RENPULSSPULSR
19Fh—Unimplemented——
Legend:– = Unimplemented locations read as ‘0’, u = unchanged, x = unkno wn, q = value depends on condition, shaded = unimplemented
Note 1:MCLR and WDT Reset does not affect the previous value data latch. The RABIF bit will be cleared upon Reset but will set again if the
———Write Buffer for the upper 5 bits of the Program Counter---0 000043,200
(2)
———WRERRWRENWRRDx--- x000119,201
———STRSYNCSTRDSTRCSTRBSTRA---0 0001144,201
——0000 00--101,201
DS40001262F-page 34 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
2.2.2.1STATUS Register
The STATUS register, shown in Register 2-1, contains:
• the arithmetic status of t he ALU
• the Reset status
• the bank select bits for data memory (GPR and
SFR)
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 r egister as dest ination 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. 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 bit s , s ee Se ction 15.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 REGIS TER
R/W-0R/W-0R/W-0R-1R-1R/W-xR/W-xR/W-x
IRPRP1RP0TO
bit 7bit 0
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
PDZDC
(1)
(1)
C
bit 7IRP: Register Bank Select bit (used for indire ct addressing)
1 = Bank 2, 3 (100h-1FFh)
0 = Bank 0, 1 (00h-FFh)
bit 6-5RP<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)
bit 4TO
bit 3PD
bit 2Z: Zero bit
bit 1DC: Digit Carry/Borrow
bit 0C: Carry/Borrow
: Time-out bit
1 = After power-up, CLRWDT instruction or SLEEP instruction
0 = A WDT time-out occurred
: Power-down bit
1 = After power-up or by th e CLRWDT instruction
0 = By execution of the SLEEP instruction
1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not zero
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 result
(1)
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
(1)
(1)
Note 1: For Borrow
second operand. For rotate (RRF, RLF) instru ctions, t his bit i s loaded w ith eithe r the high -order or low -order
bit of the source register.
The OPTION register, shown in Register 2-2, is a
readable and writable register, which contains various
control bits to configure:
• Timer0/WDT prescaler
• External RA2/INT interrupt
•Timer0
• Weak pull-ups on PORTA/PORTB
REGISTER 2-2:OPTION_REG: OPTION REGISTER
R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1
RABPU
bit 7bit 0
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
INTEDGT0CST0SEPSAPS2PS1PS0
Note:To achieve a 1:1 prescaler assignment for
Timer0, assign the prescaler to the WDT
by setting PSA bit of the OPT IO N r eg i ste r
to ‘1’. See Section 6.3 “Timer1 Pres-
caler”.
bit 7RABPU
: PORTA/PORTB Pull-up Enable bit
1 = PORTA/PORTB pull-ups are disabled
0 = PORTA/PORTB pull-ups are enabled by individual PORT latch values
bit 6INTEDG: Interrupt Edge Select bit
1 = Interrupt on rising edge of RA2/INT pin
0 = Interrupt on falling edge of RA2/INT pin
bit 5T0CS: Timer0 Clock Source Select bit
1 = Transition on R A2/T0CKI pin
0 = Internal in struction cycle clock (FOSC/4)
bit 4T0SE: Timer0 Source Edge Select bit
1 = Increment on high-to-low transition on RA2/T0CKI pin
0 = Increment on low-to-high transition on RA2/T0CKI pin
bit 3PSA: Prescaler Assignment bit
1 = Prescaler is assigned to the WDT
0 = Prescaler is assigned to the Timer0 module
bit 2-0PS<2:0>: Prescaler Rate Select bits
DS40001262F-page 36 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
2.2.2.3INTCON Register
The INTCON register, shown in Register 2-3, is a
readable and writable register , which con tains the va rious
enable and flag bits for TMR0 register overflow, PORTA
change and external RA2/AN2/T0CKI/INT/C1OUT pin
interrupts.
Note:Interrupt flag bits are set when an in terrupt
condition occurs, re gardless of the st ate 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.
REGISTER 2-3:INTCON: INTERRUPT CONTROL REGISTER
R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-x
GIEPEIET0IEINTERABIE
bit 7bit 0
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
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: Timer0 Overfl ow Interru pt Enab le bit
1 = Enables the Timer0 interrupt
0 = Disables the Timer0 interrupt
bit 4INTE: RA2/INT External Interrupt Enable bit
1 = Enables the RA2/INT external interrupt
0 = Disables the RA2/INT external interrupt
bit 3RABIE: PORTA/PORTB Change Interrupt Enable bit
1 = Enables the PORTA/PORTB change interrupt
0 = Disables the PORTA/PORTB change interrupt
bit 2T0IF: Timer0 Overflow Interrupt Flag bit
1 = TMR0 regi ster has overflowed (must be cleared in software)
0 = TMR0 register did not overflow
bit 1INTF: RA2/INT External Interrupt Flag bit
1 = The RA2/INT external interrupt occurred (must be cleared in software)
0 = The RA2/INT external interrupt did not occur
bit 0RABIF: PORTA/PORTB Change Interrupt Flag bit
1 = When at least one of the PORTA or PORTB general purpose I/O pins changed state (must be
cleared in software)
0 = None of the PORTA or PORTB general purpose I/O pins have changed state
(2)
(1,3)
(1,3)
T0IF
(2)
INTFRABIF
Note 1: IOCA or 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
Note:Interrupt flag bits ar e set when an interrupt
condition occurs, regard less of the state of
its corresponding enable bit or the global
enable bit, GIE of the INTCON register.
User software sh ould ensure the appropriate interrupt flag bits are clear prior to
enabling an interrupt.
(3)
SSPIF
(4)
CCP1IF
(4)
(2)
TMR2IF
(1)
TMR1IF
DS40001262F-page 40 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
2.2.2.7PIR2 Register
The PIR2 register contains the interrupt flag bits, as
shown in Register 2-7.
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
bit 7OSFIF: Oscillator Fail Interrupt Flag bit
1 = System oscillator failed, clock input has changed to INTOSC (must be cleared in software)
0 = System clock operating
bit 6C2IF: Comparator C2 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 C1 Interrupt Flag bit
1 = Comparator output (C1OUT bit) has changed (must be cleared in software)
0 = Comparator output (C1OUT bit) has not changed
bit 4EEIF: EE Write Operation Interrupt Flag bit
1 = Write operation completed (must be cleared in software)
0 = Write operation has not completed or has not started
bit 3-0Unimplemented: Read as ‘0’
Note:Interrupt flag bits are set when an in terrupt
condition occurs, re gardless of the st ate of
its corresponding enable bit or the global
enable bit, GIE of the INTCON register.
User software sh ould ensure the appropriate interrupt flag bits are clear prior to
enabling an interrupt.
Note 1: BOREN<1:0> = 01 in the Configuration Word register for this bit to control the BOR
: 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)
: Brown-out Reset Status bit
1 = No Brown-out Reset occurred
0 = A Brown-out Reset occurred (must be set in software after a Brown-out Reset occurs)
(1)
.
DS40001262F-page 42 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
PC
128 70
5
PCLATH<4:0>
PCLATH
Instruction with
ALU Result
GOTO, CALL
OPCODE<10:0>
8
PC
12 11 100
11
PCLATH<4:3>
PCHPCL
87
2
PCLATH
PCHPCL
PCL as
Destination
MOVLW0x20;initialize pointer
MOVWFFSR;to RAM
NEXTCLRFINDF;clear INDF register
INCFFSR;inc pointer
BTFSSFSR,4;all done?
GOTONEXT;no clear next
CONTINUE;yes continue
2.3PCL and PCLATH
The Program Counter (PC) is 13 bits wide. The lo w byte
comes from the PCL register, which is a readable and
writable register . The hig h byte (PC<12:8>) is not directl y
readable or writable and comes from PCLATH. On any
Reset, the PC is cleared. Figure 2-9 shows the two
situatio ns fo r t he l o ad i n g of t h e PC . T he u p per e x am pl e
in Figure 2-9 shows how the PC is loaded on a write to
PCL (PCLATH<4:0> PCH). The lower example in
Figure 2-9 shows how the PC is lo aded during a CALL or
GOTO instruction (PCLATH<4:3> PCH).
FIGURE 2-9:LOADING OF PC IN
DIFFERENT SITUATIONS
2.3.2STACK
The PIC16F631/677 /685/687/689/690 device s have an
8-level x 13-bit wide hardware stack (see Figures 2-2
and 2-3). The stack sp ace is not p art of either pro gram
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 st ack i s POPed i n the e vent o f a RETURN,RETLW or a RETFIE instructio n execu tion. PC LATH is
not affected by a PUSH or POP operation.
The stack operat es as a circular buf fer . This means th at
after the st ack ha s been PUSHed eight time s, th e nin th
push overwrites th e valu e that was s tore d from the firs t
push. The tenth pus h ov erwr i tes the se co nd push (and
so on).
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 execution of the
CALL, RETURN, RETLW and RETFIE
instructions or the vectoring to an
interrupt address.
2.4Indirect Addressing, INDF and
FSR Registers
2.3.1MODIFYING PCL
Executing any instruction with the PCL register as the
destination simultaneously causes the Program
Counter PC<12:8> bits (PCH) to be replaced by the
contents of the PCLATH register. Th is allo ws the enti re
contents of the program counter to be changed by
writing the desired upper five bits to the PCLATH
register. When the lower eight bits are written to the
PCL register, all 13 bits of the program counter will
change to the values co ntaine d in the PCLATH register
and those being written to the PCL register.
A computed GOTO is accomplish ed by adding an offs et
to the program counter (ADDWF PCL). Care should be
exercised when jumping into a look-up table or
program b ranch table (computed GOTO) by modifying
the PCL register. Assuming that PCLATH is set to the
table start address, if the table length is greater than
255 instructions or if the low er eight bi ts of the memo ry
address rolls over from 0xFF to 0x00 in the middle of
the table, then PCLATH must be incremented for each
address rollover that occurs between the table
beginning and the target location within the table.
For more information refer to Application Note AN556,
“Implementing a Table Read” (DS00556).
The INDF register is not a physica l register . Addr essing
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 pointe d to by the File Select Register
(FSR). Reading IN DF itself in directly wil l produc e 00h.
Writing to the INDF register indirectly results in a no
operation (although Status bits may be affected). An
effectiv e 9-bit ad dress is ob tain ed by con caten ating th e
8-bit FSR and the IRP bit of the STATUS register, as
shown in Figure 2-10.
A simple program to clear RAM location 20h-2Fh using
indirect addressing is shown in Example 2-1.
The Oscillator module has a wide variety of clock
sources and selection features that allow it to be used
in a wide range of applicati ons while maximiz ing performance and minimizing power consumption. Figure 3-1
illustrates a block diagram of the Oscillator module.
Clock sources can be configured from external
oscillators, quartz cryst al resonators , ceramic resonators
and Resistor-Capacitor (RC) circuits. In addition, the
system clock source can be configured from one of two
internal oscillators, with a choice of speeds select able via
software. Additional clo ck feat ures inc lud e:
• Selectable system clock source between external
or intern al via software.
• T wo -Speed Start-up mode, which min im iz es
latency between external oscillator start-up and
code execution.
• Fail-Safe Clock Monitor (FSCM) designed to
detect a failure of the external clock source (LP,
XT, HS, EC or RC modes) and switch
automatically to the internal oscillator.
The Oscillator mod ule can be c onfigured in one of eig ht
clock modes.
1. EC – External clock with I/O on OSC2/CLKOUT.
2. LP – 32 kHz Low-Power Crystal 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 OSC2/CLKOUT.
F
6. RCIO – External Resistor-Capacitor (RC) with
I/O on OSC2/CLKOUT.
7. INTOSC – Internal oscillator with F
OSC/4 output
on OSC2 and I/O on OSC1/CLKIN.
8. INTOSCIO – Internal oscillator with I/O on
OSC1/CLKIN and OSC2/CLKOUT.
Clock Source modes are configured by the FO SC<2:0>
bits in the Configuration Word register (CONFIG). The
internal clock can be generated from two internal
oscillators. The HFINTOSC is a calibrated highfrequency oscillator. The LFINTOSC is an uncalibrate d
low-frequency oscillator.
The Oscillator Control (OSCCON) register (Figure 3-1)
controls the system clock and frequency selection
options. The OSCCON register contains the following
bits:
• Frequency selection bits (IRCF)
• Frequency Status bits (HTS, LTS)
• System clock control bits (OSTS, SCS)
REGISTER 3-1:OSCCON: OSCILLATOR CONTROL REGISTER
U-0R/W-1R/W-1R/W-0R-1R-0R-0R/W-0
(1)
(1)
HTSLTSSCS
—IRCF2IRCF1IRCF0OSTS
bit 7bit 0
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
bit 7Unimplemented: Read as ‘0’
bit 6-4IRCF<2:0>: Internal Oscillator Frequency Select bits
bit 3OSTS: Oscillator Start-up Time-out Status bit
1 = Device is running from the clock defined by FOSC<2:0> of the CONFIG register
0 = Device is running from the internal os cillator (HFINTOSC or LFINTOSC)
bit 2HTS: HFINTOSC Status bit (High Frequency – 8 MHz to 125 kHz)
1 = HFINTOSC is stable
0 = HFINTOSC is not stable
bit 1LTS: LFINTOSC Stable bit (Low Frequency – 31 kHz)
1 = LFINTOSC is stable
0 = LFINTOSC is not stabl e
bit 0SCS: System Clock Select bit
1 = Internal oscillator is used for system clock
0 = Clock source defined by FOSC<2:0> of the CONFIG register
Note 1: Bit resets to ‘0’ with Two-Speed Start-up and LP, XT or HS selected as the Oscillator mode or Fail-Safe
mode is enabled.
DS40001262F-page 46 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
OSC1/CLKIN
OSC2/CLKOUT
(1)
I/O
Clock from
Ext. System
PIC
®
MCU
Note 1:Alternate pin functions are listed in the
Section 1.0 “Device Overview”.
3.3Clock Source Modes
Clock Source modes can be classified as external or
internal.
• External Clock m odes rely on e xternal c ircui try fo r
the clock source. Examples 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 Oscillator module. The Oscillator
module has two internal oscillators: the 8 MHz
High-Frequency Internal Oscillator (HFINTOSC)
and the 31 kHz Low-Frequency Inte rnal Osci llator
(LFINTOSC).
The system clock can be selected between extern al or
internal clock sources via the System Clock Select
(SCS) bit of the OSCCON register. See Section 3.6
“Clock Switching” for additional information.
3.4External Clock Modes
3.4.1OSCILLATOR 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) and when the Power-up Timer
(PWRT) has expired (if configured), or a wake-up from
Sleep. During this time, the program counter does not
increment and program execution is suspended. The
OST ensures that the oscillator circuit, using a quartz
crystal resonator o r ce ramic res onator, has started and
is providing a stable system clock to the Oscillator
module. When switching between clock sources, a
delay is required to allow the new clock to stabilize.
These oscillator delays are shown in Table 3-1.
In order to minimize laten cy between externa l oscillator
start-up and code execution, the Two-Speed Clock
Start -up mode ca n be selected (s ee Section 3.7 “T wo-
Speed Clock Start-up Mode”).
TABLE 3-1:OSCILLATOR DELAY EXAMPLES
Switch FromSwitch ToFrequencyOscillator Delay
Sleep/POR
Sleep/POREC, RCDC – 20 MHz2 cycles
LFINTOSC (31 kHz)EC, RCDC – 20 MHz1 cycle of each
Sleep/PORLP, XT, HS32 kHz to 20 MHz1024 Clock Cycles (OST)
LFINTOSC (31 kHz)HFINTOSC125 kHz to 8 MHz1 s (approx.)
LFINTOSC
HFINTOSC
31 kHz
125 kHz to 8 MHz
Oscillator Warm-up Delay (T
WARM)
3.4.2EC MODE
The External Clock (EC) mode allows an externally
generated logic level as the system clock source. When
operating in this mode, an external clock source is
connected to the OSC1 input and the OSC2 is available
for general purpose I/O. Figure 3-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 PIC
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.
Note 1: A series resistor (RS) 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
RF
(2)
Sleep
To Internal
Logic
PIC® MCU
Crystal
OSC2/CLKOUT
Note 1: A series resistor (RS) 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
RF
(2)
Sleep
To Internal
Logic
PIC® MCU
RP
(3)
Resonator
OSC2/CLKOUT
3.4.3LP, 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 3-3). The mode selects a low ,
medium or high gain setting of the internal inverteramplifier to support v arious resonato r types and spee d.
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 designed to
drive only 32.768 kHz tuning-fork type crystals (watch
crystals).
XT Oscillator mode selects the intermediate gain
setting of the internal inverter-amplifier. XT mode
current consumption is the medi um of the three modes.
This mode is best suited to drive resonators with a
medium drive level specification.
HS Oscillator mode select s the highest gain setting of the
internal inverter-amplifie r. H S mode current consum ption
is the highest of the three modes. This mode is best
suited for resonators that require a high drive setting.
Figure 3-3 and Figure 3-4 show typical circuits for
quartz crystal and ceramic resonators, respectively.
.
ing to type, p ackage an d manuf acturer. The
user should consult the manufacturer data
sheets for specifications and recommended
application.
2: Always verify oscillator pe rformance over
DD and temperature range that is
the V
expected for the application.
3: For oscillator desig n assistance, reference
the following Microchip Applications Notes:
• AN826, “Crystal Oscillator Basics and
Crystal Selection for rfPIC
Devices” (DS00826)
• AN849, “Basic PIC
(DS00849)
• AN943, “Practical PICAnalysis and Design” (DS00943)
• AN949, “Making Your Oscillator Work”
(DS00949)
®
and PIC®
®
Oscillator Design”
®
Oscillator
FIGURE 3-3:QUARTZ CRYSTAL
OPERATION (LP, XT OR
HS MODE)
FIGURE 3-4:CERAMIC RESONATOR
OPERATION
(XT OR HS MODE)
DS40001262F-page 48 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
OSC2/CLKOUT
(1)
CEXT
REXT
PIC® MCU
OSC1/CLKIN
F
OSC/4 or
Internal
Clock
VDD
VSS
Recommended values: 10 k REXT 100 k, <3V
3 k R
EXT 100 k, 3-5V
C
EXT > 20 pF, 2-5V
Note 1:Alternate pin functions are listed in the
Section 1.0 “Device Overview”.
2:Output depends upon RC or RCIO Clock
mode.
I/O
(2)
3.4.4EXT ERN AL RC MODES
The external Resistor-Capacitor (RC) modes support
the use of an external RC circuit. This allows the
designer maximum flexibility in frequency choice while
keeping costs to a minimum when clock accuracy is not
required. There are two modes: RC and RCIO.
In RC mode, the RC circuit connects to OSC1. OSC2/
CLKOUT 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 3-5 shows the
external RC mode connections.
FIGURE 3-5:EXTERNAL RC MODES
In RCIO mode, the RC circuit is connected to OSC1.
OSC2 becomes an additional general purpose I/O pin.
The RC oscillator frequency is a function of the supply
voltage, the resistor (R
and the operating temperature. Other factors affecting
the oscillator frequency are:
• threshold voltage variation
• component tolerances
• packaging variations in capa citance
The user also needs to take into account variation due
The Oscillator module has two independent, internal
oscillators that can be configured or selected as the
system clock sou rce.
1. The HFINTOSC (High-Frequency Internal
Oscillator) is factory calibrated and operates at
8 MHz. The frequency o f the HFINT O SC can be
user-adjusted via software using the OSCTUNE
register (Register 3-2).
2. The LFINTOSC (Low-Frequency Internal
Oscillator) is uncalibrated and operates at
31 kHz.
The system cloc k speed can be selecte d via softwar e
using the Internal Oscillator Frequency Select bits
IRCF<2:0> of the OSCCON register.
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 of the OSCCON register. See Section 3.6
“Clock Switching” for more information.
3.5.1INTOSC AND INTOSCIO MODES
The INTOSC and INTOSCIO modes configure the
internal oscillators as the system clock source when
the device is progra mmed usi ng the osc illator selectio n
or the FOSC<2:0> bits in the Configuration Word
register (CONFIG).
In INTOSC mode, OSC1/CLKIN i s available for general
purpose I/O. OSC2/CLKOUT outputs the selected
internal oscillator fre quency div ided by 4. The C LKOUT
signal may be used to provide a clock for external
circuitry, synchronization, calibration, test or other
application requirements.
In INTOSCIO mode, OSC1/CLKIN and OSC2/CLKOUT
are available for general purpose I/O.
3.5.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 via
software using the OSCTUNE register (Register 3-2).
The output of the HFINTOSC connects to a postscaler
and multiplexer (see Figure 3-1). One of seven
frequencies can be selected via software using the
IRCF<2:0> bits of the OSCCON register. See
Section 3.5.4 “Frequency Select Bits (IRCF)” for
more information.
The HFINTOSC is enabled by selecting any frequency
between 8 MHz and 125 kHz by set ting th e IRCF <2:0 >
bits of the OSCCON register 000. Then, set the
System Clock Source (SCS) bit of the OSCCON
register to ‘1’ or enabl e Two-Speed Start-up by setting
the IESO bit in the Configuration Word register
(CONFIG) to ‘1’.
The HF Internal Oscillator (HTS) bit of the OSCCON
register indicates whether the HFINTOSC is stable or not.
PIC16F631/677/685/687/689/690
3.5.2.1OSCTUNE Register
The HFINTOSC is factory calibrated but can be
adjusted in software by writing to the OSCTUNE
register (Register3-2).
The default value of the OSCTUNE register is ‘0’. The
value is a 5-bit two’s complement number.
When the OSCTUNE register is modified, the
HFINTOSC 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 af fected by the
change in frequency.
REGISTER 3-2:OSCTUNE: OSCILLATOR TUNING REGISTER
U-0U-0U-0R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
———TUN4TUN3TUN2TUN1TUN0
bit 7bit 0
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
bit 7-5Unimplemented: Read as ‘0’
bit 4-0TUN<4:0>: Frequency Tuning bits
01111 = Maximum frequency
01110 =
•
•
•
00001 =
00000 = Oscillator module is running at the factory-calibrated frequency.
11111 =
•
•
•
10000 = Minimum frequency
DS40001262F-page 50 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
3.5.3LFINTOSC
The Low-Frequency Internal Oscillator (LFINTOSC) is
an uncalibrated 31 kHz internal clock source.
The output of the LFINTOSC connects to a postscaler
and multiplexer (see Figure3-1). Select 31 kHz, via
software, using the IRCF<2:0> bits of the OSCCON
register. See Section 3.5.4 “Frequency Select Bits
(IRCF)” for more information. The LFINTO SC is also the
frequency for the Power-up Timer (PWRT), Watchdog
Timer (WDT) and Fail-Safe Clock Mo nitor (FSCM).
The LFINTOSC is enabled by selecting 31 kHz
(IRCF<2:0> bits of the OSCCON register= 000) as the
system clock source (SCS bit of the OSCCON
register = 1), or when any of the following are enable d:
• T wo-Speed Start-up IESO bit of the Con fig urati on
Word register = 1 and IRCF<2:0> bits of the
OSCCON register = 000
• Power-up Timer (PWRT)
• Watchdog Timer (WDT)
• Fail-Safe Clock Monitor (FSCM)
The LF Internal Oscillator (LTS) bit of the OSCCON
register indicates whether the LFINTOSC is stable or
not.
3.5.4FREQUENCY SELECT BITS (IRCF)
The output of the 8 MHz HFINTOSC and 31 kHz
LFINTOSC connects to a postscaler and multiplexer
(see Figure 3 -1). The Internal Oscillator Frequency
Select b its IRCF<2:0> of the OSCCON register 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 (LFINTOSC)
Note:Following any Reset, the IRCF<2:0> bits
of the OSCCON register are set to ‘110’
and the frequency selection is set to
4 MHz. The user can modify the IRCF bit s
to select a different frequency.
3.5.5HFINTOSC AND LFINTOSC CLOCK
SWITCH TIMING
When switching between the LFINTOSC and the
HFINTOSC, the new oscillator may already be shut
down to save power (s ee Figure 3-6). If this is th e case,
there is a delay after the IRCF<2:0> bits of the
OSCCON register are modified before the frequency
selection takes place. The LTS and HTS bits of the
OSCCON regis ter will re flect th e current active status
of the LFINTOSC and HFINTOSC oscillators. The
timing of a frequency selection is as follows:
1.IRCF<2:0> bits of the OSCCON register are
modified.
2. If the new clock is shut down, a clock start-up
delay is started.
3.Clock switch ci rcuitry waits for a fal ling edge of
the current cl ock.
4. CLKOUT is held low and the clock switch
circuitry waits fo r a ris ing edge in the new clock.
5. CLKOUT is now connected with the new clock.
LTS and HTS bits of the OS CCON regi ster are
updated as required.
6.Clock switch is complete.
See Figure 3-1 for more details.
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 new frequencies are derived from the HFINTOSC
via the postscaler and multi ple xe r.
Start-up delay specifications are located in the
oscillator tables of Section 17.0 “Electrical
DS40001262F-page 52 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
3.6Clock Switching
The system clock source can be switched between
external and internal clock sources via software using
the System Clock Select (SCS) bit of the OSCCON
register.
3.6.1SYSTEM CLOCK SELECT (SCS) BIT
The System C lock Select (SCS) bit of the OSCCON
register selects the system clock source that is used for
the CPU and peripherals.
• When the SCS bit of the OSCCON register = 0,
the system clock source is determined by
configuration of the FOSC<2:0> bits in the
Configuration Word register ( CONFIG).
• When the SCS bit of the OSCCON register = 1,
the system clock source is chosen by the internal
oscillator frequency selected by the IRCF<2:0>
bits of the OSCCON register. After a Reset, the
SCS bit of the OSCCON register is always
cleared.
Note:Any automatic clock switch, which may
occur from Two-Speed Start-up or FailSafe Clock Monitor, does not update the
SCS bit of the OSCCON register . The user
can monitor the OSTS bit of the OSCCON
register to determine the current system
clock source.
3.6.2O SCIL LA TOR START-UP TIME-OUT
STATUS (OSTS) BIT
The Oscillator Start-up Time-out Status (OSTS) bit of
the OSCCON register indicates whether the system
clock is running from the external clock source, as
defined by the FOSC<2:0> bits in the Configuration
Word register (CONFIG), 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.
3.7Two-Speed Clock Start-up Mode
Two-Speed Start-up mode provides additional power
savings by minimizing the latency between external
oscillator start-up and code execution. In applications
that make heavy use of the Sleep mode, Two-Speed
Start-up will remove the external oscillator start-up
time from the time spent awake and can reduce the
overall power consumption of the device.
This mode allows the application to wake-up from
Sleep, perform a few instructions using the INTOSC
as the clock source and go back to Sleep without
waiting for the primary oscillator to become stable.
When the Oscillator module is configured for LP, XT or
HS modes, the Oscillator Start-up Timer (OST) is
enabled (see Section 3.4.1 “Oscillator Start-up Timer
(OST)”). The OST will suspend program execution until
1024 oscillations are counted. Two-Speed Start-up
mode minimizes the delay in code execution by
operating from the internal oscillator as the OST is
counting. When the OST count rea ches 1024 and the
OSTS bit of the OSCCON register is set, program
execution switches to the external oscillator.
3.7.1TWO-SPEED START-UP MODE
CONFIGURATION
Two-Speed Start-up mode is configured by the
following settings:
• IESO (of the Configuration Word register) = 1;
Internal/External Switc hov er b it (Two-Speed St artup mode enabled).
• SCS (of the OSCCON register) = 0.
• FOSC<2:0> bits in the Configuration Word
register (CONFIG) configured for LP, XT or HS
mode.
Two-Speed Start-up mode is entered after:
• Power-on Reset (POR) and, if enabled, after
Power-up Timer (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 Twospeed Start-up is disabled. This is because the external
clock oscillator do es not require any stabilizat ion time
after POR or an exit from Sleep.
3.7.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<2:0>
bits of the OSCCON register.
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 falling edge
of new clock (LP, XT or HS mode).
7. System clock is switched to external clock
source.
Note:Executing a SLEEP instruction will abort
the oscillator start-up time and will cause
the OSTS b it of the OSCCON register to
remain clear.
Checking the state of the OSTS bit of the OSCCON
register will confirm if the microcontroller is running
from the external clock source, as defined by the
FOSC<2:0> bits in the Configuration Word register
(CONFIG), or the internal oscillator.
FIGURE 3-7:TWO-SPEED START-UP
DS40001262F-page 54 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
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
3.8Fail-Safe Clock Monitor
The Fail-Safe Clock Monit or (FSCM) al low s the dev ic e
to continue operat ing sh ould the external osci ll ator fai l.
The FSCM can detect oscillator failure any time after
the Oscillator Start-up Timer (OST) has expired. The
FSCM is enabled by setting the FCMEN bit in the
Configuration Word register (CONFIG). The FSCM is
applicable to all exte rnal Oscillator modes (L P, XT, HS,
EC, RC and RCIO).
FIGURE 3-8:FSCM BLOCK DIAGRAM
3.8.1FAIL-SAFE DETECTION
The FSCM module detects a failed oscillator by
comparing the ex tern al osci llator to the FS CM samp le
clock. The sample clock is generated by dividing the
LFINTOSC by 64. See Figure 3-8. 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 cle ars the latch on each ris ing edge of th e
sample clock. A fail ure is d ete cte d w h en an ent ire halfcycle of the sample clock elapses before the primary
clock goes low.
3.8.3FAIL-SAFE CONDITION CLEARING
The Fail-Safe condition is cleared after a Reset,
executing a SLEEP instruction or togg ling the S CS bit
of the OSCCON register. When th e S C S b i t i s t o g gl ed ,
the OST is restarted. While the OST is running, the
device continue s to op erat e from t he INT OSC sele cted
in OSCCON. When the OST times out, the Fail-Safe
condition is cleared and the device will be operating
from the external clock s ource. Th e Fail-Sa fe condi tion
must be cleared before th e O SFIF f lag ca n be cle are d.
3.8.4RESET OR WAKE-UP FROM SLEEP
The FSCM is designed to detect an oscillator failure
after the Oscillator Start-up Timer (OST) has expired.
The OST is used after waking up from Sleep and after
any type of Reset. The OST is not used with the EC or
RC Clock modes so that the FSCM will be active as
soon as the R eset or wake- up has complet ed. When
the FSCM is enabled, the Two-Speed Start-up is also
enabled. Therefore, the device will always be executing
code while the OST is operating.
Note:Due to the wide range of oscillator start-up
times, the Fail-Safe circuit is not active
during oscillator start-up (i.e., after exiting
Reset or Sleep). After an appropriate
amount of time, the user should check the
OSTS bit of the OSCCON register to verify
the oscillator start-up and that the system
clock switchover has successfully
completed.
3.8.2FAIL-SAFE OPERATION
When the external clock fails, the FSCM switches the
device clock to an internal cl ock sourc e and set s the b it
flag OSFIF of the PIR2 register. Setting this flag will
generate an interrupt if the OSFIE bit of the PIE2
register is also set. The device firmware can then take
steps to mitigate the problems that may arise from a
failed clock. The system clock will continue to be
sourced from the internal clock source until the device
firmware successfully restarts the external oscillator
and switches back to external operation.
The internal clock source chosen by the FSCM is
determined by the IRCF<2:0> bits of the OSCCON
register. This allows the internal oscillator to be
configured before a failure occurs.
Note:The system clock is normally at a much higher frequency than t he sample clock. The r elative frequencies in
this example have been chosen for clarity.
(Q)
Test
TestTest
Clock Monitor Output
FIGURE 3-9:FSCM TIMING DIAGRAM
TABLE 3-2:SUMMARY OF REGISTERS ASSOCIATED WITH CLOCK SOURCES
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
CONFIG
OSCCON
OSCTUNE
PIE1
PIR1
Legend:x = unknown, u = unchanged, – = unimplemented locations read as ‘0’. Shaded cells are not used by oscillators.
Note 1:Other (non Power-up) Resets include MCLR
(2)
2:See Configuration Word register (Register 14-1) for operation of all register bits.
Reset and Watchdog Timer Reset during normal operation.
Val ue on
POR, BOR
Val ue on
all other
Resets
(1)
DS40001262F-page 56 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
BCFSTATUS,RP0;Bank 0
BCFSTATUS,RP1;
CLRFPORTA;Init PORTA
BSFSTATUS,RP1;Bank 2
CLRF ANSEL;digital I/O
BSFSTATUS,RP0;Bank 1
BCFSTATUS,RP1;
MOVLW 0Ch;Set RA<3:2> as inputs
MOVWF TRISA;and set RA<5:4,1:0>
;as outputs
BCFSTATUS,RP0;Bank 0
4.0I/O PORTS
There are as many as eighteen general purpose I/O
pins available. Depending on which peripherals are
enabled, some or all of the pins may not be a vailable a s
general purpose I/O. In general, when a peripheral is
enabled, the associated pin may not be used as a
general purpose I/O pin.
4.1PORTA and the TRISA Registers
PORTA is a 6-bit wide, bidirectional port. The
corresponding data direction register is TRISA
(Regis ter 4-2). Setting a TRISA bit (= 1) will make the
corresponding PORTA pin an input (i.e., disable the
output driver). Clearing a TRIS A 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 4-1
shows how to initialize PORTA.
Reading the PORTA register (Register 4-1) reads the
status of the pins, whereas writing to it wil l write to the
PORT latch. All write operations are read-modify-write
REGISTER 4-1:PORTA: PORTA REGISTER
operations. Therefore, a write t o a port implies that the
port pins are read, this value is modified and then written
to the PORT data latch. RA3 reads ‘0’ when MCLRE = 1.
The TRISA register 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 input always read ‘0’.
Note:The ANSEL register must be initialized to
configure an analog channel as a digital
input. Pins configured as analog inputs
will read ‘0’.
EXAMPLE 4-1:INITIALIZING PORTA
U-0U-0R/W-xR/W-xR-xR/W-xR/W-xR/W-x
——RA5RA4RA3RA2RA1RA0
bit 7bit 0
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
bit 7-6U nimplemented: Read as ‘0’
bit 5-0RA<5:0>: PORTA I/O Pin bit
1 = Port pin is > V
0 = Port pin is < VIL
REGISTER 4-2:TRISA: PORTA TRI-STATE REGISTER
U-0U-0R/W-1R/W-1R-1R/W-1R/W-1R/W-1
——TRISA5TRISA4TRISA3TRISA2TRISA1TRISA0
bit 7bit 0
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
bit 7-6U nimplemented: Read as ‘0’
bit 5-0TRISA<5: 0>: PORTA Tri-S tate Control bit
Note 1:TRISA<3> always reads ‘1’.
2:TRISA<5:4> always reads ‘1’ in XT, HS and LP Oscillator modes.
1 = PORTA pin configured as an input (tri-stated)
0 = PORTA pin configured as an output
IH
PIC16F631/677/685/687/689/690
4.2Additional Pin Functions
Every PORT A pin o n this device family has an in terrupton-change option and a weak pull-up option. RA0 also
has an Ultra Low-Power Wake-up option. The next
three sections describe these functions.
4.2.1ANSEL AND ANSELH REGISTERS
The ANSEL and ANSELH registers are used to disable
the input buffers of I/O pins, which allow analog voltages
to be applied to those pins witho ut causing excessive
current. Setting the ANSx bit of a corresponding pin will
cause all digital reads of th at pin to return ‘ 0’ and also
permit analog functions of that pin to operate correctly.
The state of the ANSx bit has no effect on the digital
output function of its corresponding pin. A pin w ith the
TRISx bit clear and ANSx bit set will operate as a digital
output, together with the analog input function of that
pin. Pins with the A NSx bit set always re ad ‘0’, which
can cause unexpected behavior when execu ting read
or write operations on the port due to the r ead-modifywrite sequence of all such operations.
4.2.2WEAK PULL-UPS
Each of the PORTA pins, except RA3, has an
individually configurable internal weak pull-up. Control
bits WPUAx enable or disable each pull-up. Refer to
Register 4-4. Each weak pull-up i s automatically turne d
off when the p ort pin is co nfigured as an output. T he
pull-ups are disabled on a Power-on Reset by the
RABPU
automatically enabled for RA3 when configured as
MCLR
software control of the MCLR
bit of the OPTION register. A weak pull-up is
and disabled when RA3 is an I/O. There is no
pull-up.
4.2.3INTERRUPT-ON-CHANGE
Each PORTA pin is individually configurable as an
interrupt-on-change pin. Control bits IOCAx enable or
disable the interrupt function for each pin. Refer to
Register 4-6. The i nte r rup t -o n- c ha n ge is di sa bl e d on a
Power-on Reset.
For enabled interrupt-on-change pins, the values are
compared with the old value latch ed on the last rea d of
PORTA. The ‘mismatch’ outputs of the last read are
OR’d together to set th e PORTA Change Interrupt Flag
bit (RABIF) in the INTCON register (Register 2-6).
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 PORTA. This will end the
mismatch condition, then,
b) Clear the flag bit RABIF.
A mismatch condition will continue to set flag bit RABIF .
Reading PORTA will end the mismatch condition and
allow flag bit RABIF to be cleared. The latch holding the
last read value is not affected by a MCLR
Reset. After these R esets, the RABIF flag w il l continue
to be set if a mismatch is present.
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
RABIF interrupt flag may not get set.
nor BOR
DS40001262F-page 58 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
REGISTER 4-3:ANSEL: ANALOG SELECT REGISTER
R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1R/W-1
ANS7ANS6ANS5ANS4ANS3ANS2ANS1ANS0
bit 7bit 0
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
bit 7-0ANS<7:0>: Analog Select bits
Analog select between analog or digital function on pins AN<7:0>, respectively.
1 = Analog input. Pin is assigned as analog input
0 = Digital I/O. Pin is assigned to port or special function.
Note 1: Setting a pin to an analog input automatically disables the digital input circuitry, weak pull-ups and
interrupt-on-change if available. The corresponding TRIS bit must be set to Input mode in order to allow
external control of the voltage on the pin.
REGISTER 4-4:ANSELH: ANALOG SELECT HIGH REGISTER
(1)
.
(2)
U-0U-0U-0U-0R/W-1R/W-1R/W-1R/W-1
————ANS11ANS10ANS9ANS8
bit 7bit 0
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
bit 7-4Unimplemented: Read as ‘0’
bit 3-0ANS<11:8>: Analog Select bits
Analog select between analog or digital function on pins AN<7:0>, respectively.
1 = Analog input. Pin is assigned as analog input
0 = Digital I/O. Pin is assigned to port or special function.
Note 1: Setting a pin to an analog input automatically disables the digital input circuitry, weak pull-ups and
interrupt-on-change if available. The corresponding TRIS bit must be set to Input mode in order to allow
external control of the voltage on the pin.
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
bit 7-6Unimplemented: Read as ‘0’
bit 5-4WPUA<5:4>: Weak Pull-up Register bit
1 = Pull-up enabled
0 = Pull-up disabled
bit 3Unimplemented: Read as ‘0’
bit 2-0WPUA<2:0>: Weak Pull-up Register bit
1 = Pull-up enabled
0 = Pull-up disabled
Note 1: Global RABPU
2: The weak pull-up device is automatically disabled if the pin is in Output mode (TRISA = 0).
3: The RA3 pull-up is enabled when configured as MCLR and disabled as an I/O in the Configuration Word.
4: WPUA<5:4> always reads ‘1’ in XT, HS and LP Oscillator modes.
bit of the OPTION register must be enabled for individual pull-ups to be enabled.
REGISTER 4-6:IOCA: INTERRUPT-ON-CHANGE PORTA REGISTER
U-0U-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0
——IOCA5IOCA4IOCA3IOCA2IOCA1IOCA0
bit 7bit 0
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
bit 7-6Unimplemented: Read as ‘0’
bit 5-0IOCA<5:0>: Interrupt-on-change PORTA Control bit
Note 1: Global Interrupt Enable (GIE) must be enabled for individual interrupts to be recognized.
2: IOCA<5:4> always reads ‘1’ in XT, HS and LP Oscillator modes.
DS40001262F-page 60 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
BCFSTATUS,RP0;Bank 0
BCFSTATUS,RP1;
BSFPORTA,0;Set RA0 data latch
BSFSTATUS,RP1;Bank 2
BCFANSEL,0;RA0 to digital I/O
BSFSTATUS,RP0;Bank 1
BCFSTATUS,RP1;
BCFTRISA,0;Output high to
CALLCapDelay;charge capacitor
BSFPCON,ULPWUE;Enable ULP Wake-up
BSFIOCA,0;Select RA0 IOC
BSFTRISA,0;RA0 to input
MOVLWB’10001000’;Enable interrupt
MOVWFINTCON;and clear flag
BCFSTATUS,RP0;Bank 0
SLEEP;Wait for IOC
NOP;
4.2.4ULTRA LOW-POWER WAKE-UP
The Ultra Low-Power Wake-up (ULPWU) on RA0 allows
a slow falling voltage to generate an interrupt-on-change
on RA0 without excess current consumption. The mode
is selected by setting the ULPWUE bit of the PCON
register. This enables a small c urrent sink, which can be
used to discharge a capacitor on RA0.
Follow these steps to use this feature:
a) Charge the capacitor on RA0 by configuring the
RA0 pin to output (= 1).
b) Configure RA0 as an input.
c) Enable interrupt-on-change for RA0.
d) Set the ULPWUE bit of the PCON register to
begin the capacitor discharge.
e) Execute a SLEEP instruction.
When the voltage on RA0 drops below V
will be generated which will cause the device to wakeup and execute the next instruction. If the GIE bit of the
INTCON register is set, the device will then call the
interrupt vector (0004h). See Section 4.4.2 “Interrupt-
on-change” and Section 14.3.3 “PORTA/PORTB
Interrupt” for more information.
This feature provides a low-power technique for
periodically waking up the device from Sleep. The
time-out is dependent on the discharge time of the RC
circuit on RA0. See Example 4-2 for initializing the
Ultra Low-Power Wake-up module.
IL, an interrupt
A series resistor between RA0 and the external
capacitor provides overcurrent protection for the RA0/
AN0/C1IN+/ICSPDAT/ULPWU pin and can allow for
software calibration of the time-out (see Figure 4-1). A
timer can be used to measure the charge time and
discharge time of the capacitor. The charge time can
then be adjusted to pro vi de th e de si red i nter r upt d ela y.
This technique will compensate for the affects of
temperature, voltage and component accuracy. The
Ultra Low-Power Wake-up peripheral can also be
configured as a simple Programmable Low-Voltage
Detect or temperature sensor.
Note:For more information, refer to Application
Note AN879, “Using the Microchip UltraLow-Power Wa ke-up Modul e” (DS00879).
Each PORT A pin is multiplexed with other functio ns. The
pins and their combined functions are briefly described
here. For specific information about individual functions
such as the comparator or the A/D Converter (ADC),
refer to the appropriate section in this data sheet.
FIGURE 4-1:BLOCK DIAGRAM OF RA0
4.2.5.1RA0/AN0/C1IN+/ICSPDAT/ULPWU
Figure 4-2 shows the diagram for this pin. The RA0/
AN0/C1IN+/ICSPDAT/ULPWU pin is configurable to
function as one of the following:
• a general purpose I/O
• an analog input for the ADC (except PIC16F631)
• an analog input to Comparator C1
• In-Circuit Serial Programming™ data
• an analog input for the Ultra Low-Power Wake-up
DS40001262F-page 62 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
I/O Pin
VDD
VSS
D
Q
CK
Q
D
Q
CK
Q
D
Q
CK
Q
D
Q
CK
Q
VDD
D
EN
Q
D
EN
Q
Weak
Data Bus
WR
WPUA
RD
WPUA
RD PORTA
RD
PORTA
WR
PORTA
WR
TRISA
RD
TRISA
WR
IOCA
RD
IOCA
Interrupt-on-
To Compar ator
Analog
(1)
Input Mode
RABPU
Analog
(1)
Input Mode
Change
Q3
Note 1: ANSEL determines Analog Input mode.
2: Not implemented on PIC16F631.
To A/D Converter
(2)
I/O Pin
VDD
VSS
D
Q
CK
Q
D
Q
CK
Q
D
Q
CK
Q
D
Q
CK
Q
VDD
D
EN
Q
D
EN
Q
Weak
Analog
(1)
Input Mode
Data Bus
WR
WPUA
RD
WPUA
RD
PORTA
WR
PORTA
WR
TRISA
RD
TRISA
WR
IOCA
RD
IOCA
To A/D Converter
(2)
0
1
C1OUT
C1OUT
Enable
To INT
To Timer0
Analog
(1)
Input Mode
RABPU
RD PORTA
Interrupt-on-
Change
Q3
Note 1: ANSEL determines Analog Input mode.
2: Not implemented on PIC16F631.
4.2.5.2RA1/AN1/C12IN0-/VREF/ICSPCLK
Figure 4-2 shows the diagram for this pin. The RA1/
TABLE 4-1:SUMMARY OF REGISTERS ASSOCIATED WITH PORTA
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
ADCON0
ANSEL
CM1CON0
INTCONGIE
IOCA
OPTION_REG
PORTA
SSPCON
T1CON
TRISA
WPUA
Legend:x = unknown, u = unchanged, – = unimplemented locations read as ‘0’. Shaded cells are not used by PORTA.
PORTB is a 4-bit wide, bidirectional port. The
corresponding data direct ion regist er is TRISB (Regis ter
4-6). Setting a TRISB bit (= 1) will make the
corresponding PORTB pin an input (i.e., put the
corresponding output d river in a Hi gh-Impedance mod e).
Clearing a TRIS B bi t (= 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 4-3 shows how to initialize PORTB. Reading
the PORTB register (Register 4-5) reads the st atus of the
pins, whereas writ ing to it will wri te to the PORT l atch. All
write operations are read-modify-write operations.
Therefore, a write to a port implies that the port pins are
read, this value is m odified and then writte n to the PO RT
data latch.
The TRISB register 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 input always read ‘0’.
EXAMPLE 4-3:INITIALIZING PORTB
Note:The ANSELH register must be initialized
to configure an analo g channel as a di gital
input. Pins configured as analog inputs
will read ‘0’.
4.4Additional PORTB Pin Functions
PORTB pins RB<7 :4> on the device fam ily device have
an interrupt-on-change option and a weak pull-up
option. The following three sections describe these
PORTB pin functions.
4.4.1WEAK PULL-UPS
Each of the PORTB pins has an indiv idually configurable
internal weak pull-up. Control bits WPUB<7:4> enable or
disable each pull-up (see Register 4-9). Each weak
pull up is automatically turned off when the port pin is
configured as an output. All pull-ups are disabled on a
Power-on Reset by the RABPU
bit of the OPTION
register.
4.4.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 4-10. The interrupt-on-change feature
is disabled on a Power-on Reset.
For enabled interrupt-on-change pins, the present
value is compared w ith the old value latched on the las t
read of PORTB to determine which bits have changed
or mismatch the old value. The ‘mismatch’ outputs are
OR’d together to set the PORTB Change Interrupt flag
bit (RABIF) 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 RABIF.
A mismatch conditio n will contin ue to set flag bit RABIF.
Reading or writing PORTB will end the mismatch
condition and allow flag bit RABIF to be cleared. The latch
holding the last read value is not affected by a MCLR
Brown-out Reset . After these Resets, the RABIF fla g will
continue to be set if a mismatch is present.
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 RABIF
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.
nor
REGISTER 4-7:PORTB: PORTB REGISTER
R/W-xR/W-xR/W-xR/W-xU-0U-0U-0U-0
RB7RB6RB5RB4
bit 7bit 0
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
DS40001262F-page 68 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
I/O Pin
VDD
VSS
D
Q
CK
Q
D
Q
CK
Q
D
Q
CK
Q
D
Q
CK
Q
VDD
D
EN
Q
D
EN
Q
Weak
Data Bus
WR
WPUB
RD
WPUB
RD PORTB
RD
PORTB
WR
PORTB
WR
TRISB
RD
TRISB
WR
IOCB
RD
IOCB
Interrupt-on-
To SSPSR
Analog
(1)
Input Mode
RABPU
Analog
(1)
Input Mode
Change
Q3
To A/D Converter
(2)
ST
SSPEN
0
1
1
0
Available on PIC16F677/PIC16F687/PIC16F689/PIC16F690
only.
Note 1:ANSEL determines Analog Input mode.
2:Not implemented on PIC16F631.
0
1
1
0
SSPSR
From
SSP
4.4.3PIN DESCRIPTIONS AND
DIAGRAMS
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 SSP, I
appropriate section in this data sheet.
2
C™ or interrupts, refer to the
4.4.3.1RB4/AN10/SDI/SDA
Figure 4-7 shows the diagram for this pin. The RB4/
BCFSTATUS,RP0;Bank 0
BCFSTATUS,RP1;
CLRFPORTC;Init PORTC
BSFSTATUS,RP1;Bank 2
CLRFANSEL;digital I/O
BSFSTATUS,RP0;Bank 1
BCFSTATUS,RP1;
MOVLW0Ch;Set RC<3:2> as inputs
MOVWFTRISC;and set RC<5:4,1:0>
;as outputs
BCFSTATUS,RP0;Bank 0
4.5PORTC and TRISC Registers
PORTC is a 8-bit wide, bidirectional port. The
corresponding data direction regi s ter is T RISC (Register
4-10). Setting a TRISC bit (= 1) will make the
corresponding PORTC pin an input (i.e., put the
correspondin g output dr iver in a H igh-Impeda nce mode ).
Clearing a TRISC bit (= 0) will make the corresponding
PORTC pin an output (i.e ., en able the ou tput d rive r and
put the contents of t he outp ut l atc h on t h e se le cted pi n) .
The TRISC register controls the PORTC pin output
drivers, even w hen they are be ing used as analo g inputs .
The user should ensure the bits in the TRISC register are
maintained set when using them as analog inputs. I/O
pins config ure d a s an al og i np ut always read ‘0’.
Note:The ANSEL and ANSELH registers must
be initialized to configure an analog
channel as a digital input. Pins configu red
as analog inputs will read ‘0’.
Example 4-4 sh ows how to initialize P ORTC. Reading
the PORTC regi ster (Register 4 -9) reads the st atus of the
EXAMPLE 4-4:INITIALIZING PORTC
pins, whereas wr iting to it will wr ite to the POR T la tch. 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.
REGISTER 4-11:PORTC: PORTC REGISTER
R/W-0R/W-xR/W-xR/W-xR/W-xR/W-xR/W-xR/W-x
RC7RC6RC5RC4RC3RC2RC1RC0
bit 7bit 0
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
bit 7-0RC<7:0>: PORTC General Purpose I/O Pin bit
1 = Port pin is > V
0 = Port pin is < VIL
IH
REGISTER 4-12:TRISC: PORTC TRI-STATE REGISTER
R/W-1R/W-1R/W-1R/W-1R-1R/W-1R/W-1R/W-1
TRISC7TRISC6TRISC5TRISC4TRISC3TRISC2TRISC1TRISC0
bit 7bit 0
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
bit 7-0TRISC<7: 0>: PORTC Tri-State Control bit
DS40001262F-page 74 2005-2015 Microchip Technology Inc.
1 = PORTC pin configured as an input (tri-stated)
0 = PORTC pin configured as an output
PIC16F631/677/685/687/689/690
VDD
VSS
D
Q
CK
Q
D
Q
CK
Q
Data Bus
WR
PORTC
WR
TRISC
RD
TRISC
To A/D Converter
(2)
RD
PORTC
Analog Input
Mode
(1)
To Compar ators
Note 1:ANSEL determines Analog Input mode.
2:Not implemented on PIC16F631.
I/O Pin
VDD
VSS
D
Q
CK
Q
D
Q
CK
Q
Data Bus
WR
PORTC
WR
TRISC
RD
TRISC
To Comparators
RD
PORTC
Analog Input
Mode
(1)
CCP1OUT
CCP1OUT
Enable
Available on PIC16F685/PIC16F690 only.
Note 1:ANSEL determines Analog Input mode.
2:Not implem ented on PIC16F631.
0
1
1
0
I/O Pin
To A/D Converter
(2)
4.5.1RC0/AN4/C2IN+
The RC0 is configurable to function as one of the
following:
• a general purpose I/O
• an analog input for the ADC (except PIC16F631)
• an analog input to Comparator C2
4.5.2 RC1/AN5/C12IN1-
The RC1 is configurable to function as one of the
following:
• a PWM output
Note 1: Enabling both C2OUT and P1B will cause
a conflict on RC4 and create
unpredictable results. Therefore, if
C2OUT is enabled, the ECCP+ can not be
used in Half-Bridge or Full-Bridge mode
and vise-versa.
2: P1B is available on PIC16F685/
PIC16F690 only.
(1, 2)
is configurable to function
FIGURE 4-13:BLOCK DIAGRAM OF RC4
4.5.6RC5/CCP1/P1A
The RC5/CCP1/P1A
one of the following:
• a general purpose I/O
• a digital input/output for the Enhanced CCP
• a PWM output
Note 1: CCP1 and P1A are available on
PIC16F685/PIC16F690 only.
(1)
is configurable to function as
FIGURE 4-14:BLOCK DIAGRAM OF RC5
DS40001262F-page 76 2005-2015 Microchip Technology Inc.
Legend:x = unknown, u = unchanged, – = unimplemented locations read as ‘0’. Shaded cells are not used by PORTC .
Note 1:PIC16F687/PIC16F689/PIC16F690 only.
DS40001262F-page 78 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
T0CKI
T0SE
pin
TMR0
Watchdog
Timer
WDT
Time-out
PS<2:0>
WDTE
Data Bus
Set Flag bit T0IF
on Overflow
T0CS
Note 1: T0SE, T0CS, PSA, PS<2:0> are bits in the OPTION register.
2: SWDTEN and WDTPS<3:0> are bits in the WDTCON register.
3: WDTE bit is in the Configuration Word register.
0
1
0
1
0
1
Sync 2
cycles
8
8
8-bit
Prescaler
0
1
FOSC/4
PSA
PSA
PSA
16-bit
Prescaler
16
WDTPS<3:0>
31 kHz
INTOSC
SWDTEN
5.0TIMER0 MODULE
The Timer0 module is an 8-bit timer/c ounter with the
following features:
• 8-bit timer/counter register (TMR0)
• 8-bit prescaler (shared with Watchdog Timer)
• Programmable internal or external clock source
• Programmable external clock edge selection
• Interrupt on overflow
Figure 5-1 is a block diagram of the Timer0 module.
5.1Timer0 Operation
When used as a timer, the Timer0 module can be used
as either an 8-bit timer or an 8-bit counter.
5.1.18-BIT TIMER MODE
When used as a timer, the Timer0 module will
increment every instruction cyc le (without prescaler).
Timer mode is selected by clearing the T0CS bit of the
OPTION register to ‘0’.
When TMR0 is written, the increment is inhibited for
two instruction cycles immediately following the write.
Note:The value written to the TMR0 register
can be adjus ted, in order to account for
the two instruction cycle delay when
TMR0 is written.
5.1.28-BIT COUNTER MODE
When used as a counter, the Timer0 module will
increment on every rising or falling edge of the T0CKI
pin. The incrementing edge is determined by the T0SE
bit of the OPTION register. Counter mode is selected by
setting the T0CS bit of the OPTION register to ‘1’.
FIGURE 5-1:BLOCK DIAGRAM OF THE TIMER0/WDT PRESCALER
;prescaler
BANKSEL OPTION_REG ;
MOVLWb’11110000’;Mask TMR0 select and
ANDWFOPTION_REG,W; prescaler bits
IORLWb’00000011’;Set prescale to 1:16
MOVWFOPTION_REG ;
5.1.3SOFTWARE PROGRAMMABLE
PRESCALER
A single software programmable prescaler is available
for use with either Timer0 or the Watchdog Timer
(WDT), but not both simultaneously. The prescaler
assignment is controlle d by the PSA bit o f the OPTION
register. To assign the prescaler to Tim er 0, th e PSA b it
must be cleared to a ‘0’.
There are eight prescaler options for the Timer0 module ranging from 1:2 to 1:256. The prescale values are
selectable via th e PS<2:0> bits of the OPTION regi ster .
In order to have a 1:1 prescaler value for the Timer0
module, the prescaler must be assigned to the WDT
module.
The prescaler is not readable or writable. When the
prescaler is assigned to the Timer0 module, all
instructions writing to the TMR0 register will clear the
prescaler.
When the prescaler is assigned to WDT, a CLRWDT
instruction will clear the prescaler along with the WDT.
5.1.3.1Switching Prescaler Between
Timer0 and WDT Modules
As a result of hav ing the prescaler assigned to either
Timer0 or the WDT, it is possible to generate an
unintended device Reset when switching prescaler
values. When changin g th e presca le r ass ig nme nt fro m
Timer0 to the WDT module, the instr uction sequence
shown in Example 5-1, must be executed.
EXAMPLE 5-1:CHANGING PRESCALER
(TIMER0 WDT)
When changing the prescaler assignment from the
WDT to the Timer0 module, the following instruction
sequence must be executed (see Example 5-2).
EXAMPLE 5-2:CHANGING PRESCALER
(WDT TIMER0)
5.1.4TIMER0 INTERRUPT
Timer0 will generate an interrupt when the TMR0
register overflows from FFh to 00h. The T0IF interrupt
flag bit of the INTCON register is set every time the
TMR0 register overflows, regardless of whether or not
the Timer0 interrupt is enabled. The T0IF bit must be
cleared in software. The Timer0 interrupt enable is the
T0IE bit of the INTCON register.
Note:The Timer0 interrupt cannot wake the
processor from Sleep since the timer is
frozen dur ing Sleep.
5.1.5USING TIMER0 WITH AN
EXTERNAL CLOCK
When Timer0 is in Coun ter mo de, t he synchronizatio n
of the T0CKI input and the Timer0 register is accomplished by sampling the prescaler output on the Q2 and
Q4 cycles of the internal phas e clocks. Ther efore, t he
high and low periods of the extern al cl oc k so urc e mus t
meet the timing requirements as shown in
Section 17.0 “Electrical Specifications”.
DS40001262F-page 80 2005-2015 Microchip Technology Inc.
The Timer1 module is a 16-bit timer/counter with the
following features:
• 16-bit timer/counter reg ister p air (TMR1 H:TMR1 L)
• Programmable internal or external clock source
• 3-bit prescaler
• Optional LP oscillator
• Synchronous or asynchronous operation
• Timer1 gate (count enable) via comparator or
T1G
pin
• Interrupt on overflow
• Wake-up on overflow (external c lock,
Asynchronous mode only)
• Time base for the Capture/Compare function
(PIC16F685/PIC16F690 only)
• Special Event Trigger (with ECCP)
(PIC16F685/PIC16F690 only)
• Comparator output synchronization to Timer1
clock
Figure 6-1 is a block diagram of the Timer1 module.
6.1Timer1 Operation
The Timer1 module is a 16-bit incrementing counter
which is accessed through the TMR1H:TMR1L register
pair. Writes to TMR1H or TMR1L directly update the
counter.
When used with an interna l clock so urce, the module i s
a timer. When used with an ext ernal cl ock sou rce, t he
module can be used as either a timer or counter.
6.2Clock Source Selection
The TMR1CS bit of the T1CON register is used to select
the clock source. When TMR1CS = 0, the clock source
OSC/4. When TMR1CS = 1, the clock source is
is F
supplied externally.
Clock
Source
OSC/4xxxx0
F
T1CKI pin0xxx1
T1LPOSC1LP or
T1OSCEN
FOSC
Mode
INTOSCIO
TMR1CS
1
DS40001262F-page 82 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
TMR1HTMR1L
Oscillator
T1SYNC
T1CKPS<1:0>
Prescaler
1, 2, 4, 8
Synchronize
(3)
det
1
0
0
1
Synchronized
clock input
2
Set flag bit
TMR1IF on
Overflow
TMR1
(2)
TMR1GE
TMR1ON
T1OSCEN
1
0
SYNCC2OUT
(4)
T1GSS
T1GINV
To C2 Comparator Module
Timer1 Clock
TMR1CS
OSC2/T1G
OSC1/T1CKI
Note 1:ST Buffer is low power type when using LP oscillator, or high speed type when using T1CKI.
2:Timer1 register increments on rising edge.
3:Synchronize does not operate while in Sleep.
4:SYNCC2OUT is synchronized when the C2SYNC bit of the
When the internal clock source is selected the
TMR1H:TMR1L register pair will increment on multiples
OSC as determined by the Timer1 prescaler.
of F
6.2.2EXTERNAL CLOCK SOURCE
When the external clock source is selected, the Timer1
module may work as a timer or a counter.
When counting, 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.
If an external clock oscillator is needed (and the
microcontroller is us ing the INTOS C without CLKOUT),
Timer1 can use the LP oscillator as a clock source.
Note 1: In Counter mode, a falling edge must be
registered by the counter prior to the first
incrementing risin g edge af ter any one or
more of the following conditions:
•Timer1 enabled after POR reset
•Write to TMR1H or TMR1L
•Timer1 is disabled
•Timer1 is dis abled (TMR1O N 0) when
T1CKI is high then Timer1 is
enabled (TMR1ON=1) when T1CKI
is low.
2: See Figure 6-2
6.3Timer1 Prescaler
Timer1 has four prescaler options allowing 1, 2, 4 or 8
divisions of the clock input. The T1CKPS bits of the
T1CON register control the prescale counter. The
prescale counter is not directly readable or writable;
however, the prescaler counter is cl eared upon a write to
TMR1H or TMR1L.
6.4Timer1 Oscillator
A low-power 32.768 kHz crystal oscillator is built-in
between pins OSC1 (input) and OSC2 (amplifier
output). The oscillator is enabled by setting the
T1OSCEN control bit of the T1CON register. The
oscillator will continue to run during Sleep.
The Timer1 oscillator is shared with the system LP
oscillator. Thus, Timer 1 can use th is mode on ly when
the primary system clock is derived from the internal
oscillator or when the oscillator is in the LP mode. The
user must provide a software time delay to ensure
proper oscillator start-up.
TRISA5 and TRISA4 bits are set when the Timer1
oscillator is enabled. RA5 and RA4 bit s rea d a s ‘0’ and
TRISA5 and TRISA4 bits read as ‘1’.
Note:The oscillator requires a start-up and
stabilization time before use. Thus,
T1OSCEN should be set and a suitable
delay observed prior to enabling Timer1.
6.5Timer1 Operation in
Asynchronous Counter Mode
If control bit T1SYNC of the T1CON register is set, the
external clock input is not synchronized. The timer
increments asynchronously to the internal phase
clocks. If external clock source is selected then the
timer will continue to run during Sleep and can
generate an interrupt on overflow, which will wake-up
the processor. However, special precautions in
software are needed to read/write the timer (see
Section 6.5.1 “Reading and Writing Timer1 in
Asynchronous Counter Mode”).
Note:When switching from synchronous to
asynchronous operation, it is possible to
skip an increment. When switching from
asynchronous to synchronous operation,
it is possible to produce an additional
increment.
6.5.1READING AND WRITING TIMER1 IN
ASYNCHRONOUS COUNTER
MODE
Reading TMR1H or TMR1L while the timer is running
from an external asyn chronous cl ock will e nsure a valid
read (taken care of in hardware). However, the user
should keep in mind that rea ding t he 1 6-bit ti mer in two
8-bit values itself, poses certain problems, since the
timer may overflow between the reads.
For writes, it is recommend ed that th e user s imply sto p
the timer and write the desired values. A write
contention may occ ur by w ritin g to th e timer regist ers,
while the register is incrementi ng. This may pro duce an
unpredictable value in the TMR1 H:TMR1L register pair .
6.6Timer1 Gate
The Timer1 gate (when enable d) allows T imer1 to count
when Timer1 gate is active. Timer1 gate source is
software configurable to be the T1G
Comparator C2. This allows the device to directly time
external events using T1G
Comparator C2. See the CM2CON1 register
(Register 8-3) for selecting the Timer1 gate source. This
feature can simplify the software for a Delta-Sigma A/D
converterand many other applications.
pin or the output of
or analog events using
DS40001262F-page 84 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
Note:TMR1GE bit of the T1CON register must
be set to use either T1G
Timer1 gate source. See the CM2CON1
register (Register 8-3) for more information on selecting the Timer1 gate source.
Timer1 gate can be in verted using the T1GINV bit of
the T1CON register , wh ether it originate s from the T1G
pin or Comparator C2 output. This configures Timer1 to
measure either the active-high or active-low time
between ev ents.
or C2OUT as the
6.7Timer1 Interrupt
The Timer1 register pair (TMR1H:TMR1L) increments
to FFFFh and rolls over to 0000h. When Timer1 rolls
over , the T i mer1 in terrupt fl ag bit of th e PIR1 regis ter is
set. To enable the interrupt on rollover, you must set
these bits:
• TMR1ON bit of the T1CON register
• TMR1IE bit of the PIE1 register
• PEIE bit of the INTCON register
• GIE bit of the INTCON register
The interrupt is cleared by clearing the TMR1IF bit in
the Interrupt Service Routine.
In Compare mode, an event is trigge red when the value
CCPR1H:CCPR1L register pair matches the value in
the TMR1H:TMR1L register pair. This event can be a
Special Event Trigger.
For more information, see Section 11.0 “Enhanced
Capture/Compare/PWM Module”.
6.10ECCP Special Event Trigger
When the ECCP is configured to trigger a special
event, the trigger will clear the TMR1H:TMR1L register
pair. This special event does not cause a Timer1 interrupt. The ECCP module may still be configured to
generate a ECCP interrupt.
In this mode of operation, the CCPR1H:CCPR1L
register pair becomes the period register for Timer1.
Timer1 should be synchronized to the F
the Special Event Trigger. Asynchronous operation of
Timer1 can cause a Special Event Trigger to be
missed.
In the event that a write to TMR1H or TM R1L coinci des
with a Special Event Trigger from the ECCP, the write
will take precedence.
For more information, see Section 11.2.4 “Special
Event Trigger”.
OSC to utilize
Note:The TMR1H:TTMR1L register pair and
the TMR1IF bit should be cleared before
enabling interrupts.
6.8Timer1 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:
• TMR1ON bit of the T1CON register must be set
• TMR1IE bit of the PIE1 register must be set
• PEIE bit of the INTCON register must be set
• T1SYNC
• TMR1CS bit of the T1CON register must be set
• T1OSCEN bit of the T1CON register (can be set)
The device will wake-up on an overflow and execute
the next instructions. If the GIE bit of the INTCON
register is set, the device will call the Interrupt Service
Routine (0004h).
bit of the T1CON register must be set
6.9ECCP Capture/Compare Time Base
The ECCP module uses the TMR1H:TMR1L register
pair as the time base when operating in Capture or
Compare mode.
In Capture mode, the value in the TMR1H:TMR1L
register pair is copied into the CCPR1H:CCPR1L
register pair on a configured event.
6.11Comparator Synchronization
The same cl ock used to increment Timer1 can also be
used to synchronize the comparator output. This
feature is enabled in the Comparator module.
When using the comparator for Timer1 gate, the
comparator output should be synchronized to Timer1.
This ensures Timer1 does not miss an increment if the
comparator chang es.
pin or C2OUT, as selected by t he T1GSS b it of th e CM 2CON1
PIC16F631/677/685/687/689/690
TABLE 6-1:SUMMARY OF REGISTERS ASSOCIATED WITH TIMER1
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
CM2CON1 MC1OUT MC2OUT
INTCONGIEPEIE
PIE1
PIR1
TMR1HHolding Register for the Most Significant Byte of the 16-bit TMR1 Registerxxxx xxxxuuuu uuuu
TMR1LHolding Register for the Least Significant Byte of the 16-bit TMR1 Registerxxxx xxxxuuuu uuuu
T1CONT1GINVTMR1GE T1CKPS1 T1CKPS0 T1OSCEN T1SYNC
Legend:x = unknown, u = unchanged, — = unimplemented, read as ‘0’. Shaded cells are not used by the Timer1 module.
• Software programmable postscaler (1:1 to 1:16)
See Figure 7-1 for a block diagram of Timer2.
7.1Timer2 Operation
The clock input to the Timer2 module is the system
instruction clock (F
Timer2 prescaler, which has prescale options of 1:1,
1:4 or 1:16. The output of the prescal er is the n use d to
increment the TMR2 register.
The values of T MR2 and PR2 are co nstan tly comp ared
to determine when they match. TMR2 will increment
from 00h until it matches the value in PR2. When a
match occurs, two things happen:
• TMR2 is reset to 00h on the next increment cycle.
• The Timer2 postscaler is incremented
The match output of the Timer2/PR2 comparator is fed
into the Timer2 postscaler. The postscaler has
postscale op tions of 1 :1 to 1:16 i nclusiv e. The out put of
the Timer2 postscaler is used to set the TMR2IF
interrupt flag bit in the PIR1 register.
OSC/4). The clock is fed into the
The TMR2 and PR2 registers are both fully readable
and writable . On any Re set, th e TMR2 regis ter is set to
00h and the PR2 register is set to FFh.
Timer2 is turned on by setting the TMR2ON bit in the
T2CON register to a ‘1’. Timer2 is turned of f by clearing
the TMR2ON bit to a ‘0’.
The Timer2 prescale r is co ntro lle d by the T2CKPS bits
in the T2CON register. The Timer2 postscaler is
controlled by the TOUTPS bits in the T2CON register.
The prescaler and postscaler counters are cleared
when:
• A write to TMR2 occurs.
• A write to T2CON occurs.
• Any device Reset oc curs (Power-on Rese t, MCLR
Reset, Watchdog Timer Reset or Brown-out
Reset).
comparator represents the uncertainty
due to input offsets and response time.
8.0COMPARATOR MODULE
Comparators are used to interface analog circuits to a
digital circuit by comparing two analog voltages and
providing a digital indication of their relative magnitudes.
The comparators are very useful mixed signal building
blocks because they provide analog functionality
independent of program execution. The Analog
Comparator module includes the following features:
• Independent comparator control
• Programmable input selection
• Comparator output is available internally/externally
• Programmable output polarity
• Interrupt-on-change
• Wake-up from Sleep
•PWM shutdown
• Time r1 gate (co unt ena ble )
• Output synchronization to Timer1 clock input
•SR Latch
• Programmable and Fixed V olt age R eference
Note:Only Comparator C2 can be linked to
Timer1.
8.1 Comparator Overview
FIGURE 8-1:SINGLE COMPARATOR
A single comparator is shown in Figure 8-1 along with
the relationship between the analog input levels and
the digital output. When the analog voltage at V
less than the analog voltage at V
comparator is a digital low level. When the analog
voltage at V
V
IN-, the output of the comp arator is a dig ital hig h level.
Note 1:When C1ON = 0, the C1 comparator will produce a ‘0’ output to the XOR Gate.
2:Q1 and Q3 are phases of the four-phase system clock (F
OSC).
3:Q1 is held high during Sleep mode.
0
1
C1R
MUX
C1IN+
0
1
MUX
To other peripherals
C1OUT (to SR latch)
CVREF
C1VREN
FixedRef
MUX
C2
C2POL
C2OUT
0
1
2
3
C2ON
(1)
C2CH<1:0>
2
From TMR1
Clock
DQ
EN
DQ
EN
CL
DQ
RD_CM2CON0
Q3*RD_CM2CON0
Q1
Set C2IF
To
NRESET
C2V
IN-
C2V
IN+
C12IN0C12IN1C12IN2C12IN3-
0
1
C2SYNC
C2POL
Data Bus
MUX
Note 1:When C2ON = 0, the C2 comparator will produce a ‘0’ output to the XOR Gate.
2:Q1 and Q3 are phases of the four-phase system clock (F
OSC).
3:Q1 is held high during Sleep mode.
0
1
C2R
CV
REF
MUX
C2IN+
0
1
MUX
SYNCC2OUT
to Timer1 Gate, SR latch
C2V
REN
FixedRef
and other peripherals
FIGURE 8-2:COMPARATOR C1 SIMPLIFIED BLOCK DIAGRAM
FIGURE 8-3:COMPARATOR C2 SIMPLIFIED BLOCK DIAGRAM
DS40001262F-page 92 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
8.2Comparator Control
Each comparator has a separate control and
Configuration register: CM1CON0 for Comparator C1
and CM2CON0 for Comparator C2. In addition,
Comparator C2 has a second control register,
CM2CON1, for controlling the interaction with Timer1 and
simultaneous reading of both comparator outputs.
The CM1CON0 and CM2CON0 registers (see Registers
8-1 and 8-2, respe ctively) cont ain the contro l and St atus
bits for the follow ing:
• Enable
• Input selection
• Reference selection
•Output selection
• Output polarity
8.2.1COMPARATOR 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.
8.2.2COMPARATOR INPUT SELECTION
The CxCH<1:0> bits of the CMxCON0 register direct
one of four analog input pins to the comparator
inverting input.
Note:To use CxIN+ and C12INx- pins a s analog
inputs, the appropriate bits must be set in
the ANSEL register and the
correspondi n g TR IS b i ts mus t al so b e se t
to disable the output drivers.
8.2.3COMPARATOR REFERENCE
SELECTION
Setting the CxR bit of the CMxCON0 register directs an
internal voltage reference or an analog input pin to the
non-inverting input of the comparator. See Section 8.9
“Comparator SR Latch” for more information on the
Internal V ol tage Reference module.
8.2.4COMPARATOR OUTPUT
SELECTION
The output of the comparator can be monitored by
reading either the CxOUT bit of the CMxCON0 register
or the MCxOUT bit of the CM2CON1 register. In order
to make the output available for an external connection,
the following conditions must be true:
• CxOE bit of the CMxCON0 register must be set
• Corresponding TRIS bit must be cleared
• CxON bit of the CMxCON0 register must be set
Note 1: The CxOE bit overrides the PORT data
latch. Setting the Cx ON has no im pact on
the port ov erride.
2: The internal output of the comparator is
latched with each instruction cycle.
Unless otherwise specified, external
outputs are not latched.
8.2.5COMPARATOR 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 result s in a non-inverted output.
Table 8-1 shows the output state versus input
conditions, including polarity control.
T ABLE 8-1:COMPARATOR OUTPUT
STATE VS. INPUT CONDITIONS
Input ConditionCxPOLCxOUT
CxV
IN- > CxVIN+00
CxVIN- < CxVIN+01
CxVIN- > CxVIN+11
IN- < CxVIN+10
CxV
8.3Comparator 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 volta ge. This period is referred to as
the response time. The response time of the
comparator differs from the settling time of the voltage
reference. Therefore, both of these times must be
considered when determining the total response time
to a comparator input change. See the Comparator and
Voltage Reference Specifications in Section 17.0
The comparator interrupt flag can be set whenever
there is a chang e in the o utput val ue of the compa rator .
Changes are recognized by means of a mismatch
circuit which consists of two latches and an exclusiveor gate (see Figure 8 -2 an d Figure8-3). One latch is
updated with the comparator output level when the
CMxCON0 register i s read . Th is la tc h re t ai ns the v alu e
until the next read of the CMxCON0 register or the
occurrence of a Reset. The other latc h of the mism atch
circuit is updated on every Q1 system clock. A
mismatch condition will occur when a comparator
output change is clocked through the second latch on
the Q1 clock cycle. At this point the two mismatch
latches have opposite output levels which is detected
by the exclusive-or gate and fed to the interrupt
circuitry. The mismatch condition persists until either
the CMxCON0 register is read or the comparator
output returns to the previous state.
Note 1: A write operation to the CMxCON0
register will also clear the mismatch
condition because all writes include a read
operation at the beginning of the write
cycle.
2: Comparator interrupts will operate
correctly regardless of the state of CxOE.
FIGURE 8-4:COMPARATOR
INTERRUPT TIMING W/O
CMxCON0 READ
FIGURE 8-5:COMPARATOR
INTERRUPT TIMING WITH
CMxCON0 READ
The comparator interrupt is set by the mismatch edge
and not the mismatch level. This means that the interrupt flag can be reset without the additional step of
reading or writing the CMxCON0 register to clear the
mismatch regi sters. Wh en the mism atch regis ters are
cleared, an interrup t will oc cur upon the compara tor’s
return to the previous state, otherwise no interrupt will
be generated.
Software will need to maintain information about the
status of the comparator output, as read from the
CMxCON0 register, or CM2CON1 register, to determine
the actual change that has occurred.
The CxIF bit of the PIR1 register is the comparator
interrupt flag. This bit must be reset in software by
clearing it to ‘0’. Since it is als o poss ible t o writ e a ‘1’ to
this register, an interrupt can be generated.
The CxIE bit of the PIE1 re gister and the PEIE and GIE
bits of the INTCON register must all be set to enable
comparator interrupts. If any of these bits are cleared,
the interrupt is not enabl ed, althou gh the CxIF bi t of the
PIR1 register will still be set if an interrupt condition
occurs.
Note 1: If a change in the CMxCON0 register
(CxOUT) should occur when a read
operation is being executed (start of the
Q2 cycle), then the CxIF of the PIR1
register interrupt flag may not get set.
2: When either comparator is first enabled,
bias circuitry in the Comparator module
may cause an invalid output from the
comparator until the bias circuitry is
stable. Allow about 1 s for bias settling
then clear the mismatch condition and
interrupt flags before enabling
comparator interrupt s.
DS40001262F-page 94 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
8.5Operation During Sleep
The comparator , if enabled b efore entering Sleep mode,
remains active during Sleep. The additional current
consumed by the comparator is show n sep arately in the
Section 17.0 “Electrical Specifications”. If the
comparator is not used to wake the device, power
consumption can be minimized while in Sleep mode by
turning off the comparator . Each comparator is turned off
by clearing the CxON bit of the CMxCON0 register.
A change to the comparator output can wake-up the
device from Sleep. To enable the comparator to wake
the device from Sleep, the CxIE bit of the PIE1 reg ister
and the PEIE bit of the INTCON register must be set.
The instruction following the Sleep instruction always
executes following a wake from Sleep. If the GIE bit of
the INTCON register is also set, the device will then
execute the Interrupt Service Routine.
8.6Effects of a Reset
A device Reset forces the CMxCON0 and CM2CON1
registers to their Reset states. This forces both
comparators and the voltage references to their OFF
states.
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 Section 17.0 “Electrical Specifications”
8.7Analog Input Connection
Considerations
A simplified circuit for an analog input is shown in
Figure 8-6. Since the analog inpu t pins sh are their co n-
nection with a digital input, they have reverse biased
ESD protection diodes to V
input, therefore, must be between V
input voltage deviates from this range by more than
0.6V in either direction, one of the diodes is forward
biased and a latch-up may occur.
A maximum source impedance of 10 k is recommended
for the analog sources. Also, any external component
connected to an analog input pin, such as a capacitor or
a Zener diode, should have very little leakage curr ent to
minimize inaccuracies introduced.
FIGURE 8-6:ANALOG INPUT MODEL
DD and VSS. The analog
SS and VDD. If the
Note 1: When reading a PORT register, all pins
configured as ana log input s wi ll 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 c ause the input buf fer to
consume more current than is specified.
DS40001262F-page 98 2005-2015 Microchip Technology Inc.
PIC16F631/677/685/687/689/690
8.8Additional Comparator Features
There are three additional comparator features:
• Timer1 count enable (gate)
• Synchronizing output with Timer1
• Simultaneous read of comparator outputs
8.8.1COMPARATOR C2 GATING TIMER1
This feature can be used to time the duration or interval
of analog events. Clearing the T1GSS bit of the
CM2CON1 register will enable Timer1 to increment
based on the output of Comparator C2. This requires
that Timer1 is on and gating is enabled. See
Section 6.0 “Timer1 Module with Gate Control” for
details.
It is recommended to synchronize the comparator with
Timer1 by setting the C2SYNC bit when the comparator
is used as t he T imer1 gate sou rce. T his ensur es T imer1
does not miss an increme nt if the compar a tor ch an ges
during an increment.
8.8.2SYNCHRONIZING COMPARATOR
C2 OUTPUT TO TIMER1
The Comparator C2 output can be synchronized with
Timer1 by setting the C2SYNC bit of the CM2CON1
register. When enabled, the C2 output is latched on the
falling edge of the Timer1 clock source. 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 latch ed on the falling e dge of the
Timer1 clock source and Timer1 increments on the
rising edge of its clock source. See the Comparator
Block Diagram (Figure 8-3) and the Timer1 Block
Diagram (Figure 6-1) for more information.
8.8.3SIMULTANEOUS COMPARATOR
OUTPUT READ
The MC1OUT and MC2OUT bits of the CM2CON1
register are mirror copies of both comparator outputs.
The ability to read both outputs simultaneously from a
single register eliminates the timing skew of reading
separate regi sters.
Note 1: Obtaining the status of C1OUT or
C2OUT by reading CM2CON1 does not
affect the comparator interrupt mismatch
registers.
REGISTER 8-3:CM2CON1: COMPARATOR C2 CONTROL REGISTER 1
R-0R-0U-0U-0U-0U-0R/W-1R/W-0
MC1OUTMC2OUT————T1GSSC2SYNC
bit 7bit 0
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
bit 7MC1OUT: Mirror Copy of C1OUT bit
bit 6MC2OUT: Mirror Copy of C2OUT bit
bit 5-2Unimplemented: Read as ‘0’
bit 1T1GSS: Timer1 Gate Source Select bit
1 = Timer1 gate source is T1G
0 = Timer1 gate source is SYNCC2OUT.
bit 0C2SYNC: Comparator C2 Output Synchronization bit
1 = Output is synchronous to falling edge of Timer1 clock
0 = Output is asynchronous
Note 1:If R = 1 and S = 1 simultaneously, Q = 0, Q = 1
2:Pulse generator causes a 1/2 Q-state (1 Tosc) pulse width.
3:Output shown for reference only. See I/O port pin block diagram for more detail.
Pulse
Gen
(2)
Pulse
Gen
(2)
SYNCC2OUT (from comparator)
C1OUT (from comparator)
C2OUT pin
(3)
C1OE
C1OUT pin
(3)
0
1
MUX
1
0
MUX
SR
Latch
(1)
8.9Comparator SR Latch
The SR Latch module provides additional control of the
comparator outputs. The module consists of a single
SR latch and output multiplexers. The SR latch can be
set, reset or toggled by the comparator outputs. The SR
latch may also be set or reset, independent of
comparator output, by control bits in the SRCON control
register. The SR latch output multiplexers select
whether the latch outputs or the comparator outputs are
directed to the I/O port logic for eventual output to a pin.
8.9.2LATCH OUTPUT
The SR<1:0> bits of the SRCON register control the
latch output multiplexers and determine four possible
output configurations. In these four configurations, the
CxOUT I/O port logic is connected to:
• C1OUT and C2OUT
• C1OUT and SR latch Q
• C2OUT and SR latch Q
• SR latch Q and Q
After any Reset, the default output configuration is the
8.9.1LATCH OPERATION
The latch is a Set-Reset latch that does not depend on a
clock source. Each of the Set and Reset inputs are
active-high. Each latch input is connected to a
comparator output and a software controlled pulse
generator. The latch can be set by C1OUT or the PULSS
bit of the SRCON register. The latch can be reset by
C2OUT or the PULSR bit of the SRCON register. The
latch is reset-dominant, therefore, if both Set and Reset
inputs are high, the latch will go to the Reset st ate. Both
unlatched C1OUT and C2OUT mode. This maintains
compatibility with devices that do not have the SR latch
feature.
The applicable TRIS bits of the corresponding ports
must be cleared to enable the port pin output drivers.
Additionally, the CxOE comparator output enable bits of
the CMxCON0 registers must be set in order to make the
comparator or latch outputs available on the output pins.
The latch configuration enable states are completely
independent of the enable states for the comparators.
the PULSS and PULSR bits are self resetting which
means that a single write to either of the bits is all that is
necessary to complete a latch set or reset operation.
FIGURE 8-7:SR LATCH SIMPLIFIED BLOCK DIAGRAM
DS40001262F-page 100 2005-2015 Microchip Technology Inc.
Loading...
+ hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.