Go Down

Topic: Saving arrays in EEPROM using ARDUINOMEGA (Read 7060 times) previous topic - next topic

Daniel Formosa

It is stored in address 0 1 and 2 i think.
Code: [Select]
#include <EEPROM.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for ( i = 0; i < sizeof(value); i++)
  EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
  *p++ = EEPROM.read(ee++);
    return i;
}
void setup()
{
  Serial.begin(9600);
  int reference = 323;
  int serial;

  EEPROM_writeAnything(0, reference);
  //EEPROM_readAnything(0,serial);
  serial = EEPROM.read(0) + EEPROM.read(1) + EEPROM.read(2);
       
  Serial.print(serial);
}
void loop()
{}


serial = EEPROM.read(0) + EEPROM.read(1) + EEPROM.read(2);
  Removing read(2) will not give 323

AWOL

Quote
It is stored in address 0 1 and 2 i think

Think again.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

serial = EEPROM.read(0) + EEPROM.read(1) + EEPROM.read(2);
  This 'prints' 323

serial = EEPROM.read(0) + EEPROM.read(1)
This 'prints' 68

why then??
(And hehe i am on a laptop so shaking my keyboard wont make it that better :p)

tnnx

PaulS

The value is not stored digit by digit in consecutive addressed. I think you need to go back and look at reply #11 (not to sound like a broken record or anything).

Daniel Formosa

"You provide the first EEPROM address to be written, and the functions return how many bytes were transferred."
So if in this code it returns 2, meaning 2 bytes were transferred right?

But still I cannot understand the fact that using the code; EEPROM.read(0) + EEPROM.read(1) it 'prints' 68.

I'm sorry if im being such a bi*** but I really need to understand this

tnx again

PaulS

The function returns the high byte and the low byte on the two calls. The integer is constructed by shifting the high byte 8 places to the left and adding the low byte, as was shown in reply #11. It is NOT constructed by simply adding the two bytes.

The values that you are getting are 1 and 67. Shifting the 1 8 places is equivalent to multiplying by 256. So, 256 + 67 = ? That's right. 323.

Daniel Formosa

aaaaaaaaaaaaaa tnx!!!!!!

now for tomorrow to continue looking in this....

TNX ALL!!!

AWOL

Quote
serial = EEPROM.read(0) + EEPROM.read(1) + EEPROM.read(2);
  Removing read(2) will not give 323


In case you were wondering:
323 decimal = 0x0143

0x01 + 0x43 + 0xFF (the contents of address 2, because you haven't written anything to it) = 0x143 = 323.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

Another question pls...

If for example I need integer 1511.  I thought that it would need 3 bytes but 2 are needed(serialmonitered it).

Reading addreses 0 and address 1 i get these values
address 0 - 231
address 1 - 5

So my question is, (looking at reply11), 5 is the HIGH byte right?
How is it worked out?
Moving 5 - 101B to shift 8 places - 10100000000 - 1280??
Adding 231 = 1511

Is this right?
tnx

GaryP

What is 101B, where did it came from?

But if your high byte is 5, then 5 * 256 =1280. That is correct.
1280 + 231 =1511, so that is true as well.


Kari
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

AWOL

Quote
What is 101B, where did it came from?

5 in binary.

OP: Start thinking in hex.
1511 is 0x05E7, so two bytes only,  0x05 and 0xe7.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GaryP

#41
Mar 10, 2011, 09:40 pm Last Edit: Mar 10, 2011, 09:42 pm by GaryP Reason: 1

Quote
What is 101B, where did it came from?

5 in binary.

OP: Start thinking in hex.
1511 is 0x05E7, so two bytes only,  0x05 and 0xe7.


I'm dummy. That "B" confused me, I though IT WAS hex 101B. I don't know how they should be expressed in these posts, I like to say "bin" or "hex" just to be sure, not combining binary and decimals in the same sentence.
;)

Cheers,
Kari

EDIT. By the way, what does the "OP:" stand for? OT= off topic, but OP?
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GaryP


Original Poster


Ok, I though it was for me.

This has been very enlighten conversation for me, only thing that bothers me is the short lifeline of the internal eeprom.

If I decide to use external eeprom IC, is there anything that makes life easier or more complicated, comparing to internal eeprom? Is it faster or exactly opposite? Programming issues are more likely the biggest consern, not the speed.

Cheers,
Kari
The only law for me; Ohms Law: U=R*I       P=U*I
Note to self: "Damn! Why don't you just fix it!!!"

AWOL

Quote
only thing that bothers me is the short lifeline of the internal eeprom.


I thought the retention time was of the order of a century - do you plan on leaving your Arduino to your grandchildren?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up