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

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

RE: [avr-gcc-list] int to EEPROM


From: Ron Kreymborg
Subject: RE: [avr-gcc-list] int to EEPROM
Date: Thu, 9 Aug 2001 22:24:12 +1000

>
> Saw nice example of storing float to eeprom, could some one please post
> the same kind of example of storing & retreiving and int type variable
> from/to eeprom ?
>
> With regards,
>
> Pasi
>

Well here is one possible organisation. For the int case where (for example)
the variable in eeprom you are concerned with is called "TheVariable ". It
is declared in the usual way:

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

int TheVariable EEPROM = 0;

You extract it with:

    int x = eInt(&TheVariable);

and you can write it back with:

    eWriteInt(&x, &TheVariable);

Don't forget the ampersand! Here is a complete set...

//--------------------------------------------------------------------------
---
float eFloat(float *ptr)
{
    float a;

    eeprom_read_block(&a, (int)ptr, 4);
    return a;
}

//--------------------------------------------------------------------------
---
long eLong(long *ptr)
{
    long a;

    eeprom_read_block(&a, (int)ptr, 4);
    return a;
}

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

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

//--------------------------------------------------------------------------
---
BYTE eByte(BYTE *ptr)
{
    BYTE a;

    eeprom_read_block(&a, (int)ptr, 1);
    return a;
}

//--------------------------------------------------------------------------
---
void eWriteFloat(float *numPtr, void *addr)
{
    BYTE *ptr, *num;

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

//--------------------------------------------------------------------------
---
void eWriteLong(long *numPtr, void *addr)
{
    BYTE *ptr, *num;

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

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

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

//--------------------------------------------------------------------------
---
void eWriteByte(BYTE *numPtr, void *addr)
{
    BYTE *ptr, *num;

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


-------------------------------------------
Ron Kreymborg
Jennaron Research
Melbourne, Australia




reply via email to

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