Lab 13: Read and Write to internal EEPROM

EEPROM (Electrically Erasable Programmable Read-Only Memory) is a type of non-volatile memory which can be programmed, erased, and re-programmed electrically while it is on the circuit board. A majority of PIC microcontrollers come with some built-in EEPROM which is a great place to store data that should not be lost when the system is powered down. A good example is a digital lock system where the access code can be stored in the EEPROM of microcontroller so that the contents remain intact even after the power supply has been removed. In my ‘Beginner’s data logger‘ project, I used the internal EEPROM of PIC12F683 to store the measured temperature values. In today’s experiment, we will learn how to perform the basic Read and Write operations to the internal EEPROM of PIC16F628A.

Read/Write operation with PIC's internal EEPROM

Theory

PIC16F628A microcontroller has got 128 bytes of data EEPROM with an address range from 00h to 7Fh. There are four Special Function Registers used to read and write this memory, which are EECON1, EECON2, EEDATA, and EEADR. EEADR holds the address in the EEPROM which you wish to read or write to, while EEDATA holds the 8-bit data that you have just read, or which you wish to write to the EEPROM. EECON1 and EECON2 are control registers that are used to enable the EEPROM read/write operations. We will ignore the details of these registers here because we will be using mikroC compiler for programming, where the built-in library routines for EEPROM operations takes care of configuring these registers. However, you can find the details of these registers in the datasheet of PIC16F628A.

Circuit setup

This experiment requires a 16×2 character LCD for display and three tactile switches for user inputs. The circuit diagram below shows the connections of the LCD and the switches with the PIC16F628A microcontroller.

Circuit diagram for Lab 13

The three tact switches are connected to RB0, RB1, and RB3 pins. The LCD is operating in 4-bit mode and the data pins are driven by RB4 through RB7 pins of PIC16F628A. The LCD RS and E control pins are connected to RA0 and RA1 pins. The functions of the three tact switches are discussed in software section.

Experimental circuit setup on breadboard

Software

We will write a small application software for PIC16F628A that will do Read, Write and Delete operations to the selected internal EEPROM locations (00h through 0Fh) of PIC16F628A. When the Read button is pressed, the 16 bytes of information stored at 00h-0Fh locations will be read and displayed on the LCD screen. Pressing the Write button will write a character string ‘Embedded-Lab.com’ to these locations, whereas if the Zero button is pressed, the values in these locations will be replaced by all 0s.

The mikroC Pro for PIC compiler provides the following functions for EEPROM read and write operations:

unsigned short EEPROM_Read(unsigned short address) :- returns a byte from the specified address

void EEPROM_Write(unsigned short address, unsigned short data) :- writes the data at the specified address

You can download the complete source code and hex files from the link below.

The default values in an erased EEPROM locations are all FFh (255). All 5×10 dots are active on the screen when this value (FFh) is displayed on LCD. So when you first read the EEPROM, which is in an erased state, you will read all FFh (see pictures below). Once the Write operation is completed, turn off the power for a few seconds, then turn it back again, and press the Read button. You will see the character string (Embedded-Lab.com) is still retained inside the EEPROM.

Hi and thank you so much for your awesome PIC related articles and projects as I greatly learned tons of stuff specially that I am targeting the MikroC IDE/Compiler series as I found it much easier and sensible to learn than the other IDE/Compiler sets. I even think it is way better than the MPLAB by far. HighTech or CCS are lame!

May I request for an article/project to cover as follows please:

It’d be really useful to have a programmable presets in separate memory banks at run-time where then one could control multiple relays to set multiple devices On or Off, using push buttons which can select the banks in either Up or Down addresses and then another set of push buttons to On or Off any or multiple connected relays to the particular Port(s), where then it can be saved into the selected memory bank as a preset which could be recalled for later use or edit/save again.

is the one which is responsible for reading the memory location, so if it doesn’t get inside the if block what should I do because I’ve enclosed it in curled brackets so eventually it will be inside now the problem remains where is it?

Hello R.B can you help me out, I’ve done a project which utilizes most of the tutorial you provide but the problem is that the EEPROm doesn’t store the values. the code is as below, rectify me where am wrong, am using PIC16F887

hey i love dis site and all of d tutorials….im a great fan of dis site….but i wanna see the tutorials of all the complicated stuffs of microcontroller just like interrupt,capture/compare,i2c…still waiting 4 releasing pic timer and counter part2..