PLCcheck

Migrating S5 Counter Programs (Z/ZV/ZR) to S7

How to migrate S5 counter programs to S7. Covers ZV (count up), ZR (count down), BCD format differences, S7 equivalents (S_CU, S_CD, CTU, CTD, CTUD), and common pitfalls.

·10 min read
S5S7counterZVZRCTUCTDCTUDBCDmigrationZähler

Diesen Artikel auf Deutsch lesen

Migrating S5 Counter Programs (Z/ZV/ZR) to S7

S5 counters use a BCD-encoded value with a range of 0–999. S7 offers both S5-compatible counters (S_CU, S_CD, S_CUD with BCD 0–999) and IEC counters (CTU, CTD, CTUD with INT range). The migration is straightforward for basic counting, but BCD handling and the "U Z n" query behavior require attention. This guide covers all aspects.

S5 Counter Overview

S5 provides two counter operations and several support operations:

OperationFunctionTrigger
ZV Z nCount up (increment by 1)Rising edge of VKE
ZR Z nCount down (decrement by 1)Rising edge of VKE
S Z nSet counter to preset value (from ACCU1, KZ format)VKE = TRUE
R Z nReset counter to 0VKE = TRUE
U Z nQuery: Is counter > 0? (boolean result in VKE)
L Z nLoad current counter value into ACCU1 (BCD format)
LC Z nLoad current counter value into ACCU1 (BCD, coded)

Counter range: 0 to 999 (BCD). The counter cannot go below 0 — ZR at 0 has no effect. The counter cannot exceed 999 — ZV at 999 has no effect.

Edge detection: ZV and ZR trigger on the rising edge of the VKE. If the VKE stays TRUE across multiple scan cycles, the counter increments/decrements only once (not continuously).

S7 Equivalents

Option A: S5-Compatible Counters (S_CU, S_CD, S_CUD)

These are S7 STL functions that behave identically to S5 counters. They use BCD format (0–999) and have the same edge behavior.

S5 AWLS7 STL FunctionDescription
ZV Z nS_CU (Count Up)Increment on rising edge
ZR Z nS_CD (Count Down)Decrement on rising edge
ZV + ZR on same ZS_CUD (Count Up/Down)Both directions

S7 STL example (S5-compatible):

A   I 0.0          // Count up input
L   C#100          // Preset value = 100
S_CU Z 1           // Count up

Advantage: Direct 1:1 migration. Minimal code changes. Disadvantage: Limited to 0–999 (BCD), not recommended for S7-1500.

IEC counters use INT (or DINT on S7-1500) instead of BCD. They require an instance data block (like IEC timers).

IEC CounterFunctionCount Range
CTUCount Up0 to 32,767 (INT) or higher (DINT)
CTDCount Down32,767 to 0
CTUDCount Up/DownFull INT range

S7 SCL example (IEC):

#Counter_Instance.CTU(CU := #Part_Sensor,    // Count input (rising edge)
                       R  := #Reset_Button,   // Reset to 0
                       PV := 100);            // Preset value

#Batch_Complete := #Counter_Instance.Q;       // TRUE when CV >= PV
#Current_Count := #Counter_Instance.CV;       // Current value (INT)

Advantage: Larger range, standard IEC behavior, works on all S7 platforms. Disadvantage: Requires instance DB, different interface than S5 counters.

Key Differences to Watch

1. BCD vs. Integer Format

S5 counters store and return values in BCD format. When you L Z n in S5, ACCU1 contains BCD (e.g., counter value 123 = hex 0x0123, not 0x007B).

In S7, the L instruction for S5-compatible counters still returns BCD. But IEC counters return plain INT. If your S5 program performs arithmetic on counter values (e.g., L Z 1 / +F / T MW 10), you must handle the BCD-to-INT conversion.

S5 code that needs attention:

L  Z 1            // BCD value in ACCU1
L  KF +10         // INT value in ACCU1
+F                // BCD + INT = WRONG result!

This is a bug in the S5 code (mixing BCD and INT). But it may "work" by coincidence for small values where BCD and binary representations happen to be similar (0–9). During migration, fix these bugs explicitly.

2. Preset Value Format

S5 uses KZ format for counter presets: L KZ 100 / S Z 1

S7 compatible counters use C# format: L C#100 / S C 1

IEC counters use plain INT: PV := 100

3. The "U Z n" Query

In S5, U Z n checks if the counter value is greater than 0. It returns TRUE if the count ≥ 1 and FALSE if count = 0.

S7 compatible counters: Same behavior with A C n (EN) / U Z n (DE). IEC counters: Use the Q output — Q is TRUE when CV ≥ PV (different semantics!).

This is a common migration error. S5 "U Z n" means "counter > 0". IEC CTU "Q" means "counter >= preset". These are not the same thing unless the preset is 1.

Fix: If the S5 program uses U Z n to check "counter not zero", use #Counter_Instance.CV > 0 in SCL instead of #Counter_Instance.Q.

Migration Decision

ScenarioRecommended Approach
Simple part counting, no arithmetic on valuesS5-compatible (S_CU/S_CD) — least effort
Counter values used in calculationsIEC (CTU/CTD) with INT values
Counter range > 999 neededIEC only (S5 counters limited to 999)
Migrating to S7-1500 (new project)IEC always — S5-compatible counters are legacy

How PLCcheck Pro Helps

Upload your S5 code →

Frequently Asked Questions

Can I use the same counter numbers in S7?

Yes. S7 supports counter numbers Z 0 to Z 255 (S7-300/400) for S5-compatible counters. IEC counters do not use counter numbers — they use instance DBs.

What if my S5 program uses both ZV and ZR on the same counter?

This is a combined up/down counter. In S7, use S_CUD (compatible) or CTUD (IEC). Both handle simultaneous count-up and count-down inputs.

Is the S5 counter edge detection identical in S7?

Yes. Both S5 counters and S7 S5-compatible counters (S_CU, S_CD) trigger on the rising edge of the VKE. IEC counters also trigger on the rising edge of CU/CD inputs.


Maintained by PLCcheck.ai. Last update: March 2026. Not affiliated with Siemens AG.

Related Articles

Analyze your PLC code with AI

PLCcheck Pro explains, documents, optimizes, and migrates PLC code — automatically.

Try PLCcheck Pro →
← Back to Blog

Not affiliated with Siemens AG. S5, S7, STEP 5, STEP 7, and TIA Portal are trademarks of Siemens AG.