Converting Between REAL and TIME Data Types in SIMATIC PCS 7 CFC

Converting Between REAL and TIME Data Types in SIMATIC PCS 7 CFC

In SIMATIC PCS 7 CFC, there is no single monolithic elementary block to convert directly between REAL (floating-point seconds) and TIME (IEC millisecond time duration). In STEP 7, data type TIME is represented internally as a 32-bit signed integer of milliseconds (e.g. T#1s = 1000 ms). To convert TIME to REAL (in seconds), pass the TIME variable through SCL or elementary CFC blocks: convert TIME to DINT (TIME_TO_DINT), cast to REAL (DINT_TO_REAL), and divide by 1000.0 via block DIV_R. Conversely, to convert REAL (seconds) to TIME, multiply by 1000.0 via MUL_R, convert to DINT via R_TO_DI, and cast to TIME via DI_TO_T. In APL, timing blocks (such as TimerP) utilize REAL seconds natively.

Table of Contents

Executive Summary

Automation engineers implementing custom timing algorithms, countdown displays, or batch phase durations in SIMATIC PCS 7 frequently encounter data type mismatches between mathematical calculation blocks (which process 32-bit IEEE 754 floating-point REAL values) and system timers (which demand the standard IEC TIME data type). While operators and CFC setpoint blocks parameterize durations in human-readable floating-point seconds (e.g. 12.5 seconds), STEP 7 timers and standard library functions operate strictly on IEC TIME, formatted as a 32-bit signed integer counting milliseconds. Because standard PCS 7 libraries do not supply a dedicated REAL_TO_TIME block, engineers must implement conversion cascades in CFC or author custom SCL conversion functions. This technical guide explains the internal binary formats and details the conversion procedures.

Data Representation in STEP 7: REAL Floating-Point vs IEC TIME

Understanding the internal data formats is essential to prevent scaling errors:

  • REAL (IEEE 754): 32-bit floating-point format comprising 1 sign bit, 8 exponent bits, and 23 mantissa bits. In PCS 7 process control, REAL variables typically represent engineering units (e.g. seconds, hours, bar, °C).
  • TIME (IEC 61131-3): Stored in S7 memory as a 32-bit signed integer (DINT) quantifying time in milliseconds. For example, T#2s is stored as +2000, T#1m is stored as +60000, and T#1d is stored as +86400000.

The Millisecond Scaling Factor (1000.0)

Because TIME counts in milliseconds while process REAL tags typically represent seconds, converting between the two requires a mathematical scaling factor of 1,000.0:

  • Seconds (REAL) = DINT_TO_REAL(TIME_TO_DINT(Time_Val)) / 1000.0
  • Time (TIME) = DINT_TO_TIME(REAL_TO_DINT(Seconds_REAL * 1000.0))

Conversion Implementations: CFC Block Cascades vs SCL Function

Engineers can choose between two primary approaches:

  1. CFC Block Cascade: Interconnects standard elementary math blocks from the CFC Library: MUL_R -> R_TO_DI -> DI_TO_T.
  2. Custom SCL Function (FC_RealToTime): A lightweight custom function that executes the conversion in a single line of SCL code, minimizing chart clutter and CPU instruction cycles.

Data Type Conversion and Parameter Mapping Matrix

The following table compares conversion approaches and mathematical transformations in PCS 7:

Conversion DirectionSource TypeIntermediate MathTarget TypeExample InputResulting Output ValueREAL to TIMEREAL (Seconds)Multiply by 1000.0, cast to DINTTIME1.5 (Seconds)T#1s_500ms (1500 ms)REAL to TIMEREAL (Minutes)Multiply by 60000.0, cast to DINTTIME2.0 (Minutes)T#2m (120000 ms)TIME to REALTIME (IEC Duration)Cast to DINT, divide by 1000.0REAL (Seconds)T#5s (5000 ms)5.0 (Seconds)TIME to REALTIME (IEC Duration)Cast to DINT, divide by 60000.0REAL (Minutes)T#3m (180000 ms)3.0 (Minutes)APL Native BlockREAL (Seconds)Handled internally in TimerPREAL (Seconds)10.0 (Seconds)Directly processed by APL

Step-by-Step Implementation Procedure

Method 1: Cascading Elementary Blocks in CFC

  1. Open the CFC chart in SIMATIC Manager.
  2. In the CFC catalog, expand Libraries > CFC Library > Blocks > MATH.
  3. Drag block MUL_R (Multiply Real) onto the chart. Connect the floating-point second value to input IN1, and set IN2 = 1000.0.
  4. Expand the CONVERT family in the catalog. Drag block R_TO_DI (Real to Double Integer) onto the chart.
  5. Connect MUL_R.OUT to R_TO_DI.IN.
  6. Drag block DI_TO_T (Double Integer to Time) onto the chart.
  7. Connect R_TO_DI.OUT to DI_TO_T.IN.
  8. Connect DI_TO_T.OUT directly to the TIME parameter of the target timer or block.

Method 2: Authoring a Clean SCL Conversion Function

  1. In SIMATIC Manager, insert an SCL Source file into the S7 Program Sources folder.
  2. Enter the following SCL code:
    scl FUNCTION FC_RealToTime : TIME VAR_INPUT rSeconds : REAL; END_VAR BEGIN FC_RealToTime := DINT_TO_TIME(REAL_TO_DINT(rSeconds * 1000.0)); END_FUNCTION
  3. Compile the SCL source to generate the function in the block container.
  4. Drag FC_RealToTime onto your CFC chart for compact, single-block conversion.

Verification

To verify that time conversion operates accurately:

  1. CFC Test Mode: Switch the CFC editor to Test Mode (Debug > Test Mode).
  2. Fractional Value Test: Inject rSeconds = 2.75. Confirm that the output evaluates exactly to T#2s_750ms.
  3. Zero / Limit Test: Inject rSeconds = 0.0. Confirm output is T#0ms. Inject negative numbers; confirm behavior conforms to your plant safety specifications.

Limitations and Operational Precautions

  • Negative Time Limit: STEP 7 TIME is signed; negative values are technically valid (-T#24d_20h...) but cause undefined behavior in standard timers. Clamp inputs to >= 0.0 before conversion.
  • Maximum Range Overflow: Data type TIME has a maximum limit of T#24d_20h_31m_23s_647ms (approx. 2,147,483.647 seconds). Passing a REAL larger than this causes DINT overflow.
  • APL Standard Alignment: In modern PCS 7, avoid legacy S5 timers; use APL TimerP which operates entirely in floating-point REAL seconds.

Engineer FAQ

Why doesn't Siemens provide a standard REAL_TO_TIME block in the CFC library?

Because conversion requires contextual engineering assumptions: some applications define REAL as seconds, while others define it as minutes, hours, or milliseconds.

Can S7-300 or S7-400 timers accept REAL directly?

No. System functions (SFBs such as TON / SFB4, TOF / SFB5) strictly require data type TIME conforming to IEC 61131-3.

Does R_TO_DI round or truncate during conversion?

R_TO_DI performs standard IEEE round-to-nearest-even on the fractional part before outputting the double integer.

Technical References

Related Articles

Related Tags

  • https://industrialautomation.net/search?query=simatic-pcs7
  • https://industrialautomation.net/search?query=cfc-editor
  • https://industrialautomation.net/search?query=data-types
  • https://industrialautomation.net/search?query=scl
6 respuestas