The objective of this tutorial is to explain how to create function-type subroutines 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
A Function is a type of POU used for logical or mathematical processing that returns a single value, having no internal state.
It is ideal for calculations, validations, and simple decisions that need to be reused at different points in the program.
In this example, a function called fun_partida_direta will be created, with a BOOL return type, implemented in Ladder (LD).
Difference between Function, Function Block, and Subroutine
| Type | Internal State | Instance Variable | Return |
| Subroutine (Program) | No | No | No |
| Function Block (FB) | Yes | Yes | No |
| Function (FUN) | No | No | Yes (1 value) |
Typical applications of functions
- Logical validations
- Permission comparisons
- Mathematical calculations
- Starting and interlocking conditions
- Reusable rules
Creating a Function
To create a function:
- Right-click.
- Select Add Object.
- Choose POU.
- In the Type field, select Function.
- In the Name field, type fun_direct_start.
- Set the return type to BOOL.
- Choose the implementation language LD – Ladder Diagram.
- Confirm the creation.
After that, the function will be open for editing.
Figure 2. Creating a Function-Type POU
Function Structure and Operation
A function has:
- Inputs (VAR_INPUT) → parameters used in processing
- Return value → logical or numeric result of the function
Differently from Function Blocks:
- The function does not maintain state
- Does not require an instance variable
- Always returns a single value
Developing the function logic
Within the function fun_partida_direta:
- Create the necessary input variables (e.g., start button, stop button, permissions)
- Develop the Ladder logic that represents the direct start condition
- The result of the logic will be directly assigned to the function's return value
Conceptual example:
- If all permissions are correct → return TRUE
- Otherwise → return FALSE
Figure 3. Creating the function logic
Calling the Function in the logic
To use the function:
- Open UserPrg or another POU.
- Insert an Empty Box with EN/ENO or use the function directly in a logic block.
- Type the name fun_partida_direta.
- Parameterize the function inputs.
- Use the returned value (BOOL):
- In series with contacts
- To activate a coil
- As a condition in other logic blocks
Since the function has no state, it is evaluated every time it is called.
Figure 4. Function call via UserPrg
Finalization and testing
After implementing the function:
- Download the application.
- Put the CPU in RUN mode.
- Test the function inputs.
- Observe the return value being used in the main logic.
The use of Functions allows you to create cleaner, more reusable and organized code, being ideal for encapsulating simple rules and avoiding repetition of logic in the project.
Figure 5. Function Operation
Comments
0 comments
Please sign in to leave a comment.