How do I write and read data from EEPROM using the XC8 compiler?

EEPROM can be accessed using different methods in the MPLAB® XC8 Compiler:

  1. Macro : __EEPROM_DATA()
  2. Functions: eeprom_read() and eeprom_write() routines.
  3. EEPROM Access Macros: EEPROM_READ and EEPROM_WRITE
  4. eeprom qualifier

Method 1: Using the __EEPROM_DATA macro

The __EEPROM_DATA() macro can be used to place initial values into the HEX file ready for programming. This macro cannot be used to write to EEPROM locations during runtime; it is used for pre-loading EEPROM contents at program time only. It is recommended that the macro be placed outside any function definition. The macro has eight parameters, representing eight data values. Each value should be a byte in size. Unused values should be specified with zero.

Example:

Method 2: Using eeprom_read() and eeprom_write() routines

The library functions eeprom_read() and eeprom_write(), can be called to read from and write to the EEPROM during program execution.

Example:

Method 3: EEPROM Access Macro

Example:

Method 4: eeprom Qualifier

The eeprom qualifier may be used to indicate that variables should be positioned in EEPROM. This is only available for baseline and mid-range devices (PIC10/12/16). Some devices may not implement EEPROM, and this qualifier is not supported for those non-EEPROM devices. Stack-based variables (autos and parameters) cannot use the eeprom specifier. The eeprom qualifier can be used for non-auto Primary and Secondary data types.

Example:

© 2023 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.