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: Jack Valmadre
Subject: Re: [avr-gcc-list] using EEPROM
Date: Mon, 20 Oct 2003 08:32:42 +1000

I'm been working on EEPROM a bit more, below is the program I've just tried
out on a 2313:

What I want the program to do is write a single byte (localVar) to EEPROM
address 0, then read it and output it to PORTB.  However all the pin on
PORTB are just going low.  What have I done wrong and how can I fix it?

Thanks,
Jack

#include <avr/io.h>
#include <avr/eeprom.h>

#define EEPROM __attribute__((section(".eeprom")))
unsigned int eVar1 EEPROM = 0;
unsigned int eVar2 EEPROM = 0;

unsigned char i;
unsigned int localVar;

unsigned int eInt(unsigned int *ptr);
void eWriteInt(unsigned int *numPtr, void *addr);

int main(void) {
 DDRB = 0xFF;
 localVar = 3;
 eWriteInt(&localVar, &eVar1);
 while(~EECR & (1<<EEWE))
  ;
 PORTB = eInt(&eVar1);
 for(;;)
  ;
}

unsigned int eInt(unsigned int *ptr) {
 unsigned char a;
 eeprom_read_block(&a, ptr, 1);
 return a;
}

void eWriteInt(unsigned int *numPtr, void *addr) {
 unsigned char *ptr, *num;

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



----- Original Message ----- 
From: "Ron Kreymborg" <address@hidden>
To: "AVR GCC List" <>
Sent: Thursday, October 02, 2003 11:42 PM
Subject: RE: [avr-gcc-list] using EEPROM


> 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
>
>
> _______________________________________________
> avr-gcc-list mailing list
> address@hidden
> http://www.avr1.org/mailman/listinfo/avr-gcc-list



reply via email to

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