Lessons
2012 Microchip Technology Inc. DS41628B-page 93
3.14 LESSON 13: EEPROM
3.14.1 Introduction
This lesson provides code for writing and reading a single byte onto the on-board
EEPROM. EEPROM is nonvolatile memory, meaning that it does not lose its value
when power is shut off. This is unlike RAM, which will lose its value when no power is
applied. The EEPROM is useful for storing variables that must still be present during
no power. It is also convenient to use if the entire RAM space is used up. Both the
PIC16F1829 and the PIC18F14K22 have 256 bytes of EEPROM available. Writes and
reads to the EEPROM are relatively quick, and are much faster than program memory
operations.
3.14.2 Hardware Effects
Press the switch to save the LED pattern, and then disconnect the power. When power
is then applied again, the program will start with that same LED pattern lit.
3.14.3 Summary
When the lesson is first programmed, no LEDs will light up, even with movement of the
POT. When the switch is pressed, the LED corresponding to the ADC reading at that
instant will be lit, and the PIC MCU will go to Sleep until the switch is pressed again.
Each press of the switch saves the ADC value into EEPROM. The PIC MCU uses inter-
rupts to wake-up from Sleep, take an ADC reading, save to EEPROM, and then goes
back to Sleep.
FIGURE 3-22: PROGRAM FLOW
Debounce
Init
Sleep
Write ADC value to EEPROM
Switch pressed/released
Still down
Not down
Read EEPROM and move data onto LEDs
Check if ADC result is ‘0’
Take ADC reading and move it onto LEDs