Note the following details of the code protection feature on Microchip devices:
•Microchip products meet the specification contained in their particular Microchip Data Sheet.
•Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
•There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
•Microchip is willing to work with the customer who is concerned about the integrity of their code.
•Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digit al Millennium Copyright Act. If suc h a c t s
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device
applications and the like is provided only for your convenience
and may be superseded by updates. It is your responsibility to
ensure that your application meets with your specifications.
MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED,
WRITTEN OR ORAL, STATUTORY OR OTHERWISE,
RELATED TO THE INFORMATION, INCLUDING BUT NOT
LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE,
MERCHANTABILITY OR FITNESS FOR PURPOSE.
Microchip disclaims all liability arising from this information and
its use. Use of M icrochip’s prod ucts as critical components in
life support systems is not authorized except with express
written approval by Microchip. No licenses are conveyed,
implicitly or otherwise, under any Microchip intellectual property
rights.
Trademarks
The Microchip name and logo, the Microchip logo, Accuron,
dsPIC, K
EELOQ, microID, MPLAB, PIC, PICmicro, PICSTART,
PRO MATE, PowerSmart, rfPIC, and SmartShunt are
registered trademarks of Microchip Technology Incorporated
in the U.S.A. and other countries.
AmpLab, FilterLab, MXDEV, MXLAB, PICMASTER, SEEVAL,
SmartSensor and The Embedded Control Solutions Company
are registered trademarks of Microchip Technology
Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, dsPICDEM,
dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR,
FanSense, FlexROM, fuzzyLAB, In-Circuit Serial
Programming, ICSP, ICEPIC, Migratable Memory, MPASM,
MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net,
PICLAB, PICtail, PowerCal, PowerInfo, PowerMate,
PowerTool, rfLAB, rfPICDEM, Select Mode, Sm art Serial,
SmartTel and Total Endurance are trademarks of Microchip
Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
Microchip received ISO/TS-16949:2002 quality system certification for
its worldwide headquarters, design and wafer fabrication facilities in
Chandler and Tempe, Arizona and Mountain View, California in
October 2003. The Company’s quality system processes and
procedures are for its PICmicro
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
3.0Data EEPROM and Flash Program Memory.............................................................................................................................. 25
10.0 Synchronous Serial Port (SSP) Module ..................................................................................................................................... 71
12.0 Special Features of the CPU.................................................... ..................... ............................................................................. 89
13.0 Instruction Set Summary.......................................................................................................................................................... 103
14.0 Development Support............................................................................................................................................................... 111
16.0 DC and AC Characteristics Graphs and Tables....................................................................................................................... 143
Index .................................................................................................................................................................................................. 165
Systems Information and Upgrade Hot Line...................................................................................................................................... 171
PIC16F818/819 Product Identification System ..................................................................................................................................173
TO OUR VALUED CUSTOMERS
It is our intention to provide our valued customers with the best documentation possible to ensure successful use of your Microchip
products. To this end, we will continue to improve our publications to better suit your needs. Our publications will be refined and
enhanced as new volumes and updates are introduced.
If you have any questions or c omm ents regarding t his publication, p lease c ontact the M arket ing Co mmunications Department via
E-mail at docerrors@microchip.com or fax the Reader Response Form in the back of this data sheet to (480) 792-4150. We
welcome your feedback.
Most Current Data Sheet
To obtain the most up-to-date version of this data sheet, please register at our Worldwide Web site at:
http://www.microchip.com
You can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page.
The last character of the literature number is the version number, (e.g., DS30000A is version A of document DS30000).
Errata
An errata sheet, describing minor operational differences from the data sheet and recommended workarounds, may exist for current
devices. As device/documentation issues become known to us, we will publish an errata sheet. The errata will specify the revision
of silicon and revision of document to which it applies.
To determine if an errata sheet exists for a particular device, please check with one of the following:
• Microchip’s Worldwide Web site; http://www.microchip.com
• Your local Microchip sales office (see last page)
When contacting a sales office, please specify which device, revision of silicon and data sheet (include literature number) you are
using.
Customer Notification System
Register on our web site at www.microchip.com to receive the most current information on all of our products.
2004 Microchip Technology Inc.DS39598E-page 3
PIC16F818/819
NOTES:
DS39598E-page 4 2004 Microchip Technology Inc.
PIC16F818/819
1.0DEVICE OVERVIEW
This document contains device specific information for
the operation of the PIC16F818/819 devices. Additional
information may be found in the “PICmicro® Mid-Range
MCU Family Reference Manual” (DS33023) which may
be downloaded from the Microchip web site. The
Reference Manual should be considered a comp lementary document to this data sheet and is highly
recommended reading for a better understanding of the
device architecture and operation of the peripheral
modules.
The PIC16F818/819 belongs to the Mid-Range family
of the PICmicro
other in the amount of Flash program memory, data
memory and data EEPROM (see Table 1-1). A block
diagram of the devices is shown in Figure 1-1. These
devices contain features that are new to the PIC16
product line:
• Internal RC oscillator with eight selectable
frequencies, including 31.25 kHz, 125 kHz,
250 kHz, 500 kHz, 1 MHz, 2 MHz, 4 MHz and
8 MHz. The INTRC can be configured as the
system clock via the configuration bits. Refer to
Section 4.5 “Internal Oscillator Block” and
Section 12.1 “Configuration Bits” for further
details.
• The Timer1 module current consumption has
been greatly reduced from 20 µA (previous PIC16
devices) to 1.8µA typical (32 kHz at 2V), which is
ideal for real-time clock applications. Refer to
Section 6 .0 “Timer0 Module” for further details.
• The amount of oscill ator selections ha s increased.
The RC and INTRC modes can be selected with
an I/O pin configured as an I/O or a clock output
OSC/4). An external clock can be configured
(F
with an I/O pin. Refer to Section 4.0 “Oscillator Configurations” for further details.
®
devices. The device s differ from each
TABLE 1-1:AVAILABLE MEMORY IN
PIC16F818/819 DEVICES
Device
PIC16F8181K x 14128 x 8128 x 8
PIC16F8192K x14256 x 8256 x 8
There are 16 I/O pins that are user configurable on a
pin-to-pin basis. Some pins are multiplexed with other
device functions. These functions include:
• External Interrupt
• Change on PORTB Interrupt
• Timer0 Clock Input
• Low-Power Timer1 Clock/Oscillator
• Capture/Compare/PWM
• 10-bit, 5-channel Analog-to-Digital C onverter
2
• SPI/I
•MCLR (RA5) can be configured as an Input
Table 1-2 details the pinout of the devices with
2: This buffer is a Schmitt Trigger input when used in Serial Programming mode.
3: This buffer is a Schmitt Trigger input when configured in RC Oscillator mode and a CMOS input otherwise.
SSOP
SOIC
Pin#
171923
182024
1126
2227
3328
441
151720
161821
Pin#
QFN
Pin#
I/O/P
Type
I/O
I
I/O
I
I/O
I
I
I/O
I
I
I/O
I
I
I
I
P
I/O
O
O
I/O
I
I
Buffer
Type
TTL
Analog
TTL
Analog
TTL
Analog
Analog
TTL
Analog
Analog
ST
Analog
ST
ST
ST
–
ST
–
–
ST
ST/CMOS
–
Description
PORTA is a bidirectional I/O port.
Bidirectional I/O pin.
Analog input channel 0.
Bidirectional I/O pin.
Analog input channel 1.
Bidirectional I/O pin.
Analog input channel 2.
A/D reference voltage (low) input.
Bidirectional I/O pin.
Analog input channel 3.
A/D reference voltage (high) input.
Bidirectional I/O pin.
Analog input channel 4.
Clock input to the TMR0 timer/counter.
Input pin.
Master Clear (Reset). Input/programming
voltage input. This pin is an active-low Reset
to the device.
Programming threshold voltage.
Bidirectional I/O pin.
Oscillator cryst al outpu t. Connect s to crys tal or
resonator i n Crystal Oscill ator mode.
In RC mode, this pin outputs CLKO signal
which has 1/4 the frequency of OSC1 and
denotes the instruction cycle rate.
Note 1: This buffer is a Schmitt Trigger input when configured as the external interrupt.
2: This buffer is a Schmitt Trigger input when used in Serial Programming mode.
3: This buffer is a Schmitt Trigger input when configured in RC Oscillator mode and a CMOS input otherwise.
SSOP
SOIC
Pin#
677
788
899
91010
101112
111213
121315
131416
Pin#
QFN
Pin#
I/O/P
Type
I/O
I
I/O
I
I/O
I/O
O
I/O
I/O
I/O
I
I/O
I/O
I
I/O
I
I/O
O
I
I
I/O
I
I
Buffer
Type
TTL
(1)
ST
TTL
ST
ST
TTL
ST
ST
TTL
ST
ST
TTL
ST
ST
TTL
TTL
TTL
ST
ST
(2)
ST
TTL
ST
(2)
ST
Description
PORTB is a bidirectional I/O port. PORTB can be
software programmed fo r in tern al weak pull-up on
all inputs.
Bidirectional I/O pin. Interrupt-on-change pin.
Synchronous serial clock input/output for SPI.
Synchronous serial clock input for I
Bidirectional I/O pin. Interrupt-on-change pin.
Slave select for SPI in Slave mode.
Interrupt-on-change pin.
Timer1 Oscillator output.
Timer1 clock input.
In-circuit debugger and ICSP program m ing
clock pin.
Interrupt-on-change pin.
Timer1 oscillator input.
In-circuit debugger and ICSP program m ing
data pin.
2
C.
DS39598E-page 8 2004 Microchip Technology Inc.
PIC16F818/819
2.0MEMORY ORGANIZATION
There are two memory blocks in the PIC16F818/819.
These are the program memory and the data memory.
Each block has its own bus, so access to each block
can occur during the same oscillator cycle.
The data memory can be further broken down into the
general purpose RAM and the Special Function
Registers (SFRs). The operation of the SFRs that
control the “core” are described here. The SFRs used
to control the peripheral modules are described in the
section discussing each individual peripheral module.
The data memory area also contains the data
EEPROM memory . This memory is no t directly mapped
into the data memory but is indirectly mapped. That is,
an indirect addres s poin ter speci fies th e addre ss of th e
data EEPROM memory to read/write. The PIC16F818
device’s 128 bytes of dat a EEPROM memory h ave th e
address range of 00 h-7Fh and the PIC16 F819 devic e’s
256 bytes of data EEPROM memo ry hav e the add res s
range of 00h-FFh. More details on the EEPROM
memory can be found in Section 3.0 “Data EEPROMand Flash Program Memory”.
Additional informa tion on devi ce memory may be found
in the “PICmicro
(DS33023).
®
Mid-Range Reference Manual”
2.1Program Memory Organization
The PIC16F818/819 devices have a 13-bit program
counter capable of addressing an 8K x 14 program
memory space. For the PIC16F818, the first 1K x 14
(0000h-03FFh) is physically implemented (see
Figure 2-1). For the PIC16F819, the first 2K x 14 is
located at 0000h-07FFh (see Figure 2-2). Accessing a
location above the ph ysicall y implem ented addres s will
cause a wraparound. For example, the same instruction will be accessed at locations 020h, 420h, 820h,
C20h, 1020h, 1420h, 1820h and 1C20h.
The Reset vector is a t 0000h and t he in terrupt vecto r i s
at 0004h.
FIGURE 2-1:PROGRAM MEMORY MAP
AND STACK FOR
PIC16F818
PC<12:0>
CALL, RETURN
RETFIE, RETLW
On-Chip
Program
Memory
Stac k Lev el 1
Stac k Lev el 2
Stack Level 8
Reset Vector
Interru pt Vector
Page 0
Wraps to
0000h-03FFh
13
0000h
0004h
0005h
03FFh
0400h
FIGURE 2-2:PROGRAM MEMORY MAP
AND STACK FOR
PIC16F819
PC<12:0>
CALL, RETURN
RETFIE, RETLW
On-Chip
Program
Memory
Stac k Lev el 1
Stac k Lev el 2
Stack Level 8
Reset Vector
Interrupt Vector
Page 0
Wraps to
0000h-07FFh
13
0000h
0004h
0005h
07FFh
0800h
1FFFh
2004 Microchip Technology Inc.DS39598E-page 9
1FFFh
PIC16F818/819
2.2Data Memory Organization
The data memory is p arti tioned into m ultip le ban ks th at
contain the Ge neral Purpose Reg isters and the Spe cial
Function Registers. Bits RP1 (Status<6>) and RP0
(Status<5>) are the bank select bits.
RP1:RP0Bank
000
011
102
113
Each bank extends up to 7Fh (128 bytes). The lower
locations of each bank are reserved for the Special
Function Registers. Ab ove the Speci al Function Registers are th e Genera l Purpos e Regist ers, im plement ed
as static RAM. All implemented bank s contain SFRs.
Some “high use” SFRs from one bank may be mirrored
in another bank for code reduction and quicker access
(e.g., the Status register is in Banks 0-3).
Note:EEPROM dat a memory description can be
found in Section 3.0 “Data EEPROM andFlash Program Memory” of this data
sheet.
2.2.1GENERAL PURPOSE REGISTER
FILE
The register file can be accessed either directly or
indirectly through the File Select Register, FSR.
Note 1: These registers are reserved; maintain these registers clear.
DS39598E-page 12 2004 Microchip Technology Inc.
General
Purpose
Register
80 Bytes
Accesses
70h-7Fh
Bank 1
EFh
F0h
FFh
General
Purpose
Register
80 Bytes
Accesses
70h-7Fh
Bank 2
16Fh
170h
17Fh
Accesses
20h-7Fh
1FFh
Bank 3
PIC16F818/819
2.2.2SPECIAL FUNCTION REGISTERS
The Special Function Registers are registers used by
the CPU and peripheral modules for controlling the
desired operation of the device. These registers are
implemented as static RAM. A list of these registers is
given in Table 2-1.
The Special Function Registers can be classified into
two sets: core (CPU) and peripheral. Those registers
associated with the core functions are described in
detail in this section. Those related to the operation of
the peripheral features are described in detail in the
peripheral feature section.
TABLE 2-1:SPECIAL FUNCTION REGISTER SUMMARY
AddressNameBit 7Bit 6Bit 5Bit 4Bit 3Bit 2 Bit 1Bit 0
Bank 0
(1)
00h
01hTMR0Timer0 Module Registerxxxx xxxx53, 17
02h
03h
04h
05hPORTAPORTA Data Latch when written; PORTA pins when readxxx0 000039
06hPORTBPORTB Data Latch when written; PORTB pins when readxxxx xxxx43
07h—Unimplemented——
08h—Unimplemented——
09h—Unimplemented——
0Ah
0Bh
0ChPIR1
0DhPIR2
0EhTMR1LHolding Register for the Least Significant Byte of the 16-bit TMR1 Registerxxxx xxxx57
0FhTMR1HHolding Register for the Most Significant Byte of the 16-bit TMR1 Registerxxxx xxxx57
2:The upper byte of the program counter is not directly accessible. PCLATH is a holding register for the PC<12:8>, whose contents are
transferred to the upper byte of the program counter.
3:Pin 5 is an input only; the state of the TRISA5 bit has no effect and will always read ‘1’.
INTEDGT0CST0SEPSAPS2PS1PS01111 111117, 54
——FREEWRERRWRENWRRDx--x x00026
High Byte
Value on
POR, BOR
---- -xxx25
Details on
page:
2004 Microchip Technology Inc.DS39598E-page 15
PIC16F818/819
2.2.2.1Status Register
The St atus register , s hown in Register2-1, contains the
arithmeti c statu s of th e ALU, the Re set sta tus an d the
bank select bits for data memory.
The Status register can be the destination for any
instruction, as with any other register. If the Status
register is the destination for an instruction that affects
the Z, DC or C bits, then the write to these three bits is
disabled. These bit s are set or cleared ac cording to the
device logic. Furthermore, the TO
writable. Therefore, the result of an instruction with the
Status register as destination may be different than
intended.
and PD bits are not
For example, CLRF STATUS, w ill c lear the upper three
bits and set the Z bit. Thi s 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
Stat us regist er because the se inst ructions do not af fect
the Z, C or DC bits from the Status register. For other
instructions not affecting any status bits, see
Section 13.0 “Instruction Set Summary”.
Note:The C and DC bits operate as a borrow
and digit borrow bit, respectively, in
subtraction. See the SUBLW and SUBWF
instructions for examples.
REGISTER 2-1:STATUS: STATUS REGISTER (ADDRESS 03h, 83h, 103h, 183h)
R/W-0R/W-0R/W-0R-1R-1R/W-xR/W-xR/W-x
IRPRP1RP0TO
bit 7bit 0
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)
11 = Bank 3 (180h-1FFh)
10 = Bank 2 (100h-17Fh)
01 = Bank 1 (80h-FFh)
00 = Bank 0 (00h-7Fh)
Each bank is 128 bytes.
bit 4TO
bit 3PD
bit 2Z: Zero bit
bit 1DC: Digit carry/borrow
bit 0C: Carry/borrow
: Time-out bit
1 = After power-up, CLRWDT instruction or SLEEP instruction
0 = A WDT time-out occurred
: Power-down bit
1 = After power-up or by 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
bit (ADDWF, ADDLW, SUBLW and SUBWF ins tru cti ons )
1 = A carry-out from the 4th low order bit of the result occurred
0 = No carry-out from the 4th low order bit of the result
bit (ADDWF, ADDLW, SUBLW and 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
PDZDCC
(1)
(1,2)
Note 1: For borrow, the polarity is reversed. A subtraction is executed by adding the two’s
complement of the second operand.
2: For rotate (RRF, RLF) instructions, this bi t is loa ded with e ither the high or l ow-order
bit of the source register.
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
DS39598E-page 16 2004 Microchip Technology Inc.
PIC16F818/819
2.2.2.2OPTION_RE G Regist er
The OPTION_REG register is a readable and writable
register that contains various control bits to configure
the TMR0 prescaler/WDT postscaler (single assignable register known also as the prescaler), the external
INT interrupt, TMR0 and the weak pull-u ps o n POR TB.
Note:To achieve a 1:1 prescaler assignment for
the TMR0 register, assign the prescaler to
the Watchdog Timer.
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
2004 Microchip Technology Inc.DS39598E-page 17
PIC16F818/819
2.2.2.3INTCON Register
The INTCON register is a readable and writable register that contains various enable and flag bits for the
TMR0 register overflow, RB port change and external
RB0/INT pin interrupts.
Note:Interrupt flag bits get set when an interrupt
condition occurs regardle ss of the sta te of
its corresponding enable bit or the Global
Interrupt Enable bit, GIE (INTCON<7>).
User software shoul d ensure the appropriate interrupt flag bits are clear prior to
enabling an interrupt.
REGISTER 2-3:INTCON: INTERRUPT CONTROL REGISTER (ADDRESS 0Bh, 8Bh, 10Bh, 18Bh)
R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-0R/W-x
GIEPEIETMR0IEINTERBIETMR0IFINTFRBIF
bit 7bit 0
bit 7GIE: Global Interrupt Enable bit
1 = Enables all unmasked interrupts
0 = Disables all interrupts
bit 6PEIE: Peripheral Interrupt Enable bit
1 = Enables all unmasked peripheral interrupts
0 = Disables all peripheral interrupts
bit 5TMR0IE: TMR0 Overflow Interrupt Enable bit
1 = Enables the TMR0 interrupt
0 = Disables the TMR0 interrupt
bit 4INTE: RB0/INT External Interrupt Enable bit
1 = Enables the RB0/INT external interrupt
0 = Disables the RB0/INT external interrupt
bit 3RBIE: RB Port Change Interrupt Enable bit
1 = Enables the RB port change interrupt
0 = Disables the RB port change interrupt
bit 2TMR0IF: TMR0 Overflow Interrupt Flag bit
1 = TMR0 register has overflowed (must be cleared in software)
0 = TMR0 register did not overflow
bit 1INTF: RB0/INT External Interrupt Flag bit
1 = The RB0/INT external interrupt occurred (must be cleared in software)
0 = The RB0/INT external interrupt did not occur
bit 0RBIF: RB Port Change Interrupt Flag bit
A mismatch condi tio n wil l c on tin ue to set flag bit RBIF. Reading PORTB will end the mis mat ch
condition and allow flag bit RBI F to be cleared.
1 = At least one of the RB7:RB4 pins changed state (must be cleared in software)
0 = None of the RB7:RB4 pins have changed state
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
DS39598E-page 18 2004 Microchip Technology Inc.
PIC16F818/819
2.2.2.4PIE1 Regi st er
This register contains the individual enable bits for the
peripheral interrupts.
bit 7Unimplemented: Read as ‘0’
bit 6ADIE: A/D Converter Interrupt Enable bit
1 = Enables the A/D converter interrupt
0 = Disables the A/D converter interrupt
bit 5-4Unimplemented: Read as ‘0’
bit 3SSPIE: Synchronous Serial Port Interrupt Enable bit
1 = Enables the SSP interrupt
0 = Disables the SSP interrupt
bit 2CCP1IE: CCP1 Interrupt Enable bit
1 = Enables the CCP1 interrupt
0 = Disables the CCP1 interrupt
bit 1TMR2IE: TMR2 to PR2 Match Interrupt Enable bit
1 = Enables the TMR2 to PR2 match interrupt
0 = Disables the TMR2 to PR2 match interrupt
bit 0TMR1IE: TMR1 Overflow Interrupt Enable bit
1 = Enables the TMR1 overflow interrupt
0 = Disables the TMR1 overflow interrupt
Legend:
R = Readable bitW = Writable bitU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
2004 Microchip Technology Inc.DS39598E-page 19
PIC16F818/819
2.2.2.5PIR1 Register
This register contains the individual flag bits for the
peripheral interrupts.
Note:Interrupt flag bits are set when an in terrupt
condition occurs regardle ss of the sta te of
its corresponding enable bit or the Global
Interrupt Enable bit, GIE (INTCON<7>).
User software shoul d ensure the appropriate interrupt flag bits are clear prior to
enabling an interrupt.
bit 7Unimplemented: Read as ‘0’
bit 6ADIF: A/D Converter Interrupt Flag bit
1 = An A/D conversion completed
0 = The A/D conversion is not complete
bit 5-4Unimplemented: Read as ‘0’
bit 3SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit
1 = The SSP interrupt condition has occurred and must be cleared in software befo re returning
from the I n t err upt S erv ic e Ro ut i ne . T h e co ndi ti o ns t hat wil l s et t hi s b i t a re a t ran sm is si on /
reception has taken place.
0 = No SSP interrupt condition has occurred
bit 2CCP1IF: CCP1 Interrupt Flag bit
Capture mode:
1 = A TMR1 register capture occurred (must be cleared in software)
0 = No TMR1 register capture occurred
Compare mode:
1 = A TMR1 register compare match occurred (must be cleared in software)
0 = No TMR1 register compare match occurred
PWM mode:
Unused in this mode.
bit 1TMR2IF: TMR2 to PR2 Match Interrupt Flag bit
1 = TMR2 to PR2 match occurred (must be cleared in software)
0 = No TMR2 to PR2 match occurred
bit 0TMR1IF: TMR1 Overflow Interrupt Flag bit
1 = TMR1 register overfl owed (must be cleared in software)
0 = TMR1 register did not overflow
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
DS39598E-page 20 2004 Microchip Technology Inc.
PIC16F818/819
2.2.2.6PIE2 Regi st er
The PIE2 register contains the individual enable bit for
the EEPROM write operation interrupt.
bit 7-5Unimplemented: Read as ‘0’
bit 4EEIE: EEPROM Write Operation Interrupt Enable bit
1 = Enable EE write interrupt
0 = Disable EE w rite interrupt
bit 3-0Unimplemented: Read as ‘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
2.2.2.7PIR2 Register
The PIR2 register contains the fla g bit for the EEPROM
write operation interrupt.
.
Note:Interrupt flag bits are set when an in terrupt
condition occurs regardles s of the sta te of
its corresponding enable bit or the Global
Interrupt Enable bit, GIE (INTCON<7>).
User software shoul d ensure the appropriate interrupt flag bits are clear prior to
enabling an interrupt.
bit 7-5Unimplemented: Read as ‘0’
bit 4EEIF: EEPROM Write Operation Interrupt Enable bit
1 = Enable EE write interrupt
0 = Disable EE w rite interrupt
bit 3-0Unimplemented: Read as ‘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
2004 Microchip Technology Inc.DS39598E-page 21
PIC16F818/819
2.2.2.8PCON Regist er
Note:Interrupt flag bits get set when an interrupt
condition occurs regardless of the state of
its corresponding enable bit or the Global
Interrupt Enable bit, GIE (INTCON<7>).
User software should en sure the ap propriate interrupt flag bits are clear prior to
enabling an interrupt.
The Power Control (PCON) register contains a flag bit
to allow differentiation between a Power-on Reset
(POR), a Brown-out Reset, an external MCLR Reset
and WDT Reset.
Note:BOR is unknown on Power-on Reset. It
must then be set by the us er an d c hec ke d
on subsequent Resets to see if BOR is
clear , indicating a brown-out has occurred.
The BOR status bit is a ‘don’t care’ and is
not necessarily predictable if the brownout circuit is disabled (by clearing the
BOREN bit in the Configuration word).
REGISTER 2-8:PCON: POWER CONTROL REGISTER (ADDRESS 8Eh)
U-0U-0U-0U-0U-0U-0R/W-0R/W-x
——————PORBOR
bit 7bit 0
bit 7-2Unimplemented: R e ad as ‘0’
bit 1POR
bit 0BOR
: Power-on Reset Status bit
1 = No Power-on Reset occurred
0 = A Power-on Reset occurred (must be set in software after a Power-on Reset occurs)
: Brown-out Reset Status bit
1 = No Brown-out Reset occurred
0 = A Brown-out Reset occurred (must be set in software after a Brown-out Reset occurs)
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
DS39598E-page 22 2004 Microchip Technology Inc.
PIC16F818/819
2.3PCL and PCLATH
The Program Counter (PC) is 13 bits wide. The low
byte comes from the PCL register, which is a readable
and writable register. The upper bits (PC<12:8>) are
not readable but are indirectly writable through the
PCLATH register. On any Reset, the upper bits of the
PC will be cleared. Fig ure2-5 shows the two situations
for the loading of the PC. The upper example in the
figure shows how the PC is loaded on a write to PCL
(PCLATH<4:0> → PCH). The lower example in the
figure 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
8
Instruction with
PCL as
Destination
ALU
GOTO,CALL
Opcode <10:0>
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 th e valu e that was s tored fro m the firs t
push. The tenth pus h ov erwr i tes the se co nd push (and
so on).
Note 1: There are no status bits to indicate stack
overflow or stack underflow conditions.
2: There are no instructions/mnemonics
called PUSH or POP. These are actions
that occur from the execution of the
CALL, RETURN, RETLW and RETFIE
instructions or the vectoring to an
interrupt address.
2.4Indirect Addressing: INDF and
FSR Registers
The INDF register is not a physica l register . Addr essing
INDF actually addresse s the regi st er whose ad dress i s
contained in the FSR reg ister (FSR is a pointer). This is
indirect addressing.
EXAMPLE 2-1:INDIR ECT ADDRESSING
• Register file 05 contains the value 10h
• Register file 06 contains the value 0Ah
• Load the value 05 into the FSR register
• A read of the INDF regi ste r will ret urn the value
of 10h
• Increment the value of the FSR register by one
(FSR = 06)
• A read of the INDF register now will return the
value of 0Ah
2.3.1COMPUTED GOTO
A computed GOTO is accomplish ed by adding an offset
to the program counter (ADDWF PCL). When doing a
table read using a computed GOTO method, care
should be exercise d i f the t able loca tio n cros ses a PCL
memory boundary (each 256-byte block). Refer to the
application note AN556, “Implementing a Table Read”
(DS00556).
2.3.2STACK
The PIC16F818/819 family has an 8-level deep x 13-bit
wide hardware stack. The stack space is not part of
either program or data space and the Stack Pointer is
not readable or writable. The PC is PUSHed onto the
stack when a CALL instruction is executed or an
interrupt causes a branch. The stack is POPed in the
event of a RETURN, RETLW or a RETFIE instruction
execution. PCLATH is not affected by a PUSH or POP
operation.
Reading INDF itself indirectly (FSR = 0) will produce
00h. Writing to the INDF register indirectly results in a
no operation (although status bits may be affected).
A simple program to clear RAM locations, 20h-2Fh,
using indirect addressing is shown in Example 2-2.
EXAMPLE 2-2:HOW TO CLEAR RAM
USING INDIRECT
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
CONTINUE
:;YES, continue
An effective 9-b it addre ss is obtained by concatenating
the 8-bit FSR register and the IRP bit (Status<7>) as
shown in Figure 2-6.
2004 Microchip Technology Inc.DS39598E-page 23
PIC16F818/819
FIGURE 2-6:DIRE CT/INDI RECT ADDRESSING
RP1:RP 06
From Opcode
0
Indirect AddressingDirect Addressing
IRPFSR Register
7
0
Bank Select Location Select
00011011
00h
Data
(1)
Memory
7Fh
Bank 0Bank 1Bank 2Bank 3
Note 1:For register file map detail, see Figure 2-3 or Figure 2-4.
80h
FFh
100h
17Fh
180h
1FFh
Bank Select
Location Select
DS39598E-page 24 2004 Microchip Technology Inc.
PIC16F818/819
3.0DATA EEPROM AND FLASH
PROGRAM MEMORY
The data EEPROM and Flash program memory are
readable and writable during normal operation (over
the full V
in the register file space. Instead, it is indirectly
addressed through the Special Function Registers.
There are six SFRs used to read and write this
memory:
• EECON1
• EECON2
• EEDATA
• EEDATH
• EEADR
• EEADRH
This section focuses on reading and writing data
EEPROM and Flash program memory during normal
operation. Refer to the appropriate device programming specification document for serial programming
information.
When interfacing the data memory block, EEDATA
holds the 8-bit data for read/write and EEADR holds the
address of the EEPROM location being accessed.
These devices have 128 or 256 bytes of data
EEPROM, with an address range from 00h to 0FFh.
Addresses from 80h to FFh are unimplemented on the
PIC16F818 device and will read 00h. When writing to
unimplemented locations, the charge pump will be
turned off.
When interfacing the program memory block, the
EEDATA and EEDATH registers form a two-byte word
that holds the 14-bit data fo r read/write and the EEADR
and EEADRH registers f orm a two-byte wor d that holds
the 13-bit address of the EEPROM location being
accessed. These devices have 1K or 2K words of
program Flash, with an address range from 0000h to
03FFh for the PIC16F818 and 0000h to 07FFh for the
PIC16F819. Addresse s abov e the range of the respe ctive device will wra paround to t he beginning o f program
memory.
The EEPROM data memory allows single byte read
and write. The Flash program memory allows singleword reads and four-word block writes. Program
memory writes must first start with a 32-word block
erase, then write in 4-word blocks. A byte write in data
EEPROM memory automatically erases the location
and writes the new data (erase before write).
The write time is controlled by an on-chip timer. The
write/erase voltages are generated by an on-chip
charge pump, rated to operate over the voltage range
of the device for byte or word operations.
DD range). This memory is not directl y mapped
When the device is code-protected, the CPU may
continue to read and write th e data EEPROM memory.
Depending on the settings of the write-protect bits, the
device may or may not be able to write certain blocks
of the program memory; ho wever , reads of the program
memory are allowed. When co de-prote cted, the devic e
programmer can no longer access data or program
memory; this does NOT inh ib it in tern al re ad s or w ri tes .
3.1EEADR and EEADRH
The EEADRH:EEADR register pair can address up to
a maximum of 256 bytes of data EEPROM or up to a
maximum of 8K words of program EEPROM. When
selecting a data address value, only the LSB of the
address is written to the EEADR regist er. When selecting a program address value, the MSB of the address
is written to the EEADRH register and the LSB is
written to the EEADR register.
If the device cont ains less me mory than the fu ll address
reach of the address register pair, the Most Significant
bits of the regist ers are not im plem ented. F or exam ple,
if the device has 128 b yte s o f da t a EEPROM , th e Mos t
Significant bit of EEADR i s not im plement ed o n acces s
to data EEPROM.
3.2EECON1 and EECON2 Registers
EECON1 is the control register for memory accesses.
Control bit, EEPGD, determines if the access will be a
program or data memory access. When clear, as it is
when Reset, any subsequent operations will operate
on the data memory. When set, any subsequent
operations will operate on the program memory.
Control bits, RD and WR, initiate read and write,
respectiv ely. These bits cannot be cleared, only set in
software. They are cleared in hardware at completion
of the read or write operation. The inability to clear the
WR bit in software prevents the accidental, premature
termination of a write operation.
The WREN bi t, when set, wil l allow a write or erase
operation. On power-up, the WREN bit is clear. The
WRERR bit is set when a write (or erase) operation is
interrupted by a MCLR
during normal operation. In these situations, following
Reset, the user can check the WRERR bit and rewrite
the location. The data and address will be unchanged
in the EEDATA and EEADRregisters.
Interrupt flag bit, EEIF in the PIR2 register, is set when
the write is complete. It 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 EEPROM write sequence.
or a WDT Time-out Reset
2004 Microchip Technology Inc.DS39598E-page 25
PIC16F818/819
REGISTER 3-1:EECON1: EEPROM ACCESS CONTROL REGISTER 1 (ADDRESS 18Ch)
R/W-xU-0U-0R/W-xR/W-xR/W-0R/S-0R/S-0
EEPGD——FREEWRERRWRENWRRD
bit 7bit 0
bit 7EEPGD: Program/Data EEPROM Select bit
1 = Accesses program memory
0 = Accesses data memory
Reads ‘0’ after a POR; this bit cannot be changed while a write operation is in progress.
bit 6-5Unimplemented: Read as ‘0’
bit 4FREE: EEPROM Forced Row Erase bit
1 = Erase the program memory row a ddressed by EEADRH:EEADR on th e next WR command
0 = Perform write-only
bit 3WRERR: EEPROM Error Flag bit
1 = A write operation is prematurely terminated (any MCLR
operation)
0 = The write operation completed
bit 2WREN: EEPROM Write Enable bit
1 = Allows write cycles
0 = Inhibits write to the EEPROM
bit 1WR: Write Control bit
1 = Initiates a write cycle. The bit is cleared by hardware once write is complete. The WR bit
can only be set (not cleared) in software.
0 = Write cycle to the EEPROM is complete
bit 0RD: Read Control bit
1 = Initiates an EEPROM read, RD is cleared in hardware. The RD bit can only be set (not
cleared) in software.
0 = Does not initiate an EEPROM read
or any WDT Reset during normal
Legend:
R = Readable bitW = Writable bit S = Set onlyU = Unimplemented bit, read as ‘0’
-n = Value at POR‘1’ = Bit is set‘0’ = Bit is clearedx = Bit is unknown
DS39598E-page 26 2004 Microchip Technology Inc.
PIC16F818/819
3.3Reading Data EEPROM Memory
T o read a d ata memory loca tion, the user must write the
address to the EEADR register, clear the EEPGD
control bit (EECON1<7>) and then set control bit, RD
(EECON1<0>). The data is available in the very next
cycle in the EEDATA register; therefore, it can be read
in the next instruction (see Example 3-1). EEDATA will
hold this value until another read or until it is written to
by the user (during a write operation).
The steps to reading the EEPROM data memory are:
1.Write the address to EEADR. Make su re that the
address is not larger than the memory size of
the device.
2.Clear the EEPGD bit to point to EEPROM data
memory.
3.Set the RD bit to start the read operation.
4.Read the data from the EEDATA regi ster.
EXAMPLE 3-1:DATA EEPROM READ
BANKSEL EEADR; Select Bank of EEADR
MOVFADDR, W;
MOVWFEEADR; Data Memory Address
; to read
BANKSEL EECON1; Select Bank of EECON1
BCFEECON1, EEPGD ; Point to Data memory
BSFEECON1, RD; EE Read
BANKSEL EEDATA; Select Bank of EEDATA
MOVFEEDATA, W; W = EEDATA
The steps to write to EEPROM data memory are:
1.If step 10 is not implemented, check the WR bit
to see if a write is in progress.
2.Write the address to EEADR. Make su re that the
address is not larger than the memory size of
the device.
3.Write the 8-bit data value to be programmed in
the EEDATA register.
4.Clear the EEPGD bit to point to EEPROM data
memory.
5.Set the WREN bit to enable program ope rations.
6.Disable interrupts (if enabled).
7.Execute the special five instruction sequence:
• Write 55h to EECON2 in two steps (first to W,
then to EECON2)
• Write AAh to EECON2 in two steps (first to W,
then to EECON2)
• Set the WR bit
8.Enable interrupts (if using interrupts).
9.Clear the WREN bit to disable program
operations.
10. At the completion of the write cycle, the WR bit
is cleared and the EEIF interrupt flag bit is set
(EEIF must be cleared by firmware). If step 1 is
not implemented, then firmware should check
for EEIF to be set, or WR to b e cle ar, to indicate
the end of the program cycle.
3.4Writing to Data EEPROM Memory
To write an EEPROM data location, the user must first
write the address to the EEADR register and the data
to the EEDATA register. Then, the user must follow a
specific write sequence to initiate the write for each
byte.
The write will not initiate if the write 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 (see Example 3-2).
Additionally, the WREN bit in EECON1 must be set to
enable write. This mechanism prevents accidental
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. EEIF must be
cleared by software.
EXAMPLE 3-2:DATA EEPROM WRITE
BANKSEL EECON1; Select Bank of
BTFSCEECON1, WR; Wait for write
GOTO$-1; to complete
BANKSEL EEADR; Select Bank of
To read a program memory location, the user must
write two bytes of the address to the EEADR and
EEADRH registers, set the EEPGD control bit
(EECON1<7>) and then set control bit, RD
(EECON1<0>). Once the read control bit is set, the
program memo ry Flash con troller wil l use the sec ond
instruction cycle to read the data. This causes the
second instruction immediately following the
“BSF EECON1,RD” instruction to be ignore d. The da ta
is availabl e in the very next cycle in the EEDATA and
EEDATH registers; therefore, it can be read as two
bytes in the following instructions. EEDATA and
EEDA TH registe rs will h old this valu e until ano ther read
or until it is written to by the user (during a write
operation).
EXAMPLE 3-3:FLASH PROGRAM READ
BANKSEL EEADRH; Select Bank of EEADRH
MOVFADDRH, W;
MOVWFEEADRH; MS Byte of Program
; Address to read
MOVFADDRL, W;
MOVWFEEADR; LS Byte of Program
; Address to read
BANKSEL EECON1; Select Bank of EECON1
BSFEECON1, EEPGD ; Point to PROGRAM
; memory
BSFEECON1, RD; EE Read
;
NOP; Any instructions
; here are ignored as
NOP; program memory is
; read in second cycle
; after BSF EECON1,RD
BANKSEL EEDATA; Select Bank of EEDATA
MOVFEEDATA, W; DATAL = EEDATA
MOVWFDATAL;
MOVFEEDATH, W; DATAH = EEDATH
MOVWFDATAH;
3.6Erasing Flash Program Memory
The minimum erase block is 32 words. Only through
the use of an external programmer, or through ICSP
control, can larger blocks of program memory be bulk
erased. Word erase in the Flash array is not supp orted.
When initiating an erase sequence from the microcontroller itse lf, a block of 32 words of program memor y
is erased. The Most Significant 11 bits of the
EEADRH:EEADR point to the block being erased.
EEADR< 4:0> are ignored.
The EECON1 register commands the erase operation.
The EEPGD bit must be set to point to the Flash
program memory. The WREN bit must be set to enabl e
write operations. The FR EE bit is set to select an eras e
operation.
For protection, the wri te i nit iat e s equ enc e f or EEC ON 2
must be used.
After the “BSF EECON1, WR” instruction, the processor
requires two cycles to set up the erase operation. The
user must place two NOP ins tructions after the WR bit is
set. The processor will halt internal operations for the
typical 2 ms, only during the cycle in which the erase
takes place. This is not Sleep mode, as the clocks and
peripherals will continue to run. After the erase cycle,
the processor will resume operation with the third
instruction after the EECON1 write instruction.
3.6.1FLASH PROGRAM MEMORY
ERASE SEQUENCE
The sequence of events for erasing a block of internal
program memory location is:
1.Load EEADRH:EEADR with address of row
being erased.
2.Set EEPGD bit to point to program memory; set
WREN bit to enable writes and set FREE bit to
enable the erase.
3.Disable interrupts.
4.Write 55h to EECON2.
5.Write AAh to EECON2.
6.Set the WR bit. This will begin the row erase
cycle.
7.The CPU will stall for duration of the erase.
DS39598E-page 28 2004 Microchip Technology Inc.
Loading...
+ 146 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.