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

CTUD

Function Block Name

CTUD

Description

CTUD is an up and down counter function block and it is typically used to indicate when a count has decreased to 0 or increased to a specified maximum number.

Arguments

CU {BOOL}

Determines when the counter increases. The counter will go up by 1 when the CD input changes from False to True. The counter does not increase again until the CD input changes from True to False and then back to True (although it may decrease, if the CD input changes from False to True). The counter is only affected by the CD 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 CD input continues to change from False to True.

CD {BOOL}

Determines when the counter decreases. The counter will go down by 1 when the CD input changes from False to True. The counter does not decrease again until the CD input changes from True to False and then back to True (although it may increase if the CU input changes from False to True). The counter is only affected by the CD input changing from False to True.

If the counter reaches 0, it will no longer decrease, even if the CD 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 and CD values have changed.

LD {BOOL}

Indicates whether there is a Load. If the LD value is True, the CV output is set to the same value as PV irrespective of the count; if the LD value is False, the CV output has a value that corresponds to the number of times the CU and CD values have changed.

PV {INT}

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

Returns

QU {BOOL}

The QU output defines whether the up 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 QU output is True and the counting up stops.

QD {BOOL}

The QD output defines whether the down count is active or inactive. If the CV output is greater than 0, the QD output is False and the counting is active; if the CV output reaches 0, the QD output is True and the counting down 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 and decreases by 1 when the CD input changes from False to True.

The CV is set to 0 when the R input is True and is set to the same number as the PV input when the LD input is True.

Example - CTUD Function Block Diagram:

In this Function Block Diagram, we assume that the CV counter begins at 0 following the R input being True. The R input then changes to False and the CU input changes from False to True (as shown). This causes the CV value to increase by 1. The QU and QD outputs remain as False as the CV value is greater than 0 and less than the PV value.

The CD input changes from False to True. This makes the CV counter decrease by 1 and so it returns to 0. As the CV value is 0, the QD output changes to True.

Example - CTUD ST Program:

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

  • VAR
    • CU_IN AT %M(.Digital Point1): BOOL;
    • CD_IN AT %M(.Digital Point2): BOOL;
    • R_IN AT %M(.Digital Point3): BOOL;
    • LD_IN AT %M(.Digital Point4): BOOL;
  • END_VAR
  • VAR
    • PV_IN: INT;
    • QU_OUT: BOOL;
    • QD_OUT: BOOL;
    • CV_OUT: INT;
    • FB : CTUD;
  • END_VAR
  • FB( CU := CU_IN, CD := CD_IN, R := R_IN, LD := LD_IN, PV := 10, QU => QU_OUT, QD => QD_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 CTUD 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 CD input variable is named CD_IN). In this example, the direct variables for the CU, CD, R and LD inputs (CU_IN, CD_IN, R_IN and LD_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, CD := CD_IN, R := R_IN, LD := LD_IN, PV := 10, QU => QU_OUT, QD => QD_OUT, CV => CV_OUT );

So, the ST Program uses:

  • The CU_IN input as the CU input
  • The CD_IN input as the CD input
  • The R_IN input as the R input
  • The LD_IN input as the LD input
  • 10 as the PV input.

The FB declaration sets the ST program to use a CTUD type of function block.

For the outputs, the ST program uses the QU_OUT variable for the QU output, the QD_OUT variable for the QD output, and the CV_OUT variable for the CV output.


ClearSCADA 2015 R2