In this tutorial we will discuss how to access the PIC18F4520 internal EEPROM memory to store and retrieve the data. Eeprom is basically used to store the non volatile data which is required to be stored even if there is power loss or controller resets. Pic16f877a Eeprom.png

PIC18F4520 Memories

PIC18F4520 comes with three memories Flash,RAM and EEPROM. Below table shows the memory capacity of PIC18F4520:

Memory Size Description
FLASH 8k-bytes Used to store the programs
RAM 368-bytes Temporary/ScratchPad memory used during program execution.
EEPROM 256-bytes Used to store the non-volatile data across power cycles



Prerequisites

Please check this tutorial for detailed explanation on PIC18F4520 Internal Eeprom.

Code

Below is the program to write the data(A-Z) to eeprom and then read it back.

#include <pic16f877a.h>
#include "uart.h"
#include "eeprom.h"
/* start the main program */
int main()
{
unsigned char eeprom_address = 0, write_char, read_char;
UART_Init(9600);
for(write_char='A';write_char<='Z';write_char++)
{
UART_Printf("\n\rEeprom Write: %c ",write_char); //Print the message on UART
EEPROM_WriteByte(eeprom_address, write_char); // Write the data at memoryLocation 0x00
read_char = EEPROM_ReadByte(eeprom_address); // Read the data from memoryLocation 0x00
UART_Printf("Eeprom Read: %c",read_char); //transmit the data read from Eeprom
}
while (1);
return 0;
}

Pic16f877a EepromOutput.png