You are here: Core Reference > Coding > Logic > CTU

CTU

Function Block Name

CTU

Description

CTU is an up counter function block and it is typically used to indicate when a count has increased to a maximum value.

Arguments

CU {BOOL}

Determines when the counter increases. The counter will go up by 1 when the CU input changes from False to True. The counter does not change again until the CU input changes from True to False and then back to True. The counter is only affected by the CU input changing from False to True.

If the counter reaches the number specified by the PV input, the counter will no longer increase, even if the CU input continues to change from False to True.

R {BOOL}

Indicates whether there is a reset. If the R value is True, the CV output is set to 0 irrespective of the count; if the R value is False, the CV output has a value that corresponds to the number of times the CU value has changed.

PV {INT}

This is an integer input that defines the maximum number for the counter.

Returns

Q {BOOL}

The Q output defines whether the count is active or inactive. If the CV output is less than the PV number, the Q output is False and the counting is active; if the CV output reaches the PV number, the Q output is True and the counting stops.

CV {INT}

The CV output is the value of the counter. It increases by 1 each time the CU input changes from False to True.

The CV output begins at 0. It can increase until it reaches the PV value, at which point the Q output becomes True and the counting stops.

Example - CTU Function Block Diagram:

In this Function Block Diagram, the R output begins as True, and so the CV output is set to 0. The R output then becomes False and so the counting begins (as the CV output can now be affected by the CU input). The CU input changes from False to True. This causes the CV output to increase by 1 as the CU input has changed from False to True once. The Q output remains as False as the CV output is less than the PV input. The Q output will only change to True when the CU input has changed from False to True a further 9 times (increasing the CV output to 10). When the Q output changes to True, the counting stops.

Example - CTU ST Program:

The syntax for entering a CTU function block in an ST program is:

  • VAR
    • CU_IN AT %M(.Digital Point1): BOOL;
    • R_IN AT %M(.Digital Point2): BOOL;
  • END_VAR
  • VAR
    • PV_IN: INT;
    • Q_OUT: BOOL;
    • CV_OUT: INT;
    • FB : CTU;
  • END_VAR
  • FB( CU := CU_IN, R := R_IN, PV := 10, Q => Q_OUT, CV => CV_OUT );

The direct variables are declared in a separate VAR block to the internal variables. The input and output variables for the CTU function block are also allocated names that are different to the names of the inputs and outputs in the function block as this makes it easier to understand the ST program (for example, the CU input variable is named CU_IN). In this example, the direct variables for the CU and R inputs (CU_IN and R_IN) reference digital points (the Boolean values for these inputs are taken from digital points in the database).

In the second VAR block, the internal variables are declared, including an FB variable that defines the type of function block.

The ST Program can then reference the variables as inputs and outputs for the function block:

  • FB( CU := CU_IN, R := R_IN, PV := 10, Q => Q_OUT, CV => CV_OUT );

So, the ST Program uses the CU_IN input as the CU input for the FB function block (which has already been declared as a CTU type). For the R input, it uses the R_IN variable, and for the PV input it uses the value 10. For its outputs, it uses the Q_OUT variable for the Q output and the CV_OUT variable for the CV output.


ClearSCADA 2015 R2