Program AVR EEPROM directly from C source
Yes, you can manually write default data to EEPROM in the source code. First, check out this awesome guide on the EEPROM with AVR: Dean's AVR EEPROM Tutorial. Also, I should add that it is a better idea to create a .eep file containing the EEPROM data using the makefile which will be programmed to the device along with the source code. However, if you are unfamiliar with various makefile and linker operations, it can still be done from within your source code file - it will just happen as soon as the circuit is powered, stalling the initial program operation.
At the beginning of the program (before any sort of main loop) you could do something like this:
#include <avr/eeprom.h>
#define ADDRESS_1 46 // This could be anything from 0 to the highest EEPROM address
#define ADDRESS_2 52 // This could be anything from 0 to the highest EEPROM address
#define ADDRESS_3 68 // This could be anything from 0 to the highest EEPROM address
uint8_t dataByte1 = 0x7F; // Data for address 1
uint8_t dataByte2 = 0x33; // Data for address 2
uint8_t dataByte3 = 0xCE; // Data for address 3
eeprom_update_byte((uint8_t*)ADDRESS_1, dataByte1);
eeprom_update_byte((uint8_t*)ADDRESS_2, dataByte2);
eeprom_update_byte((uint8_t*)ADDRESS_3, dataByte3);
The "update" function checks first to see if that value is already there, to save on unnecessary writes, preserving EEPROM lifespan. However, doing this for very many location can take quite a bit of time. It might be better to check a single location. If it is the desired value, then the rest of the updates can be skipped completely. For example:
if(eeprom_read_byte((uint8_t*)SOME_LOCATION) != DESIRED_VALUE){
eeprom_write_byte((uint8_t*)SOME_LOCATION, DESIRED_VALUE);
eeprom_update_byte((uint8_t*)ADDRESS_1, dataByte1);
eeprom_update_byte((uint8_t*)ADDRESS_2, dataByte2);
eeprom_update_byte((uint8_t*)ADDRESS_3, dataByte3);
}
If you are looking to update large amounts of data, try using the other functions such as eeprom_update_block(...)
. And definitely read that tutorial; it is well written.
You could put all of the EEPROM update statements in a single preprocessor conditional statement. This is very simple to do:
#if defined _UPDATE_EEPROM_
#define ADDRESS_1 46 // This could be anything from 0 to the highest EEPROM address
uint8_t dataByte = 0x7F; // Data for address 1
eeprom_update_byte((uint8_t*)ADDRESS_1, dataByte1);
#endif // _UPDATE_EEPROM_
This bit of code will not even be compiled unless you do the following:
#define _UPDATE_EEPROM_
You could leave this there as a comment, then uncomment if you need to change the default EEPROM values. For more information on the C preprocessor, check out this online manual. I think you may be most interested in the sections on macros and conditional statements.
With avr-gcc the EEMEM
macro can be used on the definition of a variable, see the libc
docs and an example here:
#include <avr/eeprom.h>
char myEepromString[] EEMEM = "Hello World!";
declares the array of characters to reside in a section named ".eeprom" which after compilation tells the programmer that this data is to be programmed to the EEPROM. Depending on your programmer software, you may need to explicitly give the name of the ".eep"-file created during the build process to the programmer, or it may implicitly find it by itself.