convert two Int to one Float

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

convert two Int to one Float

Post by George_B »

Hello to everyone !


In modbus, sometimes, we need to obtain float variables to describe a value.

As i have understand so far there will be need to send two integer variables in order to describe this float variable.


My question is, if i have a float variable = 24.256, how it is possible to split that into two integers ?

Thank in advance !

George

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: convert two Int to one Float

Post by stefan.erni »

Hi George

If you only have this value that you want to convert from float to integer then multiply the value by 1000 then you have an integer. You just have to divide by 1000 somewhere.

regards


Stefan

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: convert two Int to one Float

Post by George_B »

Hi Stafan, nice to hear from you.

Maybe i was not describe the issue with details.


In Modbus, when you define the address you read as float type, it will automatically read two addresses with one integer on each address in order to obtain the float value.

For example:
If you want to read the register 40001 as float then,

you have to read address 40001 as int
you also have to read address 40002 as int

then you have to union those two to end up with the float value.



Since i want to implement my program from a slave modbus device perspective, i need to do the opposite from what i describe above.
I have the float value and i need to send the two integer values through modbus fo as a float value will be obtained in the modbus master device .


Another example is that:

Int_var_1 = 16992
Int_var_2 = -32768

Float_var = 56.125

I can not figure out how to calculate the Int_var_1 and Int_var_2 when i know the Float_var = 56.125


Best Regards
George

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: convert two Int to one Float

Post by Steve »

It sounds like the FLOAT is being represented by 4 bytes. An INT is 2 bytes and so reading the 2 INTs gives you the 4 bytes representing the FLOAT.

Once you have these 4 bytes, you need to know how they represent the FLOAT. For example, see here:
https://en.wikipedia.org/wiki/Single-pr ... int_format

You will also have to understand how these 4 bytes are ordered. This is called "endianness":
https://en.wikipedia.org/wiki/Endianness

There's a discussion about this here:
viewtopic.php?f=7&t=21810

So it's complicated! But we have a component in Flowcode (v8 and v9) that should help with this conversion. It's called "TypeConversion".

Hope this helps.
Steve.

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: convert two Int to one Float

Post by George_B »

Hi Steve, thanks for the reply and help.


I am using Flowcode 7, at the moment i can not use FC8.

I thought it would be easy somehow to implement this conversion using C code.

I have seen this post with 4 bytes but i could not figure out any result since in my case i need to use two integer values.


Regards
George

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: convert two Int to one Float

Post by Benj »

Hi George,

This topic should help.
viewtopic.php?f=28&t=12561&p=49907

In your case the union could instead look like this.

Code: Select all

typedef union
{
  MX_FLOAT AsFloat;
  MX_UINT16  AsInt[2];
} MX_UnionFloat;

George_B
Posts: 128
Joined: Wed Jul 04, 2012 11:21 pm
Location: Greece
Has thanked: 51 times
Been thanked: 19 times
Contact:

Re: convert two Int to one Float

Post by George_B »

hi Benj


Thank you very much for your reply.

This is the Union as you said.

In my case ineed to do the "Seperation" from one Float to two Integers.

Would it be the same as it is for bytes?

Code: Select all

Temp.AsByte[0] = FCV_BYTE0;
Temp.AsByte[1] = FCV_BYTE1;
Temp.AsByte[2] = FCV_BYTE2;
Temp.AsByte[3] = FCV_BYTE3;
FCV_FVAR = Temp.AsFloat;

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: convert two Int to one Float

Post by Benj »

Hello,

In your case it would look like this.

Code: Select all

Temp.AsInt[0] = FCV_INT0;
Temp.AsInt[1] = FCV_INT1;
FCV_FVAR = Temp.AsFloat;

Post Reply