
Using EEPROM Memory
This example illustrates write to and read from built-in EEPROM memory. The program works as follows. The main loop constantly reads EEPROM memory location at address 5 (decimal). The program then enters an endless loop in which PORTB is incremented and PORTA.2 input state is checked. At the moment of pressing the push button called MEMO, a number stored in PORTB will be saved in EEPROM and directly read and shown on PORTD in binary form.
/*Header******************************************************/
void main() {
ANSEL = 0; // All I/O pins are configured as digital
ANSELH = 0;
PORTB = 0; // Port B initial value
TRISB = 0; // All port B pins are configured as outputs
PORTD = 0; // Port B initial value
TRISD = 0; // All port D pins are configured as outputs
TRISA = 0xFF; // All port A pins are configured as inputs
PORTD = EEPROM_Read(5); // Read EEPROM memory at address 5
do {
PORTB=PORTB++; // Increment port B by 1
Delay_ms(100); // 100mS delay
if (PORTA.F2){
EEPROM_Write(5,PORTB); // If MEMO is pressed, save PORTB
PORTD = EEPROM_Read(5); // Read written data
do;
while (PORTA.F2); // Remain in this loop as long as the button is
// pressed
}
}
while(1); // Endless loop
}
In order to check this circuit, it is sufficient to press the MEMO button and turn off the device. After restarting the device, the program will display the saved value on port D. Remember that at the moment of writing, this value was displayed on port B).
In order to make this example work properly, it is necessary to tick off the EEPROM library in the Library Manager prior to compiling: