English
Language : 

PIC16F872_06 Datasheet, PDF (29/168 Pages) Microchip Technology – 28-Pin, 8-Bit CMOS Flash Microcontroller with 10-Bit A/D
PIC16F872
The steps to write to program memory are:
1. Write the address to EEADRH:EEADR. Make
sure that the address is not larger than the mem-
ory size of the device.
2. Write the 14-bit data value to be programmed in
the EEDATH:EEDATA registers.
3. Set the EEPGD bit to point to FLASH Program
memory.
4. Set the WREN bit to enable program operations.
5. Disable interrupts (if enabled).
6. 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
7. Execute two NOP instructions to allow the micro-
controller to setup for write operation.
8. Enable interrupts (if using interrupts).
9. Clear the WREN bit to disable program
operations.
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). Since the microcontroller
does not execute instructions during the write cycle, the
firmware does not necessarily have to check either
EEIF or WR to determine if the write had finished.
EXAMPLE 3-4:
BSF
BCF
MOVF
MOVWF
MOVF
MOVWF
MOVF
MOVWF
MOVF
MOVWF
BSF
BSF
BSF
BCF
MOVLW
MOVWF
MOVLW
MOVWF
BSF
NOP
NOP
BSF
BCF
FLASH PROGRAM WRITE
STATUS, RP1
STATUS, RP0
ADDRL, W
EEADR
ADDRH, W
EEADRH
VALUEL, W
EEDATA
VALUEH, W
EEDATH
STATUS, RP0
EECON1, EEPGD
EECON1, WREN
INTCON, GIE
0x55
EECON2
0xAA
EECON2
EECON1, WR
INTCON, GIE
EECON1, WREN
;
;Bank 2
;Write address
;of desired
;program memory
;location
;Write value to
;program at
;desired memory
;location
;Bank 3
;Point to Program memory
;Enable writes
;Only disable interrupts
;if already enabled,
;otherwise discard
;Write 55h to
;EECON2
;Write AAh to
;EECON2
;Start write operation
;Two NOPs to allow micro
;to setup for write
;Only enable interrupts
;if using interrupts,
;otherwise discard
;Disable writes
3.6 Write Verify
The PIC16F87X devices do not automatically verify the
value written during a write operation. Depending on
the application, good programming practice may dic-
tate that the value written to memory be verified against
the original value. This should be used in applications
where excessive writes can stress bits near the speci-
fied endurance limits.
3.7 Protection Against Spurious Writes
There are conditions when the device may not want to
write to the EEPROM Data memory or FLASH program
memory. To protect against these spurious write condi-
tions various mechanisms have been built into the
device. On power-up, the WREN bit is cleared and the
Power-up Timer (if enabled) prevents writes.
The write initiate sequence and the WREN bit together
help prevent any accidental writes during brown-out,
power glitches or firmware malfunction.
© 2006 Microchip Technology Inc.
DS30221C-page 27