You are on page 1of 5

S7-200 Instruction Set

Chapter 6

Counter Instructions
SIMATIC Counter Instructions
Count Up Counter
The Count Up instruction (CTU) counts up from the current value
each time the count up (CU) input makes the transition from off to
on. When the current value Cxx is greater than or equal to the
preset value PV, the counter bit Cxx turns on. The counter is reset
when the Reset (R) input turns on, or when the Reset instruction is
executed. The counter stops counting when it reaches the
maximum value (32,767).
STL operation :
H Reset input: Top of stack
H Count Up input: Value loaded in the second stack location

Count Down Counter


The Count Down instruction (CTD) counts down from the current
value of that counter each time the count down (CD) input makes
the transition from off to on. When the current value Cxx is equal
to 0, the counter bit Cxx turns on. The counter resets the counter
bit Cxx and loads the current value with the preset value PV when
the load input LD turns on. The counter stops upon reaching zero,
and the counter bit Cxx turns on.
STL operation:
H Load input: Top of stack
H Count Down input: Value loaded in the second stack location.

Count Up/Down Counter


The Count Up/Down instruction (CTUD) counts up each time the count up (CU) input makes the
transition from off to on, and counts down each time the count down (CD) input makes the transition
from off to on. The current value Cxx of the counter maintains the current count. The preset value PV is
compared to the current value each time the counter instruction is executed.
Upon reaching maximum value (32,767), the next rising edge at the count up input causes the current
count to wrap around to the minimum value (--32,768). On reaching the minimum value (--32,768), the
next rising edge at the count down input causes the current count to wrap around to the maximum
value (32,767).
When the current value Cxx is greater than or equal to the preset value PV, the counter bit Cxx turns
on. Otherwise, the counter bit turns off. The counter is reset when the Reset (R) input turns on, or when
the Reset instruction is executed. The CTUD counter stops counting when it reaches PV.
STL operation:
H Reset input: Top of stack
H Count Down input: Value loaded in the second stack location
H Count Up input: Value loaded in the third stack location
Table 6-21

Valid Operands for the SIMATIC Counter Instructions

Inputs/Outputs

Data Types

Operands

Cxx

WORD

Constant (C0 to C255)

CU, CD, LD, R

BOOL

I, Q, V, M, SM, S, T, C, L, Power Flow

PV

INT

IW, QW, VW, MW, SMW, SW, LW, T, C, AC, AIW, *VD, *LD, *AC, Constant

107

S7-200 Programmable Controller System Manual

Tip
Since there is one current value for each counter, do not assign the same number to more than one
counter. (Up Counters, Up/Down Counters, and Down counters with the same number access the
same current value.)
When you reset a counter using the Reset instruction, the counter bit is reset and the counter current
value is set to zero. Use the counter number to reference both the current value and the counter bit of
that counter.
Table 6-22

Operations of the Counter Instructions

Type

Operation

Counter Bit

Power Cycle/First Scan

CTU

CU increments the current value.

The counter bit turns on when:

Counter bit is off.

Current value continues to increment


until it reaches 32,767.

Current value >= Preset

Current value can be retained.1

CU increments the current value.


CD decrements the current value.

The counter bit turns on when:

Counter bit is off.

Current value >= Preset

Current value can be retained.1

The counter bit turns on when:

Counter bit is off.

Current value = 0

Current value can be retained.1

CTUD

Current value continues to increment


or decrement until the counter is reset.
CTD

CD decrements the current value until


the current value reaches 0.

You can select that the current value for the counter be retentive. See Chapter 4 for information about memory retention
for the S7-200 CPU.

Example: SIMATIC Count Down Counter Instruction


Network 1

LD
LD
CTD

//Count down counter C1 current value counts from 3 to 0


//with I0.1 off,
//I0.0 Off--on decrements C1 current value
//I0.1 On loads countdown preset value 3
I0.0
I0.1
C1, +3

Network 2
LD
=

//C1 bit is on when counter C1 current value = 0


C1
Q0.0

Timing Diagram
I0.0 Down
I0.1 Load
3

2
1

C1 (current)
C1 (bit) Q0.0

108

S7-200 Instruction Set

Chapter 6

Example: SIMATIC Count Up/Down Counter Instruction


Network 1

LD
LD
LD
CTUD

//I0.0 counts up
//I0.1 counts down
//I0.2 resets current value to 0
I0.0
I0.1
I0.2
C48, +4

Network 2
LD
=

Timing Diagram

//Count Up/Down counter C48 turns on C48 bit


//when current value >= 4
C48
Q0.0

I0.0 (up)
I0.1 (down)
I0.2 (reset)

C48 (current) 0

5
4
3

C48 (bit) Q0.0

109

S7-200 Programmable Controller System Manual

IEC Counter Instructions


Up Counter
The Count Up instruction (CTU) counts up from the current value
to the preset value (PV) on the rising edges of the Count Up (CU)
input. When the current value (CV) is greater than or equal to the
preset value, the counter output bit (Q) turns on. The counter
resets when the reset input (R) is enabled. The Up Counter stops
counting when it reaches the preset value.

Down Counter
The Count Down instruction (CTD) counts down from the preset
value (PV) on the rising edges of the Count Down (CD) input.
When the current value (CV) is equal to zero, the counter output
bit (Q) turns on. The counter resets and loads the current value
with the preset value when the load input (LD) is enabled. The
Down Counter stops counting when it reaches zero.

Up/Down Counter
The Count Up/Down instruction (CTUD) counts up or down from
the current value (CV) on the rising edges of the Count Up (CU) or
Count Down (CD) input. When the current value is equal to preset,
the up output (QU) turns on. When the current value is equal to
zero, the down output (QD) turns on. The counter loads the current
value with the preset value (PV) when the load (LD) input is
enabled. Similarly, the counter resets and loads the current value
with 0 when the reset (R) is enabled. The counter stops counting
when it reaches preset or 0.
Table 6-23

Valid Operands for the IEC Counter Instructions

Inputs/Outputs

Data Types

Operands

Cxx

CTU, CTD, CTUD

Constant (C0 to C255)

CU, CD, LD, R

BOOL

I, Q, V, M, SM, S, T, C, L, Power Flow

PV

INT

IW, QW, VW, MW, SMW, SW, LW, AC, AIW, *VD, *LD, *AC, Constant

Q, QU, QD

BOOL

I, Q, V, M, SM, S, L

CV

INT

IW, QW, VW, MW, SW, LW, AC, *VD, *LD, *AC

Tip
Since there is one current value for each counter, do not assign the same number to more than one
counter. (Up Counters, Down Counters, and Up/Down Counters access the same current value.)

110

S7-200 Instruction Set

Chapter 6

Example: IEC Counter Instructions


Timing Diagram
I4.0
CU -- Up
I3.0
CD -- Down
I2.0
R -- Reset
I1.0
LD -- Load
4

VW0
CV -Current Value

3
2

4
3

3
2

1
0

Q0.0
QU -- Up

Q0.1
QD -- Down

111

You might also like