Go Down

Topic: Saving long numbers into eeprom (Read 9 times) previous topic - next topic

Dimitris

I have been looking for a way to save signed long numbers into the arduino's eeprom.
My initial idea was to use simple math to break it into separate bytes and then feed them to eeprom one by one.

Although it will probably work, I don t know how efficient this would be.

Any suggestions on doing this in a more graceful way?

AWOL

The interface to the EEPROM is byte-wide, so yes, you're going to have to break down the values.
Different ways of doing this include casting byte pointers to the address of the variable you want to write, or use a union.
Or, as you say, simple shift and mask.
"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.

Andrew


Dimitris


macsimski

that is beyond me. it should obvious work, but because there are no comments in the code, i have no idea what is happening. i can code plain c, but this seems to be c++.

:( pity.
--
"We're all in this together..."

AWOL

http://www.arduino.cc/playground/Code/EEPROMWriteAnything
Yes, it's C++, but there's an example there.
The idea of the template is that it'll work for any datatype.
"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.

macsimski

another reason to start studying c++. this seems to be a good example. but for now i have to find another plain c way to write and read a int to eeprom.
--
"We're all in this together..."

AWOL

#7
Dec 04, 2009, 10:10 pm Last Edit: Dec 04, 2009, 10:14 pm by AWOL Reason: 1
Shifts and ANDs do it for me.
[edit]Also some ORs, oh yes, definitely some of those[/edit]
"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.

macsimski

yea. solved it that way. i only had to do a INT to two BYTE.

void readeventsCounter(){
 hiByte = EEPROM.read(0);
 loByte = EEPROM.read(1);
 eventsCounter = (hiByte << 8)+loByte;
}

void writeeventsCounter(){
 loByte = byte(eventsCounter);
 hiByte = byte(eventsCounter >> 8);
 EEPROM.write(0, hiByte);
 EEPROM.write(1, loByte);
}


but i have'nt tested it yet. the reference is not clear about the functionality of the typecast to byte. i assume (i know, dangerous!) that it takes the lower byte of the int, but i have to find out in debugging.



got a nice quote from a friend of mine about c++:

"If C gives you enough rope to hang yourself, then C++ gives you enough rope to bind and gag your neighborhood, rig the sails on a small ship, and still have enough rope to hang yourself from the yardarm"

he is into cocoa now.
--
"We're all in this together..."

AWOL

Code: [Select]
void writeeventsCounter(){
EEPROM.write(0, eventsCounter >> 8);
EEPROM.write(1, eventsCounter & 0xff);
}


Better not to think of it as an "int", rather think of it as a "short", or an "int16"
"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.

Mike Murdock

macsimski,

See http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html.  This library has routines to read and write bytes, ints and longs -- all in C, no C++.

This library is already part of the Arduino environment.  Don't forget to  add the
Code: [Select]
#include <avr/eeprom.h>to the top of your sketch.

I've used some of these routines, and they work well.

Regards,

-Mike

macsimski

--
"We're all in this together..."

peep rada

to those like me who wish to read and write long integers to EEPROM without using pointers.

Code: [Select]

 // read double word from EEPROM, give starting address
 unsigned long EEPROM_readlong(int address) {
 //use word read function for reading upper part
 unsigned long dword = EEPROM_readint(address);
 //shift read word up
 dword = dword << 16;
 // read lower word from EEPROM and OR it into double word
 dword = dword | EEPROM_readint(address+2);
 return dword;
}

//write word to EEPROM
 void EEPROM_writeint(int address, int value) {
 EEPROM.write(address,highByte(value));
 EEPROM.write(address+1 ,lowByte(value));
}
 
 //write long integer into EEPROM
 void EEPROM_writelong(int address, unsigned long value) {
 //truncate upper part and write lower part into EEPROM
 EEPROM_writeint(address+2, word(value));
 //shift upper part down
 value = value >> 16;
 //truncate and write
 EEPROM_writeint(address, word(value));
}

unsigned int EEPROM_readint(int address) {
 unsigned int word = word(EEPROM.read(address), EEPROM.read(address+1));
 return word;
}

Go Up