DIV
IEC operator for the division of one variable by another one:
Allowed types:
BYTE
WORD
DWORD
LWORD
SINT
USINT
INT
UINT
DINT
UDINT
LINT
ULINT
REAL
LREAL
TIME
TIME variables can be divided by integer variables.
1. series of DIV
boxes
2. single DIV
box
3. DIV
box with EN/ENO
parameters
Different target systems may behave differently concerning a division by zero error. It can lead to a controller HALT, or may go undetected.
WARNING | |
---|---|
You can use the following check functions to verify the value of the divisor in order to avoid a division by 0 and adapt them, if necessary:
CheckDivDInt
CheckDivLint
CheckDivReal
CheckDivLReal
For information on inserting the function, refer to the description of the function.
The check functions are called automatically before each division found in the application code.
See the following example for an implementation of the function CheckDivReal
.
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 suggestion for implementation
IF divisor = 0 THEN
CheckDivReal:=1;
ELSE
CheckDivReal:=divisor;
END_IF;
The operator DIV
uses the output of function CheckDivReal
as a divisor. In the following example, a division by 0 is prohibited as with the 0 initialized value of the divisor d
is changed to 1 by CheckDivReal
before the division is executed. Therefore, the result of the division is 799.
PROGRAM PLC_PRG
VAR
erg:REAL;
v1:REAL:=799;
d:REAL;
END_VAR
erg:= v1 / d;