Floating point.

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
JAW
Posts: 30
Joined: Tue Jun 16, 2009 2:25 pm
Been thanked: 4 times
Contact:

Floating point.

Post 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

User avatar
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.

Post 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

}

JAW
Posts: 30
Joined: Tue Jun 16, 2009 2:25 pm
Been thanked: 4 times
Contact:

Re: Floating point.

Post 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
Attachments
Save String.fcf
Test File
(7.5 KiB) Downloaded 423 times

User avatar
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.

Post 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.

JAW
Posts: 30
Joined: Tue Jun 16, 2009 2:25 pm
Been thanked: 4 times
Contact:

Re: Floating point.

Post 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

User avatar
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.

Post 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.

JAW
Posts: 30
Joined: Tue Jun 16, 2009 2:25 pm
Been thanked: 4 times
Contact:

Re: Floating point.

Post 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 !!!!
Attachments
C Code eprom.fcf
(7.5 KiB) Downloaded 420 times

User avatar
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.

Post 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.

GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Re: Floating point.

Post 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
Attachments
Float to EEPROM_Test.fcf
(10 KiB) Downloaded 334 times

Post Reply