Reading and Writing to PIC Internal EEPROM – MikroC
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.
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:
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 mikroC EEPROM library which simplifies everything for us.
MikroC Pro for PIC EEPROM Library
This library has two routines “EEPROM_Read” to read and “EEPROM_Write” to write. Below is a quick descriptions of the EEPROM library, for more information, please visit online the mikroC pro for PIC EEPROM Library page.
This function reads a byte from a specified address of the EEPROM. The address is of type integer for PIC18 family and of short type for PIC16 family. A 20ms delay should be used between successive reads from the EEPROM to guarantee the return of correct data.
Example: //Read a byte stored in address 0x2F of the EEPROM and store it in variable TempData.
TempData = Eeprom_Read(0x2F);
This function writes a byte to a specified address of the EEPROM. The address is of type integer for PIC18 family and of short type for PIC16 family. A 20ms delay should be used between successive reads or writes to the EEPROM to guarantee the correct transfer of data to the EEPROM.
Example: // Number 0x06 is written to address 0x2F of the EEPROM.