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.
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:
| Operation | Function | Trigger |
|---|---|---|
| ZV Z n | Count up (increment by 1) | Rising edge of VKE |
| ZR Z n | Count down (decrement by 1) | Rising edge of VKE |
| S Z n | Set counter to preset value (from ACCU1, KZ format) | VKE = TRUE |
| R Z n | Reset counter to 0 | VKE = TRUE |
| U Z n | Query: Is counter > 0? (boolean result in VKE) | — |
| L Z n | Load current counter value into ACCU1 (BCD format) | — |
| LC Z n | Load 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 AWL | S7 STL Function | Description |
|---|---|---|
| ZV Z n | S_CU (Count Up) | Increment on rising edge |
| ZR Z n | S_CD (Count Down) | Decrement on rising edge |
| ZV + ZR on same Z | S_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.
Option B: IEC Counters (CTU, CTD, CTUD) — Recommended
IEC counters use INT (or DINT on S7-1500) instead of BCD. They require an instance data block (like IEC timers).
| IEC Counter | Function | Count Range |
|---|---|---|
| CTU | Count Up | 0 to 32,767 (INT) or higher (DINT) |
| CTD | Count Down | 32,767 to 0 |
| CTUD | Count Up/Down | Full 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
| Scenario | Recommended Approach |
|---|---|
| Simple part counting, no arithmetic on values | S5-compatible (S_CU/S_CD) — least effort |
| Counter values used in calculations | IEC (CTU/CTD) with INT values |
| Counter range > 999 needed | IEC only (S5 counters limited to 999) |
| Migrating to S7-1500 (new project) | IEC always — S5-compatible counters are legacy |
How PLCcheck Pro Helps
- Identifies all counter references (Z n, ZV, ZR, S Z, R Z, U Z, L Z)
- Flags programs that mix BCD counter values with INT arithmetic
- Generates the S7-equivalent code for each counter pattern
- Warns about "U Z n" vs. IEC Q semantic differences
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
Handling S5 Special Function Blocks (OB, SB) in S7
How to migrate S5 organization blocks (OB) and step blocks (SB) to S7. Covers OB number mapping, startup OBs, interrupt OBs, and SB conversion strategies.
12 min read
migration-guideS5 Absolute Addressing vs. S7 Symbolic Addressing
Why S5 uses absolute addresses (E 0.0, MW 10, DB10.DW5) and S7 prefers symbolic names (Start_Button, Temperature, Motor.Speed). Migration strategy for converting absolute to symbolic.
8 min read
migration-guideUnderstanding S5 Data Blocks (DB0, DB1, DX0) in S7
What are S5 system data blocks DB0, DB1, and DX0? Why they exist, what they contain, and how to handle them during S5 to S7 migration.
10 min read
Analyze your PLC code with AI
PLCcheck Pro explains, documents, optimizes, and migrates PLC code — automatically.
Try PLCcheck Pro →Not affiliated with Siemens AG. S5, S7, STEP 5, STEP 7, and TIA Portal are trademarks of Siemens AG.