Page 1 of 1

Floating point.

Posted: Fri Jun 19, 2009 8:53 am
by JAW
Hi All,

Is their any way that a floating point can be saved to the eeprom eBlocks module? I have tried floattostring and then taking each single character of the sting and converting it into a byte. This works fine for the numbers but I get stuck with the decimal place. Any ideas?

Thanks

James

Re: Floating point.

Posted: Fri Jun 19, 2009 9:33 am
by Benj
Hello James

How are you converting the string into a byte, Can you not simply write the bytes of the string directly to the EEPROM.

E.g.

Code: Select all

String Manipulation:  string_var = FloatToString( Float_Var )
String Manipulation:  string_length = length ( string_var )

Calculation: IDX = 0

While: IDX < string_length
{

Component Macro: EEPROM Write: IDX, string_var[IDX]

Calculation: IDX = IDX + 1

}
Maybe you could store the string_length byte as the first byte of the EEPROM so you know how many bytes to read back to rebuild the string.

E.g.

Code: Select all

String Manipulation:  string_var = FloatToString( Float_Var )
String Manipulation:  string_length = length ( string_var )

Component Macro: EEPROM Write: 0, string_length

Calculation: IDX = 1

While: IDX < ( string_length + 1 )
{

Component Macro: EEPROM Write: IDX, string_var[IDX]

Calculation: IDX = IDX + 1

}

Re: Floating point.

Posted: Sun Jun 21, 2009 8:36 pm
by JAW
Hi Ben,

Thanks for that I didn't realise you could do some of the suggestions you made. How would you reassemble the data in the eeprom to a floating point ? I've attached my test file so far if anyone else wants to make use of it.

Many Thanks

James

Re: Floating point.

Posted: Mon Jun 22, 2009 7:45 am
by Steve
A neater way would be to use the code-customization feature of the component and write C-code functions that save and retrieve a float directly. A floating point number is stored in 4 bytes, and there will be a way of directly saving these 4 bytes to EEPROM by directly accessing the memory used to store the number in the chip's RAM.

Re: Floating point.

Posted: Mon Jun 22, 2009 10:19 am
by JAW
Hi Steve,

I have not used C for a long time. Could it be done with Flowcode components ? Or could you maybe point me in the right direction of writing a custom component in C?

Many Thanks

James

Re: Floating point.

Posted: Mon Jun 22, 2009 10:41 am
by Steve
The C code is something like this (assumes you have a Flowcode float variable called MyFloatVar):

Code: Select all

unsigned char* pMem;
unsigned char float_array[4];

pMem = (unsigned char*)&FCV_MYFLOATVAR;
float_array[0] = *pMem;
pMem++;
float_array[1] = *pMem;
pMem++;
float_array[2] = *pMem;
pMem++;
float_array[3] = *pMem;

//TODO: save float_array to EEPROM
The 4 bytes in the "float_array" variable could then be saved to EEPROM. To work backwards, do the opposite:

Code: Select all

unsigned char* pMem;
unsigned char float_array[4];
//TODO: populate float_array from EEPROM

pMem = (unsigned char*)&FCV_MYFLOATVAR;
*pMem = float_array[0];
pMem++;
*pMem = float_array[1];
pMem++;
*pMem = float_array[2];
pMem++;
*pMem = float_array[3];
NOTE: I've not tried this, but it might work! And also, there may be better ways of doing this.

Re: Floating point.

Posted: Tue Jun 23, 2009 7:41 am
by JAW
I can't get the code to work. I would really appreciate some help. The code compiled, I loaded it into the pic and no joy.

Thanks

James

PS I don't know if it's worth starting another thread but I'm also keen on getting the displayed floating numbers down to 2 decimal places.

Seem to be a bit obsessed with floating points at the moment !!!!

Re: Floating point.

Posted: Tue Jun 23, 2009 12:18 pm
by Steve
The code I gave you is not complete and is an example only. Once you have converted the float to the 4-character array, you can save this to the EEPROM. Later in your code, you can retrieve these values from EEPROM and then recreate the float value.

Look at my "TODO" statements in the code.

Re: Floating point.

Posted: Sat Aug 25, 2012 8:35 pm
by GTF
I know this is an old thread but what about something like this example for those of us who are code challenged.

Grant