Tuesday, October 20, 2009

Experiment No. 6: Read/Write Internal EEPROM Memory

An EEPROM (Electrically-Erasable Programmable ROM) data memory is one of the important features of flash-based PIC microcontrollers. It is called non-volatile to indicate that it retains the data even when the power is down. Practically speaking, if you want to design a digital lock system, then the password to unlock the system can be saved into the EEPROM, so that when the power is down, the password will still be saved. And other good thing is that the data can be easily modified or overwritten with software control. In this experiment, I am going to show you how to read and write in to the internal EEPROM memory of PIC16F628A using mikroC EEPROM library functions. Here is what we are going to do:

We will write 0s to 10 EEPROM locations. We will read them first, then write 0-9 to these locations, and turn the power off. We will turn the power on, and read the data in those locations and see. I have created a simple menu on LCD with Read, Write and Delete functions.

Experimental Setup:
Connect the three push buttons on the board to RB.0, RB.1, and RB.2, and plug-in the LCD module.

  Project Name: Read/Write Internal EEPROM
  * Copyright:
     (c) Rajendra Bhatt, 2009.
 * Description:
     This code is an example of accessing internal EEPROM.

 * Test configuration:
     MCU:             PIC16F628A
     Oscillator:      XT, 4.0 MHz

// LCD module connections
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
// Define Messages
char message1[] = "1.READ";
char message2[] = "2.WRITE";
char message3[] = "3.Delete";
char message4[] = "WRITE COMPLETED";
char message5[] = "Read Data";
char message6[] = "Data Deleted";

char digits[] = "0000000000";
unsigned short  i, NUM ;
unsigned int ADD = 0x00, temp; // Start EEPROM Location

void main() {
CMCON  |= 7;      // Disable Comparators
TRISB = 0x0F;
PORTB = 0x00;
Lcd_Cmd(_LCD_CLEAR);               // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
Lcd_Out(1,1,message1);             // Write message1 in 1st row
do {
// Read Operation
if (Button(&PORTB, 0, 1, 0)) {  // Detect logical one to zero
      for (i=0; i<=9; i++) {
      temp = ADD+i;
      NUM = EEPROM_Read(temp);
      digits[i] = NUM+48;
      goto start;
// Write Operation
if (Button(&PORTB, 1, 1, 0)) {  // Detect logical one to zero
      for (i=0; i<10; i++) {
      temp = ADD + i;
      goto start;

// Delete Operation
if (Button(&PORTB, 2, 1, 0)) {  // Detect logical one to zero
      for (i=0; i<=9; i++) {
      temp = ADD+i;
      EEPROM_Write(temp, 0);
      delay_ms(2000) ;
      goto start;
 } while(1);

Experimental Output Video:

No comments:

Post a Comment

Microcontroller-based Embedded Systems Design