Datasheet
PIC16F526
DS41326E-page 24 2010 Microchip Technology Inc.
5.2.2 WRITING TO FLASH DATA
MEMORY
Once a cell is erased, new data can be written.
Program execution is suspended during the write cycle.
The following sequence must be performed for a single
byte write.
1. Load EEADR with the address.
2. Load EEDATA with the data to write.
3. Set the WREN bit to enable write access to the
array.
4. Set the WR bit to initiate the erase cycle.
If the WR bit is not set in the instruction cycle after the
WREN bit is set, the WREN bit will be cleared in
hardware.
Sample code that follows this procedure is included in
Example 3.
EXAMPLE 3: WRITING A FLASH DATA
MEMORY ROW
5.3 Write Verify
Depending on the application, good programming
practice may dictate that data written to the Flash data
memory be verified. Example 4 is an example of a write
verify.
EXAMPLE 4: WRITE VERIFY OF FLASH
DATA MEMORY
REGISTER 5-1: EEDATA: FLASH DATA REGISTER
REGISTER 5-2: EEADR: FLASH ADDRESS REGISTER
BANKSEL EEADR
MOVLW EE_ADR_WRITE ; LOAD ADDRESS
MOVWF EEADR ;
MOVLW EE_DATA_TO_WRITE ; LOAD DATA
MOVWF EEDATA ; INTO EEDATA REGISTER
BSF EECON,WREN ; ENABLE WRITES
BSF EECON,WR ; INITITATE ERASE
Note 1: Only a series of BSF commands will work
to enable the memory write sequence
documented in Example 2. No other
sequence of commands will work, no
exceptions.
2: For reads, erases and writes to the Flash
data memory, there is no need to insert a
NOP into the user code as is done on mid-
range devices. The instruction immediately
following the “BSF EECON,WR/RD” will be
fetched and executed properly.
MOVF EEDATA, W ;EEDATA has not changed
;from previous write
BSF EECON, RD ;Read the value written
XORWF EEDATA, W ;
BTFSS STATUS, Z ;Is data the same
GOTO WRITE_ERR ;No, handle error
;Yes, continue
R/W-x R/W-x R/W-x R/W-x R/W-x R/W-x R/W-x R/W-x
EEDATA7 EEDATA6 EEDATA5 EEDATA4 EEDATA3 EEDATA2 EEDATA1 EEDATA0
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
bit 7-0 EEDATA<7:0>: 8-bits of data to be read from/written to data Flash
U-0 U-0 R/W-x R/W-x R/W-x R/W-x R/W-x R/W-x
— — EEADR5 EEADR4 EEADR3 EEADR2 EEADR1 EEADR0
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
bit 7-6 Unimplemented: Read as ‘0’.
bit 5-0 EEADR<5:0>: 6-bits of data to be read from/written to data Flash