English
Language : 

PIC16F872_06 Datasheet, PDF (28/168 Pages) Microchip Technology – 28-Pin, 8-Bit CMOS Flash Microcontroller with 10-Bit A/D
PIC16F872
3.4 Reading the FLASH Program
Memory
Reading FLASH Program memory is much like that of
EEPROM Data memory, only two NOP instructions
must be inserted after the RD bit is set. These two
instruction cycles that the NOP instructions execute will
be used by the microcontroller to read the data out of
program memory and insert the value into the
EEDATH:EEDATA registers. Data will be available fol-
lowing the second NOP instruction. EEDATH and
EEDATA will hold their value until another read opera-
tion is initiated, or until they are written by firmware.
The steps to reading the FLASH 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. Set the EEPGD bit to point to FLASH Program
memory.
3. Set the RD bit to start the read operation.
4. Execute two NOP instructions to allow the micro-
controller to read out of program memory.
5. Read the data from the EEDATH:EEDATA
registers.
EXAMPLE 3-3:
BSF
BCF
MOVF
MOVWF
MOVF
MOVWF
BSF
BSF
BSF
NOP
NOP
BCF
MOVF
MOVWF
MOVF
MOVWF
FLASH PROGRAM READ
STATUS, RP1
;
STATUS, RP0
;Bank 2
ADDRL, W
;Write the
EEADR
;address bytes
ADDRH,W
;for the desired
EEADRH
;address to read
STATUS, RP0
;Bank 3
EECON1, EEPGD
;Point to Program memory
EECON1, RD
;Start read operation
;Required two NOPs
;
STATUS, RP0
;Bank 2
EEDATA, W
;DATAL = EEDATA
DATAL
;
EEDATH,W
;DATAH = EEDATH
DATAH
;
3.5 Writing to the FLASH Program
Memory
Writing to FLASH Program memory is unique in that the
microcontroller does not execute instructions while pro-
gramming is taking place. The oscillator continues to
run and all peripherals continue to operate and queue
interrupts, if enabled. Once the write operation com-
pletes (specification #D133), the processor begins exe-
cuting code from where it left off. The other important
difference when writing to FLASH Program memory is
that the WRT configuration bit, when clear, prevents
any writes to program memory (see Table 3-1).
Just like EEPROM Data memory, there are many steps
in writing to the FLASH Program memory. Both
address and data values must be written to the SFRs.
The EEPGD bit must be set and the WREN bit must be
set to enable writes. The WREN bit should be kept
clear at all times, except when writing to the FLASH
Program memory. The WR bit can only be set if the
WREN bit was set in a previous operation, i.e., they
both cannot be set in the same operation. The WREN
bit should then be cleared by firmware after the write.
Clearing the WREN bit before the write actually com-
pletes will not terminate the write in progress.
Writes to program memory must also be prefaced with
a special sequence of instructions that prevent inad-
vertent write operations. This is a sequence of five
instructions that must be executed without interruption
for each byte written. These instructions must then be
followed by two NOP instructions to allow the microcon-
troller to setup for the write operation. Once the write is
complete, the execution of instructions starts with the
instruction after the second NOP.
DS30221C-page 26
© 2006 Microchip Technology Inc.