Down counter
The function block CTD (count down) allows you to program counting procedures.
Input
count down
the value 1 is subtracted from the current value CV for each rising edge detected at CD, except when LD is set or CV has reached the value zero.
Load
with LD the counter state is reset to PV
Preset value
is the value subjected to subtraction during the first counting procedure
Output
signal output
is set if CV = zero
Current value
contains the current subtraction result (CV = current value)
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
initialize_count: BOOL:=TRUE;
(*for setting the current value
to starting value*)
count: BOOL:=FALSE;
(*1 is subtracted from the current
value for each leading edge
detected at count*)
load: BOOL:=FALSE;
(*sets the curr_value back to
set_value, if TRUE*)
set_value: INT:=2;
(*the starting value*)
output_value: BOOL:=FALSE;
(*is set, if the current value is
zero*)
curr_value: INT:=0;
(*the current_value*)
ctd_1: CTD;
(*instance of the counter*)
END_VAR
If set is set (status = TRUE), the preset_value (PV) is loaded in the current_value (CV). The value 1 will be subtracted from the current_value each time a rising edge is detected at clock. This procedure will be repeated until the current_value is greater than/equal to zero. Then, signal_output will be set.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 7 ;
NETWORK_BODY
B(B_FB,CTD!,copy_name,8,2,13,7,,?BCD?BLD?BPV?AQ?ACV);
B(B_VARIN,,output_value,6,5,8,7,);
B(B_VAROUT,,current_value,13,4,15,6,);
B(B_CONTACT,,set,3,4,5,6,);
B(B_CONTACT,,clock,3,2,5,4,);
B(B_COIL,,signal_output,20,3,22,5,);
L(7,4,8,4);
L(1,5,3,5);
L(5,5,8,5);
L(1,3,3,3);
L(5,3,7,3);
L(7,3,7,4);
L(13,4,20,4);
L(1,0,1,7);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF set THEN (* first cycle *)
load:=TRUE; (* load has to be TRUE,
to set current_value to output_value *)
clock:=FALSE;
END_IF;
copy_name(CD:= clock, LOAD:= set, PV:= output_value, Q=> signal_output, CV=> current_value);
load:=FALSE; (* now current_value got the right value, load doesn't need to be *)
(* TRUE any longer *);
If you want to call the function block in an Instruction List editor (IL), enter the following: