AVR microcontrollers are loaded with some amount of EEPROM (Electronically Erasable Read-Only Memory ) memory. This is a handy feature allowing developers to store program parameters like service information, constants, menu strings, etc. Atmel states that AVR EEPROM memory can be rewritten over 1000000 times. Reading is unlimited.
In this article, I am going to show how to store data to EEPROM by defining variables.
For this, we need to include eeprom.h header from avr directory (#include “avr/eeprom.h” ).
Then we can write a simple variable declaration using the simple attribute 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);
}
EEMEM keyword indicates to the compiler that variables are stored in EEPROM, and it creates a separate .eep file that has to be written to chip separately.
Se what I have got after compiling the above 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 is indicating the size of occupied EEPROM memory. In this particular case, we see that size is 8 bytes: one-byte variable, one word (two bytes), and a five-byte array – total 8bytes.
Open .eep file located in the project folder. The compiler compiled Intel Hex File of EEPROM data:
:0800000054657374005555109E
:00000001FF
The first line shows 8-byte data stored at address location 0. The second line is the same for all hex files – it indicates the end of the file record.
Don’t forget that the .eep file must be written to the avr microcontroller separately as the writing compiled program doesn’t write EEPROM data. If you use PonyProg programmer and AVR ISP cable, 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.
In the memory viewer, there are both memory locations displayed: Flash and EEPROM. You may notice that the EEPROM memory area has a different color than Flash. I hope you get the picture and can start working with AVR EEPROM memory.
You can download the source files of the AVR EEPROM using the WinAVR example to try by yourself.
Thank you so much for the information…