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.
| Memory | Survives power loss? | Typical size | Used for |
|---|---|---|---|
| RAM | No | Tens to a few thousand bytes | Variables while the program runs |
| Flash (program memory) | Yes | Several kilobytes+ | The compiled program itself |
| EEPROM | Yes | Tens to a few hundred bytes | Small 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.
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;
}