English
Language : 

PIC16F526 Datasheet, PDF (24/122 Pages) Microchip Technology – 14-Pin, 8-Bit Flash Microcontroller
PIC16F526
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
BANKSEL
MOVLW
MOVWF
MOVLW
MOVWF
BSF
BSF
EEADR
EE_ADR_WRITE
; LOAD ADDRESS
EEADR
;
EE_DATA_TO_WRITE ; LOAD DATA
EEDATA
; INTO EEDATA REGISTER
EECON,WREN
; ENABLE WRITES
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.
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
MOVF EEDATA, W
BSF
XORWF
BTFSS
GOTO
EECON, RD
EEDATA, W
STATUS, Z
WRITE_ERR
;EEDATA has not changed
;from previous write
;Read the value written
;
;Is data the same
;No, handle error
;Yes, continue
REGISTER 5-1: EEDATA: FLASH DATA REGISTER
R/W-x
R/W-x
R/W-x
R/W-x
R/W-x
EEDATA7 EEDATA6 EEDATA5 EEDATA4 EEDATA3
bit 7
R/W-x
EEDATA2
R/W-x
EEDATA1
R/W-x
EEDATA0
bit 0
Legend:
R = Readable bit
-n = Value at POR
W = Writable bit
‘1’ = Bit is set
U = Unimplemented bit, read as ‘0’
‘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
REGISTER 5-2:
U-0
—
bit 7
EEADR: FLASH ADDRESS REGISTER
U-0
R/W-x
R/W-x
R/W-x
—
EEADR5 EEADR4 EEADR3
R/W-x
EEADR2
R/W-x
EEADR1
R/W-x
EEADR0
bit 0
Legend:
R = Readable bit
-n = Value at POR
W = Writable bit
‘1’ = Bit is set
U = Unimplemented bit, read as ‘0’
‘0’ = Bit is cleared
x = Bit is unknown
bit 7-6
bit 5-0
Unimplemented: Read as ‘0’.
EEADR<5:0>: 6-bits of data to be read from/written to data Flash
DS41326D-page 24
 2010 Microchip Technology Inc.