Microchip Technology IC MCU 8BIT PIC18F65K22-I/PT TQFP-64 MCP PIC18F65K22-I/PT Data Sheet

Product codes
PIC18F65K22-I/PT
Page of 550
PIC18F87K22 FAMILY
DS39960D-page 116
 2009-2011 Microchip Technology Inc.
7.4
Erasing Flash Program Memory
The erase blocks are:
• PIC18FX5K22 and PIC18FX6K22 – 32 words or 
64 bytes
• PIC18FX7K22 – 64 words or 128 bytes
Word erase in the Flash array is not supported.
When initiating an erase sequence from the micro-
controller itself, a block of 64 or 128 bytes of program
memory is erased. The Most Significant 16 bits of the
TBLPTR<21:6> point to the block being erased. The
TBLPTR<5:0> bits 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 enable
write operations. The FREE bit is set to select an erase
operation.
For protection, the write initiate sequence for EECON2
must be used.
A long write is necessary for erasing the internal Flash.
Instruction execution is halted while in a long write
cycle. The long write will be terminated by the internal
programming timer.
7.4.1
FLASH PROGRAM MEMORY 
ERASE SEQUENCE
The sequence of events for erasing a block of internal
program memory location is:
1.
Load the Table Pointer register with the address
of the row to be erased.
2.
Set the EECON1 register for the erase operation:
• Set the EEPGD bit to point to program memory
• Clear the CFGS bit to access program memory
• Set the WREN bit to enable writes
• Set the FREE bit to enable the erase
3.
Disable the interrupts.
4.
Write 0x55 to EECON2.
5.
Write 0xAA to EECON2.
6.
Set the WR bit.
This begins the row erase cycle.
The CPU will stall for the duration of the erase
for T
IW
. (See Parameter 
7.
Re-enable interrupts.
EXAMPLE 7-2:
ERASING A FLASH PROGRAM MEMORY ROW 
MOVLW
CODE_ADDR_UPPER
; load TBLPTR with the base
MOVWF
TBLPTRU 
; address of the memory block
MOVLW
CODE_ADDR_HIGH
MOVWF
TBLPTRH 
MOVLW
CODE_ADDR_LOW
MOVWF
TBLPTRL 
ERASE_ROW 
BSF 
EECON1, EEPGD
; point to Flash program memory
BCF
EECON1, CFGS
; access Flash program memory
BSF
EECON1, WREN
; enable write to memory
BSF 
EECON1, FREE
; enable Row Erase operation
BCF
INTCON, GIE
; disable interrupts
Required
MOVLW
0x55
Sequence
MOVWF
EECON2 
; write 55h
MOVLW
0xAA
MOVWF
EECON2 
; write 0AAh
BSF
EECON1, WR
; start erase (CPU stall)
BSF
INTCON, GIE
; re-enable interrupts