Reading Flash Memory on a 16-bit PIC® MCU
Flash memory on a 16-bit PIC® MCU is primarily used to contain program instructions. It can also be used to store data (constants). Under program control, the data in individual words and bytes of the program memory can be read.
Reading with PSV and EDS
Each 16-bit PIC MCU has one of the two access mechanisms for reading Flash memory: Program Space Visibility (PSV) or Extended Data Space (EDS). With PSV or EDS, application programs read the value of constants stored in Flash the same way they access variables stored in data memory. This site's pages on PSV and Extended Data memory give the details of reading Flash memory.
Table Read Instructions
In addition to either PSV or EDS, all 16-bit MCUs can directly read the contents of any Flash memory location using one of the two table read Instructions. Both of these table read instructions have the option of returning eight bits of data.
Table Read Low TBLRDL
- Returns bits <15:0> of the specified program memory address.
Table Read Low Byte TBLRDL.B
- Returns bits <7:0> of the specified program memory address if the address has an LSB of 0
- Returns bits <15:8> of the specified program memory address if the address has an LSB of 1
Table Read High TBLRDH
- Returns 16 bits of data. The lower eight bits of the response are bits <23:16> of the specified program memory address. The upper eight bits returned are 0 (they don't exist).
Table Read High Byte TBLRDH.B
- Returns bits <23:16> if the specified program memory address if the Least Significant Bit (LSB) of the specified program memory address is 0
- Returns a value of 0 if the specified program memory address if the LSB of the specified program memory address is 1 (the upper eight bits of the upper word of program memory is non-existent phantom memory).
Specifying the Address to Read
Table read instructions accept two working registers as parameters. The first working register, combined with TABLPAG specifies the program memory address to be read. The second parameter is the register in which the instruction stores the result.
Assembly Language Examples of Table Read Instructions
The following assembly language examples demonstrate various implementations of the table read instructions. These examples presume:
- The specified memory address to read will be 0x010800
- The contents of the specified program memory location is 0x123456
- W1 is the register used to pass the address to the instructions
- W2 is used as the result register
Specifying the Address
The address of the memory address being read must be loaded into TBLPAG and a working register.
Reading the Lowest 16 bits.
The TBLRDL instruction reads the lowest 16 bits of the specified memory address.
Reading the Lowest byte
The TBLRDL.B instruction reads the lowest eight bits of the specified memory address.
Reading the 'Middle' byte
The middle byte is actually the upper eight bits of the lower word of program memory. The TBLRDL.B instruction is used to read this byte. The address passed to TBLRDL.B indicates which of the two bytes should be returned. If the specified address' LSB is 0, the lower byte is returned. If the specified address's LSB is 1, the upper byte is turn.
To read the middle byte the specified address must be incremented by one before the TBLRDL.B is instruction is executed.
Reading the Highest byte
The TBLRDH instruction is used to read the highest 16 bits of the specified memory address. Since the upper eight bits of the result are always returned as 0, this instruction effectively returns on the upper eight bits of the 24-bit wide program memory.
Accessing this byte with TBLRDH requires the specified address be set at the even ( LSB = 0) memory address.
Using the C Language to Read Program Memory
The MPLAB XC16 compiler provides two built-in functions that implement the table read instructions.
__builtin_tblrdl returns the lowest 16 bits of the specified memory address.
__builtin_tblrdh returns the highest 16 bits of the specified memory address.
Table Read Function Prototypes
Both built-in table read functions accept the 16-bit page "offset" for the address of the CONST being read. The instructions determine the specific memory location by combining the current value of TBLPAG register to the offset parameter.
Determining the address
MPLAB XC16 provides support functions to assist in setting the address of the memory to be read:
- __builtin_tblpage accepts a 16-bit pointer to a memory address and returns the value to be loaded into TBLPAG
- __builtin_tbloffset accepts a pointer to the memory address and returns the value to be passed as the offset
Sample C code
Learn More
Program Space Visibility
Extended Data Space
Writing to Flash Memory on a 16-bit PIC MCU