Standard Function Blocks

The SoftPLC provides all of the standard function blocks defined by IEC 61131-3. Standard function blocks are always available, and do not need to be declared. Like any function block, you declare an instance of a standard function block as a variable, and call the instance once per scan.

VAR
    StartDelay : TON;
END_VAR

StartDelay(IN := StartButton, PT := T#2s);
Motor := StartDelay.Q;

Contents

Bistables

Bistables store a boolean state, which is set by one input and reset by another. They are also known as latches or flip-flops.

SR (Set Dominant)

Pin Type Description
SET1 (input) BOOL Sets Q1 when TRUE, even if RESET is TRUE.
RESET (input) BOOL Resets Q1 when TRUE and SET1 is FALSE.
Q1 (output) BOOL The stored state.

RS (Reset Dominant)

Pin Type Description
SET (input) BOOL Sets Q1 when TRUE and RESET1 is FALSE.
RESET1 (input) BOOL Resets Q1 when TRUE, even if SET is TRUE.
Q1 (output) BOOL The stored state.

The reset dominant bistable is typically used for start/stop logic, so that the stop button always takes priority:

VAR
    MotorLatch : RS;
END_VAR

MotorLatch(SET := StartButton, RESET1 := StopButton OR EmergencyStop);
Motor := MotorLatch.Q1;

Edge Detection

Edge detection blocks detect when a boolean signal changes. The output is TRUE for a single scan, which is useful for actions that should only happen once, such as counting a part or starting a command.

R_TRIG (Rising Edge)

Pin Type Description
CLK (input) BOOL The signal.
Q (output) BOOL TRUE for one scan when CLK changes from FALSE to TRUE.

F_TRIG (Falling Edge)

Pin Type Description
CLK (input) BOOL The signal.
Q (output) BOOL TRUE for one scan when CLK changes from TRUE to FALSE.
VAR
    PartArrived : R_TRIG;
    Parts : DINT;
END_VAR

PartArrived(CLK := Sensor);
IF PartArrived.Q THEN
    Parts := Parts + 1;
END_IF;

R_EDGE and F_EDGE

A boolean input of a function block can be declared with the R_EDGE or F_EDGE qualifier, instead of using an edge detection block. The input is then only TRUE in the call where the value assigned to it changes from FALSE to TRUE (R_EDGE) or from TRUE to FALSE (F_EDGE). The following function block counts parts in the same way as the example above:

FUNCTION_BLOCK PartCounter
VAR_INPUT
    Sensor : BOOL R_EDGE;    // TRUE only in the call where the sensor turns on.
END_VAR
VAR_OUTPUT
    Parts : DINT;
END_VAR

IF Sensor THEN
    Parts := Parts + 1;
END_IF;
END_FUNCTION_BLOCK

The qualifiers can only be used on the inputs of function blocks. Please see the variables page for more details.

Timers

Timers measure simulation time, which advances by the timestep with each scan. The elapsed time ET is therefore always a multiple of the timestep. Since the SoftPLC is deterministic, timers behave identically when the simulation runs faster or slower than real-time.

All timers have the same pins:

Pin Type Description
IN (input) BOOL The input signal.
PT (input) TIME The preset time.
Q (output) BOOL The output signal.
ET (output) TIME The elapsed time, which is limited to PT.

TON (On Delay)

The output Q becomes TRUE once IN has been TRUE for the preset time PT. The Q and ET outputs are reset as soon as IN becomes FALSE. The on delay timer is the most commonly used timer, for example to wait for a cylinder to extend, or to detect that a sensor has been blocked for too long.

VAR
    JamTimer : TON;
END_VAR

JamTimer(IN := Sensor, PT := T#5s);
JamAlarm := JamTimer.Q;     // The sensor has been blocked for 5 seconds.

TOF (Off Delay)

The output Q becomes TRUE as soon as IN becomes TRUE, and remains TRUE for the preset time PT after IN becomes FALSE. For example, an off delay timer can keep a conveyor running for a short time after the last part has passed a sensor.

VAR
    RunOn : TOF;
END_VAR

RunOn(IN := PartDetected, PT := T#3s);
Conveyor := RunOn.Q;

TP (Pulse)

A rising edge of IN starts a pulse, where Q is TRUE for the preset time PT. The pulse cannot be restarted while it is running, and always lasts for PT, even if IN becomes FALSE.

VAR
    Kick : TP;
END_VAR

Kick(IN := RejectPart, PT := T#200ms);
Ejector := Kick.Q;

Long Timers

The function blocks TON_LTIME, TOF_LTIME and TP_LTIME behave identically to TON, TOF and TP, but PT and ET have the LTIME type.

Counters

Counters count the rising edges of their inputs. The count never decreases below 0 or increases beyond the largest value of its type. The count CV and the preset value PV have the INT type. Typed variants are also available, which use a different integer type for CV and PV. The name of a typed variant is the name of the counter followed by the type, for example CTU_DINT, CTD_UDINT or CTUD_LINT. The types INT, DINT, LINT, UINT, UDINT and ULINT are supported.

CTU (Up Counter)

Pin Type Description
CU (input) BOOL CV increases by one at each rising edge.
RESET (input) BOOL Sets CV to 0 while TRUE.
PV (input) INT The preset value.
Q (output) BOOL TRUE when CV is greater than or equal to PV.
CV (output) INT The current count.
VAR
    BoxCounter : CTU;
END_VAR

BoxCounter(CU := BoxSensor, RESET := PalletRemoved, PV := 12);
PalletFull := BoxCounter.Q;

CTD (Down Counter)

Pin Type Description
CD (input) BOOL CV decreases by one at each rising edge.
LOAD (input) BOOL Sets CV to PV while TRUE.
PV (input) INT The preset value, which is loaded into CV.
Q (output) BOOL TRUE when CV is less than or equal to 0.
CV (output) INT The current count.

CTUD (Up/Down Counter)

Pin Type Description
CU (input) BOOL CV increases by one at each rising edge.
CD (input) BOOL CV decreases by one at each rising edge.
RESET (input) BOOL Sets CV to 0 while TRUE.
LOAD (input) BOOL Sets CV to PV while TRUE.
PV (input) INT The preset value.
QU (output) BOOL TRUE when CV is greater than or equal to PV.
QD (output) BOOL TRUE when CV is less than or equal to 0.
CV (output) INT The current count.

The up/down counter can track the number of parts in a buffer, counting up at the infeed and down at the outfeed:

VAR
    Buffer : CTUD;
END_VAR

Buffer(CU := InfeedSensor, CD := OutfeedSensor, PV := 20);
BufferFull := Buffer.QU;
BufferEmpty := Buffer.QD;