====== Simatic S7 SCL reference: Counters ====== Back to reference overview: [[de:s7:scl_reference|Simatic S7 SCL reference with examples]] ===== IEC Counters ===== Counters - similar to IEC times - can be called in two ways, embedded or with a separate DB. I don't feel like describing this separately, please take a look at the times: [[de:s7:scl_reference_timers#iec_timers|IEC Timers]]. ===== CTU: Count up ===== {{anchor:ctu}} The function that only counts upwards. The CTU counts the positive edges of the "CU:" parameter. As soon as it reaches the value of "PV" with the calculation, the output "Q" changes to "TRUE". The function continues to count. The function can be restarted with the "R" input. **CTU types:** * CTU_SINT / CTU_USINT: the calculation goes with the variable types [[de:s7:tia_datatypes#sint|SINT]], [[de:s7:tia_datatypes#usint|USINT]] * CTU_INT / CTU_UINT: the calculation goes with the variable types [[de:s7:tia_datatypes#int|INT]], [[de:s7:tia_datatypes#uint|UINT]] * CTU_DINT / CTU_UDINT: the calculation goes with the variable types [[de:s7:tia_datatypes#dint|DINT]], [[de:s7:tia_datatypes#udint|UDINT]] * CTU_LINT / CTU_ULINT: the calculation goes with the variable types [[de:s7:tia_datatypes#lint|LINT]], [[de:s7:tia_datatypes#ulint|ULINT]] (only on S7-1500 PLC) \\ \\ |< 100% 15% 15% 15%>| |[[de:s7:tia_knowhow#fb|_FB_]] **CTU (**| | | | | |**CU:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Counter signal:** The function monitors the positive edge of this binary signal. The counter counted, if signal change from "FALSE" to "TRUE".| | |**R:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Reset:** Reset CV value.| | |**PV:** [[de:s7:tia_datatypes#int|INT]] **,** |input|**Set value to Q:** Value at which the Q output is set| | |**Q:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|output|**Result:** When the counter reaches "PV".| | |**CV:** \\ [[de:s7:tia_datatypes#int|INT]] / \\ [[de:s7:tia_datatypes#char|CHAR]] / \\ [[de:s7:tia_datatypes#wchar|WCHAR]] / \\ [[de:s7:tia_datatypes#date|DATE]] |output|**Current counter value**| |**);**|||| ==== Example ==== {{:de:s7:ctu_example.png|CTU example}} The example program below can be downloaded here: {{ :de:s7:ctu_example.scl |ctu_example.scl}}. \\ \\ Here is a description of how to import the downloaded program to the TIA Portal: [[de:s7:tia_knowhow#import|Import source code to the TIA portal]]. ===== CTD: Count down ===== {{anchor:ctd}} The function that only counts downwards. The CTD counts the positive edges of the "CD" parameter, the current counter value of the CV parameter is decremented by one. The function can be restarted with "LD" input, in this case, the countdown starts again from the "PV" value. If the current counter value is less than or equal to zero, the "Q" parameter is set to signal state "TRUE". **CTD types:** * CTD_SINT / CTD_USINT: the calculation goes with the variable types [[de:s7:tia_datatypes#sint|SINT]], [[de:s7:tia_datatypes#usint|USINT]] * CTD_INT / CTD_UINT: the calculation goes with the variable types [[de:s7:tia_datatypes#int|INT]], [[de:s7:tia_datatypes#uint|UINT]] * CTD_DINT / CTD_UDINT: the calculation goes with the variable types [[de:s7:tia_datatypes#dint|DINT]], [[de:s7:tia_datatypes#udint|UDINT]] * CTD_LINT / CTD_ULINT: the calculation goes with the variable types [[de:s7:tia_datatypes#lint|LINT]], [[de:s7:tia_datatypes#ulint|ULINT]] (only on S7-1500 PLC) \\ \\ |< 100% 15% 15% 15%>| |[[de:s7:tia_knowhow#fb|_FB_]] **CTD (**| | | | | |**CD:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Counter signal:** The function monitors the positive edge of this binary signal. The counter downcounted, if signal change from "FALSE" to "TRUE".| | |**LD:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Reset:** Load PV value to CV. (Counter newstart)| | |**PV:** [[de:s7:tia_datatypes#int|INT]] **,** |input|**Downcounter start value:** The value from which the counter counts down. It can be reloaded with the LD parameter.| | |**Q:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|output|**Result:** If the current counter value is less than or equal to zero, the parameter is set to signal state "TRUE".| | |**CV:** \\ [[de:s7:tia_datatypes#int|INT]] / \\ [[de:s7:tia_datatypes#char|CHAR]] / \\ [[de:s7:tia_datatypes#wchar|WCHAR]] / \\ [[de:s7:tia_datatypes#date|DATE]] |output|**Current counter value**| |**);**|||| ==== Example ==== {{:de:s7:ctd_example.png|CTD example}} The example program below can be downloaded here: {{ :de:s7:ctd_example.scl |ctd_example.scl}}. \\ \\ Here is a description of how to import the downloaded program to the TIA Portal: [[de:s7:tia_knowhow#import|Import source code to the TIA portal]]. ===== CTUD: Count up ===== {{anchor:ctud}} The function that counts up- and downwards. The CTUD counts up the positive edges of the "CU" input, and counts down with "CD" input. As soon as it reaches the value of "PV" with the calculation, the output "QU" changes to "TRUE". The function continues to count. The function can be restarted with the "R" input. The downcounts function can be restarted with "LD" input, in this case, the countdown starts again from the "PV" value. If the current counter value is less than or equal to zero, the "QD" parameter is set to signal state "TRUE". **CTUD types:** * CTUD_SINT / CTUD_USINT: the calculation goes with the variable types [[de:s7:tia_datatypes#sint|SINT]], [[de:s7:tia_datatypes#usint|USINT]] * CTUD_INT / CTUD_UINT: the calculation goes with the variable types [[de:s7:tia_datatypes#int|INT]], [[de:s7:tia_datatypes#uint|UINT]] * CTUD_DINT / CTUD_UDINT: the calculation goes with the variable types [[de:s7:tia_datatypes#dint|DINT]], [[de:s7:tia_datatypes#udint|UDINT]] * CTUD_LINT / CTUD_ULINT: the calculation goes with the variable types [[de:s7:tia_datatypes#lint|LINT]], [[de:s7:tia_datatypes#ulint|ULINT]] (only on S7-1500 PLC) \\ \\ |< 100% 15% 15% 15%>| |[[de:s7:tia_knowhow#fb|_FB_]] **CTUD (**| | | | | |**CU:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Counter up signal:** The function monitors the positive edge of this binary signal. The counter counted up, if signal change from "FALSE" to "TRUE".| | |**CD:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Counter down signal:** The function monitors the positive edge of this binary signal. The counter downcounted, if signal change from "FALSE" to "TRUE".| | |**R:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Reset upcounter:** Reset upcounter CV value.| | |**LD:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|input|**Reset downcounter:** Load PV value to CV. (Downcounter newstart)| | |**PV:** [[de:s7:tia_datatypes#int|INT]] **,** |input|**Set value to Q / startpoint for downcounter** Value at which the Q output is set / The value from which the counter counts down. It can be reloaded with the LD parameter.| | |**QU:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|output|**Result:** When the counter reaches "PV".| | |**QD:** [[de:s7:tia_datatypes#bool|BOOL]] **,**|output|**Result:** If the current counter value is less than or equal to zero, the parameter is set to signal state "TRUE".| | |**CV:** \\ [[de:s7:tia_datatypes#int|INT]] / \\ [[de:s7:tia_datatypes#char|CHAR]] / \\ [[de:s7:tia_datatypes#wchar|WCHAR]] / \\ [[de:s7:tia_datatypes#date|DATE]] |output|**Current counter value**| |**);**||||