Hi,
please, how can I convert a real to int? I need to multiply an input by 100 and divide by 16384. I can't do this and return a int value. I get an error when I multiply a big number ( 12000) that excede de int value.
My input is from 0 to 16384 (int) and I need transforme in percent and return the result in int ( 0 to 100).
How can I do that?
Tks any help!
Converting 0 to 16384 (int) to 0 to 100% (int)
-
- Posts: 13
- Joined: Mon Apr 03, 2006 1:00 pm
- Location: Brazil
conversion
Hi,
First convert it to real with ITD, DTR, then do the calculation, then round it back to INT.
First convert it to real with ITD, DTR, then do the calculation, then round it back to INT.
You can use FC105 Scale
Library>Standard Library>TI-S7 converting>FC105
Description
The SCALE function takes an integer value (IN) and converts it to a real value in engineering units scaled between a low and a high limit (LO_LIM and HI_LIM). The result is written in OUT. The SCALE function uses the equation:
The constants K1 and K2 are set based upon whether the input value is BIPOLAR or UNIPOLAR.
· BIPOLAR: The input integer value is assumed to be between –27648 and 27648, therefore, K1 = –27648.0 and K2 = +27648.0
· UNIPOLAR: The input integer value is assumed to be between 0 and 27648, therefore, K1 = 0.0 and K2 = +27648.0
If the input integer value is greater than K2, the output (OUT) is clamped to HI_LIM, and an error is returned. If the input integer value is less than K1, the output is clamped to LO_LIM, and an error is returned.
Reverse scaling can be obtained by programming LO_LIM > HI_LIM. With reverse scaling, the value of the output decreases as the value of the input increases.
Example
If the signal state of input I0.0 is 1 (activated), the SCALE function is executed. In this example, the integer value 22 will be converted to a real value scaled between 0.0 and 100.0 and written to OUT. The input value is BIPOLAR, as indicated by the signal state of I2.0.
If the function is executed without error, the signal states of ENO and Q0.0 are set to 1 and RET_VAL is set equal to W#16#0000.
----------STL------------
Library>Standard Library>TI-S7 converting>FC105
Description
The SCALE function takes an integer value (IN) and converts it to a real value in engineering units scaled between a low and a high limit (LO_LIM and HI_LIM). The result is written in OUT. The SCALE function uses the equation:
Code: Select all
OUT = [ ((FLOAT (IN) – K1)/(K2–K1)) * (HI_LIM–LO_LIM)] + LO_LIM
· BIPOLAR: The input integer value is assumed to be between –27648 and 27648, therefore, K1 = –27648.0 and K2 = +27648.0
· UNIPOLAR: The input integer value is assumed to be between 0 and 27648, therefore, K1 = 0.0 and K2 = +27648.0
If the input integer value is greater than K2, the output (OUT) is clamped to HI_LIM, and an error is returned. If the input integer value is less than K1, the output is clamped to LO_LIM, and an error is returned.
Reverse scaling can be obtained by programming LO_LIM > HI_LIM. With reverse scaling, the value of the output decreases as the value of the input increases.
Example
If the signal state of input I0.0 is 1 (activated), the SCALE function is executed. In this example, the integer value 22 will be converted to a real value scaled between 0.0 and 100.0 and written to OUT. The input value is BIPOLAR, as indicated by the signal state of I2.0.
If the function is executed without error, the signal states of ENO and Q0.0 are set to 1 and RET_VAL is set equal to W#16#0000.
----------STL------------
Code: Select all
CALL "SCALE" // CALL FC105
IN :="MHL".SW_Out_current
HI_LIM :=6.750000e+001 //max value
LO_LIM :=0.000000e+000 //min value
BIPOLAR:=FALSE
RET_VAL:="temp_word" //Error code
OUT :="MHL".Current //Scaled result (floit point)
Code: Select all
L "YourInput" (your Input 0-16384)
ITD (convert your input to DoubleInteger Format)
DTR (convert your input to Real format)
L 100.0 (constant % in Real Format)
*R (= YourInput * 100.0)
L 16384.0 (Max Scale of YourInput)
/R (= [YourInput * 100.0] / 16384.0)
RND (Convert the result to Integer)
T "Result" (Your result in Integer Fotmat scaled to 0 - 100 %)
enjoy