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:
#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:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.