FUNCTION_BLOCK SCALE
VAR_INPUT
RAW : INT; //Enter value to be scaled
rInput_Min : REAL; //Enter input value @ rest (real)
rInput_Max : REAL; //Enter input value @ max (real)
rScaled_Min : REAL; //Enter minimum engineering units (real)
rScaled_Max : REAL; //Enter maximum engineering units (real)
Gen_Alarm : BOOL;
END_VAR
VAR_OUTPUT
iScaled_Out : INT;
rScaled_Out : REAL;
END_VAR
VAR
rRAW : REAL;
Rate : REAL;
Offset : REAL;
END_VAR
BEGIN
rRAW:=INT_TO_REAL(RAW); //Convert incoming integer to REAL
IF Gen_Alarm = FALSE THEN //If General Alarm isn't present
Rate := (rScaled_Max - rScaled_Min) / (rInput_Max - rInput_Min); //solve for X
Offset := rScaled_Min - (rInput_Min * Rate); //solve for B
rScaled_Out:= (rRAW * Rate) + Offset; // solve Y=MX+B where M = RAW, X = Rate B = Offset
iScaled_Out:=REAL_TO_INT(rScaled_Out);
ELSE
rScaled_Out := 0; // GenAlarm Alarm is true, output 0
iScaled_Out := 0;
END_IF;
END_FUNCTION_BLOCK