Pages: [1]   Go Down
Author Topic: -ve long storage in EEPROM  (Read 397 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Guys

Been lurking a while, but new to Arduino in general.

I'm developing a Ham Radion WSPR beacon using Arduino and I have everything working just fine, except....

I need to have a Calibration Factor to get my Arduino interfaced DDS exactly on frequency and I am trying to store and read back that value from EEPROM. It works fine for +ve numbers but not -ve. I've seen the tutorials on EEPROM writing but nothing I have seen has helped with -ve numbers.

I have tried including a bit test for the value read back, but this doesn't work either:

if (CalFactor >> 31 ==1) { CalFactor = CalFactor - (pow(2,32)-1); }

Can any kind soul here help with this connundrum of mine?

Here's the bit of code I can't get to function with -ve numbers, the value read back for a write of -2 is 288,741,374:

Code:
#include <EEPROM.h>
#include <math.h>

long CalFactor;
byte temp;
int j;
int i;

void setup()
{

   Serial.begin(9600);        // connect to the serial port

   CalFactor = -2;

   //Writes CalFactor to address 50 + 3 bytes of EEprom
   for (j=0; j<4; j++)  
   { // Step through 4 bytes to make 32 bit word
      for (i=0; i<8; i++)
      { // Step through 8 bit to make 1 byte
        bitWrite(temp,i,(bitRead(CalFactor,j*i+i))); // Assemble bits to make 1 byte
      }
      EEPROM.write(50+j,temp); // Write byte to EEprom
   }
  
   Serial.println("Written to EERPROM ");
   Serial.println(CalFactor);
   Serial.println(CalFactor,BIN);

   CalFactor = 0; // set all bits to zero
  
   // Read back from EEPROM
   for(j=0; j<4; j++)
   {  // Step through 4 bytes to make 32 bit word
      temp = EEPROM.read(50+j); // Get bytes 50 through 53
      for (i=0; i<8; i++)
      { // Set through 8 bit to make 1 byte
         bitWrite(CalFactor,i*j+i,(bitRead(temp,i))); // Assemble bits to make 32 bit word
      }
   }
  
   if (CalFactor >> 31 == 1)
   {
      Serial.println("1st Bit is 1");
//      CalFactor = CalFactor - (pow(2,32)-1);
   }
  
   Serial.println("Read from EERPROM");
   Serial.println(CalFactor);
   Serial.println(CalFactor,BIN);
}

void loop()
{  
}


Moderator edit: [code] [/code] tags added.
« Last Edit: June 07, 2012, 03:03:36 pm by Coding Badly » Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Personally I'd set myself up a union to do this:

Code:

struct CalFact {
  union {
    long value;
    struct {
      unsigned char b1;
      unsigned char b2;
      unsigned char b3;
      unsigned char b4;
    }__attribute__((packed));
  }__attribute__((packed));
}__attribute__((packed));

struct CalFact CalFactor;

You can then access the calibration factor's value with:

Code:
CalFactor.value

as in:

Code:
CalFactor.value=-4983231;

And you can access the individual bytes for reading and writing to eeprom with:

Code:
CalFactor.b1
through
CalFactor.b4

Write the whole lot to eeprom:

Code:
EEPROM.write(50,CalFactor.b1);
EEPROM.write(51,CalFactor.b2);
EEPROM.write(52,CalFactor.b3);
EEPROM.write(53,CalFactor.b4);

Read it all back:

Code:
CalFactor.b1 = EEPROM.read(50);
CalFactor.b2 = EEPROM.read(51);
CalFactor.b3 = EEPROM.read(52);
CalFactor.b4 = EEPROM.read(53);
« Last Edit: June 07, 2012, 10:10:28 am by majenko » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It can be very ease:

Writing:
Code:
for (byte n = 3; n != 255; n--) {
EEPROM.write(address+n, value & 0xFF);
value = value >> 8;
}

Reading:
Code:
long ival = 0;
for (byte n = 0; n < 4; n++) {
ival = (ival <<8) + EEPROM.read(address + n);
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much!

I have actually spotted why my code didnt work (typical after 3 days staring at it!) but the solutions offered are so much simpler. It's the complexity of mine that had tripped me up.

Thanks again.

Mark.
Logged

Pages: [1]   Go Up
Jump to: