avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] using EEPROM


From: Ron Kreymborg
Subject: RE: [avr-gcc-list] using EEPROM
Date: Thu, 2 Oct 2003 23:42:13 +1000

A few more suggestions. I usually encapsulate all eeprom stuff in a set of
data specific functions. First I define some shorthand:

#define  EEPROM   __attribute__((section(".eeprom")))

I then have a .c module that collects all the eeprom data the program uses
in the one place. For now let's say it has two integer variables:

int e_Variable1   EEPROM = 0;
int e_Variable2   EEPROM = 0;

They could have initial values of course. A .h header file is provided for
modules that will be using these variables with the simple structure:

extern int e_Variable1 EEPROM;
etc

I have functions for bytes, strings and structures too, but the read/write
functions for integers are:

//--------------------------------------------------------
int eInt(int *ptr)
{
    int a;

    eeprom_read_block(&a, ptr, 2);
    return a;
}

//--------------------------------------------------------
void eWriteInt(int *numPtr, void *addr)
{
    uint8 *ptr, *num;

    ptr = (uint8*)addr;
    num = (uint8*)numPtr;
    eeprom_wb((int)ptr++, *num++);
    eeprom_wb((int)ptr++, *num++);
}

Now to read an integer from eeprom in some other module in the program I
need just use:

    localVariable = eInt(&e_Variable1);

and to write it back to eeprom (presumably because I have altered it), I
use:

    eWriteInt(&localVariable, &e_Variable1);

Hope this helps.

Ron



reply via email to

[Prev in Thread] Current Thread [Next in Thread]