Search This Blog

Friday, March 28, 2008

Working / Using EEPROM in WinAVR (GCCAVR)

VR microcontrollers like many any other Harvard architecture MCU's are ships with some amount of EEPROM (Electronically Erasable Read-Only memory
) memory. This type of memory allows developers to store program
parameters, constants, menu strings etc. EEPROM memory is good that you
can read like one byte and store modified, while FLASH memory usually
is written in pages.

In this article I am going to show how to store data to EEPROM by defining a particular variable types.

For this we need to include eeprom.h header library from avr directory #include “avr/eeprom.h”.

Following is simple variable declaration using simple attribute word EEMEM:

#include “inttypes.h”

#include “avr/io.h”

#include “avr/iom8.h”

#include “avr/eeprom.h”

//store initial byte to eeprom

uint8_t EEMEM eeprombyte=0x10;

//store initial word to eeprom

uint16_t EEMEM eepromword=0x5555;

//store string to eeprom

uint8_t EEMEM eepromstring[5]={"Test\0"};

int main(void)


//RAM byte variable

uint8_t RAMbyte;

//RAM word variable

uint16_t RAMword;

//RAM array of bytes

uint8_t RAMstring[5];

//read byte from EEPROm and store to RAM

RAMbyte = eeprom_read_byte(&eeprombyte);

//read word from EEPROM and store to RAM

RAMword = eeprom_read_word(&eepromword);

//copy string fro mEEPROM to RAM

eeprom_read_block ((void *)&RAMstring, (const void *)&eepromstring,5);

return (0);


keyword indicates to compiler that variables has to be stored in EEPROM
memory and it creates separate .eep file which has to be written to
chip separately during AVR microcontrolling programming..

Lets see what I have got after compiling with AVR-GCC compiler the above example code:

Size after:

main.elf :

section size addr

.text 156 0

.data 0 8388704

.bss 0 8388704

.noinit 0 8388704

.eeprom 8 8454144

.stab 876 0

.stabstr 132 0

.debug_aranges 20 0

.debug_pubnames 74 0

.debug_info 486 0

.debug_abbrev 316 0

.debug_line 240 0

.debug_str 271 0

Total 2579

You can see compiler information about compiled code sizes. The bold line (.eeprom 8 8454144)
is indicating the size of occupied EEPROM memory in EEPROM memory
space. In this particular case we see that size is 8 bytes: one byte
variable, one word (two bytes) and five byte array – total 8bytes.

If you open .eep file located in project folder – you will see Hex File of EEPROM data:



first line shows 8 byte data stored at address location 0. Second line
is the same for all hex files – it indicates end of file record.

forget, that .eep file has to be written to avr microcontroller
separately as writing compiled program doesn't write EEPROM data. If
you use PonyProg programmer this
can be done easily. Open .eep file by selecting File->Open Data
(EEPROM) File...and then select command: Command->Write Data
(EEPROM). The same can be done with tool-bar buttons.


the PonyProg memory viewer there are both memory locations displayed:
Flash and EEPROM. You may noticed that EEPROM memory area has different
color than Flash. I hope this helps to some of you to get the picture
and can start working with AVR EEPROM memory.

No comments: