Easy start with AVR EEPROM using WinAVR
AVR microcontrollers are loaded with some amount of EEPROM (Electronically Erasable Read-Only memory ) memory. This is 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 a variables.
For this we need to include eeprom.h header from avr directory (#include “avr/eeprom.h” ).
Then we can just write simple variable declaration using 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=0×10;
//store initial word to eeprom
uint16_t EEMEM eepromword=0×5555;
//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 compiler that variables are stored in EEPROM and it creates separate .eep file which 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 five byte array – total 8bytes.
Open .eep file located in 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. Second line is the same for all hex files – it indicates end of file record.
Dont 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 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 noticed that EEPROM memory area has 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 WinAVR example to try by yourself.
Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post | TrackBack URI for this post
New on WinAVR Tutorial
Running TX433 and RX433 RF modules with AVR microcontrollers,Sometimes in embedded design you may want to go wireless. Might be you will want to log various readi …Programming AVR ADC module with WinAVR,Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solut … |
New on WinARM Tutorial
What are differences between WinARM and WinAVR,Everyone who is working with AVR microcontrollers knows this powerful tool – WinAVR (http://win …LPC2000 watchdog timer,As in all microcontrollers watchdog timers purpose isto reset microcontroller after reasonable amount … |

June 16th, 2007 at 4:00 pm
[...] very helpful was this post on EEPROM at [...]