Converting Raw Analog Values (WORD) to Engineering Values (REAL) in SIMATIC PCS 7 CFC - Linear Scaling vs Type Casting

Converting Raw Analog Values (WORD) to Engineering Values (REAL) in SIMATIC PCS 7 CFC - Linear Scaling vs Type Casting

To convert the raw integer DAC output (VALUE : WORD) of an analog output channel driver (such as classic CH_AO FC276 or APL Pcs7AnOu) back into a floating-point engineering value (REAL), do not use binary type casting (such as DWORD_TO_REAL in SCL). Binary type casting treats the 16-bit integer bits as IEEE 754 floating-point mantissa and exponent components, yielding nonsensical values. In Siemens S7 hardware, the raw analog value is a signed 16-bit integer scaled from 0 to 27648 (representing 0% to 100% of nominal range). To convert it back to REAL, apply linear mathematical scaling: convert WORD to INT, convert INT to REAL, divide by 27648.0, and multiply by the measurement span (High_Limit - Low_Limit) + Low_Limit, or deploy standard scaling blocks like SCALE (FC105) or Pcs7AnIn.

Table of Contents

Anatomy of Siemens S7 Analog I/O Signals: The 0-27648 Integer Standard

In SIMATIC S7-300, S7-400, and ET 200 distributed I/O modules, analog-to-digital converters (ADC) and digital-to-analog converters (DAC) operate using a standardized internal 16-bit signed integer format:

  • Nominal Range (Unipolar 4–20 mA / 0–10 V): 0 to 27648 decimal (16#0000 to 16#6C00). 0 represents 0% (e.g., 4 mA) and 27648 represents 100% (e.g., 20 mA).

  • Overrange / Underrange: Values between 27649 and 32511 represent overrange (up to 22.8 mA); values below 0 (down to -4864 for 4–20 mA) represent underrange.

  • Overflow / Wire Break: 32767 (16#7FFF) signals wire break or positive overflow; -32768 (16#8000) signals negative overflow.

When an analog output channel driver (e.g., CH_AO or Pcs7AnOu) processes a floating-point control demand U (e.g., 50.0%), it calculates the integer 13824 and writes it to output parameter VALUE : WORD for transmission to the physical analog card.

The Type Casting Trap: Why DWORD_TO_REAL Fails on Raw Word Values

Engineers attempting to reverse-calculate the engineering value from VALUE : WORD often attempt type casting in SCL or CFC:

// THE INCORRECT APPROACH:
Temp_DWORD := WORD_TO_DWORD(Raw_VALUE);
Real_Result := DWORD_TO_REAL(Temp_DWORD); // YIELDS NONSENSICAL NUMBER!

This fails completely because DWORD_TO_REAL does not perform a numerical conversion; it performs a bit-pattern reinterpretation (type casting). It takes the 32 raw bits of the DWORD and maps them directly onto the IEEE 754 floating-point memory schema.

IEEE 754 Floating-Point Structure vs Signed 16-Bit Integer Representation

To understand why type casting fails, compare the binary architectures:

  • Signed 16-Bit Integer (INT / WORD): Bit 15 is the sign bit; Bits 0–14 represent the two's-complement integer magnitude.

  • 32-Bit IEEE 754 Single-Precision Float (REAL):

  • Bit 31: Sign bit.
  • Bits 23–30 (8 bits): Biased Exponent.
  • Bits 0–22 (23 bits): Fractional Mantissa.

When a 16-bit WORD (e.g., 13824 = 16#3600) is converted to DWORD by padding high-order bytes with zeros (16#00003600), the IEEE 754 exponent bits are all zero! The floating-point processor interprets this as a denormalized number close to 10^-41, which rounds to zero.

Mathematical Linear Scaling Formulas for Unipolar and Bipolar Signals

To accurately convert raw integer words to engineering floating-point numbers, true linear mathematical scaling must be executed:

Process_Value (REAL) = (WORD_TO_INT(Raw_VALUE) / 27648.0) * (High_Limit - Low_Limit) + Low_Limit

For example, for a 0–100 bar pressure transmitter where Raw_VALUE = 13824:

Process_Value = (13824.0 / 27648.0) * (100.0 - 0.0) + 0.0 = 0.5 * 100.0 = 50.0 bar

Comparative Signal Representation Matrix: Raw WORD vs Percentage vs REAL Engineering Unit

The following table details signal states across standard scaling ranges:

Physical Current (mA)Raw Integer Value (Hex)Raw Integer Value (Dec)Process Percentage (%)Engineering Value (0-100 bar)0.0 mA (Wire Break)16#ED00-4864-17.59%-17.59 bar (Underflow)4.0 mA (0% Nominal)16#000000.00%0.00 bar12.0 mA (50% Nominal)16#36001382450.00%50.00 bar20.0 mA (100% Nominal)16#6C0027648100.00%100.00 bar22.8 mA (Overrange)16#7EF032511117.59%117.59 bar> 22.8 mA (Overflow)16#7FFF32767FaultOverflow (Bad = 1)

Step-by-Step Implementation in CFC Using Math Blocks and SCL

Follow this procedure to build an accurate raw-to-real scaling converter in CFC:

  1. Option A: Deploying Standard CFC Math Blocks:
    - From CFC Math Library, insert: W_BO (or WORD_TO_INT conversion), INT_TO_REAL, DIV_R, MUL_R, and ADD_R.
    - Feed Raw_VALUE into INT_TO_REAL to convert to floating-point representation.
    - Connect the output to DIV_R.In1 and set DIV_R.In2 = 27648.0.
    - Connect the divided result to MUL_R.In1 and set MUL_R.In2 = (High_Limit - Low_Limit).
    - Connect the multiplied result to ADD_R.In1 and set ADD_R.In2 = Low_Limit.
    - The output of ADD_R yields the exact engineering REAL value.

  2. Option B: Compact Custom SCL Function Block:
    - Author a lightweight SCL block:
    ```scl
    FUNCTION_BLOCK FB1550
    VAR_INPUT
    Raw_Val : WORD;
    Low_Lim : REAL := 0.0;
    Hi_Lim : REAL := 100.0;
    END_VAR
    VAR_OUTPUT
    Out_Real : REAL;
    END_VAR

    VAR_TEMP
    Int_Val : INT;
    END_VAR

    // Convert WORD bit pattern to signed INT, then calculate REAL
    Int_Val := WORD_TO_INT(Raw_Val);
    Out_Real := (INT_TO_REAL(Int_Val) / 27648.0) * (Hi_Lim - Low_Lim) + Low_Lim;
    ```

Verification

Verify scaling accuracy through these checks:

  1. In CFC Test Mode (Ctrl+T), monitor Raw_VALUE and the computed Out_Real.
  2. Apply Raw_VALUE = 16#0000 (0): verify that Out_Real = 0.0.
  3. Apply Raw_VALUE = 16#3600 (13824): verify that Out_Real = 50.0.
  4. Apply Raw_VALUE = 16#6C00 (27648): verify that Out_Real = 100.0.
  5. Apply a negative underrange value (e.g., -1382 for 3.0 mA): verify that Out_Real calculates -5.0 accurately.

Limitations and Operational Precautions

  • Division by Zero Protection: If using dynamic range inputs, ensure (Hi_Lim - Low_Lim) > 0 to prevent mathematical exceptions.
  • Bipolar vs Unipolar: For bipolar inputs (-10 V to +10 V), raw range is -27648 to +27648. Adjust scaling formulas accordingly.
  • Diagnostic Bits in Custom Modules: Certain third-party specialized modules place diagnostic status bits in Bits 0–2 of the analog WORD. Verify module documentation to determine if bitmask masking (AND 16#FFF8) is required before scaling.

Engineer FAQ

Why is 27648 the nominal maximum for Siemens analog modules instead of 32767?

Siemens reserves integer values between 27649 and 32511 for the overrange band (e.g., measuring process excursions up to 22.8 mA) and uses 32767 exclusively to signal wire break/overflow.

Can I use the classic SCALE block (FC105) in PCS 7?

Yes. SCALE (FC105 from the S7 Standard Library) accepts an INT raw value and parameters LO_LIM and HI_LIM, executing linear scaling automatically.

What is the difference between INT_TO_REAL and DWORD_TO_REAL in SCL?

INT_TO_REAL performs mathematical conversion, transforming integer magnitude into floating-point format. DWORD_TO_REAL is a bitwise reinterpretation that maps raw bit patterns onto IEEE 754 memory without calculation.

Does APL Pcs7AnOu output a REAL value?

Pcs7AnOu accepts a REAL input (PV_In) and outputs both the raw peripheral word (PV_Chn) and readback status (PV_Out).

Why does WORD_TO_INT work when WORD is technically unsigned?

In S7 memory, both WORD and INT occupy 16 bits. WORD_TO_INT instructs the compiler to treat the highest bit (Bit 15) as an arithmetic sign.

Technical References

Related Articles

Related Tags

  • Analog Scaling
  • WORD to REAL
  • Linear Scaling
  • 27648
  • IEEE 754
  • CH_AO
  • CFC Math
6 respuestas