# What is the difference between EEPROM Data Memory and RAM

Since a few days I've been messing around with PIC microcontrollers and a Pickit 3. I was writing some test programs in MPLAB where I came across a nice statistics chart like this here:

After making some variables and expanding my program, I noticed the "Data 25" part to increase. After making some integer arrays and compiling those the data was 100% and the program couldn't compile because of insufficient memory. I unfortunately do need this extra memory to finish my program.

Here I come to my first questions

• Is it true that the Data 25 equals the variables used?
• Is it true that the Program 512 the amount of memory used to store the program?

These are two side questions I have, here comes my main question.

Assuming my two statements were true, I headed to the microchip website to look for a chip with more Data (RAM) available. Here's where I looked. As you can see there is also a column which says "EEPROM Data Memory".

My question is: What is the difference between Data (RAM) as shown in the picture and this EEPROM Data Memory? What is the EEPROM Data Memory useful for? I'm new with electronics and have been messing around with a pic12f508 so far. Hope someone can clarify this for me since I don't understand this concept. Thanks!

• In the snippet you've posted, 25 is the total amount of RAM available in the device (in bytes) - just below that you can see that you have used 13 bytes and have 12 available. Similarly for the Program space. – brhans Mar 15 '17 at 18:56

maybe this makes sense by way of an example, I dont have a pic compiler handy but it doesnt matter with respect to the question.

Take this code

unsigned int xyz=5;
unsigned int fun ( unsigned int x )
{
return(x+xyz);
}


Compile it, assemble and link. Then disassemble to show the result of that

Disassembly of section .text:

00000000 <_start>:
0:   e3a0d902    mov sp, #32768  ; 0x8000
4:   eb000000    bl  c <fun>
8:   eafffffe    b   8 <_start+0x8>

0000000c <fun>:
c:   e59f3008    ldr r3, [pc, #8]    ; 1c <fun+0x10>
10:   e5933000    ldr r3, [r3]
14:   e0800003    add r0, r0, r3
18:   e12fff1e    bx  lr
1c:   20000000

Disassembly of section .data:

20000000 <xyz>:
20000000:   00000005


So we have a global variable xyz, this is data, this goes in ram, we can read and write it, change its value, whatever, it has to be in read/write ram.

The program which is the machine code basically is from address 0x00000000 to 0x0000001F above.

So program memory also known as .text is the program the instructions. Data memory also known as .data is the read/write variables in particular ones like this that are pre-initialized to some value. Ones that are not initialized to some value fall into the .bss segment but are still in ram since they are read/write variables.

When the microcontroller is off, ram is off, it doesnt work it cannot store values. EEPROM and FLASH and other forms of nonvolatile memory are used. The processor/hardware is designed to know how to start using that memory and the programs are designed to operate from that memory. You can see we have an issue, we need to remember that the variable xyz needs to start with the value 5, but ram is volatile. So we need to have in non volatile memory both the program itself and any other items that we need to know. So all of .text and in the case of a microcontroller generally the program is executed from on chip flash and is accessed by the microcontrollers processor at the addresses we have linked. The .data values I have not shown the bootstrap magic here, but the bootstrap that runs before the entry point C function (usually main but that is arbitrary) has a few jobs minimum to do. first is set the stack pointer so we have a stack, normally the processor core does not know how much ram there is so the software does it. Second it needs to copy any .data values to ram so that the C program can find them, so in this case the value 0x00000005 will be somewhere in flash and we will know we have to copy that value to address 0x20000000 in ram. Third and not necessarily in this order, there will be address and size information for .bss, I dont have any bss variables in this example, but if there were we would have a starting address and number of bytes information that we would then have bootstrap code to zero out as programmer sometimes assume that a variable without a value assigned at compile time will be zero if they read it before they write it.

So data memory is the ram, I assume from your output that it is telling you how many bytes of variables the compiler has detected your program needs. The program memory is the program itself the machine code. Flash which you didnt ask about will hold the program memory and information about the data memory so your program can work. And then there is EEPROM, which is another form of non-volatile memory where you can save some items. often accessed through a library or code you write but not normally accessed as a simple variable name nor executed through as a program (in a microcontroller). Say your device is an mp3 player and the music is also in the flash perhaps in the form of a file system with files. You might want to add the feature that when the device is turned off, the last file being played and where in that file be saved so that when the power comes back it can continue. You could store this information in ram and have the battery in the device keep the ram alive, or you can store it in flash if there is space, or you can store it in EEPROM if your device has EEPROM. You would often use EEPROM for runtime information that you want to preserve through a power cycle. Like the odometer reading on a car, even if the battery is disconnected you want to remember the mileage for the car, so that when it is up and running again you can continue from that value and not start from zero every time. remember the users radio station preferences for shortcut buttons.

The data memory in pics are physical registers within the device and is very limited, though quantity is dependent on device.

Program memory is a separate memory area on the device that can be programmed by stimulating the device in a special way. This memory is tied to the PICS instruction executing state machine.

EEPROM data memory is an area of memory on the chip you can programmatically store values to that will be retained through power outages of the device. Once programmed the values will stay there for the life of the device or until you re-program them. It is however, an IO function.

EEPROMS are generally slow to write and read, and have a limited over-write life compared to normal memory. Generally the EEPROM is used to store setup conditions, or counters you need to retain through times when the battery is dead or whatever.

In addition to Trevor's answer, I would like to add a few things. Accessing data in an EEPROM takes more time than accessing RAM, because fetching data from EEPROM usually needs more instructions, and you are limited to the speed of EEPROM. Because of this, if your memory space is limited, it is a good practice to place your constant data/tables and/or your not so frequently used data to EEPROM. This will increase your overall speed and helps you use your memory more efficiently.

You ask for difference between EEPROM and RAM...

EEPROM is Non-volatile (remembers data after power down) which is the interesting part. However it is:

• Slow
• Usually requires special instructions or libraries to read/write
• Has limited write endurance

EEPROM compared to Flash:

• Flash is faster and cheaper (which is why your micro uses it to store its program)
• EEPROM has much higher write endurance (100k-1M cycles versus 1k-10k)
• EEPROM allows erasing/modifying a single byte whereas Flash requires erasing a complete block

Each type of memory has its use: