Skip to main content

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.




Software:
/*
  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_Init();
start:
Lcd_Cmd(_LCD_CLEAR);               // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
Lcd_Out(1,1,message1);             // Write message1 in 1st row
Lcd_Out(1,8,message2);
Lcd_Out(2,1,message3);
do {
// Read Operation
if (Button(&PORTB, 0, 1, 0)) {  // Detect logical one to zero
      Delay_ms(300);
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,message5);
      for (i=0; i<=9; i++) {
      temp = ADD+i;
      NUM = EEPROM_Read(temp);
      digits[i] = NUM+48;
      }
      Lcd_Out(2,1,digits);
      delay_ms(3000);
      goto start;
   }
// Write Operation
if (Button(&PORTB, 1, 1, 0)) {  // Detect logical one to zero
      Delay_ms(300);
      for (i=0; i<10; i++) {
      temp = ADD + i;
      EEPROM_Write(temp,i);
      }
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,message4);
      delay_ms(2000);
      goto start;
  }

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

Experimental Output Video:

Comments

Popular posts from this blog

Contact less tachometer using PIC16F628A

Introduction Tachometer is a device that gives you the information about the rotational speed of any shaft or disc. It usually measures the speed in revolutions per minute (RPM). Today we are going to make a simple tachometer that could measure the rotation speed of a disk without making any physical contact (that's why it is contact less) with the rotating object. The range of this tachometer is 0 - 9999 RPM and displays the RPM on a multiplexed 4-digit seven-segment display. Of course, we are going to do this project on our usual PIC16F628A development board. Infrared sensor Contact-less measurement of RPM will be achieved through an IR sensor. An IR diode will send a beam of infrared towards the rotating disc, and any reflected pulse will be received by a photo diode. The resistance of a photo diode drops drastically when exposed to infrared. An infrared is reflected by a white surface and absorbed by the dark ones. The test disc for this project is shown below. You can see

Experiment No. 2 : Push Button and Seven Segment Display Interface

In this experiment, we will program the PIC16F628A as an UP/DOWN Decade Counter. The count value will be displayed on a Seven-Segment Display and will be incremented/decremented by two push buttons on the board. Experimental Setup: The board has built in interface for a multiplexed 4-digit seven segment display (HS-5461AS2 from www.futurlec.com ).We will select only one digit by connecting a Digit Select pin to Vcc, as shown in figure below. A black jumper wire is used for this purpose. The seven segments will be driven through PORTB (already wired on the board). Connect Push Buttons (PB3 and PB4) to RA1 and RA0 female headers using jumper wires.

PIC16F628A Development Board

The development board we are going to make for our experimental microcontroller PIC16F628A will look like this. Here are the features it is going to have: Access to all I/O pins through female header pins 4 Push Buttons for Input 4 LEDs for Output An LCD Interface Port A 4-digit Seven-Segment Display Interface LCD Backlight Switch and Contrast Adjustment ICSP Programming (Very Important)