Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Reading and Writing to PIC Internal EEPROM – XC8

Reading and Writing to PIC Internal EEPROM – XC8


There are three types of memories in a PIC Microcontroller, The Flash Program Memory, The Data Memory (RAM) and The EEPROM Data Memory. The code that is written by the user to perform a specific task by the microcontroller is stored in the Flash Program Memory of a microcontroller. Flash memory makes it possible to program a microcontroller many times because it is re-writable, this memory can be written into and erased many times. RAM Data Memory is used for storing data temporarily during program execution and it is volatile. Once the power is OFF or after CPU reset, this memory will be cleared and lose all its content. RAM Data Memory locations are also called General Purpose Registers (GPR). These two memories have faster response time. The third memory is EEPROM memory which is an abbreviation for Electrically Erasable Programmable Read Only Memory. EEPROM memory can be read and write electrically, can be accessed through program. It is a non volatile memory but has slower response time. EEPROM memory can be used to store data which should not be loss during power loss or CPU reset. such data could be like device parameters or settings which could be entered once and stored in the EEPROM.

EEPROM Registers

The data in the EEPROM and Flash Program Memory can be read/write during normal operations (over full VDD range). These memories are not mapped in the register file space, instead of it can be accessed through Special Function Registers (SFR) for read and write operations. According to the 18F45K22 datasheet, page 105 we have the following Special Function Registers:

  • EECON1
  • EECON2

EEDATA register hold 8-bit data for read/write and EEADR holds the address of EEPROM memory location to be accessed. PIC Microcontrollers usually have 128/256 bytes of data EEPROM memory with address ranging from 00h to FFh. The PIC18F45K22 has 256 bytes of EEPROM memory.

Note: The Address ranges from 00h to FFh for devices having 256 bytes while for 128 bytes devices it is 00h to 7Fh.

The datasheet explains how data can be read or written to the EEPROM, but to make things easier, we are going to use the XC8 EEP library which simplifies everything for us.

XC8 EEP Library Functions

The EEP Peripheral Library is used to get access to the EEPROM memory of a PIC microcontroller. This library has three functions: The Write_Byte_EEP, the Read_b_eep and the Busy_eep. Below is a quick descriptions of the EEP library, for more information, please check the PIC18F Peripheral Library Help Document and select your PIC device. You can access the PIC18F Peripheral Library Help Document found inside your compiler installation directory in: ..Program Files (x86)Microchipxc8v1.34docsMPLAB_XC8_Peripheral_Libraries.pdf (assuming you installed your compiler in the Program Files (x86) directory. v1.34 is the version of your compiler, it might be different if you are using a different compiler).
Search for the PIC you are going to use, click on: “CLICK HERE for the Peripheral Library Support Details for this Device” look for EEP library ).


This function Writes a single byte of data to Internal EEP at a specified location. The function is written in this format: Write_b_eep( EEPROM address, data to be written )

NB: User has to refer data sheet to get the address range and availability of internal EEPROM on specific device.


This function Reads a single byte from Internal EEPROM from the user specified location. The format of the function is: Read_b_eep( EEPROM address )


This function checks and waits the status of ER bit in EECON1 register. If the EEPROM is busy, then the function will be in continuous loop till the EEPROM is free. The format of this function is: Busy_eep ( void )


This is a simple program to write a number into the EEPROM and reads it again into a variable.


Share this post

Select your currency
EUR Euro
USD United States (US) dollar