Pages: [1]   Go Down
Author Topic: Saving long numbers into eeprom  (Read 9298 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 491
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or use this, which does it for you: http://www.arduino.cc/playground/Code/EEPROMWriteAnything

Andrew
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 65
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very nice

Thanks a lot guys
Logged

Amsterdam
Offline Offline
Full Member
***
Karma: 1
Posts: 139
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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++.

 smiley-sad pity.
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Amsterdam
Offline Offline
Full Member
***
Karma: 1
Posts: 139
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Shifts and ANDs do it for me.
[edit]Also some ORs, oh yes, definitely some of those[/edit]
« Last Edit: December 04, 2009, 04:14:24 pm by AWOL » Logged

"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.

Amsterdam
Offline Offline
Full Member
***
Karma: 1
Posts: 139
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 << smiley-cool+loByte;
}

void writeeventsCounter(){
  loByte = byte(eventsCounter);
  hiByte = byte(eventsCounter >> smiley-cool;
  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.
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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"
Logged

"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.

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include <avr/eeprom.h>
to the top of your sketch.

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

Regards,

-Mike
Logged

Amsterdam
Offline Offline
Full Member
***
Karma: 1
Posts: 139
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

gogging avr/eeprom.h i found a nice tutorial in pdf:
http://www.teslabs.com/openplayer/docs/docs/prognotes/EEPROM%20Tutorial.pdf
Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
 // 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;
}
Logged

Pages: [1]   Go Up
Jump to: