ABC of Electronics cosycom.com
PIC BASICS · LESSON 18

On-Chip EEPROM & Non-Volatile Storage

Program memory forgets everything the moment power drops. On-chip EEPROM is the small, separate memory built specifically to remember things across a power cycle.

Ordinary variables in a PIC program live in RAM, which loses its contents the instant power is removed — exactly as intended, since RAM is meant to hold values that only matter while the program is running. But some values — a saved configuration setting, a running total, the last-used mode — need to survive being switched off and back on. For that, many PIC chips include a small block of EEPROM (Electrically Erasable Programmable Read-Only Memory), a separate memory area built to retain its contents with no power at all, for years.

EEPROM trades two things for that persistence: capacity and speed. It's typically far smaller than program memory — commonly a few hundred bytes rather than kilobytes — and each write takes a small but real amount of time (often a few milliseconds), during which the chip's EEPROM controller is busy and further writes must wait.

Comparing the memory types on a typical PIC
MemorySurvives power loss?Typical sizeUsed for
RAMNoTens to a few thousand bytesVariables while the program runs
Flash (program memory)YesSeveral kilobytes+The compiled program itself
EEPROMYesTens to a few hundred bytesSmall values that must persist

Reading and writing EEPROM is done through a small set of dedicated registers, usually wrapped by simple library functions that hide the low-level handshake of setting an address, triggering a write, and waiting for it to complete.

main.c — saving and restoring a value across power cycles
void eeprom_write(unsigned char address, unsigned char value) {
    EEADR = address;
    EEDATA = value;
    EECON1bits.WREN = 1;   // enable writing
    EECON1bits.WR = 1;     // start the write
    while (EECON1bits.WR);  // wait for it to finish
    EECON1bits.WREN = 0;   // disable writing again (safety)
}

unsigned char eeprom_read(unsigned char address) {
    EEADR = address;
    EECON1bits.RD = 1;
    return EEDATA;
}
KEY TAKEAWAY
EEPROM is small and slower than RAM by design — it's meant for a handful of important values that must outlive a power-off, not for data your program touches constantly.