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 gives 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

The previously shown C code makes the presumption that the flash memory location pointed to by MyConst (i.e. address 0x010800) was altered by a Flash Table Write instruction before is was read.

 Learn More

Program Space Visibility
Extended Data Space
Writing to Flash Memory on a 16-bit PIC MCU

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.