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
Floating point.
Moderator: Benj
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Floating point.
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.
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.
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
}
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
}
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: Floating point.
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
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
- Attachments
-
- Save String.fcf
- Test File
- (7.5 KiB) Downloaded 423 times
- Steve
- Matrix Staff
- Posts: 3422
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Floating point.
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.
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
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
- Steve
- Matrix Staff
- Posts: 3422
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Floating point.
The C code is something like this (assumes you have a Flowcode float variable called MyFloatVar):
The 4 bytes in the "float_array" variable could then be saved to EEPROM. To work backwards, do the opposite:
NOTE: I've not tried this, but it might work! And also, there may be better ways of doing this.
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
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];
Re: Floating point.
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 !!!!
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 !!!!
- Attachments
-
- C Code eprom.fcf
- (7.5 KiB) Downloaded 420 times
- Steve
- Matrix Staff
- Posts: 3422
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Floating point.
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.
Look at my "TODO" statements in the code.
-
- Posts: 170
- Joined: Sat Dec 10, 2011 7:21 pm
- Location: Canada
- Has thanked: 20 times
- Been thanked: 52 times
- Contact:
Re: Floating point.
I know this is an old thread but what about something like this example for those of us who are code challenged.
Grant
Grant
- Attachments
-
- Float to EEPROM_Test.fcf
- (10 KiB) Downloaded 334 times