Description
You can use the "Count up and down" instruction to increment and decrement the counter value at the CV output. If the signal state at the CU input changes from "0" to "1" (positive signal edge), the current counter value is incremented by one and stored at the CV output. If the signal state at the CD input changes from "0" to "1" (positive signal edge), the current counter value at the CV output is decremented by one. If there is a positive signal edge at the CU and CD inputs in one program cycle, the current counter value at the CV output remains unchanged.
The counter value can be incremented until it reaches the high limit of the data type specified at the CV output. When the high limit is reached, the counter value is no longer incremented on a positive signal edge. The counter value is no longer decremented once the low limit of the specified data type has been reached.
When the signal state at the LD input changes to "1", the counter value at the CV output is set to the value of the PV parameter and stored in a edge memory bit. As long as the LD input has signal state "1", the signal state at the CU and CD inputs has no effect on the instruction.
The counter value is set to "0" and stored in an edge memory bit when the signal state at input R changes to "1". As long as the R input has signal state "1", a change in the signal state of the CU, CD and LD inputs has no effect on the "Count up and down" instruction.
You can query the status of the up counter at the QU output. If the current counter value is greater than or equal to the value of the PV parameter, the QU output is set to signal state "1". In all other cases, the QU output has signal state "0". You can also specify a constant for the PV parameter.
You can query the status of the down counter at the QD output. If the current counter value is less than or equal to zero, the QD output is set to signal state "1". In all other cases, the QD output has signal state "0".
|
Note Only use a counter at a single point in the program to avoid the risk of counting errors. |
Each call of the "Count up and down" instruction must be assigned an IEC counter in which the instruction data is stored. An IEC counter is a structure with one of the following data types:
For S7-1200 CPU
|
Data block of system data type IEC_<Counter> (Shared DB) |
Local tag |
|---|---|
|
|
For S7-1500 CPU
|
Data block of system data type IEC_<Counter> (Shared DB) |
Local tag |
|---|---|
|
|
You can declare an IEC counter as follows:
-
Declaration of a data block of system data type IEC_<Counter> (for example, "MyIEC_COUNTER")
-
Declaration as a local tag of the type CTUD_<Data type> or IEC_<Counter> in the "Static" section of a block (for example #MyIEC_COUNTER)
When you set up the IEC counter in a separate data block (single instance), the instance data block is created by default with "optimized block access" and the individual tags are defined as retentive. For additional information on setting retentivity in an instance data block, refer to "See also".
When you set up the IEC counter as local tag (multi-instance) in a function block with "optimized block access", it is defined as retentive in the block interface.
The execution of the "Count up and down" instruction requires a preceding logic operation. It can be placed within or at the end of the network.
Parameters
The following table shows the parameters of the "Count up and down" instruction:
|
Parameter |
Declaration |
Data type |
Memory area |
Description |
|
|---|---|---|---|---|---|
|
S7-1200 |
S7-1500 |
||||
|
CU |
Input |
BOOL |
I, Q, M, D, L or constant |
I, Q, M, D, L or constant |
Count up input |
|
CD |
Input |
BOOL |
I, Q, M, D, L or constant |
I, Q, M, D, L or constant |
Count down input |
|
R |
Input |
BOOL |
I, Q, M, D, L, P or constant |
I, Q, M, T, C, D, L, P or constant |
Reset input |
|
LD |
Input |
BOOL |
I, Q, M, D, L, P or constant |
I, Q, M, T, C, D, L, P or constant |
Load input |
|
PV |
Input |
Integers |
I, Q, M, D, L, P or constant |
I, Q, M, D, L, P or constant |
Value at which the QU output is set / value to which the CV output is set when LD = 1. |
|
QU |
Output |
BOOL |
I, Q, M, D, L |
I, Q, M, D, L |
Up-counter status |
|
QD |
Output |
BOOL |
I, Q, M, D, L |
I, Q, M, D, L |
Down-counter status |
|
CV |
Output |
Integers, CHAR, WCHAR, DATE |
I, Q, M, D, L, P |
I, Q, M, D, L, P |
Current counter value |
You can select the data type of the instruction from the "???" drop-down list of the instruction box.
Example
The following example shows how the instruction works:
If the signal state at the "TagIn_CU" or "TagIn_CD" input changes from "0" to "1" (positive signal edge), the "Count up and down" instruction is executed. When there is a positive signal edge at the "TagIn_CU" input, the current counter value is incremented by one and stored at the "Tag_CV" output. When there is a positive signal edge at the "TagIn_CD" input, the counter value is decremented by one and stored at the "Tag_CV" output. When there is a positive edge at the CU input, the counter value is incremented until it reaches the high limit (INT = 32767). If input CD has a positive signal edge, the counter value is decremented until it reaches the low limit (INT = -32768).
The "TagOut_GU" output has signal state "1" as long as the current counter value is greater than or equal to the value at the "Tag_PV" input. In all other cases, the "TagOut_QU" output returns the signal state "0".
The "TagOut_QD" output has signal state "1" as long as the current counter value is less than or equal to "0". In all other cases, the "TagOut_QD" output has signal state "0".