Datasheet
PICkit™ 3 Debug Express Lessons
© 2009 Microchip Technology Inc. DS41370C-page 63
3.11.1 Erasing and Writing Flash Program Memory
Unlike writing Data EEPROM Memory, writing Flash program memory requires that the
locations being written are erased first. When erased, a program memory location has
all bits set to ‘1’. Thus an erased byte has the hex value 0xFF. Writing a program
memory location sets the appropriate bits to ‘0’, but a write cannot set a bit ‘1’. Also
different from EEPROM operations is that program memory erases and writes cannot
operate on a single byte, but instead operation on “blocks” of a particular number of
bytes.
The PIC18F45K20 erase block size is 64 bytes. This means it will always erase 64
sequential bytes at once, and the block must start at an address that is a multiple of 64.
For example, we could erase the 64 bytes from address 128 through 191 at once, but
not the 64 bytes from address 100 through 163.
To erase a 64 byte block of program memory, we use a rom pointer to set the address
of the block to be erased, and use EECON1 to control the erase. Setting the pointer
address puts the address in the TBLPTRx Special Function Registers. These 3
registers hold the address for program memory operations with TBLRD and TBLWR
assembly instructions. The MPLAB C Compiler handles these tasks for us. The
EEPGD bit, EECON1, is set to ‘1’, so the operation affects program memory and not
data EEPROM. The CFGS bit is set to ‘0’, as we do not want to select the Configuration
bits. To select an erase operation as opposed to a write operation, bit FREE of
EECON1 is set to ‘1’. WREN is then set to ‘1’ to enable write/erase operations.
// point to address 2176, which is a multiple of 64
rom_pointer = (near rom char *)0x880;
EECON1bits.EEPGD = 1; // point to flash program memory
EECON1bits.CFGS = 0; // not configuration registers
EECON1bits.FREE = 1; // we're erasing
EECON1bits.WREN = 1; // enable write/erase operations
Next, the EECON2 sequence must be followed as with data EEPROM writes, and the
WR bit of EECON1 is set to initiate the write.
INTCONbits.GIE = 0; // Disable interrupts
EECON2 = 0x55; // Begin Write sequence
EECON2 = 0xAA;
EECON1bits.WR = 1; // Set WR bit to begin EEPROM write
INTCONbits.GIE = 1; // re-enable interrupts
As with a data EEPROM write, an erase or write to Flash program memory takes up to
several ms to complete. While there is an active erase or a write operation to program
memory, all microcontroller program execution is halted since it is possible the
microcontroller might attempt to execute instructions from the locations being erased
or written. This would be illegal, as the program memory location’s value is in an
indeterminate state until the operation has completed.
The PIC18F45K20 write block size is 32 bytes. This requires that we write 32
sequential bytes at a time. As with erasing, the first byte must be at an address that is
a multiple of the block size, 32.
The sequence for writing program memory is very similar to that for erasing. The
differences are that a ROM pointer is used to write the 32 locations, and that the
EECON1 bit, FREE, is cleared to select a write operation. Don’t forget that the locations
to be written must be erased first!