The objective of this tutorial is to explain how to use the CTUD function block, which is an incremental and decremental 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
CTUD Counter (Counter Up/Down)
The CTUD is a counter that combines the two functions in a single block:
- Counting up (UP)
- Counting down (DOWN)
It is used when the application needs to increment and decrement the same value, keeping everything centralized in a single counter.
CTUD Characteristic (Insertion Method)
Unlike CTU and CTD, CTUD cannot be inserted directly from the function block list.
It must be called through an empty box with EN/ENO, where the block will be instantiated manually.
This characteristic exists because CTUD is a more complete block, with multiple control inputs.
CTUD Functionality
The operation of CTUD is as follows:
- On each rising edge of the CU input, the counter increments the value.
- On each rising edge of the CD input, the counter decrements the value.
- The initial count value is defined in PV (Preset Value).
- When:
- CV ≥ PV, the QU output is activated.
- CV = 0, the QD output is activated.
- The R (Reset) input resets the counter.
- The LD (Load) input loads the PV value into CV.
Inserting CTUD into the Logic
To add CTUD:
- Create a new network.
- Insert an empty box with EN/ENO, in one of the following ways:
- Through the Toolbox → General → Box with EN/ENO
- Or by right-clicking on the line and selecting Empty Box with EN/ENO
- After inserting the box, associate the box with the CTUD block by clicking on the internal "???" of the box.
Figure 2. Inserting the CTUD block
Declaration of the counter variable (required)
Like other counters, CTUD is a function block and requires an instance variable.
Example:
ctud_0 : CTUD;Important:
Without this variable, the counter will not work, as it stores the internal state of the count.
Figure 3. CTUD Block Variable Declaration
CTUD Inputs and Outputs
Inputs
- CU (BOOL): rising edge count
- CD (BOOL): falling edge count
- RESET (BOOL): counter reset
- LOAD (BOOL): loads PV into CV
- PV (INT / DINT): preset value
PV can be a constant or a variable, depending on the application's needs.
Outputs
- QU (BOOL): activates when CV ≥ PV
- QD (BOOL): activates when CV = 0
- CV (INT / DINT): current count value
Use of variables in CTUD parameters
- The input parameters (CU, CD, RESET, LOAD, and PV) must be instantiated, as they power the block's operation.
- The output parameters (QU, QD, and CV) do not necessarily require variables, as they can be viewed directly in the block during logic execution.
Using PV as a Constant or Variable
- PV as a constant: Ideal when the initial count value is fixed.
- PV as a variable: Allows changing the initial value at runtime, via logic or a variable table.
Figure 4. CTUD Parameterization
Practical Application
After setting up the logic with CTUD:
- Download the application.
- Put the CPU in RUN mode.
- Activate:
- CU to increment
- CD to decrement
- Observe:
- The CV value varying up and down
- QU activating at the upper limit
- QD activating when it reaches zero
CTUD is ideal for applications that require bidirectional counting control, such as inventory control, positioning, reversible cycles, and forward/reverse systems, keeping the logic more compact and organized.
Figure 5. CTUD Block Operation
Comments
0 comments
Please sign in to leave a comment.