Division checks (POUs CheckDivDInt, CheckDivLInt, CheckDivReal, CheckDivLReal)
Functions for avoiding the divisor value "0": CheckDivDInt, CheckDivLInt, CheckDivReal and CheckDivLReal
The functions CheckDivDInt, CheckDivLInt, CheckDivReal and CheckDivLReal can be used to avoid division by 0. If you integrate these functions in the PLC project, they are called before each division that occurs in the code.
Do not change the declaration part To maintain the functionality of the monitoring functions, the declaration part must not be modified. The only exception is to add local variables. |
Standard implementation of the function CheckDivReal
Declaration part:
// Implicitly generated code : DO NOT EDIT
FUNCTION CheckDivReal : REAL
VAR_INPUT
divisor : REAL;
END_VAR
Implementation part:
// Implicitly generated code : Only an Implementation suggestion
{noflow}
IF divisor = 0 THEN
CheckDivReal := 1;
ELSE
CheckDivReal := divisor;
END_IF
{flow}
The DIV operator uses the output from the function CheckDivReal as divisor. In the sample program below, division by 0 is avoided by changing the value of the divisor "fDivisor", with was implicitly initiated with "0", to "1" via the function CheckDivReal before the division is executed. Thus the result of the division is 799.
PROGRAM MAIN
VAR
fResult : REAL;
fDivident : REAL := 799;
fDivisor : REAL := 0;
END_VAR
fResult := fDivident / fDivisor;