How to write a file to EEPROM?


I would like to be able to write a file containing settings for my program to EEPROM on an AVR. I know how to write to EEPROM from within the program, but I couldn’t find any good information about writing a setting file. I am using AVR Studio 4 and, of course, there is the possibility to use the EEPROM option under “Connect to the selected AVR programmer”. Still, I am not really sure how.

In order to use the settings I need to have the values at specific addresses of the EEPROM. I could just write the hex file manually by writing all values I want to use in a list (every entry = 8 bits) and making sure they are at the correct address by their position within the list.

0x00 --> address 0x00
0x45 --> address 0x01
0xA1 --> address 0x02

Would this work? But when using floating point variables etc., this is certainly not an elegant solution.
So how can I write a C type file that contains just values for specific EEPROM addresses and create a hex file from it?

I’d greatly appreciate any help. Maybe someone knows a link to some quick tutorial or so.


I don’t know of any good tutorials for doing this, but the hex file format that AVR studio uses is described here. It’s fairly dense.

A good place to start would be erasing your AVR, then downloading the blank eeprom contents to a text file, and start from there. It should look something like this:

The ‘:’ signifies the start of a line, while Pairs of ASCII characters represent hex values.

The first hex byte of a line represents the number of data bytes to follow in that line, usually 0x10 (16 bytes) for AVRs, but not necessarily!

The next two bytes represent the memory address to write the first byte of data on this line to.

The next byte represents what type of line this is (there are six types, but right now all you need to worry about is 0x00 means data and 0x01 means end of the hex file).

The next N bytes (where N is the value of the first byte in the line, usually 0x10, or 16) are the data to be written at sequential addresses, starting at the address given earlier in the line. “Erasing” these bytes sets them to 0xFF.

Finally the last byte is a simple checksum, such that if you add the byte values of all the bytes in the line (everything after the ‘:’) rounding off to a single byte you will get 0. I use an Excel (or Open Office if you prefer) spreadsheet to calculate checksums quickly, which you can download here. Note that it will only work for 16-data byte lines.

The last line is a simple file terminator, and should read “:00000001FF”.

As for how you store your data, it’s up to you, but if you plan on using the AVR-libc EEPROM functions you should read up on them, or use them to write some test values to the EEPROM you can look at. I remember it confused me for a while that integers were written little-endian (least-significant-byte first).

Anyway, I hope that gets you started. What kind of program are you writing that you want to be able to configure?


Another way you could write settings to EEPROM is to use the program AVRDUDE, included with WinAVR, to write a file in a raw format. For example,

avrdude -U eeprom:w:file.dat:r -c avrisp2 -P COM3 -p m168

will write a binary file.dat directly to EEPROM on a mega168 via an Orangutan Programmer on COM3. In any programming language on your PC, it should be really easy to write out file.dat; just remember to use little endian format for any multi-byte values. With the binary format, you don’t have to worry about formatting the hex file at all, and you could even easily edit the values directly with a good hex editor.


Thanks a lot Adam and Paul for your very fast responses. I will try that as soon as possible!


Sorry Adam,

I didn’t answer your question. Well, right now I am not working on anything really meaningful. I still don’t have a lot of experience with AVRs, so I am trying to check everything out and to understand how it works and what I can do with it. So for now I am just thinking about storing stuff like melodies etc. But I hope to acquire the skills to come up with some cool project.

Thanks again for your thourough answers!