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
- The Type Casting Trap: Why DWORD_TO_REAL Fails on Raw Word Values
- IEEE 754 Floating-Point Structure vs Signed 16-Bit Integer Representation
- Mathematical Linear Scaling Formulas for Unipolar and Bipolar Signals
- Comparative Signal Representation Matrix: Raw WORD vs Percentage vs REAL Engineering Unit
- Step-by-Step Implementation in CFC Using Math Blocks and SCL
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
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):
0to27648decimal (16#0000to16#6C00).0represents 0% (e.g., 4 mA) and27648represents 100% (e.g., 20 mA). -
Overrange / Underrange: Values between
27649and32511represent overrange (up to 22.8 mA); values below0(down to-4864for 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:
16#ED00-4864-17.59%-17.59 bar (Underflow)16#000000.00%0.00 bar16#36001382450.00%50.00 bar16#6C0027648100.00%100.00 bar16#7EF032511117.59%117.59 bar16#7FFF32767Bad = 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:
-
Option A: Deploying Standard CFC Math Blocks:
- From CFC Math Library, insert:W_BO(orWORD_TO_INTconversion),INT_TO_REAL,DIV_R,MUL_R, andADD_R.
- FeedRaw_VALUEintoINT_TO_REALto convert to floating-point representation.
- Connect the output toDIV_R.In1and setDIV_R.In2 = 27648.0.
- Connect the divided result toMUL_R.In1and setMUL_R.In2 = (High_Limit - Low_Limit).
- Connect the multiplied result toADD_R.In1and setADD_R.In2 = Low_Limit.
- The output ofADD_Ryields the exact engineeringREALvalue. -
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_VARVAR_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:
- In CFC Test Mode (Ctrl+T), monitor
Raw_VALUEand the computedOut_Real. - Apply
Raw_VALUE = 16#0000(0): verify thatOut_Real = 0.0. - Apply
Raw_VALUE = 16#3600(13824): verify thatOut_Real = 50.0. - Apply
Raw_VALUE = 16#6C00(27648): verify thatOut_Real = 100.0. - Apply a negative underrange value (e.g.,
-1382for 3.0 mA): verify thatOut_Realcalculates-5.0accurately.
Limitations and Operational Precautions
- Division by Zero Protection: If using dynamic range inputs, ensure
(Hi_Lim - Low_Lim) > 0to prevent mathematical exceptions. - Bipolar vs Unipolar: For bipolar inputs (-10 V to +10 V), raw range is
-27648to+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
- SIMATIC S7-300/400 Module Data Manual - Section 5: Principles of Analog Value Processing
- Siemens Industry Online Support (SIOS) Entry ID 182780 - Umwandlung von Rohwert (WORD) zu Prozesswert (REAL) in CFC-Plan
- SIMATIC S7-SCL V5.6 for S7-300/400 Manual - Data Types and Type Conversion Functions
- IEEE Standard 754-2008 for Floating-Point Arithmetic
Related Articles
- Extracting Half-Bytes (Nibbles) in SIMATIC PCS 7 - Bitmasking and Shift Operations
- Evaluating DWORD Bitmasks in WinCC VBScript - Converting C-Script Bitwise Logic
- Necessity and Architectural Role of Channel Drivers in SIMATIC PCS 7
- Viewing and Analyzing Background STL Code of CFC Blocks in SIMATIC PCS 7
Related Tags
- Analog Scaling
- WORD to REAL
- Linear Scaling
- 27648
- IEEE 754
- CH_AO
- CFC Math