Go Down

Topic: eeprom native library (Read 387 times) previous topic - next topic

HugoPT

Hello
Today I was using the eeprom using the native  library of avrlibc #include <avr/eeprom.h>
The function in documentation is:
Code: [Select]
uint8_t eeprom_read_byte ( const uint8_t *  __p )
Read one byte from EEPROM address __p.

it return a byte and needs as argument a pointer to an adress
So far so good, my problem starts now when I use the function passing the address of the position.
Code: [Select]
int adress = 0;//I want position 0
int readValue = eeprom_read_byte(&i);//It wount work as expected don´t understand why
int readValue = eeprom_read_byte((uint8_t *)i);//Doing a cast work's well but why ?
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

PaulS

The eeprom_read_byte() function expects an address. You defined one. But, what the heck is i?

You are reading a byte, and storing it in an int. Why?

HugoPT

Sorry my mistake typing
Here is the correct one:
Code: [Select]

uint8_t adress = 0;//I want position 0
uint8_t readValue = eeprom_read_byte(&adress);//It wount work as expected don´t understand why
uint8_t readValue = eeprom_read_byte((uint8_t *)adress);//Doing a cast work's well but why ?
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Delta_G

So you want the byte at address 0 in eeprom?  Then take the ampersand off.  With the ampersand you are giving it the address of the variable adress and that isn't 0. 

Jack Christensen

I may have had a similar question recently, see it this thread is of any help:
http://arduino.cc/forum/index.php/topic,140376.msg1054626.html
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

HugoPT

Quote
With the ampersand you are giving it the address of the variable adress and that isn't 0. 

But the function expect an address!!
I cannot pass the variable
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

PaulS

Quote
But the function expect an address!!

But not the address of the variable that holds the address. Making that function take a pointer was probably not the brightest move. But, you know how to pass it what it wants, so quit worrying about why that works. Just accept that it does.

HugoPT

Quote
But not the address of the variable that holds the address

Uhmm I was thinking that passing the address of the variable was in fact making the functions "looks" to the value 0 which was the eeprom address I want.
Perhaps you are right, time to move on and just use the cast.
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Delta_G


Quote
But not the address of the variable that holds the address

Uhmm I was thinking that passing the address of the variable was in fact making the functions "looks" to the value 0 which was the eeprom address I want.
Perhaps you are right, time to move on and just use the cast.



That's it.  With the ampersand you were passing it an address in SRAM where the variable adress lives.  If adress was at say address 32 in SRAM, then you were passing the 32 and not the 0 to the function. 

Go Up