PLCcheck

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.

·8 min read
S5S7counterBCDINTBTIITBconversionCTUmigrationformat

Diesen Artikel auf Deutsch lesen

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:

In KOP/FUP, counters expose two outputs:

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

For values 0–9, BCD and binary are identical. At 10 and above, they differ:

Counter ValueBCD (hex)Binary (hex)Binary (decimal)
50x00050x00055
90x00090x00099
100x00100x000A10
990x00990x006399
1000x01000x0064100
2550x02550x00FF255
9990x09990x03E7999

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

InstructionFrom → ToRangeDescription
BTIBCD Word → INT-999 to +999Converts 3-digit BCD in ACCU 1 to 16-bit integer
BTDBCD DWord → DINT-9999999 to +9999999Converts 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

InstructionFrom → ToRangeDescription
ITBINT → BCD Word-999 to +999Converts 16-bit integer to 3-digit BCD
DTBDINT → BCD DWord-9999999 to +9999999Converts 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 TypeCV Data TypeRange
S5 legacy (Z n)BCD (3-digit)0–999
S7 legacy (S_CU, S_CD)BCD + binary0–999
IEC (CTU, CTD, CTUD)INT-32768 to +32767
IEC on S7-1500DINT option-2147483648 to +2147483647

Migration Checklist: Counter Format

When migrating counter code from S5/S7 legacy to IEC:

  1. Remove BTI/ITB instructions — IEC counters output INT directly, no BCD conversion needed
  2. Adjust comparison logic — S5 U Z n tests CV > 0; IEC CTU.Q tests CV ≥ PV (see Counter Migration Guide)
  3. 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.
  4. 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

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.