Site icon 14core.com

Write/Read Data to EEPROM – XC8 and PIC18F Microcontroller

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 MCU is stored in the FPM (Flash Program Memory) of the MCU. Flash memory makes it possible to program a MCU many times because it is re-writable, this memory can be written and erased many times. The chip RAM Data Memory is used for storing data once the power is OFF or after CPU reset, this memory will be cleared and lose all its content. Random Access Memory location are also called GPR or General Purpose Registers, these two memories it has faster response time 3rd memory is EEPROM memory. EEPROM memory can be read/write electrically and can be accessed through program code, EEPROM memory is used to store data which is cannot be loss during the operation or power loss / reset, such as configuration, parameters, settings which could be entered one and stored in the EEPROM.

The data on the EEPROM / Flash Memory can be read/write during normal operations over full VDD range. These memories are not mapped at the register file space, instead it can be accessed through Special Function Registers (SFR) for read/write operations, according to the PIC18F452 datasheet (You can download the datasheet below), at page 105  it has following Special Function Registers: EECON1 | EECON2 | EEDATA | EEADR | EEADRH.

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

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.

The data-sheet 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.

THE XC8 EEP LIBRARY FUNCTION

EEP / Peripheral Library is used to get access to the EEPROM memory of a PIC micro-controller. 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.  We can access the PIC18F Peripheral Library Help Document found inside your compiler installation directory located in: /Program Files (x86) Microchipxc8v1.34docsMPLAB_XC8_Peripheral_Libraries.pdf, let say you already installed your compiler in the Program Files directory. v1.34 is the version of your compiler, (in this case 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 that is EEP library ).

Write_Byte_EEP

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

Read_Byte_EEP

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

Busy__EEP

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

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

Below is a simple code to write a No. into the EEPROM and reads again into a variable
[crayon-6766d08b33e05306480355/]
Download PIC18F45K22 Datasheet | PDF

Write/Read Data to EEPROM – XC8 and PIC18F Microcontroller
Exit mobile version