In this tutorial we will learn how easy it is to save data using the EEPROM present in the PIC16F877A Microcontroller. In most real time projects we might have to save some data which should not be erased even when the power is turned off. This might sound like a complicated process, but with the help of XC8 Compiler this task can be done by just using a single line of code. If the data is large in terms of Mega bytes then we can interface a storage device like an SD card and store those data on them. But we can avoid those tiring process if the data is small, we can simply use the EEPROM present in the PIC Microcontroller to save our data and retrieve it at anytime we want.
This PIC EEPROM tutorial is a part of a sequence of PIC Microcontroller Tutorials in which we started from a very basic level. If you haven’t learnt the previous tutorials then it would be better to have a look at them now, because this tutorial assumes that you are familiar with Interfacing LCD with PIC Microcontroller and Using ADC with PIC Microcontroller.
EEPROM in PIC16F877A:
EEPROM stand for “Electronically Erasable and Programmable Read Only Memory”. As the name suggests it is a memory present inside the PIC Microcontroller in which we can write/read data by programming it to do so. The data saved in this will be erased only if it is mentioned to do so in the program. The amount of storage space available in EEPROM varies upon each microcontroller; the details will be given in Datasheet as usual. In our case for PIC16F877A the available space is 256 bytes as mentioned in its specification datasheet. Now let us see how we can use these 256 bytes to read/write data by using a simple experimental setup.
Circuit Diagram and Explanation:
The circuit diagram for the project is shown above. We have interfaced an LCD to visualize the data getting saved and retrieved. A normal potentiometer is connected to AN4 Analog channel so feed in variable voltage, this variable voltage will be used as the data to be saved in the EEPROM. We have also used a push button on RB0, when this button is pressed the data from the Analog channel will be saved in the EEPROM.
This connection can be made on a breadboard. The pinouts of the PIC Microcontroller is shown in the table below.
S.No: |
Pin Number |
Pin Name |
Connected to |
1 |
21 |
RD2 |
RS of LCD |
2 |
22 |
RD3 |
E of LCD |
3 |
27 |
RD4 |
D4 of LCD |
4 |
28 |
RD5 |
D5 of LCD |
5 |
29 |
RD6 |
D6 of LCD |
6 |
30 |
RD7 |
D7 of LCD |
7 |
33 |
RBO/INT |
Push button |
8 |
7 |
AN4 |
Potentiometer |
Simulation of Using PIC EEPROM :
This project also involves a Simulation designed using Proteus, using which we can simulate the working of the project without any hardware. The program for this simulation is given at the end of this tutorial. You can simply use the Hex file from here and simulate the whole process.
During simulation you can visualize the Current ADC value and data saved in the EEPROM on the LCD screen. To save the current ADC value into the EEPROM simply press the switch connected to RB0 and it will be saved. A snapshot of the simulation is shown below.
Programming PIC for EEPROM:
The complete code for this tutorial is given at the end of this tutorial. In our program we have to read the Values from ADC module and when a button is pressed we have to save that value in our EEPROM. Since we have already learnt about ADCs and LCD interfacing, I will further explain the code to save and retrieve data from EEPROM.
According to Datasheet “These devices have 4 or 8K words of program Flash, with an address range from 0000h to 1FFFh for the PIC16F877A”. This means that each EEPROM storage space has an address through which it can be accessed and in our MCU the address starts from 0000h to 1FFFh.
To save a data inside a particular EEPROM address simply use the below line.
eeprom_write(0,adc);
Here “adc” is a variable of type integer in which the data to be saved is present. And “0” is the address of the EEPROM on which our data is saved. The syntax “eeprom_write” is provided by our XC8 complier hence the registers will be automatically taken care by the compiler.
To retrieve a data that is already stored in EEPROM and save it to a variable the following line of code can be used.
Sadc = (int)eeprom_read(0);
Here, “Sadc” is the variable in which the data from the EEPROM will be saved. And “0” is the address of EEPROM from which we are retrieving the data. The syntax “eeprom_read” is provided by our XC8 complier hence the registers will be automatically taken care by the compiler. The data saved in EEPROM will be in hexadecimal type. Hence we convert them to integer type by prefixing a (int) before the syntax.
Working:
Once we understand how the code works and get ready with the hardware we can test out code. Upload the code to your PIC Microcontroller and power the set-up. If everything is working as expected then you should see the current ADC values displayed in the LCD. You can now press the button to save the ADC value to the EEPROM. Now you check if the value is saved by turning off the whole system and turning it on again. When powered on you should see the previously saved value on the LCD screen.
The complete working of this project to use PIC Microcontroller EEPROM is shown in the video below. Hope you understood the tutorial and enjoyed doing it. If you have any doubts you can write them on the comment section below or post them on our forums.
Comments
To save a data inside a
To save a data inside a particular EEPROM address simply use the below line.
eeprom_write(0,adc);
Here “adc” is a variable of type integer in which the data to be saved is present. And “0” is the address of the EEPROM on which our data is saved. The syntax “eeprom_write” is provided by our XC8 complier hence the registers will be automatically taken care by the compiler.
To retrieve a data that is already stored in EEPROM and save it to a variable the following line of code can be used.
Sadc = (int)eeprom_read(0);
Here, “Sadc” is the variable in which the data from the EEPROM will be saved. And “0” is the address of EEPROM from which we are retrieving the data. The syntax “eeprom_read” is provided by our XC8 complier hence the registers will be automatically taken care by the compiler. The data saved in EEPROM will be in hexadecimal type. Hence we convert them to integer type by prefixing a (int) before the syntax.
19/03/2020
I can't save data from 256 to 1024
Example: I save number 256 but EEPROM save 0000
same here quanghhuy
same here quanghhuy
ADC register is 10 bit and can save from 0 to 1023, but EEPROM storage is 8 bit and can only save from 0 to 255. after 256 it start from zero
The solution i found is to
The solution i found is to devide adc by 4 so the adc value start from 0 to 255 which is the mazimum value we can save in EEPROM
Yes this is exactly the way
Yes this is exactly the way to do it. You can use bit shifting if you wanna make things more effeciant, but your technique should also work well in most cases
Hi mister , can I use the
Hi mister , can I use the copy the code in the editor mikroC
Can you provide the details about how the data are saved by pressing the push button? and also, the connection of push button is not clear here.Can you please provide these?