Skip to content

07Data and variables

Literals and constants: 16#FF, 2#1010 and T#5s

How to write a number, a duration or a bit pattern in ST, and why the compiler sometimes rejects a number that looks fine.

Lesson 7 of 349 minIEC 61131-3

The bases

Form

Meaning

Example

123

decimal

integer

1.5 1.5e3

real

REAL

2#1010_0001

binary

bit patterns

8#777

octal

rare

16#FF 16#00FF_A1

hexadecimal

masks, Modbus addresses

TRUE FALSE

boolean

BOOL

T#1m30s

duration

TIME

'Batch A'

text

STRING

The underscore is visual only: 1_000_000 is 1000000.

Typed literals

Sometimes you have to tell the compiler which type you mean:

INT#100        (* 100 as a 16-bit integer *)
REAL#100       (* 100.0 *)
DINT#100000

You need this when a block expects a DINT and you want to pass a small number without ambiguity.

Why does the compiler reject PT := 5000?

Because PT is a TIME and 5000 is an integer. The standard allows no implicit conversion here:

HiTmr(IN := Level > 85.0, PT := T#5s);     (* correct *)
HiTmr(IN := Level > 85.0, PT := 5000);     (* error *)

Named constants

Every number in your program wants a name if it repeats or carries engineering meaning:

VAR CONSTANT
  LevelHi    : REAL := 85.0;   (* high alarm, LAH-101 *)
  LevelHiHi  : REAL := 95.0;   (* pump trip *)
  AlarmDelay : TIME := T#2s;   (* ISA-18.2 de-chatter *)
END_VAR

This is not cosmetic. A year from now, when operations ask for 88, you change one number instead of hunting 85.0 through a thousand lines.

Try it in the lab

The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.

What to try

  1. Run the program and turn S1 on: the lamps count in binary from 0 to 15, then start over.
  2. Watch Count, Limit and Ten in the watch table: 2#1111 is 15 and 16#A is 10.
  3. Change Limit to Ten and download again: counting now wraps at 10.
  4. Change T#500ms to T#200ms to count faster.

People find this lesson by searching

structured text literalshexadecimal plc 16#binary literal structured textTIME literal T#CONSTANT structured texttyped literal INT#plc number formats

Course outline

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers