PICmicro & dsPIC >> Source Share for PICmicro & dsPIC

Pages: 1
Dan HenryModerator
Guru
****

Reged: Oct 16 2003
Posts: 3387
Loc: Boulder, Colorado U.S.A.
Saving/Retrieving variables in EEPROM
      #52712 - Sun Apr 27 2008 11:58 AM

The questions of how to write and read multibyte data objects to and from EEPROM are often asked in these forums. If using the HI-TECH compiler's 'eeprom' qualifier is not suitable for whatever reason, because we know that a data object has an address and a size, a general solution can be crafted similar to that demonstrated in this simple example:

Code:
#include <htc.h>
#include <stddef.h>

struct ee_map_s {
char sn[9]; /* Serial number string */
long l;
float f;
char c;
};

#define EE_ADDR(member) (offsetof(struct ee_map_s, (member)))

void ee_read(unsigned char ee_addr, void *vp, unsigned char n)
{
unsigned char *p = vp;

while (n--) {
*p++ = eeprom_read(ee_addr++);
}
}

void ee_write(unsigned char ee_addr, void *vp, unsigned char n)
{
unsigned char *p = vp;

while (n--) {
eeprom_write(ee_addr++, *p++);
}
}

void main(void)
{
float f = 1.234;
long l = 1234;

ee_write(EE_ADDR(f), &f, sizeof f);
ee_write(EE_ADDR(l), &l, sizeof l);

ee_read(EE_ADDR(f), &f, sizeof f);

for (;;);
}



Post Extras: Print Post   Remind Me!   Notify Moderator  
VictorH
Tester - PICC PRO


Reged: Apr 14 2005
Posts: 24
Loc: Venezuela
Re: Saving/Retrieving variables in EEPROM [Re: Dan Henry]
      #57192 - Wed May 21 2008 11:59 AM

that's precisely the technique I use in my production code, which is beautifully explained in this article:
Learn a new trick with the offsetof() macro
By Nigel Jones, Embedded Systems Design


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
1 registered and 8 anonymous users are browsing this forum.

Moderator:  jtemples, Dan Henry, Andrew L, mikerj, dave g, meisty, josh stevo 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 2329

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5