Datasheet
PICkit™ 3 Debug Express Lessons
© 2009 Microchip Technology Inc. DS41370C-page 61
3.11 LESSON 11: PROGRAM MEMORY OPERATIONS
Topics covered in this include reading, writing and erasing locations in the Flash
program memory, protecting areas of program memory in the Configuration bits, and
considerations for using C pointers to program memory.
The MPLAB C Compiler simplifies working with data stored in program memory by
allowing pointers to program memory to be declared. The pointer address length is
either 16 or 24 bits, depending on which “Code Model” is selected in the project
settings. The “Small Code Model” will generate 16-bit pointers, while the “Large Code
Model” generates 24-bit pointers. For the best microcontroller performance, the “Small
Code Model” with 16-bit pointers should be used. The “Large Code Model” is
necessary for devices that have more than 64 KB of Flash program memory to be able
to point to locations above the first 64 KB of program memory. (The maximum of a
16-bit value is 65536, which is 64 x 1024 or 64K).
The Code Model settings may be changed in the MPLAB IDE by selecting Project >
Build Options… > Project. This brings up the Build Options dialog. Select the MPLAB
C18 tab and then “Memory
Model” from the “Categories” drop-down box as shown in
Figure 3-52.
An individual pointer declaration may also use the keywords near or far to explicitly
specify the pointer address length. Use of either keyword overrides the code model
settings.
near rom char *rom_pointer; // 16-bit pointer to program memory far
rom char *rom_pointer; // 24-bit pointer to program memory
For more information on project memory models, see Chapter 3 of the “MPLAB C18 C
Compiler User’s Guide” (DS51288).
Key Concepts
- Pointers declared with the ROM keyword point to program memory loca-
tions.
- The EECON1 and EECON2 SFRs control program memory erase and write
operations.
- Unlike Data EEPROM memory, the Flash program memory must be explic-
itly erased before it may be written.
- The CPx (code-protect) Configuration bits prevent programmers from read-
ing ranges of a microcontroller’s program memory.
- The WRTx Configuration bits prevent software write operations on ranges of
program memory, and the EBTRx bits prevent software read operations on
ranges of program memory.
- ROM pointers and reading Flash program memory