The objective of this tutorial is to explain how to use the multiplication and division block within the context of arithmetic operations in a MasterTool X project.
Components
Equipment: 1 computer/notebook.
Software: MasterTool X
Tutorial Sections
- ARCHITECTURE
- DEVELOPMENT
1. ARCHITECTURE
A computer/notebook will be required to use MasterTool X.
Figure 1. Computer running MasterTool X
2. DEVELOPMENT
The operators MUL (multiplication) and DIV (division) are used to perform basic mathematical operations of proportional calculation, being very common in scale adjustments, engineering calculations, and value conversions.
How Operators Work
🔹 MUL (Multiplication)
Performs multiplication between two values:
Result = First parameter × Second parameter
🔹 DIV (Division)
Performs division between two values:
Result = First parameter ÷ Second parameter
Important:
The order of the parameters matters.
The operator always executes the operation of the first parameter in relation to the second.
Adding Operators to Logic
The MUL and DIV operators are not function blocks, but mathematical operators.
To add them to the logic:
- Open the Toolbox.
- Access Mathematical Operators.
- Select MUL or DIV.
- Click and drag the operator to the Ladder logic.
Figure 2. Insertion of the mathematical operators MUL and DIV
Input and Output Parameters
Inputs
- Can receive:
- Variables
- Numeric constants
Output (mandatory)
Since MUL and DIV are not function blocks, the output must be associated with a previously declared variable.
It is not possible to simply view the result in the block without declaring an output variable.
Data Types and Variable Sizes
It is essential to correctly choose the data type, especially in multiplications and divisions, which can generate larger values or values with decimal places.
Most common types
- INT
16 bits signed
Limited range (can cause overflow) - DINT
32 bits signed
Recommended for larger calculations - UDINT
32 bits unsigned
Ideal for counting or always positive values - REAL
Allows decimal places
Suitable for engineering calculations
Best practices:
- Prefer DINT or REAL for multiplications.
- Avoid INT when there is a risk of value overflow.
- The output variable must be compatible with the expected result.
Use of variables or constants
- Constants: Used when the factor is fixed (e.g., multiply by 10).
- Variables: Allow dynamic calculations during execution.
Both can be used freely in the operator inputs.
Figure 3. Insertion of variables for the mathematical operators MUL and DIV
Operation execution
The PLC executes the operation at each scan cycle, following this order:
- Reading the first parameter
- Reading the second parameter
- Execution of the calculation
- Writing the result to the output variable
Practical application
After inserting the MUL or DIV operator:
- Correctly declare the output variable.
- Verify the type and size of the variables.
- Download the application.
- Put the CPU in RUN mode.
- Observe the output value being updated as the inputs vary.
The MUL and DIV operators are essential for more elaborate calculations, but require special attention to the data type, variable size, and especially division by zero, ensuring system safety and stability.
Figure 4. Operation of the MUL and DIV mathematical operators
Special care: Division by zero
In the DIV operator, if the second parameter is equal to zero, the controller may:
- Generate an exception
- Take the CPU to STOP mode
This is a critical error and should always be avoided.
Way to avoid division by zero
🔹Ensure an initial value different from zero
A simple way is to initialize the divisor variable with a safe value.
Example of a declaration with initialization:
divisor : REAL := 1; // initial value to avoid division by zeroThus, even before any logic acts, the value will never be zero.
Figure 5. Way to avoid division by zero
Comments
0 comments
Please sign in to leave a comment.