Sunday, May 21, 2017

Saving and Reading Data from Internal EEPROM of PIC Micro-controller

Hello Everyone in this post I will share the API’s for reading and writing into Microchip 8-bit PIC micro-controller’s EEPROM. These API can be used with PIC16 and PIC18 series of micro-controllers with almost no changes.
Before proceeding further, we must know about different types of memories Microchip PIC micro-controller has, so there are three types of memories, Flash Memory, Data Memory and EEPROM Memory.
Flash Memory is non-volatile in nature and the firmware is written into this memory, another type of memory is Data Memory, in this memory data is stored temporarily during execution of program, this memory is volatile in nature which means that, if power loss occurs all the data gets erased.
Now coming to the third type of memory the EEPROM memory, this is non-volatile memory, it can store data, and can retain the saved data even after power loss, that’s why this memory is also one of the most important type of memory for Embedded Engineers.

API’s for Reading and Writing to PIC EEPROM
The following four API’s are sufficient for reading (2 API for reading) and writing (2 API for writing) from PIC EEPROM memory, and they are as follow:

void EEPROM_Write(u8_t address,u8_t data );
void EEPROM_Write_Block( u8_t address, u8_t *data, u8_t length);
u8_t EEPROM_Read( u8_t address );
void EEPROM_Read_Block( u8_t address, u8_t *data, u8_t length );

To write a single byte into EEPROM memory use the following function.

EEPROM_Write( 0x00, 250);
The above function will write value 250 on address 0x00 of PIC internal EEPROM memory.

To write multiple bytes at once into EEPROM memory use the following function.
EEPROM_Write_Block( 0x01, "Embedded Laboratory", 19);
The above function will write the string "Embedded Laboratory" from address 0x01 in PIC internal EEPROM memory.

To read a single byte from the EEPROM memory use the following function.

eeprom_data = EEPROM_Read( 0x00);
The above function will read the data from the address 0x00 of PIC internal EEPROM and store it in variable eeprom_data.
To read multiple bytes from the EEPROM memory use the following function.
char data[19];
EEPROM_Read_Block( 0x01, data, 19);
The above function will read the data from the address 0x01 to 0x20 of PIC internal EEPROM and store it in the array data.

The following is the API source code, which can be integrated with your program to add the EEPROM functionality into your programs.
/**
 * @file eeprom.c
 * @author Embedded Laboratory
 * @date Feb 5, 2017
 * @brief EEPROM Functions Definition
 */

#include "eeprom.h"

void EEPROM_Write(u8_t address,u8_t data )
{
  EEADR = address;
  EEDATA = data;
  EECON1bits.EEPGD = 0;   // Point to EEPROM Memory
  EECON1bits.CFGS = 0;    // Access to EEPROM
  EECON1bits.WREN = 1;    // Enable Write
  DISABLE_INT();
  EECON2 = 0x55;
  EECON2 = 0xAA;
  EECON1bits.WR = 1;      // Begin Write
  ENABLE_INT();
  while(PIR2bits.EEIF == 0);  // Wait for Write to get Complete
  PIR2bits.EEIF = 0;      // Clear Flag
  EECON1bits.WREN = 0;    // Disable Write
}

void EEPROM_Write_Block( u8_t address, u8_t *data, u8_t length)
{
  u8_t i = 0;
  for(i=0; i<length; i++)
  {
    EEPROM_Write(address+i, *(data+i));
  }
}
u8_t EEPROM_Read( u8_t address )
{
  u8_t data;
 EEADR = address;
  EECON1bits.EEPGD = 0;   // Point to EEPROM
  EECON1bits.CFGS = 0;    // Access to EEPROM
  EECON1bits.RD = 1;      // Enable Read
  data = EEDATA;
  return data;
}

void EEPROM_Read_Block( u8_t address, u8_t *data, u8_t length )
{
  u8_t i = 0;
  for( i=0; i< length; i++ )
  {
    *(data+i) = EEPROM_Read(address+i);
  }
}

No comments:

Post a Comment