The objective of this tutorial is to explain how to create function block 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
Unlike a subroutine (program), a Function Block (FB) has internal state and requires the creation of an instance variable to function correctly, in the same way as timers and counters.
This feature is ideal when you want to reuse the same logic multiple times, maintaining independent states.
Main Difference Between Subroutine and Function Block
| Subroutine (Program) | Function Block |
| Does not need an instance variable | Requires an instance variable |
| Does not maintain internal state | Maintains internal state |
| Executes whenever called | Executes per instance |
| Single use per call | Can have multiple instances |
Creating a Function Block
To create a function block:
- Right-click.
- Select Add Object.
- Choose POU.
- In the Type field, select Function Block.
- Define a name for the function block.
- Choose the implementation language as LD – Ladder Diagram.
- Confirm the creation.
After that, the Function Block will be open for editing.
Figure 2. Creating a Function Block POU
Developing the logic in the Function Block
Within the Function Block:
- Develop the desired logic (e.g., contacts, coils, comparisons, etc.)
- Declare the input (VAR_INPUT), output (VAR_OUTPUT), and, if necessary, internal (VAR) variables
The block does not execute on its own—it needs to be instantiated and called.
Figure 3. Creating the logic of the function block
Calling the Function Block in the logic
To use the Function Block:
- Open UserPrg or another POU.
- Create a new network.
- Insert an Empty Box with EN/ENO.
- Click inside the box.
- Type the name of the Function Block.
Unlike a subroutine, simply calling the name is not enough.
Creating an instance variable (required)
For the Functional Block to function correctly, it is necessary to create an instance variable, similar to what occurs with TON, TOF, CTU, CTD, etc.
Example:
fb_control : FB_Control;This variable:
- Stores the internal state of the block
- Must be associated with the block call in the logic
Using multiple instances of the same block
A great advantage of the Functional Block is the possibility of using the same block multiple times in the same project:
Example:
fb_motor1 : FB_Control;
fb_motor2 : FB_Control;Each variable represents an independent instance, with its own states, even using the same code.
This is exactly the same concept applied to timers and counters.
Figure 4. Function block call via UserPrg
Finalization and testing
After configuring:
- Download the application.
- Put the CPU in RUN mode.
- Test each instance separately.
- Note that each block responds independently.
The use of Function Blocks is essential for creating reusable, organized, and scalable logic, being one of the most important practices in professional Ladder programming.
Figure 5. Subroutine Operation
Comments
0 comments
Please sign in to leave a comment.