EEPROM can be accessed using different methods in the MPLAB® XC8 Compiler:
- Macro : __EEPROM_DATA()
- Functions: eeprom_read() and eeprom_write() routines.
- EEPROM Access Macros: EEPROM_READ and EEPROM_WRITE
- 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.