Up/down counter
The function block CTUD (count up/down) allows you to program counting procedures (up and down).
Count up (CU set to TRUE)
Counting up starts at zero until the maximum value 32767 is reached. Each rising edge at count up CU increases the value at current value CV by 1.
Count down (CD set to TRUE)
Counting down starts at zero until the minimum value -32768 is reached. Each rising edge at count down CD decreases the value at current value CV by 1.
Input
count up
the value 1 is added to the current CV for each rising edge detected at CU, except when R and/or LD is/are set.
count down
the value 1 is subtracted from the current CV for each rising edge detected at CD, except when R and/or LD is/are set
if CU and CD are simultaneously set to TRUE no counting operation takes place.
Reset
CV is reset to zero for each rising edge at R and counting stops. The output QU/QD is set to FALSE.
The next falling edge at R restarts counting.
Load
if LD is set, PV is loaded to CV and QU is set to TRUE. This, however, does not apply if R is set simultaneously. In this case, LD will be ignored.
Preset value
defines the preset value which is to be attained with the addition or subtraction
If no preset value is set or the preset value is zero, the output QU is set to TRUE immediately after counting starts.
Output
signal output - count up
is set to TRUE if CV is greater than/equal to PV
is set to FALSE if a rising edge is detected at R
signal output - count down
is set to TRUE if CV = zero
is set to FALSE if a rising edge is detected at R
Current value
is the addition/subtraction result
counting up:
If CV reaches the preset value PV, the output QU is set to TRUE, but counting continues until the maximum limit 32767 is reached.
counting down:
If CV reaches zero, the output QD is set to TRUE, but counting continues until the minimum limit -32768 is reached.
The value can be changed during counting operation by writing to the variable from the programming editor.
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
CTUD1: CTUD;
bCountUp: BOOL:=FALSE;
(*increments CV if TRUE*)
bCountDown: BOOL:=FALSE;
(*decrements CV if TRUE*)
bReset: BOOL:=FALSE;
(*sets CV to zero if TRUE*)
bLoad: BOOL:=FALSE;
(*sets CV to PV if TRUE*)
iPresetValue: INT:=3;
(*if CV reaches this value,
output QU is set*)
bUpCounting_PV_reached: BOOL:=FALSE;
(*is set, if CV reaches PV*)
bDownCounting_zero_reached: BOOL:=FALSE;
(*is set, if CV reaches zero*)
iCurrentValue: INT:=0;
(*contains the current value*)
END_VAR
Count up:
If bReset is set, the iCurrentValue (CV) will be reset to zero. If bCountUp is set, the value 1 is added to the iCurrentValue. This procedure is repeated for each rising edge detected at bCountUp until the iCurrentValue is greater than/equal to the iPresetValue. Then bUpCounting_PV_reached is set. The procedure is not conducted, if bReset and/or bLoad is/are set.
Count down:
If bReset is set (status = TRUE), the iPresetValue (PV = preset value) will be loaded into iCurrentValue. If bCountDown is set, the value 1 is subtracted from iPresetValue. This procedure is repeated at each rising edge until the iCurrentValue is smaller than/equal to zero. Then, bDownCounting_zero_reached is set to TRUE. The procedure will not be conducted, if bReset and/or bLoad is/are set. If CU and CD are set at the same time, no counting operation takes place.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 14 ;
NETWORK_BODY
B(B_CONTACT,,bCountUp,5,3,7,5,);
B(B_CONTACT,,bCountDown,5,5,7,7,);
B(B_FB,CTUD!,CTUD1,12,4,18,11,,?BCU?BCD?BR?BLD?BPV?AQU?AQD?ACV);
B(B_COIL,,bUpCounting_PV_reached,25,5,27,7,);
B(B_CONTACT,,bReset,5,7,7,9,);
B(B_COIL,,bDownCounting_zero_reached,25,8,27,10,);
B(B_CONTACT,,bLoad,5,9,7,11,);
B(B_VAROUT,,iCurrentValue,20,10,22,12,);
B(B_VARIN,,iPresetValue,7,11,9,13,);
L(1,0,1,14);
L(1,4,5,4);
L(7,4,10,4);
L(10,4,10,6);
L(1,6,5,6);
L(7,6,9,6);
L(10,6,12,6);
L(18,6,25,6);
L(9,6,9,7);
L(9,7,12,7);
L(18,7,20,7);
L(20,7,20,9);
L(1,8,5,8);
L(7,8,12,8);
L(18,8,19,8);
L(19,8,19,11);
L(9,9,12,9);
L(20,9,25,9);
L(9,9,9,10);
L(1,10,5,10);
L(7,10,9,10);
L(10,10,12,10);
L(10,10,10,12);
L(19,11,20,11);
L(9,12,10,12);
END_NETWORK_BODY
END_NET_WORK
END_BODY
CTUD1(CU := bCountUp, CD := bCountDown, R := bReset, LD := bLoad,
PV := iPresetValue, QU => bUpCounting_PV_reached,
QD => bDownCounting_zero_reached, CV => iCurrentValue);