hi folks
could someone tell me how to store the value of a float varible to internal eeprom? I tried bit shifting but the compiler complained. i also tried searching thru the forum with no luck
heres a snipit of my code
float ADC_RAW, *whole, fract;
void main()
{
ADCON1 = 0x80;
TRISA = 0x7F;
ADC_RAW = Adc_Read(1); // read 10 bit value in 32 bit reg
ADC_RAW = ADC_RAW * 0.02974609375; //convert to correct voltage
//ADC_RAW should read 1022, after multiplying should be 30.4005078125
fract=modf(ADC_RAW, whole);
EEprom_Write(00, whole >> 24); // write byte 3 of whole
EEprom_Write(01, whole >> 16); // write byte 2 of whole
EEprom_Write(02, whole >> ; // write byte 1 of whole
EEprom_Write(03, whole); // write byte 0 of whole
EEprom_Write(04, fract >> 24); // write byte 3 of fract
EEprom_Write(05, fract >> 16); // write byte 2 of fract
EEprom_Write(06, fract >> ; // write byte 1 of fract
EEprom_Write(07, fract); // write byte 0 of fract
}
the compiler says Operator not applicable to the operand
so is it posible to bitshift a float? if so how. is there a workaround. I tried converting the float to a long int. The both parts got corrupt
thanks
Daryl
Float to EEprom
floatToStr to EEprom
been trying to figure this out on my own. I've tried everyway I now to store a float in eeprom. believe the code below is storeing the string correctly but the data is corrupt. I know that they must be a simple solution.
I have a question about how FloatToStr works. I'm confused about the simple code below. It takes a float "30.455" and converts to a string. it ends up being "+0.15592e2". shouldn't it be "+0.30455e2"?
float ADC_RAW;
char Batt_Voltage[13];
void main()
{
ADC_RAW = 30.455;
FloatToStr(ADC_RAW, Batt_Voltage);
EEprom_Write(0x00, ((char *)&Batt_Voltage)[0]); // write byte 12
EEprom_Write(0x01, ((char *)&Batt_Voltage)[1]); // write byte 11
EEprom_Write(0x02, ((char *)&Batt_Voltage)[2]); // write byte 10
EEprom_Write(0x03, ((char *)&Batt_Voltage)[3]); // write byte 9
EEprom_Write(0x04, ((char *)&Batt_Voltage)[4]); // write byte 8
EEprom_Write(0x05, ((char *)&Batt_Voltage)[5]); // write byte 7
EEprom_Write(0x06, ((char *)&Batt_Voltage)[6]); // write byte 6
EEprom_Write(0x07, ((char *)&Batt_Voltage)[7]); // write byte 5
EEprom_Write(0x08, ((char *)&Batt_Voltage)[8]); // write byte 4
EEprom_Write(0x09, ((char *)&Batt_Voltage)[9]); // write byte 3
EEprom_Write(0x0A, ((char *)&Batt_Voltage)[10]); // write byte 2
EEprom_Write(0x0B, ((char *)&Batt_Voltage)[11]); // write byte 1
EEprom_Write(0x0C, ((char *)&Batt_Voltage)[12]); // write byte 0
// the contents of EEprom is
// HEX: 2B 30 2E 31 35 35 39 32 65 32 20 20 00
// ASCII: "+0.15592e2<space><space><Null>"
// it should be "+0.30455e2"
}
thank you
Daryl
I have a question about how FloatToStr works. I'm confused about the simple code below. It takes a float "30.455" and converts to a string. it ends up being "+0.15592e2". shouldn't it be "+0.30455e2"?
float ADC_RAW;
char Batt_Voltage[13];
void main()
{
ADC_RAW = 30.455;
FloatToStr(ADC_RAW, Batt_Voltage);
EEprom_Write(0x00, ((char *)&Batt_Voltage)[0]); // write byte 12
EEprom_Write(0x01, ((char *)&Batt_Voltage)[1]); // write byte 11
EEprom_Write(0x02, ((char *)&Batt_Voltage)[2]); // write byte 10
EEprom_Write(0x03, ((char *)&Batt_Voltage)[3]); // write byte 9
EEprom_Write(0x04, ((char *)&Batt_Voltage)[4]); // write byte 8
EEprom_Write(0x05, ((char *)&Batt_Voltage)[5]); // write byte 7
EEprom_Write(0x06, ((char *)&Batt_Voltage)[6]); // write byte 6
EEprom_Write(0x07, ((char *)&Batt_Voltage)[7]); // write byte 5
EEprom_Write(0x08, ((char *)&Batt_Voltage)[8]); // write byte 4
EEprom_Write(0x09, ((char *)&Batt_Voltage)[9]); // write byte 3
EEprom_Write(0x0A, ((char *)&Batt_Voltage)[10]); // write byte 2
EEprom_Write(0x0B, ((char *)&Batt_Voltage)[11]); // write byte 1
EEprom_Write(0x0C, ((char *)&Batt_Voltage)[12]); // write byte 0
// the contents of EEprom is
// HEX: 2B 30 2E 31 35 35 39 32 65 32 20 20 00
// ASCII: "+0.15592e2<space><space><Null>"
// it should be "+0.30455e2"
}
thank you
Daryl
Re: Float to EEprom
I cannot check this immediately, so you have to test it yourself. There are two solutions that come to my mind:
- 1. use the Lo(), Hi(), Higher() and Highest() functions to store a float byte-by-byte;
2. use a union of float and unsigned long. Fill it up through float, and store (with >> operator)through a long;
pizon
Hi, this is what I use for reading & writing EEPROM :
works fine for any types : int, long, double, char[]... don't forget to mention the correct type with sizeof !
Bruno
Code: Select all
double dble ;
/*
* a : address
* l : length
* s : pointer to data
*/
void EEPROM_StrRead(unsigned char a, unsigned char l, unsigned char *s)
{
unsigned char i ;
for(i = 0 ; i < l ; i++)
{
*s = EEPROM_Read(a + i) ;
s++ ;
}
}
void EEPROM_StrWrite(unsigned char a, unsigned char l, unsigned char *s)
{
unsigned char i ;
for(i = 0 ; i < l ; i++)
{
EEPROM_Write(a + i, *s) ;
s++ ;
}
}
main()
{
EEPROM_StrRead(0, sizeof(double), (char *)&dble) ; // read eeprom from location 0 and store into d
EEPROM_StrWrite(0, sizeof(double), (char *)&dble) ; // store d to eeprom to location 0
}
Bruno
Re: floatToStr to EEprom
You are right, it should be "+0.30455e2". We have discovered a bug in the compiler related with the allocation of the memmory for const and static local variables, which was expressing it self with a certain combinatin of codes. The bug is now fixed and the new version of the compiler will be available in the next couple of days.gumshoe wrote:been trying to figure this out on my own. I've tried everyway I now to store a float in eeprom. believe the code below is storeing the string correctly but the data is corrupt. I know that they must be a simple solution.
I have a question about how FloatToStr works. I'm confused about the simple code below. It takes a float "30.455" and converts to a string. it ends up being "+0.15592e2". shouldn't it be "+0.30455e2"?
float ADC_RAW;
char Batt_Voltage[13];
void main()
{
ADC_RAW = 30.455;
FloatToStr(ADC_RAW, Batt_Voltage);
EEprom_Write(0x00, ((char *)&Batt_Voltage)[0]); // write byte 12
EEprom_Write(0x01, ((char *)&Batt_Voltage)[1]); // write byte 11
EEprom_Write(0x02, ((char *)&Batt_Voltage)[2]); // write byte 10
EEprom_Write(0x03, ((char *)&Batt_Voltage)[3]); // write byte 9
EEprom_Write(0x04, ((char *)&Batt_Voltage)[4]); // write byte 8
EEprom_Write(0x05, ((char *)&Batt_Voltage)[5]); // write byte 7
EEprom_Write(0x06, ((char *)&Batt_Voltage)[6]); // write byte 6
EEprom_Write(0x07, ((char *)&Batt_Voltage)[7]); // write byte 5
EEprom_Write(0x08, ((char *)&Batt_Voltage)[8]); // write byte 4
EEprom_Write(0x09, ((char *)&Batt_Voltage)[9]); // write byte 3
EEprom_Write(0x0A, ((char *)&Batt_Voltage)[10]); // write byte 2
EEprom_Write(0x0B, ((char *)&Batt_Voltage)[11]); // write byte 1
EEprom_Write(0x0C, ((char *)&Batt_Voltage)[12]); // write byte 0
// the contents of EEprom is
// HEX: 2B 30 2E 31 35 35 39 32 65 32 20 20 00
// ASCII: "+0.15592e2<space><space><Null>"
// it should be "+0.30455e2"
}
thank you
Daryl
Re: floatToStr to EEprom
thanks for your quick replay and all your trouble. glad to know it was only a bug instead of demons inside the pc. Dont worry, bugs creap in. nothing is perfect. heck some bugs are features. heheh. but for this one, I was literly pulling out my hair. went as far as to breaking down the bits of the 32 bit float word and comparing against IEEE754.
another question to clear up. Is MikroC fully compatable with IEEE 754 floating point standard? some PicMicro use a variation of the standard.
IEEE754 compliant
1bit sign
8 bit exponent
23 bit mantissa
Picmicro variation
8 bit exponent
1 bit sign
23 bit mantissa
just curious on which one it uses?
oh PS. since your releasing a new version, any way to make bit shift ">>" "<<" work with floats? if not, the above adressing scheme works just fine with a for loop and > 4K flash.
thanks
Daryl
another question to clear up. Is MikroC fully compatable with IEEE 754 floating point standard? some PicMicro use a variation of the standard.
IEEE754 compliant
1bit sign
8 bit exponent
23 bit mantissa
Picmicro variation
8 bit exponent
1 bit sign
23 bit mantissa
just curious on which one it uses?
oh PS. since your releasing a new version, any way to make bit shift ">>" "<<" work with floats? if not, the above adressing scheme works just fine with a for loop and > 4K flash.
thanks
Daryl
Re: floatToStr to EEprom
Regarding the standard, this is what we use.Picmicro variation
8 bit exponent
1 bit sign
23 bit mantissa
Shift operations are not meant for floats, and we will stick to it (i.e. to ANSI Standard). Otherwise, you can always use a union or explicit casting to perform that.PS. since your releasing a new version, any way to make bit shift ">>" "<<" work with floats? if not, the above adressing scheme works just fine with a for loop and > 4K flash.
pizon
Re: Float to EEprom
Thanks bruno
Great approach to solve this thread. Simple and effective.
You are a great mikroC coder, i like your www.micro-examples.com website!
Great approach to solve this thread. Simple and effective.
You are a great mikroC coder, i like your www.micro-examples.com website!