ST data types and conversions

Create tags in the project tag table. ST supports BOOL, INT, DINT and REAL values, plus the named members of TIMER and COUNTER tags.

Types and members

TypeUse
BOOLTRUE or FALSE.
INTSigned 16-bit whole number, −32768 to 32767. Convert to DINT or REAL before arithmetic.
DINTSigned 32-bit whole number, −2147483648 to 2147483647.
REALA floating-point number for fractional calculations.
TIMERPRE and ACC are DINT milliseconds; EN, TT and DN are BOOL.
COUNTERPRE and ACC are DINT; CU, CD, DN, OV and UN are BOOL.

Read members with a dot, such as FillTimer.DN. PRE is writable. ACC is maintained by the instruction; use RES to reset it. The BOOL status members can be assigned, but the instruction updates them when it executes. Pass a whole TIMER or COUNTER tag to an instruction, rather than assigning one register to another.

Tags to create: FillTimer — TIMER; Batch — COUNTER; Ready — BOOL.
FillTimer.PRE := T#5s;
Batch.PRE := 10;
Ready := FillTimer.DN AND Batch.DN;

No automatic DINT/REAL conversion

A DINT tag or computed DINT value does not become REAL automatically, and a REAL does not become DINT automatically. Choose a conversion to show your intent.

Tags to create: Speed — REAL; Count — DINT.
Speed := DINT_TO_REAL(Count) / 2.0;
Count := REAL_TO_DINT(Speed);

Conversion functions

Each takes one value of its named input type. REAL_TO_DINT and REAL_TO_INT round to the nearest integer, with exact halves going to the even integer. A result outside the destination’s range faults the statement. BOOL conversions return 0 or 1.

The New Tag picker currently offers DINT for whole-number tags, rather than INT. These examples use INT results inside a second conversion so you can try them with the tags you can create.

Tags to create: Speed — REAL; Count — DINT; Ready — BOOL.
Speed := DINT_TO_REAL(Count);
Count := REAL_TO_DINT(Speed);
Count := INT_TO_DINT(7);
Count := INT_TO_DINT(DINT_TO_INT(Count));
Speed := INT_TO_REAL(7);
Count := INT_TO_DINT(REAL_TO_INT(Speed));
Count := INT_TO_DINT(BOOL_TO_INT(Ready));
Count := BOOL_TO_DINT(Ready);

Where a whole number can be REAL

A whole-number literal, including a negative one, can be assigned to a REAL tag or used beside a REAL in arithmetic or a comparison. MIN, MAX and LIMIT accept whole-number literals alongside REAL arguments; SQRT accepts a whole-number literal as its REAL input. This applies to the literal itself, not a DINT tag, time literal or expression such as 7 / 2. ABS(5) returns DINT; use ABS(5.0) for a REAL result.

Tags to create: Speed — REAL; Ready — BOOL.
Speed := 10;
Speed := Speed + 2;
Ready := Speed > 5;
Speed := LIMIT(0, Speed, 100);
Speed := SQRT(9);

A bounded whole-number constant can be assigned to INT. For arithmetic on an INT tag, explicitly use INT_TO_DINT or INT_TO_REAL first.

Related