Converting 0 to 16384 (int) to 0 to 100% (int)

SIMATIC S7-200/300/400, Step7, PCS7, CFC, SFC, PDM, PLCSIM,
SCL, Graph, SPS-VISU S5/S7, IBHsoftec, LOGO ...
Post Reply
Posts: 13
Joined: Mon Apr 03, 2006 1:00 pm
Location: Brazil

Converting 0 to 16384 (int) to 0 to 100% (int)

Post by mmanu » Thu Jun 29, 2006 7:38 pm

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!



Post by Akos » Fri Jun 30, 2006 4:11 am


First convert it to real with ITD, DTR, then do the calculation, then round it back to INT.


Post by FC105 » Fri Jun 30, 2006 4:40 am

You can use FC105 Scale
Library>Standard Library>TI-S7 converting>FC105


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


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.

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
       RET_VAL:="temp_word"            //Error code
       OUT    :="MHL".Current             //Scaled result (floit point)


Post by Cristobal » Sat Jul 01, 2006 1:34 am

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 :wink:

Post Reply