Datasheet

PIC16F627A/628A/648A
DS40044G-page 94 © 2009 Microchip Technology Inc.
13.7 Using 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 an array refresh must be
performed. For this reason, variables that change
infrequently (such as constants, IDs, calibration, etc.)
should be stored in Flash program memory.
A simple data EEPROM refresh routine is shown in
Example 13-4.
EXAMPLE 13-4: DATA EEPROM REFRESH ROUTINE
Note: If data EEPROM is only used to store
constants and/or data that changes rarely,
an array refresh is likely not required. See
specification D124.
BANKSEL 0X80 ;select Bank1
CLRF EEADR ;start at address 0
BCF INTCON, GIE ;disable interrupts
BTFSC INTCON, GIE ;see AN576
GOTO $ - 2
BSF EECON1, WREN ;enable EE writes
Loop
BSF EECON1, RD ;retrieve data into EEDATA
MOVLW 0x55 ;first step of ...
MOVWF EECON2 ;... required sequence
MOVLW 0xAA ;second step of ...
MOVWF EECON2 ;... required sequence
BSF EECON1, WR ;start write sequence
BTFSC EECON1, WR ;wait for write complete
GOTO $ - 1
#IFDEF __16F648A ;256 bytes in 16F648A
INCFSZ EEADR, f ;test for end of memory
#ELSE ;128 bytes in 16F627A/628A
INCF EEADR, f ;next address
BTFSS EEADR, 7 ;test for end of memory
#ENDIF ;end of conditional assembly
GOTO Loop ;repeat for all locations
BCF EECON1, WREN ;disable EE writes
BSF INTCON, GIE ;enable interrupts (optional)