IEC Function Blocks

UniLogic supports key IEC 61131-3 function blocks directly in the ST editor, including bistables, edge detection, counters, timers, and RTC blocks.

These are stateful blocks; they maintain internal data across scan cycles, unlike simple one-shot functions. All blocks must be declared as instances.

Bistable (RS, SR)

A bistable latch remembers its state; once set, it stays set until explicitly reset, even if the trigger that set it is no longer active. This makes bistable blocks ideal for:

Two types are available. The difference is only in what happens if both inputs are TRUE at the same time:

Block

Dominant Input

Wins when both S and R are TRUE

RS 

R (Reset)

Q becomes FALSE - Stop always wins.
Use when safety requires that reset takes priority

SR 

S (Set)

Q becomes TRUE - Start always wins.
Use when you need the set condition to take precedence

 

Edge Detection

These function blocks detect signal transitions and fire a single-cycle pulse on the output Q.

Declaration and Usage

Q is a Read-only output member. CLK is the Hidden input member.
Both blocks maintain internal state (M) across cycles - never reset Q manually.

 

Counters: CTU, CTD, and CTUD

Counters track events based on signal transitions.

CTU - Count Up

Counts up from 0 to a preset value.

Property

Type

Access

Description

CU 

BIT

Input (Hidden)

Count-up trigger. A rising edge increments the counter value CV by 1

BIT

Input (Hidden)

Resets the output Q and the counter value CV

PV 

INT

Input (Hidden)

The counter limit value 

BIT

Read-only output

Set to TRUE when the counter value CV reaches PV

CV 

INT

Read-only output

The current count value

Declaration and Usage example:

VAR
    Counter : CTU;
END_VAR

Counter(
    CU := SensorPulse,
    R := ResetButton,
    PV := 10
);

ReachedLimit := Counter.Q;
CurrentCount := Counter.CV;

 

CTD - Count Down

Counts down from a preset value to 0.

Property

Type

Access

Description

CD 

BIT

Input (Hidden)

Count-down trigger. A rising edge decrements the counter value CV by 1

LD 

BIT

Input (Hidden)

Load preset value

PV 

INT

Input (Hidden)

The counter limit value 

BIT

Read-only output

Set to TRUE when the counter value CV reaches PV

CV 

INT

Read-only output

The current count value

Declaration and Usage example:

VAR
    DownCounter : CTD;
END_VAR

DownCounter(
    CD := Pulse,
    LD := LoadButton,
    PV := 20
);


ReachedLimit := Counter.Q;
CurrentCount := Counter.CV;

 

CTUD - Count Up/Down

Counts upward on rising edges of CU and downward on rising edges of CD. 

Property

Type

Access

Description

CU 

BIT

Input (Hidden)

Count-up trigger. Increments the counter on a rising edge

CD 

BIT

Input (Hidden)

Count-down trigger. Decrements the counter on a rising edge

BIT

Input (Hidden)

Resets the counter to 0

LD 

BIT

Input (Hidden)

Load preset value

PV 

INT

Input (Hidden)

Preset value 

QU 

BIT

Read-only output

TRUE when CV >= PV

QD 

BIT

Read-only output

TRUE when CV <= 0

CV 

INT

Read-only output

The current count value

Declaration and Usage example:

VAR
    UpDownCounter : CTUD;
END_VAR

UpDownCounter(
    CU := AddItem,
    CD := RemoveItem,
    R := ResetCounter,
    LD := LoadPreset,
    PV := 100
);


ReachedLimit := Counter.Q;
CurrentCount := Counter.CV;

 

  • CU and CD are edge-sensitive: holding them TRUE does not continuously increment or decrement the counter.

  • R and LD are level-sensitive and do not require a rising edge.

  • The IEC 61131-3 standard defines PV and CV as INT values, but PV may also be provided as a constant.

 

Timers: TON, TOF, and TP

UniLogic supports three IEC-standard timer function blocks. 

Function Block

Name

Description

TON 

Timer ON-Delay

Delays turning an output ON

TOF 

Timer OFF-Delay

Delays turning an output OFF

TP 

Timer Pulse

Generates a fixed-duration output pulse

 

Legacy timer implementations (from previous UniLogic versions) have been renamed with the _UNI_suffix: TON_UNI_, TOF_UNI_, TP_UNI_, TA_UNI_, TE_UNI_, and TIMER_RESET.
Existing programs using these names continue to work without modification.

 

TIME Data Type

The PT and ET properties use the TIME data type. For full details on TIME literals, supported units, syntax rules, and valid ranges, see Time Literals.

TIME literal examples:

T#5s        (* 5 seconds *)

T#2m30s     (* 2 minutes and 30 seconds *)

T#1h15m20s  (* 1 hour, 15 minutes, 20 seconds *)

TIME#500ms  (* 500 milliseconds, alternative prefix *)

 

IEC Function Block Structure

All three timer types share a common structure:

Property

Type

Access

Description

IN 

BIT

Input (Hidden)

Control input. Rising edge or level triggers the timer depending on type

PT 

TIME

Input (Hidden)

Preset time. The target duration 

BIT

Read-only output

Timer output. Meaning depends on timer type

ET 

TIME

Read-only output

Elapsed time. Counts up from 0 to PT while the timer is active

Declaring IEC Timers

VAR

    T1 : TON;

    T2 : TOF;

    T3 : TP;

END_VAR

 

TON - On-Delay Timer

Use TON to delay turning an output ON. The output Q becomes TRUE only after the input IN has been continuously TRUE for the preset duration PT. 

Behavior:

When IN becomes TRUE:

When IN becomes FALSE before ET reaches PT:

Usage example:

T1(IN := StartSignal, PT := T#5s);

Motor := T1.Q;   (* Motor starts 5 s after StartSignal rises *)

 

TOF - Off-Delay Timer

Use TOF to delay turning an output OFF. The output Q becomes TRUE immediately when IN rises, then becomes FALSE after IN has been FALSE for the preset duration PT.

Behavior:

When IN = TRUE:

When IN becomes FALSE:

If IN rises again while the timer is running:

Usage example:

T2(IN := RunSignal, PT := T#3s);

Valve := T2.Q;   (* Valve stays open 3 s after RunSignal falls *)

 

TP - Pulse Timer

Use TP to generate a fixed-duration output pulse. On a rising edge of IN, Q becomes TRUE and stays TRUE for exactly PT, regardless of the subsequent state of IN.

Behavior:

On a rising edge of IN:

While the timer is running (ET < PT):

When ET reaches PT:

Usage example:

T3(IN := Trigger, PT := T#500ms);

Buzzer := T3.Q;   (* Buzzer sounds for exactly 500 ms per trigger *)

 

RTC (Real-Time Clock)

The RTC function block maintains a continuously increasing DATE_AND_TIME value starting from a preset timestamp.

Unlike elapsed-time calculations, the CDT value represents an absolute timestamp that advances while the block is enabled.

RTC Structure

Property

Type

Access

Description

EN 

BIT

Input (Hidden)

Enables the RTC block

PDT 

DATE_AND_TIME

Input (Hidden)

Preset date and time 

BIT

Read-only output

TRUE while the RTC is counting

CDT 

DATE_AND_TIME

Read-only output

Current date and time value

Declaration and Usage Example:

VAR
MyRTC : RTC;
InitTime : DATE_AND_TIME := DT#2025-01-01-00:00:00;
END_VAR

MyRTC(
EN := TRUE,
PDT := InitTime
);

CurrentTime := MyRTC.CDT;
Running := MyRTC.Q;

 

Related Topics

ST Editor

ST Language Reference

ST Function Library