*8-bit, 8-pin Devices Protecte d by Microchip’ s Low Pin Coun t Patent: U. S. Patent N o. 5,847,450. Additi onal U.S. and
foreign patents and applications may be issued or pending.
Note the following details of the code protection feature on Microchip devices:
•Microchip products meet the specification contained in their particular Microchip Data Sheet.
•Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
•There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
•Microchip is willing to work with the customer who is concerned about the integrity of their code.
•Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digit al Millennium Copyright Act. If suc h a c t s
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device
applications and t he lik e is provided only for your convenience
and may be su perseded by upda t es . It is y our responsibility to
ensure that your application meets with your specifications.
MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHER EXPRESS OR
IMPLIED, WRITTEN OR ORAL, STATUTORY OR
OTHERWISE, RELATED TO THE INFORMATION,
INCLUDING BUT NOT LIMITED TO ITS CONDITION,
QUALITY, PERFORMANCE, MERCHANTABILITY OR
FITNESS FOR PURPOSE. Microchip disclaims all liability
arising from this information and its use. Use of Microchip
devices in life supp ort and/or safety ap plications is entir ely at
the buyer’s risk, and the buyer agrees to defend, indemnify and
hold harmless M icrochip from any and all dama ges, claims,
suits, or expenses re sulting from such use. No licens es are
conveyed, implicitly or otherwise, under any Microchip
intellectual property rights.
Trademarks
The Microchip name and logo, the Microchip logo, Accuron,
dsPIC, K
EELOQ, KEELOQ logo, microID, MPLAB, PIC,
PICmicro, PICSTART, PRO MATE, PowerSmart, rfP IC, and
SmartShunt are registered trademarks of Microchip
Technology Incorporated in the U.S.A. and other countries.
AmpLab, FilterLab, Linear Active Thermistor, Migratable
Memory, MX DEV, MXLAB, PS logo, SEEVAL, SmartSensor
and The Embedded Control Solutions Company are
registered trademarks of Microchip Technology Incorporated
in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, CodeGuard,
dsPICDEM, dsPICDEM.net, dsPICworks, ECAN,
ECONOMONITOR, FanSense, FlexROM, fuzzyLAB,
In-Circuit Serial Programming, ICSP, ICEPIC, Mindi, MiWi,
MPASM, MPLAB Certified logo, MPLIB, MPLINK, PICkit,
PICDEM, PICDEM.net, PICLAB, PICtail, PowerCal,
PowerInfo, PowerMate, PowerTool, REAL ICE, rfLAB,
rfPICDEM, Select Mode, Smart Serial, SmartT el, Total
Endurance, UNI/O, WiperLock and ZENA are trademarks of
Microchip Technology Incorporated in the U.S.A. and other
countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
Microchip received ISO/TS-16949:2002 certification for its worldwide
headquarters, design and wafer fabrication facilities in Chandler and
Tempe, Arizona, Gresham, Oregon and Mountain View, California. The
Company’s quality system processes and procedures are for its PIC
MCUs and dsPIC® DSCs, KEELOQ
EEPROMs, microperipherals, nonvolatile memory and analog
products. In addition, Microchip’s quality system for the design and
manufacture of development systems is ISO 9001:2000 certified.
11.0 Analog Front-End (AFE) Functional Description (PIC16F639 Only) .......................................................................................... 97
12.0 Special Features of the CPU......................... ........................... ........................................ ........................................................ 129
13.0 Instruction Set Summary.......................................................................................................................................................... 149
14.0 Development Support............................................................................................................................................................... 159
16.0 DC and AC Characteristics Graphs and Tables.......................................................................................................................191
Systems Information and Upgrade Hot Line..................................................................................................................................... 223
Worldwide Sales and Service ...................................................... ........................... .......................................................................... 232
It is our intention to provide our valued customers with the best documentation possible to ensure successful use of your Microchip
products. To this end, we will continue to improve our publications to better suit your needs. Our publications will be refined and
enhanced as new volumes and updates are introduced.
If you have any questions or c omm ents regarding t his publication, p lease c ontact the M arket ing Co mmunications Department via
E-mail at docerrors@microchip.com or fax the Reader Response Form in the back of this data sheet to (480) 792-4150. We
welcome your feedback.
Most Current Data Sheet
To obtain the most up-to-date version of this data sheet, please register at our Worldwide Web site at:
http://www.microchip.com
You can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page.
The last character of the literature number is the version number, (e.g., DS30000A is version A of document DS30000).
Errata
An errata sheet, describing minor operational differences from the data sheet and recommended workarounds, may exist for current
devices. As device/documentation issues become known to us, we will publish an errata sheet. The errata will specify the revision
of silicon and revision of document to which it applies.
To determine if an errata sheet exists for a particular device, please check with one of the following:
• Microchip’s Worldwide Web site; http://www.microchip.com
• Your local Microchip sales office (see last page)
When contacting a sales office, please specify which device, revision of silicon and data sheet (include literature number) you are
using.
Customer Notification System
Register on our web site at www.microchip.com to receive the most current information on all of our products.
LCCOMLCCOMAN—Common reference for analog inputs.
LCXLCXAN—125 kHz analog X channel input.
LCYLCYAN—125 kHz analog Y channel input.
LCZLCZAN—125 kHz analog Z channel input.
RA0/C1IN+/ICSPDAT/ULPWURA0TTL—General purpose I/O. Individually controlled interrupt-on-change.
C1IN+AN—Comparator1 input – positive.
ICSPDATTTLCMOS Serial Programming Data IO.
ULPWUAN—Ultra Low-Power Wake-up input.
RA1/C1IN-/V
RA2/T0CKI/INT/C1OUTRA2STCMOS General purpose I/O. Individually controlled interrupt-on-change.
RA3/MCLR
RA4/T1G
RA5/T1CKI/OSC1/CLKINRA5TTLCMOS General purpose I/O. Individually controlled interrupt-on-change.
RC0/C2IN+RC0TTLCMOS General purpose I/O.
RC1/C2IN-/CS
RC2/SCLK/ALERT
Legend:AN = Analog input or outputCMOS = CMOS compatible input or outputD= Direct
REF/ICSPCLKRA1TTLCMOS General purpose I/O. Individually controlled interrupt-on-change.
C1IN-AN—Comparator1 input – negative.
REFAN—External voltage reference
V
ICSPCLKST—Serial Programming Clock.
T0CKIST—External clock for Timer0.
INTST—External Interrupt.
C1OUT—CMOS Comparator1 output.
/VPP
/OSC2/CLKOUT
HV = High VoltageST= Schmitt Trigger input with CMOS levelsOD = Open Drain
TTL = TTL compatible inputXTAL = Crystal
RA3TTL
MCLR
V
PPHV—Programming voltage.
RA4TTLCMOS General purpose I/O. Individually controlled interrupt-on-change.
T1G
OSC2—XTALXTAL connection.
CLKOUT—CMOS T
T1CKIST—Timer1 clock.
OSC1XTAL—XTAL connection.
CLKINST—T
C2IN+AN—Comparator1 input – positive.
RC1TTLCMOS General purpose I/O.
C2IN-AN—Comparator1 input – negative.
CS
RC2TTLCMOS General purpose I/O.
SCLKTTL—Digital clock input for SPI communication.
The PIC12F635/PIC16F636/639 devices have a 13-bit
program counter capable of addressing an 8K x 14
program memory space. Only the first 1K x 14
(0000h-03FFh, for the PIC12F635) and 2K x 14
(0000h-07FFh, for the PIC16F636/639) is physically
implemented. Accessing a location above these
boundaries will cause a wraparound within the first
2K x 14 space. The Reset vector is at 0000h and the
interrupt vector is at 0004h (see Figure 2-1).
2.2Data Memory Organization
The data memory (see Figure 2-2) is partitioned into
two 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. Register locations
20h-7Fh in Bank 0 and A0h-BFh in Bank 1 are GPRs,
implemented as static RAM for the PIC16F636/639.
For the PIC12F635, reg ister locat ions 4 0h throug h 7Fh
are GPRs implemented as static RAM. Register
locations F0h-FFh in Bank 1 point to addresses
70h-7Fh in Bank 0. All other RAM is unimplemented
and returns ‘0’ when read. RP0 of the STATUS register
is the bank select bit.
RP1
RP0
00→Bank 0 is selected
01→Bank 1 is selected
10→Bank 2 is selected
11→Bank 3 is selected
The register file is organized as 64 x 8 for the
PIC12F635 and 128 x 8 for the PIC16F636/639. Each
register is accessed, either directly or indirectly,
through the File Select R egister, FSR (see Section 2.4“Indirect Addressing, INDF and FSR Registers”).
2.2.2SPECIAL FUNCTION REGISTERS
The Special Function Registers (SFRs) are registers
used by the CPU and peripheral functions for controlling
the desired operation of the device (see Figure 2-1).
These registers are static RAM.
The special re gisters can be classifi ed into two sets:
core and peripheral. The Special Function Registers
associated with the “c ore” are des cribed in this sect ion.
Those related to the operation of the peripheral
features are described in the section of that peripheral
feature.
EELOQ hardware peripheral rel ated reg is ters and require the execution o f the
®
Encoder License Agreement” may be accessed through the Microchip web site
EELOQ or by contacting your local Microchip Sales Representative.
PIC12F635/PIC16F636/639
TABLE 2-1:PIC12F635 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
01hTMR0T imer0 Module Registerxxxx xxxx61,137
02hPCLProgram Counter’s (PC) Least Significant Byte0000 000032,137
03hSTATUSIRPRP1RP0TO
04hFSRIndirect Data Me mory Address Pointerxxxx xxxx32,137
05hGPIO
06h—Unimplemented——
07h—Unimplemented——
08h—Unimplemented——
09h—Unimplemented——
0AhPCLATH
0BhINTCONGIE PEIET0IEINTERAIET0IFINTFRAIF
0Ch PIR1
0Dh—Unimplemented——
0EhTMR1LHolding Register for the Least Significant Byte of the 16-bit TMR1xxxx xxxx64,137
0FhTMR1HHolding Register for the Most Significant Byte of the 16-bit TMR1xxxx xxxx64,137
Legend:– = Unimplemented locations read as ‘0’, u = unchanged, x = unknown, q = value depends on condition,
Note 1:Other (non Power-up) Resets include MCLR
2:MCLR
(not a physical register)
PDZDC C0001 1xxx26,137
——GP5GP4GP3GP2GP1GP0--xx xx0047,137
———Write Buffer for upper 5 bits of Program Counter---0 000032,137
Legend:– = Unimplemented locations read as ‘0’, u = unchanged, x = unknown, q = value depends on condition,
Note 1:Other (non Power-up) Resets include MCLR
2:CRDAT<3:0> registers are K
(2)
Cryptographic Data Register 00000 0000 0000 0000
(2)
Cryptographic Data Register 10000 0000 0000 0000
(2)
Cryptographic Data Register 20000 0000 0000 0000
(2)
Cryptographic Data Register 30000 0000 0000 0000
shaded = unimplemented
Encoder License Agreement” regarding implementation of the module and access to related registers. The “K
Encoder License Agreement” may be accessed through the Microchip web site located at www.microchip.com/K
or by contacting your local Microchip Sales Representative.
ENC/DEC————CRREG1 CRREG0 00-- --00 00-- --00
Reset and Watchdog Timer Reset during normal operation.
hardware peripheral related registers and require the execution of the “KEELOQ
The STATUS register, shown i n Re gis t er2-1, contains:
• the arithmetic status of the 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 register as destin ation may be di fferent 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 Section13.0 “Instruction Set
Summary”
Note 1: The C and DC bits operate as a Borrow
and Digit Borrow out bit, respectively, in
subtraction.
REGISTER 2-1:STATUS: STATUS REGISTER
R/W-0R/W-0R/W-0R-1R-1R/W-xR/W-xR/W-x
IRPRP1RP0TOPDZDC
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
(1)
(1)
C
bit 7IRP: Register Bank Select bit (used for indirect 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 (ADDWF, ADDLW,SUBLW,SUBWF instructions)
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 the 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
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, the polarity is reversed. A subtraction is executed by adding the two’s complement of the
second operand. For rotate (RRF, RLF) instructions, t his bit i s loaded with eithe r the high -order or low -order
bit of the source register.
The INTCON register is a readable and writable
register which co nt ains th e vari ous e nable and fl ag bits
for TMR0 register overflow, PORTA change and
external RA2/INT pin interrupts.
Note:Interrupt flag bits are set when an interrupt
condition occurs, regard less of the state of
its corresponding enable bit or the Global
Interrupt 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
GIEPEIET0IEINTERAIE
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 Overflow Interrupt 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 3RAIE: PORTA Change Interrupt Enable bit
1 = Enables the PORTA change interrupt
0 = Disables the PORTA change interrupt
bit 2T0IF: Timer0 Overflow Interrupt Flag bit
1 = TMR0 register 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 0RAIF: PORTA Change Interrupt Flag bit
1 = When at least one of the PORTA general purpose I/O pins changed state (must be cleared in
software)
0 = None of the PORTA general purpose I/O pins have changed state
(2)
(1,3)
(1,3)
T0IF
(2)
INTFRAIF
Note 1:IOCA register must also be enabled.
2:T0IF bit is set when Timer0 rolls over. Timer0 is unchanged on Reset and should be initialized before
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 7EEIF: EE Write Complete Interrupt Flag bit
1 = The write operation completed (must be cleared in software)
0 = The write operation has not completed or has not been started
bit 6LVDIF: Low-Voltage Detect Interrupt Flag bit
1 = The supply voltage has crossed selected LVD voltage (must be cleared in software)
0 = The supply voltage has not crossed selected LVD voltage
bit 5CRIF: Cryptographic Interrupt Flag bit
1 = The Cryptographic module has completed an operation (must be cleared in software)
0 = The Cryptographic module has not completed an operation or is Idle
bit 4C2IF: Comparator 2 Interrupt Flag bit
1 = Comparator output (C2OUT bit) has changed (must be cleared in software)
0 = Comparator output (C2OUT bit) has not changed
bit 3C1IF: Comparator 1 Interrupt Flag bit
1 = Comparator output (C1OUT bit) has changed (must be cleared in software)
0 = Comparator output (C1OUT bit) has not changed
bit 2OSFIF: Oscillator Fail Interrupt Flag bit
1 = System oscillator failed, clock input has changed INTOSC (must be cleared in software)
0 = System clock operating
bit 1Unimplemented: Read as ‘0’
bit 0TMR1IF: Timer1 Overflow Interrupt Flag bit
1 = Timer1 rolled over (must be cleared in software)
0 = Timer1 has not rolled over
(1)
(1)
Note:Interrupt f lag bit s are set when an in terrupt
condition occurs, regar dless of the st ate of
its corresponding enable bit or the Global
Interrupt Enable bit, GIE of the INTCON
register. User software should ensure the
appropriate interrupt flag bits are clear
prior to enabling an interrupt.
The Program Counter (PC) is 13 bits wide. The low byte
comes from the PCL register, which is a readable and
writable register. The high byte (PC<12:8>) is not
directly readable or writable and comes from PCLATH.
On any Reset, the PC is cleared. Figure 2-5 shows the
two situations for the loading of the PC. The upper
example in Figure 2-5 shows how the PC is loaded on a
write to PCL (PCLATH<4:0> → PCH). The lower
example in Figure 2-5 shows how the PC is loaded
during a CALL or GOTO instruction (PCLATH<4:3> →
PCH).
FIGURE 2-5:LOADING OF PC IN
DIFFERENT SITUATIONS
PCHPCL
128 70
PC
PCLATH<4:0>
5
PCLATH
PCHPCL
12 11 100
PC
2
87
PCLATH<4:3>
PCLATH
11
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. This allows the entire
contents of the program counter to be changed by
writing the desired up per 5 bit s to the PCLATH register.
When the lower 8 bits are written to the PCL regis ter , all
13 bits of the program counter will chan ge to the values
contained 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 lower 8 bits of the memory
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).
Instruction with
PCL as
Destination
8
ALU Result
GOTO, CALL
Opcode<10:0>
2.3.2STACK
The PIC12F635/PIC16F636/639 family has an
8-level x 13-bit wide hardware stack (see Figure 2-1).
The stack space is not part of either program or data
space and the S ta ck Pointer i s not rea dable or writa ble.
The PC is PUSHed onto the stack when a CALL
instruction is execute d or an interrupt ca uses a branc h.
The stack is POP ed in the event of a RETURN, RETLW
or a RETFIE instruction execution. PCLATH is not
affected by a P USH or POP operation.
The stack operat es as a circular buf fer . This means th at
after the stack has been PUSHed eight times, the ninth
push overwrites the va lue tha t was s tored fro m the first
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 exec ution of the CALL,RETURN, RETLW and RETFIE instru ction s
or the vectoring to an interrupt address.
2.4Indirect Addressing, INDF and
FSR Registers
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 pointed to by the File Select
Register (FSR). Reading INDF itself indirectly will
produce 00h. Writing to the INDF register indirectly
results in a no operation (although Status bits may be
affected). An effective 9-bit address is obtained by
concatenating the 8-bit FSR and the IRP bit of the
STATUS register, as shown in Figure 2-6.
A simple program to clear RAM location 20h-2Fh using
indirect addressing is shown in Example 2-1.
EXAMPLE 2-1:INDIR ECT ADDRESSING
MOVLW 0x20;initialize pointer
MOVWF FSR;to RAM
NEXTCLRFINDF;clear INDF register
INCFFSR;INC POINTER
BTFSS FSR,4;all done?
GOTONEXT;no clear next
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 internal via software.
• Two-Speed Start-up mode, which minimizes
latency between external oscillator start-up and
code execution.
• Fail-Safe Clock Monitor (FSCM) designed to
detect a failure of the external clock source (LP,
XT, HS, EC or RC modes) and switch
automatically to the internal oscillator.
The Oscillator mod ule can be c onfigured in one of eig ht
clock modes.
1.EC – External clock with I/O on OSC2/CLKOU T.
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 FOSC <2:0>
bits in the Configuration Word register (CONFIG). The
internal clock can be generated from two internal
oscillators. The HFINTOSC is a calibrated
high-frequency oscillator. The LFINTOSC is an
uncalibrated low-frequency oscillator.
FIGURE 3-1:PIC® MCU CLOCK SOURCE BLOCK DIAGRAM
External Oscillator
OSC2
OSC1
Internal Oscillator
HFINTOSC
8 MHz
LFINTOSC
31 kHz
Sleep
Postscaler
IRCF<2:0>
(OSCCON Register)
8 MHz
4 MHz
2 MHz
1 MHz
500 kHz
250 kHz
125 kHz
31 kHz
111
110
101
100
011
010
001
000
LP, XT, HS, RC, RCIO, EC
MUX
FOSC<2:0>
(Configuration Word Register)
SCS<0>
(OSCCON Register)
MUX
(CPU and Peripherals)
INTOSC
Power-up Timer (PWRT)
Watchdog Time r (WDT)
Fail-Safe Clock Monitor (FSCM)
The Oscillator Control (OSCCON) register (Figure3-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 external clock defined by FOSC<2:0> of the Configuration Word
0 = Device is running from the internal oscillator (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 Configuration Word
Note 1:Bit resets to ‘0’ with Two-Speed Start-up and LP, XT or HS selected as the Oscillator mode or Fail-Safe
Clock Source modes can be classified as external or
internal.
• External Clock mod es rely on e xternal circui 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 Internal Oscillator
(LFINTOSC).
The system clock can be selected between external 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 st arte d 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 can be selected (see Section 3.7
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.
The LP, XT and HS modes support the use of quartz
crystal resonators or ceramic resonators connected to
OSC1 and OSC2 (Figu re 3-3). The mode selects a low ,
medium or high gain setting of the internal
inverter-amplifier to support various resonator types
and speed.
LP Oscillator mode selects the lowest gain setting of
the internal inverter-amplifier. LP mode current consumption is the least of the three modes. This mode is
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.
FIGURE 3-3:QUARTZ CRYSTAL
OPERATION (LP, XT OR
HS MODE)
PIC® MCU
OSC1/CLKIN
C1
Quartz
Crystal
RF
(2)
To Internal
Logic
Sleep
Note 1: Quartz crystal charac teristics vary a ccording
to type, package and manufacturer. The
user should consult the manu facturer data
sheets for sp ecifica tions and re comm ende d
application.
2: Always verify oscillator performance over
DD and temperature range that is
the V
expected for the application.
3: For oscillator design assistance, re ference
the following Microchip Applications Notes:
• AN826, “Crystal Oscillator Basics and
Crystal Selection for rfPIC
®
and PIC®
Devices” (DS00826)
®
• AN849, “Basic PIC
Oscillator Design”
(DS00849)
®
• AN943, “Practical PIC
Oscillator
Analysis and Design” (DS00943)
• AN949, “Making Your Oscillator Work”
(DS00949)
FIGURE 3-4:CERAMIC RESONATOR
OPERATION
(XT OR HS MODE)
PIC® MCU
OSC1/CLKIN
C1
C2
Ceramic
Resonator
RP
(3)
(1)
R
S
RF
OSC2/CLKOUT
(2)
To Internal
Logic
Sleep
C2
Note 1: A series resistor (RS) may be required for
Note 1: A series resistor (RS) may be required for
ceramic resonators with low drive level.
2: The value of R
selected (typically between 2 MΩ to 1 0 MΩ).
3: An additional parallel feedback resistor (R
may be required for proper ceramic resonator
operation.
F varies with the Oscillator mode
P)
PIC12F635/PIC16F636/639
e.
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 us ed to provide a cl ock
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
VDD
REXT
OSC1/CLKIN
CEXT
VSS
OSC/4 or
F
(2)
I/O
Recommended values: 10 kΩ ≤ REXT ≤ 100 kΩ, <3V
Note 1:Alternate pin functions are listed in the
2:Output depe nds upon RC or RCIO clo ck mod
OSC2/CLKOUT
Section 1.0 “Device Overview”.
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 (REXT) and capacito r (CEXT) values
and the operating temperature. Other factors affecting
the oscillator frequency are:
• threshold voltage variation
• component tolerances
• packaging variations in capacitance
The user also needs to take into account variation due
to tolerance of external RC components used.
PIC® MCU
(1)
3 kΩ ≤ R
C
EXT≤ 100 kΩ, 3-5V
EXT > 20 pF, 2-5V
Internal
Clock
3.5Internal Clock Modes
The Oscillator module has two independent, internal
oscillators that can be configured or selected as the
system clock source.
1.The HFINTOSC (High-Frequency Internal
Oscillator) is factory calibrated and operates at
8 MHz. The frequ ency of t he HFINT OSC can be
user-adju ste d 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 ca n be selec ted via sof tware
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 se lectio n
or the FOSC<2:0> bits in the Configuration Word
register (CONFIG). See Section 12.0 “SpecialFeatures of the CPU” for more information.
In INTOSC mode, OSC1/CLKIN i s available for general
purpose I/O. OSC2/CLKOUT outputs the selected
internal oscillator fre quency divide d by 4. The CLKO UT
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 setting 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 en able Two-Speed Start-up by se tting
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.
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 frequen cy
01110 =
•
•
•
00001 =
00000 = Oscillator module is running at the calibrated frequency.
11111 =
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 enabled:
• Two-Spe ed Start-up IESO bi t of the C o nfi gura tio n
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:Follow ing any Reset, the I RCF<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 bits to
select a different frequency.
3.5.5HF AND LF INTOSC CLOCK
SWITCH TIMING
When switching between the LFINTOSC and the
HFINTOSC, the new oscillator may already be shut
down to save power (see Figure 3-6). If this is the 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 register will reflect the 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 circuitry waits for a falling edge of
the current clock.
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 comple te.
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 A/C
Specifications (Oscillator Module) in Section 15.0“Electrical Specifications”.
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 Clock 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 regist er (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 T wo-Speed Sta rt-up or Fail-Safe
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.2OSCILLATOR 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 us e of the Sleep mode, Two-Speed
Start-up will remove the external oscillator start-up
time from the time spent awake and can reduce the
overall power consumption of the device.
This mode allows the application to wake-up from
Sleep, perform a few instructions using the INTOSC
as the clock source and go back to Sleep without
waiting for the primary oscillator to become stable.
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 Switchover bit (Two-Speed
Start-up 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
Two-Speed Start-up is disabled. This is because the
external clock oscillator does not require any
stabilization 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 fallin g edg e
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 bit 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.
The Fail-Safe Clock Monit or (FSCM) al low s the dev ic e
to continue operat ing sh oul d the external oscill 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 external oscillator modes (LP, XT, HS,
EC, RC and RCIO).
FIGURE 3-8:FSCM BLOCK DIAGRAM
Clock Monitor
External
Clock
LFINTOSC
Oscillator
31 kHz
(~32 μs)
Sample Clock
÷ 64
488 Hz
(~2 ms)
3.8.1FAIL-SAFE DETECTION
The FSCM module detects a failed oscillator by
comparing the ex tern al osci llator to the FS CM sa mple
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 failure is detected when an entire
half-cycle of the sample clock elapses before the
primary clock goes low.
Latch
S
R
Q
Q
Clock
Failure
Detected
3.8.3FAIL-SAFE CONDITION CLEARING
The Fail-Safe condition is cleared after a Reset,
executing a SLEEP instruction or togg ling the SC S bit
of the OSCCON register. When the SCS bit is toggled,
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-Saf e 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 OPERATI ON
When the external clock fails, the FSCM switches the
device clock to an internal cl ock sourc e and set s the bit
flag OSFIF of the PIR1 register. Setting this flag will
generate an interrupt if the OSFIE bit of the PIE1
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 c lock is normally at a mu ch higher frequency than the sam ple clock. The relative frequencies in
this example have been chosen for clarity.
Oscillator
Failure
Failure
Detected
TestTest
TABLE 3-2:SUMMARY OF REGISTERS ASSOCIATED WITH CLOCK SOURCES
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
CONFIG
INTCONGIE PEIE
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
There are as many as twelve 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
(Register 4-2). Setting a TRISA bit (= 1) will make the
corresponding PORTA pin an input (i.e., put the
corresponding output driver in a High-Impedance
mode). Clearing a TRISA bit (= 0) will make the
corresponding PORTA pin an output (i.e., put the
contents of the output latch on the selected pin). The
exception is RA3, which is input only and it s TRIS bit will
always read as ‘1’. Example4-1 shows how to initialize
PORT A.
Note:PORTA = GPIO
TRISA = TRISIO
Reading the PORTA register (Register 4-1) reads the
status of the pins, whereas writing to it will write to the
PORT latch. All write operations are read-modify-write
operations. Therefore, a write to a port implies that the
port pins are read, this value is modified a nd then written
to the PORT data latch. RA3 reads ‘0’ when MCLRE = 1.
The TRISA register controls the direction of the
PORT A pins, even when they are being used as an alog
inputs. The user must ensure the bits in the TRISA
register are maint ai ned set when using the m as an alog
inputs. I/O pins configured as analog inputs always
read ‘0’.
4.2Additional Pin Functions
Every PORTA pin on the PIC12F635/PIC16F636/639
has an interrupt-on-change option and a weak
pull-up/pull-down option. RA0 has an Ultra Low-Power
Wake-up option. The next three sections describe
these functions.
4.2.1WEAK PULL-UP/PULL-DOWN
Each of th e PORTA pins, excep t RA3, has a n inte rnal
weak pull-up and pull-down . The WDA bits select either
a pull-up or pull-down for an individual port bit.
Individual control bits can turn on the pull-up or
pull-down. These pull-u ps/pull-downs are automaticall y
turned off when the port pin is configured as an output,
as an alternate function or on a Power-on Reset,
setting the RAPU
pull-up on RA3 is enabled when configured as MCLR
in the Configuration Word register and disabled when
high voltage is detected, to reduce current
consumption through RA3, while in Programming
mode.
Note:PORTA = GPIO
bit of the OPTION register. A weak
TRISA = TRISIO
Note:The CMCON0 register must be initialized
to configure an analog chan nel as a digit al
input. Pins configu red as analo g input s will
read ‘0’.
EXAMPLE 4-1:INITIALIZING PORTA
BANKSEL PORTA;
CLRFPORTA;Init PORTA
MOVLW07h;Set RA<2:0> to
MOVWFCMCON0;digital I/O
BSFSTATUS,RP0;Bank 1
BCFSTATUS,RP1;
MOVLW0Ch;Set RA<3:2> as inputs
MOVWFTRISA;and set RA<5:4,1:0>
Each of the PORTA pins is individually configurable as
an interrupt-on-chang e pin. Control bit s, IOCAx, enable
or disable the interrupt function for each pin. Refer to
Register 4-5. The interrupt-on-change is disabled on a
Power-on Reset.
For enabled interrupt-on-change pins, the values are
compared with the old value la tched on the last rea d of
PORTA. The ‘mismatch’ outputs of the last read are
OR’d together to set t he PORT A C hange Interrupt Flag
bit (RAIF) in the INTCON register.
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 RAIF.
A mismatch c ond it i on wi ll cont i n ue to s et f lag bi t RA IF.
Reading PORTA will end the mismatch condition and
allow flag bit RAIF to be cleared. The latch holding the
last read value is not affected by a MCLR
Reset. After these Resets, the RAIF flag 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 bei ng executed
(start of the Q2 cycle), then the RAIF
interrupt flag may not get set.
nor BOR
REGISTER 4-5:IOCA: INTERRUPT-ON-CHANGE PORT A REGISTER
U-0U-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0
——IOCA5
bit 7bit 0
(2)
IOCA4
(2)
IOCA3
(3)
IOCA2IOCA1IOCA0
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 PORT A Control bits
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 current sink which can be
used to discharge a capacitor on RA0.
To use this feature, the RA0 pin is configured to output
‘1’ to charge the capacitor, interrupt-on-change for RA0
is enabled and RA0 is configured as an input. The
ULPWUE bit is set to begin the discharge and a SLEEP
instruction is performed. When the voltage on RA0 drops
below V
the device to wake-up. Depending on the state of the
GIE bit of the INTCON register, the device will either
jump to the interrupt vector (0004h) or execute the next
instruction when the interrupt event occurs. See
Section 4.2.2 “Interrupt-on-Change” and
Section 12.9.3 “PORTA 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 Ex ample 4-2 for initializing the Ultra
Low Power Wake-up module.
The series resistor provides overcurrent protection for the
RA0 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 provide the desired
interrupt delay. 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.
IL, an interrupt will be generated which will cause
EXAMPLE 4-2:ULTRA LOW-POWER
WAKE-UP INITIALIZATION
BANKSEL PORTA;
BSFPORTA,0;Set RA0 data latch
MOVLWH’7’;Turn off
MOVWFCMCON0; comparators
BANKSEL TRISA;
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
SLEEP;Wait for IOC
NOP;
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 , refe r to the appropriate section
in this data sheet.
FIGURE 4-1:BLOCK DIAGRAM OF RA0
Analog
(1)
RAPU
Data Bus
WR
WPUDA
RD
WPUDA
WR
WDA
RD
WDA
Input Mode
D
Q
CK
Q
D
Q
CK
Q
4.2.4.1RA0/C1IN+/ICSPDAT/ULPWU
Figure 4-2 shows the diagram for this pin. The RA0 pin
is configurable to function as one of the following:
• a general purpose I/O
• an analog input to the comparator
• In-Circuit Serial Programming™ data
• an analog input for the Ultra Low-Power Wake-up
VDD
Weak
Weak
VDD
WR
PORTA
WR
TRISA
RD
TRISA
RD
PORTA
WR
IOCA
RD
IOCA
Interrupt-on-
Change
D
Q
CK
Q
–
D
Q
CK
Q
01
Analog
Input Mode
D
Q
CK
Q
RD PORTA
(1)
D
Q
EN
D
Q
EN
+
ULPWUE
Q1
VT
IULP
SS
V
I/O pin
VSS
Note 1: Comparator mode determines Analog Input mode.
TABLE 4-1:SUMMARY OF REGISTERS ASSOCIATED WITH PORTA
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
PORTA
INTCONGIE
TMR1LHolding Register for the Least Significant Byte of the 16-bit TMR1 Registerxxxx xxxxuuuu uuuu
TMR1HHolding Register for the Most Significant Byte of the 16-bit TMR1 Registerxxxx xxxxuuuu uuuu
T1CONT1GINVTMR1GE T1CKPS1 T1CKPS0 T1OSCEN T1SYNC
CMCON1
CMCON0
OPTION_REG
TRISA
WPUDA
IOCA
WDA
Legend:x = unknown , u = unchanged, – = unimplemented locations read as ‘0’. Shaded cells are not used by PORTA.
PORTC is a general purpose I/O port consisting of 6
bidirectional pins . The pins can be con figured for e ither
digital I/O or analog input to comparator. For specific
information about individual functions, refer to the
appropriate section in this data sheet.
Note:The CMCON0 register must be initialized
to configure an analog chan nel as a digit al
input. Pins configu red as analo g input s will
read ‘0’.
EXAMPLE 4-3:INITIALIZING PORTC
BANKSEL PORTC;
CLRFPORTC;Init PORTC
MOVLW07h;Set RC<4,1:0> to
MOVWFCMCON0;digital I/O
BANKSEL TRISC;
MOVLW0Ch;Set RC<3:2> as inputs
MOVWFTRISC;and set RC<5:4,1:0>
;as outputs
REGISTER 4-6:PORTC: PORTC REGISTER
U-0U-0R/W-xR/W-xR/W-xR/W-xR/W-0R/W-0
——RC5RC4RC3RC2RC1RC0
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-0RC<5:0>: PORTC General Purpose I/O Pin bits
1 = Port pin is > V
0 = Port pin is < VIL
IH
REGISTER 4-7:TRISC: PORTC TRI-STATE REGISTER
U-0U-0R/W-1R/W-1R-1R/W-1R/W-1R/W-1
——TRISC5TRISC4TRISC3TRISC2TRISC1TRISC0
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-0TRISC<5:0>: PORTC Tri-State Control bits
1 = PORTC pin configured as an input (tri-stated)
0 = PORTC pin configured as an output
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 T im er0 mo dule ca n be use d
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 val ue written to the TMR0 register ca n
be adjusted, in order to ac count for th e 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
FOSC/4
0
1
T0CKI
pin
T0SE
WDTE
SWDTEN
31 kHz
INTOSC
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.
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 controlled by the PSA bit o f the OP TION
register. To assign t he p res caler to Timer0, th e PSA b it
must be cleared to a ‘0’.
There are 8 prescaler options for the Timer0 module
ranging from 1:2 to 1:256. The prescale values are
selectable vi a the PS<2:0> bit s of the OPTIO N register .
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
assigned to the Tim er0 module, all instructions w riting 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 from
Timer0 to the WDT module, the instr uction sequence
shown in Example 5-1, must be executed.
EXAMPLE 5-1:CHANGING PRESCALER
(TIMER0 → WDT)
BANKSEL TMR0;
CLRWDT;Clear WDT
CLRFTMR0;Clear TMR0 and
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:CHANGIN G PRESCALER
(WDT → TIMER0)
CLRWDT;Clear WDT and
;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.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 during 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 the
Section 15.0 “Electrical Specifications”.
TRISA——TRISA5 TRISA4 TRISA3 TRISA2 TRISA1 TRISA0 --11 1111 --11 1111Legend: – = Unimplemented l oc ati ons , re ad as ‘0’, u = unchanged, x = un kn ow n. Shad ed cells are not used by th e
The Timer1 module is a 16-bit timer/counter with the
following features:
• 16-bit timer/counter regist er pair (TMR1H: TMR1L)
• 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 (exter nal clock,
Asynchronous mode 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 exter nal 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
is FOSC/4. When TMR1CS = 1, the clock source is
supplied externally.
When the internal clock source is selected the
TMR1H:TMR1L register pair will increment on multiples
CY as determined by the Timer1 prescaler.
of T
6.2.2EXT ERN AL 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.
In Counter mode, a falling edge must be registered by
the counter prior to the first incrementing rising edge
after one or more of the following conditions:
• Timer1 is enabled after POR or BOR Reset
• A write to TMR1H or TMR1L
• T1CKI is high when Timer1 is disabled and when
Timer1 is reena bl ed T1 CKI is l ow. 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 enabl ed by settin g 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 in LP osci llator m ode. The us er 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
continues to increment asynchronous to the internal
phase clocks. The timer will continue to run during
Sleep and can generate an interrupt on overflow,
which will wake-up the processor. However, special
precautions in software are needed to read/write the
timer (see Section 6.5.1 “Reading and Writing
Timer1 in Asynchronous Counter Mode”).
Note: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 a single spurious
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 16-bi t 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 TMR1H:TTMR1L register
pair.
6.6Timer1 Gate
Timer1 gate source is software configurable to be the
pin or the output of Comparator 2. This allows the
T1G
device to directly time external events using T1G
analog events using Comparator 2. See the CMCON1
register (Register 7-3) for selecting the Timer1 gate
source. This feature can simplify the software for a
Delta-Sigma A/D converter and many other applications.
For more information on Delta-Sigma A/D converters,
see the Microchip web site (www.microchip.com).
Note:TMR1GE bit of the T1CON register must
be set to use either T1G
Timer1 gate so urce. See Register 7-3 for
more information on selecting the Timer1
gate source.
Timer1 gate can be inverted using the T1GINV bit of
the T1CON register , wheth er it originates from the T1G
pin or Comparator 2 output. This configures Timer1 to
measure either the active-high or active-low time
between events.
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:
• Timer1 interrupt enable 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.
Note:The TMR1 H:TTMR1L reg ister 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
The device will wake-up on an overflow and execute
the next instruction. If the GIE bit of the INTCON
register is set, the device will call the Interrupt Service
Routine (0004h).
6.9Comparator 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.
For more information, see Section 7.0 “Comparator
Module”.
FIGURE 6-2:TIMER1 INCREMENTING EDGE
T1CKI = 1
when TMR1
Enabled
T1CKI = 0
when TMR1
Enabled
Note 1: Arrows indicate counter increments.
2: In Counter mode, a falling edge must be registered by the counter prior to the firs t incrementing rising edge of
pin or C2OUT, as selected by the T1GSS bit of the CMCON1
PIC12F635/PIC16F636/639
TABLE 6-1:SUMMARY OF REGISTERS ASSOCIATED WITH TIMER1
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
CMCON1——————T 1G SSCMSYNC---- --1000-- --10
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.
Note 1:PIC16F636/639 only.
Comparators are used to interface analog circuits to a
digital circuit by comparing two analog voltages and
providing a digital indication of their relative magnitudes.
The comparators are very useful mixed signal building
blocks because they provide analog functionality
independent of the program execution. The Analog
Comparator module includes the following features:
• Dual comparators (PIC16F636/639 only)
• Multiple comparator configurations
• Comparator(s) output is available
internally/externally
• Programmable output polarity
• Interrupt-on-change
• Wake-up from Sleep
• Timer1 gate (co unt ena ble )
• Output synchronization to Timer1 clock input
• Programmable voltage reference
7.1 Comparator Overview
A comparator is shown in Figure 7-1 along with the
relationship between the analog input levels and the
digital output. When the analog voltage at VIN+ is less
than the analog voltage at V
IN-, the output of the
comparator is a digital low level. When the analog
voltage at V
V
IN-, the output of the comparat or is a digit al high le vel.
IN+ is greater than the analog voltage at
The PIC12F635 contains a single comparator as
shown in Figure 7-2.
The PIC16F636/639 devices contains two comparators
as shown in Figure 7-3 and Figure 7-4. The comparators
are not independently configurable.
FIGURE 7-1:SINGLE COMPARATOR
VIN+
IN-
V
VINVIN+
Output
Note:The black areas of the output of the
comparator represents the uncertainty
due to input offsets and response time.
A simplified circuit for an analog input is shown in
Figure 7-5. Since the analog i nput pins share thei r connection 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 7-5:ANALOG INPUT MODEL
DD and VSS. The analog
SS and VDD. If the
VDD
Note 1: When reading a PORT register, all pins
configured as anal og inp uts will read as a
‘0’. Pins configured as digital inputs will
convert as an analog input, according to
the input specification.
2: Analog levels on any pin defined as a
digital input, may cause the input buffer to
consume more current than is specified.
Rs < 10K
AIN
C
VA
Legend: CPIN= Input Capacitanc e
LEAKAGE = Leakage Current at the pin due to various junctions
There are eight mod es of operat ion fo r the comp arato r.
The CM<2:0> bits of th e CMCON0 reg ister are used to
select these modes as shown in Figures 7-6 and 7-7.
I/O lines change as a function of the mode and are
designed as follows:
• Analog function (A): digital input buffer is disabled
• Digital function (D): comparator digital output,
overrides port function
• Normal port function (I/O): independent of
The port pins denoted as “A” will read as a ‘0’
regardless of the state of the I/O pin or the I/O control
TRIS bit. Pins used as analog inputs should also have
the corresponding TRIS bit set to ‘1’ to disable the
digital output driver. Pins denoted as “D” should have
the corresponding TRIS bit set to ‘0’ to enable the
digital output driver.
Note:Comparator interr upts sh ould be dis abled
during a Comparator mode change to
prevent unintended interru pt s .
The CMCON0 register (Register 7-1) provides access
to the following comparator features:
• Mode selection
• Output state
• Output polarity
• Input switch
7.4.1COMPARATOR OUTPUT STATE
Each comparator state can always be read internally
via the CxOUT bit of the CMCON0 register. The comparator state ma y a ls o be directed to the CxO UT p in i n
the following modes:
PIC12F635
• CM<2:0> = 001
• CM<2:0> = 011
• CM<2:0> = 101
PIC16F636/639
• CM<2:0> = 110
When one of the above modes is selected, the
associated TRIS bit of the C xOUT pin mu st be cleare d.
7.4.2COMPARATOR OUTPUT POLARITY
Inverting the output of a comparator is functionally
equivalent to swapping the comparator inputs. The
polarity of a comparator output can be inverted by setting the C
XINV results in a non-inverted output. A complete
C
table showing the output state versus input conditions
and the polarity bit is shown in Table 7-1.
XINV bit of the CMCON0 register. Clearing
7.4.3COMPARATOR INPUT SWITCH
The inverting inpu t of the compa rators may be switc hed
between two analog pins in the following modes:
PIC12F635
•CM<2:0> = 101
•CM<2:0> = 110
PIC16F636/639
•CM<2:0> = 001 (Comparator C1 only)
•CM<2:0> = 010 (Comparators C1 and C2)
In the above modes, both pins remain in Analog mode
regardless of which pin is sele cted as the input. The
CIS bit of the CMCON0 register controls the comparator
input switch.
The comparator output is indeterminate for a period of
time after the chang e of an input source o r the selectio n
of a new reference voltage . This period is refe rred 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 ch ange. See the Comparato r and
Voltage Specifications in Section 15.0 “Electrical
Specifications” for more details.
7.6 Comparator Interrupt Operation
The compar ator in terrupt flag i s set wh eneve r there i s a
change in the output value of the comparator. Changes
are recognized by means of a mismatch circuit which
consists of two latc hes and an exclusiv e-or gate (see
Figures 7-8 and 7-9). One latch is updated with the
comparator output level when the CMCON0 register is
read. This latch retains the value until the next read of
the CMCON0 register or the occurrence of a Reset.
The other latc h of the mismatch circuit i s updated on
every Q1 system clock. A mismatch condition will occur
when a comparator outp ut change is clocked thro ugh
the second latch on t he Q1 cl ock cycl e. The mi smatch
condition will persist, holding the CxIF bit of the PIR1
register true , until e ither the CMCON0 re gister is read
or the comparator output returns to the previous state.
Note:A write operati on to the CMCON0 reg ister
will also clear the mismatch condition
because all writes include a read
operation at the beginning of the write
cycle.
Software will need to maintain information about the
status of the comparat or output to dete rmine 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 also possible to wr ite a ‘1’ to
this register, a simulated interrupt may be initiated.
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 CxI F bit of the
PIR1 register will still be set if an interrupt condition
occurs.
The user , in the Interru pt Service Routi ne, can cle ar the
interrupt in the following manner:
a) Any read or write of CMCON0. This will end the
mismatch condition. See Figures 7-8 and 7-9.
b) Clear the CxIF interrupt flag.
A persistent mismatch condition will preclude clearing
the CxIF interrupt flag. Reading CMCON0 will end the
mismatch condition and allow the CxIF bit to be
cleared.
Note:If a change in the CMCON0 register
(CxOUT) should occur when a read
operation is being executed (start of the
Q2 cycle), the n the CxIF int err upt flag m ay
not get set.
(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 interrupts.
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 15.0 “Electrical Specifications”. If the
comparator is not used to wake the device, power
consumption can be minimized while in Sleep mode by
7.8 Effects of a Reset
A device Reset forces the CMCON0 and CMCON1
registers to their R eset st ates . This forces th e Co mp arator module to be in the Comparator Reset mode
(CM<2:0> = 000). Thus, all comparator inputs are
analog inputs with the co mparator disab led to consume
the smallest current possible.
turning off the comparator. The comparator is turned off
by selectin g mod e C M<2:0 > = 000 or CM<2:0> = 111
of the CMCON0 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.
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 6COUT: Comparator Output bit
bit 5Unimplemented: Read as ‘0’
bit 4CINV: Comparator Output Inversion bit
bit 3CIS: Comparat or Input Switch bit
bit 2-0CM<2:0>: Comparator Mode bits (See Figure 7-5)
When CINV =
1 = VIN+ > VIN-
IN+ < VIN-
0 = V
When CINV =
1 = VIN+ < VIN0 = V
IN+ > VIN-
1 = Output inverted
0 = Output not inverted
When CM<2:0> =
1 = CIN+ connects to VIN0 = CIN- connects to V
When CM<2:0> =
CIS has no effect.
000 = CIN pins are configured as analog, COUT pin configured as I/O, Comparator output turned off
001 = CIN pins are configured as analog, COUT pin configured as Comparator output
010 = CIN pins are configured as analog, COUT pin configured as I/O, Comparator output available internally
011 = CIN- pin is configured as analog, CIN+ pin is con figured as I/O, COUT pin configured as
100 = CIN- pin is configured as analog , CIN+ pin is configured as I/O, COUT pin is configured as I/O, Comp arator output
101 = CIN pins are configured as analog and multip lexed, COUT pin is configured as
110 = CIN pins are configured as analog and multip lexed, COUT pin is configured as I/O,
111 = CIN pins are configured as I/O, COUT pin is configured as I/O, Comparator output disabled, Comparator off.
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 7C2OUT: Comparator 2 Output bit
When C2INV =
1 = C2 VIN+ > C2 VIN0 = C2 V
When C2INV =
1 = C2 VIN+ < C2 VIN0 = C2 V
bit 6C1OUT: Comparator 1 Output bit
When C1INV = 0:
1 = C1 VIN+ > C1 VIN0 = C1 V
When C1INV = 1:
1 = C1 VIN+ < C1 VIN0 = C1 V
bit 5C2INV: Comparator 2 Output Inversion bit
1 = C2 output inverted
0 = C2 output not inverted
bit 4C1INV: Comparator 1 Output Inversion bit
1 = C1 Output inverted
0 = C1 Output not inverted
bit 3CIS: Comparator Input Switch bit
When CM<2:0> = 010:1 = C1IN+ connects to C1 VIN-
C2IN+ connects to C2 V
0 = C1IN- connects to C1 V
C2IN- connects to C2 V
When CM<2:0> =
1 = C1IN+ connects to C1 VIN0 = C1IN- connects to C1 V
bit 2-0CM<2:0>: Comparator Mo de bits (See Fi gure 7-5)
000 = Comparators off. CxIN pins are configured as analog
001 = Three inputs multiplexed to two comparators
010 = Four inputs multiplexed to two comparators
011 = Two common reference comparators
100 = Two independent comparators
101 = One independent comparator
110 = Two comparators with outputs and common reference
111 = Comparators off. CxIN pins are configured as digital I/O
This feature can be used to time the duration or interval of analog events. Clearing the T1GSS bit of the
CMCON1 register will enable Timer1 to increment
based on the output of the comparator (or Comparator
C2 for PIC16F636/639). This requires that Timer1 is
on and gating is enabled. See Section 6.0 “Timer1Module with Gate Control” for details.
It is recommended to synchronize the comparator with
Timer1 by setting the CxSYNC 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.
Note:References to the comparator in this
section specifically are referring to
Comparator C2 on the PIC16F636/639.
7.10Synchronizing Comparator Output
to Timer1
The comparator (or Comparator C2 for PIC16F636/639)
output can be synchronized with Timer1 by setting the
CxSYNC bit of the CMCON1 register. When enabled,
the comparator 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 latched on the falling edge of the
Timer1 clock source and Timer1 increments on the ri sing
edge of its clock source. See the Comparator Block
Diagram (Figure 7-2) and the Timer1 Block Diagram
(Figure 6-1) for more information.
Note:References to the comparator in this
section specifically are referring to
Comparator C2 on the PIC16F636/639.
The Comparator Voltage Reference module provides
an internally generated voltage reference for the
comparators. The following features are available:
• Independent from Comparator operation
• Two 16-level voltage ranges
• Output clamped to V
• Ratiometric with VDD
• Fixed Voltage Reference
The VRCON register (Register 7-5) controls the
Voltage Reference module shown in Figure 7-10.
7.11.1INDEPENDENT OPERATION
The comparator voltage reference is independent of
the comparator configuration. Setting the VREN bit of
the VRCON register will enable the voltage reference.
7.11.2OUTPUT VOLTAGE SELECTION
The CVREFvoltage reference has 2 ranges with 16
voltage levels in each range. Range selection is
controlled by the VRR bit of the VRCON register. The
16 levels are set with the VR<3:0> bits of the VRCON
register.
The CVREF output voltage is determined by the following
equations:
SS
7.11.3OUTPUT CLAMPED TO VSS
The CVREF output voltage can be set to Vss with no
power consumption by configuring VRCON as follo ws:
•VREN=0
•VRR=1
•VR<3:0>=0000
This allows the comparator to detect a zero-crossing
while not consuming additional CV
REF module current.
7.11.4OUTPUT RATIOMETRIC TO VDD
The comparator voltage reference is VDD d erived and
therefore, the CV
DD. The tested absolute accur acy of the Comparator
V
Voltage Reference can be found in Section 15.0 “Elec-
trical Specifications”.
REF output changes with fluctuations in
EQUATION 7-1:CVREF OUTPUT VOLTAGE
(INTERNAL CV
VRR1 (low range):=
CV
REF (VR<3:0>/24)VDD×=
VRR0 (high range):=
REF(VDD/4) + =
CV
EQUATION 7-2:CV
(VR<3:0> V
REF OUTPUT VOLTAGE
(EXTERNAL CV
VRR1 (low range):=
REF (VR<3:0>/24)VLADDER×=
CV
V
RR0 (high range):=
CV
REF(VLADDER/4) + =
V
LADDERVDD=or ([VREF+] - [VREF-]) or VREF+
The full range of VSS to VDD cannot be realized due to
the construction of the module. See Figure 7-10.
Note 1: Care should be taken to ensure VREF remains
within the comparator common mode input
range. See Section 15.0 “Electrical Specifica-
tions” for more detail.
PIC12F635/PIC16F636/639
T ABLE 7-2:SUMMARY OF REGISTERS ASSOCIATED WITH THE COMPARATOR AND VOLTAGE
REFERENCE MODULES
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
CMCON0
CMCON1
INTCONGIEPEIE
PIE1
PIR1
PORTA
PORTC
TRISA
TRISC
VRCONVREN
Legend:x = unknown, u = unchanged, - = unimplemented, read as ‘0’. Shaded cells are not used for comparator.
The Programmable Low-Voltage Detect (PLVD)
module is a p ower supp ly d ete ctor whic h mon it ors t he
internal power supply. This module is typically used in
key fobs and other devices, where certain actions
need to be taken as a result of a fall ing bat tery vol tag e.
FIGURE 8-1:PLVD BLOCK DIAGRAM
8 Stages
VDD
LVDEN
The PLVD module includes the following capabilities:
• Eight programmable trip points
• Interrupt on falling V
DD
• Stab le refe renc e ind ica tio n
• Operation during Sleep
A Block diagram of the PLVD module is shown in
To setup the PLVD for operation, the following steps
must be taken:
• Enable the module by setting the LVDEN bit of the
LVDCON register.
• Configure the trip point by setting the LVDL<2:0>
bits of the LVDCON register.
• Wait for the reference voltage to become stable.
Refer to Section 8.4 “Stable Reference Indication”.
• Clear the LVDIF bit of the PIRx
The LVDIF bit will be set when V
PL VD trip po int. Th e LVDIF bit re mains s et until cleared
by software. Refer to Figure 8-2.
register .
DD falls below the
8.2Programmable T rip Point
The PLVD trip point is selectable from one of eight
voltage levels. The LVDL bits of the LVDCON register
select the trip point. Refer to Register 8-1 for the
available PLVD trip points.
8.3Interrupt on Falling VDD
When VDD falls below the PLVD trip point, the falling
edge detector will set t he LVDIF bit. See Figure 8-2. An
interrupt will be generated if the following bits are also
set:
• GIE and PEIE bits of the INTCON register
• LVDIE bit of the PIEx
The L VDIF bit must be c leared by software. An interrupt
can be generated from a simulated PLVD event when
the LVDIF bit is set by software.
register
8.4Stable Ref erence Indication
When the PLVD module is en abl ed, the re feren ce vol tage must be allowed to stabilize before the PLVD will
provide a valid result. Refer to Electrical Section,PLVD Characteristics for the stabilization time.
When the HFINTOSC is running, the IRVST bit of the
LVDCON register indicates the stability of the voltage
reference. The voltage reference is stable when the
IRVST bit is set.
8.5Operation During Sleep
To wake from Sleep, set the LVDIE bit of the PIEx
register and the PEIE bit of the INTCO N register . When
the LVDIE and PEIE bits are set, the device will wake
from Sleep and execute the next instruction. If the GIE
bit is also set, th e progr am will c all the I nterrupt Serv ice
Routine upon completion of the first instruction after
waking from Sleep.
The EEPROM data memory is readable and writable
during normal operation (full V
is not directly mapped in the register file space.
Instead, it is indirectly addressed through the Special
Function Registers. There are four SFRs used to read
and write this memory:
• EECON1
• EECON2 (not a physically implemented register)
• EEDA T
• EEADR
EEDAT holds the 8-bit data for read/write and EEADR
holds the address of the EEPROM location being
accessed. PIC16F636/639 has 256 bytes of data
EEPROM and the PIC12F635 has 128 bytes.
DD range). This memory
The EEPROM data memory allows b yte read and write.
A byte write automatically erases the location and
writes the new data (erase be fore write). The EEPROM
data memory is rated fo r high er ase/writ e cycles. T he
write time is controlled by an on-chip timer. The write
time will vary with voltage and temperature as well as
from chip-to-chip. Please refer to A/C specifications in
Section 15.0 “Electrical Specifications” for exact
limits.
When the data memory is code-protected, the CPU
may continue to read and write the data EEPROM
memory . The device progra mmer can no longer access
the data EEPROM data and will read zeroes.
REGISTER 9-1:EEDAT: EEPROM DATA REGISTER
R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0
EEDAT7EEDAT6EEDAT5EEDAT4EEDAT3EEDAT2EEDAT1EEDAT0
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-0EEDATn: Byte Value to Write To or Read From Data EEPROM bits
REGISTER 9-2:EEADR: EEPROM ADDRESS REGISTER
R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0
(1)
EEADR7
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-0EEADR: Specifies One of 256 Locations for EEPROM Read/Write Operation bits
Note 1:PIC16F636/639 only. Read as ‘0’ on PIC12F635.
EECON1 is the control register with four low-order bits
physically implemented. The upper four bits are
non-implemented and read as ‘0’s.
Control bits RD and WR initiate read and write,
respectively. These bits cannot be cleared, only set in
software. They are cleared in hardware at completion
of the read or wr i t e ope r a tio n. T he ina bi l it y t o cl ea r t he
WR bit in software prevents the accidental, premature
termination of a write operation.
The WREN bit, when set, will allow a write operation.
On power-up, the WREN bit is c lear . T he WRERR bit is
set when a write operation is interrupted by a MCLR
Reset, or a WDT Time-out Reset during normal
operation. In these situ ations, fol lowing Re set, the user
can check the WRERR bit, clear it and rewrite the
location. The data and address will be cleared.
Therefore, the EEDAT and EEADR registers will need
to be re-initialized.
Interrupt flag, EEIF bit of the PIR1 reg is ter, is set when
write is complete. This bit must be cleared in software.
EECON2 is not a physical register. Reading EECON2
will read all ‘0’s. The EECON2 register is used
exclusively in the data EEPROM write sequence.
Note:The EECON1, EEDAT and EEADR
registers should not be modified during a
data EEPROM write (WR bit = 1).
REGISTER 9-3:EECON1: EEPROM CONTROL REGISTER
U-0U-0U-0U-0R/W-xR/W-0R/S-0R/S-0
————WRERRWRENWRRD
bit 7bit 0
Legend:
S = Bit can only be set
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 3WRERR: EEPROM Error Flag bit
1 = A write operation is prematurely terminated (any MCLR
normal operation or BOR Reset)
0 = The write operation completed
bit 2WREN: EEPROM Write Enable bit
1 = Allows write cycles
0 = Inhibits write to the data EEPROM
bit 1WR: Write Control bit
1 = Initiates a write cycl e (The bit is cle ared by hardw are o nce wr ite is co mplet e. The WR b it ca n only
be set, not cleared, in software.)
0 = Write cycle to the data EEPROM is complete
bit 0RD: Read Control bit
1 = Initiates an EEPROM read (Read takes one cycle. RD is cleared i n hardware. The RD bit can o nly
To read a data memory location, the user must write the
address to the EEADR register and then set control bit
RD of the EECON1 register, as shown in Example9-1.
The data is available, in the very next cycle, in the
EEDAT register. Therefore, it can be read in the next
instruction. EEDA T holds this value until another read, or
until it is written to by the user (during a write operation).
EXAMPLE 9-1:DATA EEPROM READ
BANKSEL EEADR;
MOVLWCONFIG_ADDR;
MOVWFEEADR;Address to read
BSFEECON1,RD;EE Read
MOVFEEDAT,W;Move data to W
9.3Writing to the EEPROM Data
Memory
To write an EEPROM data location, the user must first
write the address to the EEADR register and the data
to the EEDAT register. Then the user must follow a
specific sequence to initiate the write for each byte, as
shown in Example 9-2.
The write will not initiate if the above sequence is not
exactly followed (write 55h to EECON2, write AAh to
EECON2, then set WR bit) for each byte. We strongly
recommend that interrupts be disabled during this
code segment. A cycle count is executed during the
required sequence. A ny number th at is not equa l to the
required cycles to execute the required sequence will
prevent the data from being writte n into the EEPROM.
Additionally, the WREN bit in EECON1 must be set to
enable write. This mechanism prevents accident al writes
to data EEPROM due to errant (unexpected) code
execution (i.e., lost programs). The user should keep the
WREN bit clear at all times, except when updating
EEPROM. The WREN bit is not cleared by hardware.
After a write sequence has been initiated, clearing the
WREN bit will not af fect this wri te cycle. The W R bit will
be inhibited from being set unless the WREN bit is set.
At the completion of the write cycle, the WR bit is
cleared in hardware and the EE Write Complete
Interrupt Flag bit (EEIF) is set. The user can either
enable this interrupt or poll this bit. The EEIF bit of the
PIR1 register must be cleared by software.
9.4Write Verify
Depending on the application, good programming
practice may dictate that the value written to the data
EEPROM should be verified (see Example 9 -3) to the
desired value to be written.
EXAMPLE 9-3:WRITE VERIFY
BANKSELEEDAT;
MOVFEEDAT,W;EEDAT not changed
;from previous write
BSFEECON1,RD ;YES, Read the
;value written
XORWFEEDAT,W;
BTFSSSTATUS,Z;Is data the same
GOTOWRITE_ERR ;No, handle error
:;Yes, continue
9.4.1USING THE DATA EEPROM
The data EEPROM is a high-endurance, byte
addressable array that has been optimized for the
storage of frequently changing information (e.g.,
program variables or other data that are updated
often). When variables in one section change
frequently, while variables in another section do not
change, it is possible to exceed the total number of
write cycles to the EEPROM (specification D124)
without exceeding the total number of write cycles to a
single byte (specifications D120 and D120A). If this is
the case, then a refresh of the array must be
performed. For this reason, variables that change
infrequently (such as constants, IDs, calibration, etc.)
should be stored in Flash program memo ry.
There are c onditions when the user may no t want to
write to the data EEPROM memory. To protect against
spurious EEPROM writes, various mechanisms have
been built in. On power-up, WREN is cleared. Als o, the
Power-up Timer (nominal 64 ms duration) prevents
EEPROM write.
The write initiate se quence and the WREN bit together
help preven t an accidental write duri ng:
• Brown-out
•Power Glitch
• Software Malfunction
9.6Data EEPROM Operation During
Code Protection
Data memory can be code-p rotected by progr amming
the CPD bit in the Co nfigur ation Word (Regis ter 12-1 )
to ‘0’.
When the data memory is code-protected, the CPU is
able to read and write data to the data EEPROM. It is
recommended to code-protect the program memory
when code-protecting data memory. This prevents
anyone from programming zeroes over the existing
code (which will execute as NOPs) to reach an added
routine, programmed in unused program memory,
which outputs the contents of data memory.
Programming unused locations in program memory to
‘0’ will also help prevent data memory code protection
from becoming breached.
TABLE 9-1:SUMMARY OF REGISTERS ASSOCIATED WITH DATA EEPROM
NameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
INTCONGIEPEIE
PIR1EEIF
PIE1
EEDATEEDAT7EEDAT6EEDAT5EEDAT4EEDAT3EEDAT2EEDAT1EEDAT00000 00000000 0000
EEADREEADR7
EECON1
EECON2EEPROM Control Register 2 (not a physical register)---- -------- ----
Legend:x = unknown, u = unchanged, – = unimplemented read as ‘0’, q = value depends upon condition.
Note 1:PIC16F636/639 only.
EEIELVDIECRIEC2IE
————WRERRWRENWRRD---- x000---- q000
Shaded cells are not used by the data EEPROM module.
To obtain information regarding the implementation of
EELOQ module, Microchip Technology requires
the K
the execution of the “K
Agreement”.
The “KEELOQ® Encoder Lice nse Agreement” may be
accessed through the Microchip web site located at
www.microchip.com/K
be obtained by contacting you r local Microc hip Sales
Representative.
The PIC16F639 device consists of the PIC16F636
device and low frequency (LF) Analog Front-End
(AFE), with the AFE section containing three
analog-input channels for signal detection and LF
talk-back. This sectio n des cribes t he Analog Fro nt-End
(AFE) in detail.
The PIC16F639 device can detect a 125 kHz input
signal as low as 1 mVpp and transmit data by using
internal LF talk-back modulation or via an external
transmitter. The PIC16F639 can also be used for
various bidirectional communication applications.
Figure 11-3 and Figure 11-4 show application examples
of the device.
Each analog input channel has internal tuning
capacitance, sensitivity control circuits, an input signal
strength limiter and an LF talk-back modulation
transistor. An Automatic Gain Control (AGC) loop is
used for all three input channel gains. The output of
each channel is OR’d and fed into a demodulator. The
digital output is passed to the LFDATA pin. Figure 11-1
shows the block diagram of the AFE and Figure 11-2
shows the LC input path.
There are a total of eig ht Confi guratio n regis ter s. Six of
them are used for AFE operation options, one for
column parity bits and one for status indication of AFE
operation. Each register has 9 bits including one row
parity bit. These regis te rs are readable and writable by
SPI (Serial Protocol Interface) commands except for
the STATUS register, which is read-only.
11.1RF Limiter
The RF Limiter limits LC pin input voltage by de-Q’ing
the attached LC resonant circuit. The absolute voltage
limit is defined by the silicon process’s maximum
allowed input voltage (see Section 15.0 “ElectricalSpecifications”). The limiter begins de-Q’ing the
external LC antenna when the input voltage exceeds
VDE_Q, progressively de-Q’ing harder to reduce the
antenna input voltage.
The signal levels from all 3 channels are combined
such that the limiter attenuates all 3 channels
uniformly, in respect to the channel with the strongest
signal.
11.2Modulation Circuit
The modulation circuit consists of a modulation
transistor (FET), internal tunin g capacitors and ext ernal
LC antenna components. The modulation transistor
and the internal tuning capacitors are connected
between the LC input pin and LCCOM pin. Each LC
input has its own modulation transistor.
When the modulation transistor turns o n, it s low T urn-on
Resistance (R
voltage. The coil voltage is minimized when the
modulation transistor turns-on and maximized when the
modulation transistor turns-off. The modulation
transistor’s low Turn-on Resistance (R
high modulation depth.
The LF talk-back is achieved by turning on and off the
modulation transistor.
The modulation data comes from the microcontroller
section via the digital SPI interface as “Clamp On”,
“Clamp Off” commands. Only those inputs that are
enabled will execute the clamp command. A basic
block diagram of the modulation circuit is shown in
Figure 11-1 and Figure 11-2.
The modulation FET is also shorted momentarily after
Soft Reset and Inactivity timer time-out.
M) clamps the induced LC antenna
M) results in a
11.3Tuning Capacitor
Each channel has internal tuning capacitors for external
antenna tuning. The capacitor values are programmed
by the Configuration registers
Note:The user can control the tuning c apacitor
by programming the AFE Configuration
registers.
up to 63 pF , 1 pF per step.
11.4Variable Attenuator
The variable attenuator is used to attenuate, via AGC
control, the input signal voltage to avoid saturating the
amplifiers and demodulators.
Note:The variable attenuator function is
accomplished by the device itself. The
user cannot control its function.
11.5Sensitivity Control
The sensitivity of each channel can be reduced by the
channel’s Configuration register sensitivity setting.
This is used to desensitize the channel from optimum.
Note:The user can desensitize the channel
sensitivity by programming the AFE
Configura tion registers.
The AGC controls the variable attenuator to limit the
internal signal voltage to avoid saturation of internal
amplifiers and demodulators (Refer to Section 11.4“Varia ble A ttenu ator”).
The signal levels from all 3 channels are combined
such that AGC attenuates all 3 channels uniformly in
respect to the channel with the strongest signal.
Note:The AGC c ontrol fu nctio n is ac comp lishe d
by the device itself. The user cannot
control its function .
11.7Fixed Gain Amplifiers 1 and 2
FGA1 and FGA2 provides a maximum two-stage gain
of 40 dB.
Note:The user cannot control the gain of these
two amplifiers.
11.8Auto Channel Selection
The Auto Channel Selection feature is enabled if the
Auto Channel Select bit AUTOCHSEL<8> in Configuration Register 5 (Register 11-6) is set, and disabled if
the bit is cleared. When this feature is active (i.e.,
AUTOCHSE <8> = 1), the control circuit checks the
demodulator output of each input channel immediately
after the AGC settling time (T
it allows this channel to pass data, otherwise it is
blocked.
The status of this operation is monitored by AFE Status
Register 7 bits <8:6> (Register 11-8). These bits indicate
the current status of the channel selection activity, and
automatically updates for every Soft Reset period. The
auto channel selection function resets after each Soft
Reset (or after Inactivity timer time- out) . Ther efor e, th e
blocked channels are reenabled after Soft Reset.
This feature can make the output signal cleaner by
blocking any channel that was not high at the end of
AGC. This function works only for demodulated data
T
output, and is not applied for carrier clock or RSSI
output.
STAB). If the output is hig h,
11.10 Demodulator
The Demodulator consists of a full-wave rectifier, low
pass filter, peak detector and Data Slicer that detects
the envelope of the input signal.
11. 11Data Slicer
The Data Slicer consists of a reference generator and
comparator. The Data Slicer compares the input with
the reference voltage. The reference voltage comes
from the minimum modulation depth requirement
setting and input peak voltage. The data from all 3
channels are OR’d together and sent to the output
enable filter.
11.12 Output Enable Filter
The Output Enable Filter enables the LFDATA output
once the incoming signal meets the wake-up sequence
requirements (see Section 11.15 “Configurable
Output Enable Filter”).
11. 1 3 RSSI (Received Signal Strength
Indicator)
The RSSI provides a current which is propo rtional to the
input signal amplitude (see Section 11.31.3 “Received
Signal Strength Indicator (RSSI) Output”).
11.14 Analog Front-End Timers
The AFE has an internal 32 kHz RC oscillator. The
oscillator is used in several timers:
• Inactivity timer
• Alarm timer
• Pulse Width timer
• Period timer
• AGC settling timer
11.14.1RC OSCILLATOR
The RC oscillator is low power, 32 kHz ± 10% over
temperature and volta ge variations.
11. 9Carrier Clock Detector
The Detector senses the input carrier cycles. The
output of the Detector switches di gitally at the signal
carrier frequency. Carrier clock output is available
when the output is selected by the DATOUT bit in the
AFE Configuration Register 1 (Register 11-2).