AVR EEPROM for Data Storage & More

Most AVRs have a small EEPROM which can be used to save variables, configuration settings etc when there is no power and the data is available for read/write/modify the next time AVR powers up.

This post simply shows the most basic method for saving a variable (char) in the AVR EEPROM, modifying it at every start-up and then saving it. Basically it will be a counter for how many times the AVR has been turned-ON.

Here’s the code:

#include <avr/io.h>
#include <avr/eeprom.h>
int main(void){
unsigned char counter;
unsigned char* counter_address=10;
eeprom_busy_wait();
if(eeprom_is_ready()){
 counter = eeprom_read_byte(counter_address);
 eeprom_busy_wait();
 counter++;
 eeprom_update_byte(counter_address, counter);
 }
while(1){};
return(0);
}

Now, going over this line by line (and skipping some lines in the process)

  • #include <avr/eeprom.h>

    Include file for EEPROM functions, this is required for EEPROM functions to work

  • unsigned char counter;
    unsigned char* counter_address=10;

    The counter is the char variable which we will be using to access the EEPROM and the counter_address is a pointer variable which points to a SPECIFIC address in the EEPROM.

  • eeprom_busy_wait();

    This function needs to be called before accessing the EEPROM to make sure that the EEPROM is not busy and if it is, then wait until it gets free. The function does not return anything but waits until EEPROM is ready for use.

  • eeprom_is_ready()

    This is an additional safeguard measure to avoid any conflict in EEPROM access ad tests if EEPROM is ready to be accessed for read or write, returns TRUE if EEPROM is ready, that’s why it is used in the IF statement.

  • counter = eeprom_read_byte(counter_address);

    eeprom_read_byte(ADDRESS_POINTER) reads a byte from the EEPROM specified by the address pointer and returns the byte, hence we are saving the byte returned into the counter variable.

  • counter++;

    Incrementing the counter variable.

  • eeprom_update_byte(counter_address, counter);

    eeprom_update_byte(ADDRESS_POINTER, VARIABLE) stores the value provided in the argument at the address specified by the ADDRESS_POINTER in the EEPROM

  • .

    This about sums up the example code above, there are more functions for EEPROM access, like for reading a bunch of bytes or writing complete arrays to the EEPROM etc, you can check out the AVR Libc Reference for the EEPROM.h file for proper documentation of all the EEPROM functions available. If I have missed out on something put in a comment and I will reply back to you.

    There is also a GREAT and much more detailed EEPROM Tutorial at AVRFreaks.net

    REMEMBER – there is much more to AVR EEPROM than what is provided here;

    • There is also a fuse-bit associated with the EEPROM which restricts erasing of the EEPROM when an erase cycle is performed on the AVR by a programmer
    • Almost all AVRs have sufficient EEPROM space but if you need more there is always the option of external EEPROM.
    • Also the EEPROM in AVR has a read/write count limit, which is 100,000(as I remember) but see the datasheet of your AVR to be sure.
    • The AVR-GCC and other compilers can also provide .eep files for directly writing to EEPROM via a programmer.
    • The complete EEPROM can also be read by this method if you need to get all of the data on a PC.

Posted

in

by

Tags: