NEWTC



  • HOME
  • Customer Center
  • FAQ


hit : 5,476
title Accessing EEPROM
name administrato… 14-08-14 07:04

Accessing EEPROM

The EEPROM can be accessed at runtime using library functions. Use #include  before calling these functions.

IMPORTANT: These functions do not work for the XMega. XMega support will be provided in a later release.

    EEPROM_READ(int location, object)


This macro calls the EEPROMReadBytes function (see below) to read in the data object from the EEPROM location(s). The int “object” can be any program variable, including structures and arrays. For example,

    int i;

    EEPROM_READ(0x1, i);                  // read 2 bytes into i

    EEPROM_WRITE(int location, object)


This macro calls the EEPROMWriteBytes function (see the next section) to write the data object to the EEPROM location(s). The int “object” can be any program variable, including structures and arrays. For example,

    int i;

    EEPROM_WRITE(0x1, i);                  // write 2 bytes to 0x1


There are actually 3 sets of macros and functions:

- Most classic and mega AVRs

- AVRs with 256 bytes of EEPROM

- MegaAVRs with extended IO


The IDE predefines certain macros (e.g., ATMega168), so that the right macros and functions are used when you #include the eeprom.h header file. Thus, you may use the names given here for these macros and functions.


Initializing EEPROM

EEPROM can be initialized in your program source file by allocation of a global variable to a special area called eeprom. In C source, this can be done using pragmas. See Program Areas for a discussion of the different program areas. The resulting file is .eep. For example,

    #pragma data:eeprom

    int foo = 0x1234;

    char table[] = { 0, 1, 2, 3, 4, 5 };

    #pragma data:data

    ...

    int i;

    EEPROM_READ((int)&foo, i);                  // i now has 0x1234

The second pragma is necessary to reset the data area name back to the default data.

Note that to work around the hardware bug in the older AVRs, location 0 is not used for the initialized EEPROM data for some of these older devices (mostly the pre-Mega AT90S series).

Note that when using this in an external declaration (e.g. accessing foo in another file), you do not use the pragma. For example, in another file:

    extern int foo;

    int i;

    EEPROM_READ((int)&foo, i);


Internal Functions

The following functions can be used directly if needed, but the macros described above should suffice for most if not all situations.

    unsigned char EEPROMread(int location)


Reads a byte from the specified EEPROM location.

    int EEPROMwrite(int location, unsigned char byte)


Writes byte to the specified EEPROM location. Returns 0 if successful.

    void EEPROMReadBytes(int location, void *ptr, int size)


Reads size bytes starting from the EEPROM location into a buffer pointed to by ptr.

    void EEPROMWriteBytes(int location, void *ptr, int size)


Writes size bytes to EEPROM starting with location with content from a buffer pointed to by ptr.


“Real Time” EEPROM Access

The preceding macros and functions wait until the any pending EEPROM write is finished before their access. The files rteeprom.h and rteeprom.c in the c:\iccv8avr\examples.avr directory contain source code for routines that read and write to EEPROM that do not wait. This is particularly useful for a real-time multitasking environment. A ready function is provided for you to ensure that the operation is completed. This is particularly useful for the EEPROM write function, since writing EEPROM may take a long time.