Simatic S7 SCL reference: Counters
Back to reference overview: 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: IEC Timers.
CTU: Count up
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:
_FB_ CTU ( | |||
CU: 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: BOOL , | input | Reset: Reset CV value. | |
PV: INT , | input | Set value to Q: Value at which the Q output is set | |
Q: BOOL , | output | Result: When the counter reaches „PV”. | |
CV: INT / CHAR / WCHAR / DATE | output | Current counter value | |
); |
Example
The example program below can be downloaded here: ctu_example.scl.
Here is a description of how to import the downloaded program to the TIA Portal: Import source code to the TIA portal.
CTD: Count down
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:
_FB_ CTD ( | |||
CD: 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: BOOL , | input | Reset: Load PV value to CV. (Counter newstart) | |
PV: INT , | input | Downcounter start value: The value from which the counter counts down. It can be reloaded with the LD parameter. | |
Q: BOOL , | output | Result: If the current counter value is less than or equal to zero, the parameter is set to signal state „TRUE”. | |
CV: INT / CHAR / WCHAR / DATE | output | Current counter value | |
); |
Example
The example program below can be downloaded here: ctd_example.scl.
Here is a description of how to import the downloaded program to the TIA Portal: Import source code to the TIA portal.
CTUD: Count up
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:
_FB_ CTUD ( | |||
CU: 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: 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: BOOL , | input | Reset upcounter: Reset upcounter CV value. | |
LD: BOOL , | input | Reset downcounter: Load PV value to CV. (Downcounter newstart) | |
PV: 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: BOOL , | output | Result: When the counter reaches „PV”. | |
QD: BOOL , | output | Result: If the current counter value is less than or equal to zero, the parameter is set to signal state „TRUE”. | |
CV: INT / CHAR / WCHAR / DATE | output | Current counter value | |
); |