You can declare an eeprom array and write individual positions. Note that unlike the ram array you can only read write to the eeprom array using the read/write eeprom macro.
The code below is to read/write 16bit unsigned values
#include <avr/io.h>
#include <avr/eeprom.h>
// macro for easier usage
#define read_eeprom_word(address) eeprom_read_word ((const uint16_t*)address)
#define write_eeprom_word(address,value) eeprom_write_word ((uint16_t*)address,(uint16_t)value)
#define update_eeprom_word(address,value) eeprom_update_word ((uint16_t*)address,(uint16_t)value)
//declare an eeprom array
unsigned int EEMEM my_eeprom_array[10];
// declare a ram array
unsigned int my_ram_array[10];
int main(void)
{
// write to eeprom array
write_eeprom_word(&my_eeprom_array[0], 1); // write value 1 to position 0 of the eeprom array
write_eeprom_word(&my_eeprom_array[1], 2); // write value 2 to position 1 of the eeprom array
write_eeprom_word(&my_eeprom_array[2], 3); // write value 3 to position 2 of the eeprom array
// read from eeprom array
my_ram_array[0] = read_eeprom_word(&my_eeprom_array[0]); // read value from position 0 of the eeprom array to ram array position 0
my_ram_array[1] = read_eeprom_word(&my_eeprom_array[1]); // read value from position 1 of the eeprom array to ram array position 1
my_ram_array[2] = read_eeprom_word(&my_eeprom_array[2]); // read value from position 2 of the eeprom array to ram array position 2
while(1);
}
An alternative is to use update instead of write. This function checks the content of the eeprom cell and only writes the data if it is different from the existing value in order to reduce unneeded writes and reduce the eeprom wear.
// update eeprom array
update_eeprom_word(&my_eeprom_array[0], 1); // write value 1 to position 0 of the eeprom array only if it differs
update_eeprom_word(&my_eeprom_array[1], 2); // write value 2 to position 1 of the eeprom array only if it differs
update_eeprom_word(&my_eeprom_array[2], 3); // write value 3 to position 2 of the eeprom array only if it differs
The AVR eeprom handling routines are listed
http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html
There is another alternative that can copy the entire ram array to the eeprom array and vice-versa, it uses the block write:
#include <avr/io.h>
#include <avr/eeprom.h>
// macro for easier usage
#define read_eeprom_array(address,value_p,length) eeprom_read_block ((void *)value_p, (const void *)address, length)
#define write_eeprom_array(address,value_p,length) eeprom_write_block ((const void *)value_p, (void *)address, length)
//declare an eeprom array
unsigned int EEMEM my_eeprom_array[10];
// declare a ram array and initialize
unsigned int my_ram_array[10]={1,2,3,4,5,6,7,8,9};
// declare another ram array
unsigned int my_other_ram_array[10];
int main(void)
{
// Copy data from my_ram_array to eeprom array
write_eeprom_array(my_eeprom_array,my_ram_array,sizeof(my_eeprom_array));
// restore to my_other_ram_array from eeprom
read_eeprom_array(my_eeprom_array,my_other_ram_array,sizeof(my_eeprom_array));
while(1);
}
The last parameter in the block write is how many bytes to write. In the example above the array length is 10 16bit integers which means 10 * 2bytes=20 bytes.
Instead of using
write_eeprom_array(my_eeprom_array,my_ram_array,20);
I have used the sizeof() function that returns the byte length of the array so it doesn't need to be changed every time the array size is changed
write_eeprom_array(my_eeprom_array,my_ram_array,sizeof(my_eeprom_array));
If you want you can use that value to manipulate the write/read length, for example you can write the first 3 values only
write_eeprom_array(my_eeprom_array,my_ram_array,3);
or values my_ram_array[3], my_ram_array[4] ,my_ram_array[5]
write_eeprom_array(&my_eeprom_array[3],&my_ram_array[3],3);