S5→S7 Counter Formats: BCD vs. INT — Conversion Reference
Reference for understanding and converting between S5/S7 legacy counter BCD format and IEC counter INT format. Covers BCD structure, conversion instructions (BTI, ITB, BTD, DTB), and migration pitfalls.
S5→S7 Counter Formats: BCD vs. INT — Conversion Reference
S5 and S7 legacy counters store their values in BCD (Binary-Coded Decimal) format. IEC counters (CTU, CTD, CTUD) store values as INT. Confusing these formats is one of the most common migration errors — the program compiles without error but produces wrong results for counter values above 9.
How S5/S7 Legacy Counters Store Values
When you load a counter value in STL/AWL (L Z n or L C n), the CPU places the value in Accumulator 1 in two formats simultaneously:
- Bits 0–11: Binary integer (BI) representation of the counter value
- Bits 0–11 (separate read): BCD representation when accessed via CV_BCD output
In KOP/FUP, counters expose two outputs:
- CV — Counter value as binary integer (hexadecimal display)
- CV_BCD — Counter value in BCD format
BCD Format Structure (16-bit Counter Word)
Bit: 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
0 0 0 0 | Hundreds | Tens | Ones
| (BCD) | (BCD) | (BCD)
Range: 0 to 999 (three BCD digits, each 0–9)
Example: Counter value 247
- BCD: 0000 0010 0100 0111 = 0x0247
- Binary: 0000 0000 1111 0111 = 0x00F7 = 247 decimal
For values 0–9, BCD and binary are identical. At 10 and above, they differ:
| Counter Value | BCD (hex) | Binary (hex) | Binary (decimal) |
|---|---|---|---|
| 5 | 0x0005 | 0x0005 | 5 |
| 9 | 0x0009 | 0x0009 | 9 |
| 10 | 0x0010 | 0x000A | 10 |
| 99 | 0x0099 | 0x0063 | 99 |
| 100 | 0x0100 | 0x0064 | 100 |
| 255 | 0x0255 | 0x00FF | 255 |
| 999 | 0x0999 | 0x03E7 | 999 |
The trap: If you load a BCD counter value and perform integer arithmetic on it without conversion, the result is wrong for any value ≥ 10. BCD 0x0010 is not decimal 16 — it represents the decimal value 10. But the CPU treats 0x0010 as integer 16 if you use integer arithmetic.
Conversion Instructions (STL/AWL)
BCD → Integer
| Instruction | From → To | Range | Description |
|---|---|---|---|
BTI | BCD Word → INT | -999 to +999 | Converts 3-digit BCD in ACCU 1 to 16-bit integer |
BTD | BCD DWord → DINT | -9999999 to +9999999 | Converts 7-digit BCD in ACCU 1 to 32-bit integer |
Example:
L Z 1 // Load counter 1 (BCD in ACCU 1)
BTI // Convert BCD to INT
T MW 20 // Store as integer — now safe for arithmetic
Integer → BCD
| Instruction | From → To | Range | Description |
|---|---|---|---|
ITB | INT → BCD Word | -999 to +999 | Converts 16-bit integer to 3-digit BCD |
DTB | DINT → BCD DWord | -9999999 to +9999999 | Converts 32-bit integer to 7-digit BCD |
Example:
L MW 20 // Load integer value
ITB // Convert INT to BCD
T MW 30 // Store as BCD — for BCD display or legacy interface
Overflow: If the value exceeds the BCD range (>999 for BTI/ITB, >9999999 for BTD/DTB), the OV and OS status bits are set.
SCL Conversion Functions
In SCL, use the built-in conversion functions:
// BCD Word to INT
intValue := BCD_TO_INT(bcdWord);
// INT to BCD Word
bcdWord := INT_TO_BCD(intValue);
IEC Counters: No BCD — Always INT
IEC counters (CTU, CTD, CTUD) on S7-1200/1500 store the counter value as INT (or DINT on S7-1500). There is no BCD representation. The CV output is a plain integer.
| Counter Type | CV Data Type | Range |
|---|---|---|
| S5 legacy (Z n) | BCD (3-digit) | 0–999 |
| S7 legacy (S_CU, S_CD) | BCD + binary | 0–999 |
| IEC (CTU, CTD, CTUD) | INT | -32768 to +32767 |
| IEC on S7-1500 | DINT option | -2147483648 to +2147483647 |
Migration Checklist: Counter Format
When migrating counter code from S5/S7 legacy to IEC:
- Remove BTI/ITB instructions — IEC counters output INT directly, no BCD conversion needed
- Adjust comparison logic — S5
U Z ntests CV > 0; IEC CTU.Q tests CV ≥ PV (see Counter Migration Guide) - Check arithmetic — if the old program performed arithmetic on counter values without prior BTI conversion, it may have been silently wrong for values ≥ 10. Verify the intended behavior.
- Update HMI/SCADA interfaces — if external systems read counter values via communication, change the expected data type from BCD to INT
Frequently Asked Questions
Why does my counter show wrong values only above 9?
You are likely performing integer arithmetic on a BCD value without conversion. BCD and binary are identical for 0–9, so the error only appears at 10+. Insert a BTI instruction after loading the counter.
Can I still use legacy counters (S_CU, S_CD) on S7-1500?
Yes, for compatibility. But they run in emulation mode. Siemens recommends IEC counters (CTU, CTD, CTUD) for all new development.
Convert Your Counter Code Automatically
PLCcheck Pro detects legacy counter access patterns, identifies missing BCD conversions, and generates correct IEC counter equivalents.
Upload code for counter analysis → | Counter Migration Guide →
Part of the Address Mapping Reference. Maintained by PLCcheck.ai. Not affiliated with Siemens AG.
Related Articles
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
migration-guideCross-Platform PLC Migration: Siemens to Allen-Bradley
What to expect when migrating between Siemens S7 and Allen-Bradley ControlLogix. No automatic conversion exists — this guide covers the manual approach, key differences, and planning.
8 min read
migration-guideHandling 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
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.