The objective of this tutorial is to explain how to use the CTU function block, which is an incremental counter, 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
CTU Counter (Counter Up)
The CTU is a counter of the counting-up type. It is used when you want to count how many times an event occurs, incrementing the count value with each pulse received at the input.
CTU Functionality
The CTU works as follows:
- On each rising edge at the CU input, the counter increments the current value.
- When the counted value (CV – Current Value) reaches or exceeds the value configured in PV – Preset Value, the Q output is activated.
- The counter maintains the counted value, even if the input returns to FALSE.
- The R (Reset) input resets the counter, returning CV = 0 and Q = FALSE.
The CTU does not count levels; it counts events (pulses).
Inserting the CTU into the logic
The CTU block is a function block, therefore it cannot be inserted directly as a contact or coil.
To add it to the logic:
- Open the Toolbox.
- Access Function Blocks.
- Select CTU.
- Drag the block to the Ladder logic.
Figure 2. Inserting the CTU block
Declaration of the counter variable (required)
Because it is a function block, the CTU requires an instance variable to store its internal state.
Example of naming convention:
ctu_0 : CTU;Important:
This rule applies to all function blocks in MasterTool IEC XE.
Without this variable, the counter will not function correctly.
Figure 3. Declaration of the CTU block variable
CTU Inputs and Outputs
The CTU block has the following variables:
Inputs
- CU (BOOL): Count input. Each rising edge increments the counter.
- RESET (BOOL): Reset input. When TRUE, resets the counter.
- PV (INT / DINT): Preset value. Defines the number of events to be counted.
PV can be configured with either a constant value or a variable, depending on the application's needs.
Outputs
- Q (BOOL): Turns to TRUE when CV ≥ PV.
- CV (INT / DINT): Current count value.
Using Variables in CTU Parameters
- The input parameters (CU, RESET, and PV) must be instantiated, as they power the counter's operation.
- The output parameters (Q and CV) do not necessarily require the creation of variables, since their values can be viewed directly in the block itself during the logic's execution.
Creating variables for the outputs is optional and recommended when you want to reuse or monitor these values at other points in the program.
Using PV as a Constant or Variable
- PV as a constant: Ideal when the number of counts is fixed.
- PV as a variable: Allows changing the count value at runtime, via logic or a variable table.
Good practice: use variables only when there is a real need for dynamic adjustment.
Figure 4. CTU Parameterization
Practical Application
After assembling the logic with the CTU:
- Download the application.
- Put the CPU in RUN mode.
- Trigger pulses on the CU input.
- Observe:
- The CV value incrementing with each pulse.
- The Q output activating when the PV value is reached.
- Press RESET to reset the counter.
The CTU is essential in applications involving counting parts, cycles, events, or actuations, ensuring precise and predictable control within Ladder logic.
Figure 5. CTU Block Operation
Comments
0 comments
Please sign in to leave a comment.