Read and write PIC EEPROM

More a little post-it than a post, I wanted to put somewhere the commands :

Using the eeprom is as simple as these two commands :


A little example (reading from the USART a value and putting it in the EEPROM address 0x00)

while (!DataRdyUSART());
char data = ReadUSART();

Note that theses header are defined in the standard xc8 header which will include (or , …) or High Tech C headers if you come from the past :

// MACROS for EEPROM Access
/* macro versions of EEPROM read and write */

EEPROM_READ() is NOT safe to use immediately after any
write to EEPROM, as it does NOT wait for WR to clear. This is by
design, to allow minimal code size if a sequence of reads is
desired. To guarantee uncorrupted writes insert
before calling EEPROM_READ().

#if _EEPROMSIZE > 0 && defined(_PLIB)
#define EEPROM_READ(addr) Read_b_eep(addr)
#define eeprom_read(addr) Read_b_eep(addr)
#define EEPROM_READ(addr) 0 // Added only for code portability
#define eeprom_read(addr) 0

#if _EEPROMSIZE > 0 && defined(_PLIB)
#define EEPROM_WRITE(addr, value) (Busy_eep(), Write_b_eep(addr,value))
#define eeprom_write(addr, value) (Busy_eep(), Write_b_eep(addr,value))
#define EEPROM_WRITE(addr, value) // Added only for code portability
#define eeprom_write(addr, value)

3 Replies to “Read and write PIC EEPROM”

  1. Hey thanks for posting this, but what are all the preprocessor directives? Are those needed as well, or is only the first section of code required?

    Also, can a single address be rewritten?

    I have never used EEPROM before, and am new to microcontroller programming in general, so I’m not entirely sure if anything needs to be initialized before using the READ/WRITE functions.


    1. Hi,

      The preprocessors and so on aren’t needed, it was just to show how to find such things while programming PICs (by searching in the header, simply).

      Good luck,

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.