INTERNAL EEPROM WITH PIC18F452 MICROCONTROLLER: There are three types of memories in a PIC Microcontroller namely, Flash Program Memory, Data Memory (RAM) and EEPROM Data Memory.

Flash Memory:

Programs are written in the Flash Program Memory of a microcontroller. Because of this we can program a microcontroller and can change it many times before or after installation to device. It has fast response time.

Data Memory (RAM):

It is used for storing data temporarily during program execution. It is volatile memory and is cleared when the power is off or when CPU resets. RAM Data memory locations are also called General Purpose Registers (GPR). It also has fast response time.

Internal EEPROM Data Memory:

EEPROM stands for Electrically Erasable Programmable Read Only Memory. EEPROM can be electrically read and write. It can be accessed through a program. It isnon-volatile memory but has slower response time. The data will remain present in the memory until it is electrically erased or reprogrammed.

Now in this article I will explain how to use internal EEPROM of PIC18F452. Internal EEPROM memory can be used to store data such as sensor logs or device parameters and this data should not be loss during power off or when CPU reset’s. EEPROM are built in of PIC microcontroller. But these chips are also available to interface externally with the microcontroller.


The internal EEPROM data memory in PIC microcontroller is not directly mapped in the register file space.It is indirectly addressed through the Special Function Registers (SFR). There are four SFRs used to read and write this memory. These registers are:

  • EECON1
  • EECON2


  1. EECON1 Register:

The important control related with the internal EEPROM is the EECON1 register which is given below. EECON1 is the control register whose five lower order bits are physically implemented. Others are physically unimplemented and used to initiate the read/write.


Bit 7                                                                                                                Bit 0
EEPGD:          Flash Program or Data EEPROM Memory Select bit

1 = Access Flash program memory
0 = Access EEPROM data memory

CFGS: Flash Program/Data EEPROM or Configuration Select bit

1 = Access Configuration registers
0 = Access data EEPROM memory

EEIF:              EEPROM Write Operation Interrupt Flag bit

1 = Write operation completed (must be cleared in software)
0 = Write operation is not completed or not started

WRERR:         EEPROM Error Flag bit

1 = Write operation is terminated (by any reset e.g MCLR reset or any WDT reset)
0 = Write operation completed

WREN:           EEPROM Write Enable bit

1 = Allows write into EEPROM
0 = Inhibits write to the data EEPROM

WR:                 Write Control bit

1 = Initiates an EEPROM write cycle
0 = Write cycle to the data EEPROM is complete

RD:                 Read Control bit

1 = Initiates an EEPROM read
0 = Read cycle to EEPROM is complete

When WREN bit is set, it will allow a write operation. Control bits RD and WR initiate read and write respectively. These bits cannot be cleared and only setin software. They are cleared in hardware at completion of the read or write operation. This inability to clear the bit in software prevents the accidental or premature termination of a write operation. On power-up, the WREN bit is clear. The WRERR bit is set when a write operation is interrupted by a MCLR reset or a Watchdog time-out reset during normal operation. When this situation occurs,we can check the WRERR bit and rewrite the location. Interrupt flag bit EEIF is set when write is complete. It must be cleared in software.

  1. EECON2 Register:

EECON2 is not a physical register. For Reading, EECON2 register will read all bits as ‘0’. The EECON2 register is used exclusively in the Data EEPROM write sequence.

  1. EEDATA Register:

EEDATA Register holds the 8-bit data for read/write operation. EEPROM data memory allows only single byte to be read and write. When a byte is writing, it automatically erases the location and writes the new data. The EEPROM data memory is rated for high erase/write cycles. The write time is controlled by an on-chip timer.

  1. EEADR Register:

EEADR registerholds the address of the EEPROM location which is to be access. It can access up to 256 locations of Data EEPROM in PIC18F452.The address range always starts at 00h, and goes all over the memory available.

EEPROM size             Address range

64                                            (00-3F)h

128                                          (00-7F)h

256                                          (00-FF)h


MikroC provides some library function for comfortable work with EEPROM.

Library Routines:



  1. The address of the memory location to be read is given to EEADRregister.
  2. Select EEPROM data memory by clearing the EEPGD control bit of EECON1 Register.
  3. Set the RD bit of EECON1 Register to initiate the read cycle.
  4. Then we can read data through EEDATA register.


unsigned char readEEPROM(unsigned char address)


EEADR = address; //Address to be read

EECON1.EEPGD = 0;//Selecting EEPROM Data Memory

EECON1.RD = 1;//Initialise read cycle

return EEDATA; //Returning data






For PIC 18 family                   unsigned shortEEPROM_Read(unsigned int address);

It Reads data from specified address. Parameter address is microcontrollers dependent. For PIC16 family it is of short type, and for PIC18 family it is of integer type. Here this function’s parameter (int) supports microcontrollers with more than 256 bytes of EEPROM. Give a delay of minimum 20ms between successive use of routines EEPROM_Write and EEPROM_Read,otherwise microcontroller may return an undefined result.


  1. The address of the memory location where data to be write is given to EEADR register.
  2. 8-bit data which is to write is written in the EEDATA register.
  3. Select EEPROM data memory by clearing the EEPGD control bit of EECON1 Register.
  4. Set the WREN control bit control bit of EECON1 Register to enable write operations.
  5. Store the interrupt register INTCON if interrupts are enabled in the program, and then disable them.
  6. There exists a special instruction sequence using EECON2 register is executed.
  7. Set the WR bit to initiate an EEPROM write cycle.
  8. Enable Interrupts if using.
  9. Disable the write operations by clearing WREN control bit.
  10. WR control bit is cleared if write cycle to the data EEPROM is complete or EEIF interrupt flag bit is set when write operation is completed. EEIF bit must be cleared in the program.
void writeEEPROM(unsigned char address, unsigned char data)


unsigned char INTCON_SAVE;//To save INTCON register value

EEADR = address; //Address to write

EEDATA = data; //Data to write

EECON1.EEPGD = 0; //Selecting EEPROM Data Memory

EECON1.WREN = 1; //Enable writing of EEPROM

INTCON_SAVE=INTCON;//Backup INCON interupt register

INTCON=0; //Diables the interrupt

EECON2=0x55; //Required sequence for write to internal EEPROM

EECON2=0xAA; //Required sequence for write to internal EEPROM

EECON1.WR = 1; //Initialise write cycle

INTCON = INTCON_SAVE;//Enables Interrupt

EECON1.WREN = 0; //To disable write

while(PIR2.EEIF == 0)//Checking for complition of write operation


asmnop; //do nothing


PIR2.EEIF = 0; //Clearing EEIF bit




For PIC 18 family                   unsigned short EEPROM_ Write (unsigned int address);

This function Writes data to specified address. All interrupts should be disabled during execution of EEPROM_Write routine.


internal EEPROM


In this example we firstly write data to the EEPROM data memory and then it is read through some PORT. We write 00000001 to the first memory location, 00000010 to second, 000000100 to third and then sequentially up to 10000000. After that, it is read sequentially and output through PORTC.

CODE of internal EEPROM

void main()


unsignedinti, a;                  //i for address, a for data

TRISC  = 0;                            //PORT C as output port





Eeprom_Write(i, a);      //writing data to first memory location and then goes upto last location


        a = a<<1;




         PORTC = Eeprom_Read(i);        //reading data sequentially and output through PORT C





This is a complete tutorial about how to use EEPROM of pic microcontroller. It is very easy to use and Mikro c compiler makes it very easy due to its built in libraries.

    We highly recommend EasyEDA for Circuit Design and PCB Prototype

Low Cost PCB Manufacturing & Fabrication Service - EasyEDA   

 PCB Prototype: Only $8.21 for 10pcs,100x100mm PCBs, 2-3 days' delivery    

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,045 other subscribers